How do we replace function with another?

if (CallInst *call = dyn_cast(I))
{
Function *calledFunction = call->getCalledFunction();
if (calledFunction && calledFunction->getName() == “malloc”)
{
Value *address = cast(call);
Value *returnValue = call;
auto Val1 = call->getOperand(0);

      // vector<Value *> args;
      // // args.push_back(address);
      // args.push_back(size);

      auto FT = FunctionType::get(returnValue->getType(), Val1->getType(), false);
      auto Callee = M->getOrInsertFunction("mymalloc", FT);
      SmallVector<Value*, 1> FuncArgs{ Val1 };
      auto Call = CallInst::Create(Callee, FuncArgs, "");
      ReplaceInstWithInst(I, Call);

}
}

I have the following code to replace all calls of malloc to my version of it “mymalloc”, (mymalloc is defined from a shared library that I will include when running the newly generated IR after running the pass) however, I am getting a segmentation fault just from running the pass:

resolved