Hi. Below is my code for adding an if-else statement. I’m checking if the argument is zero then print ‘The value is zero.’ However, I get segmentation fault. Can someone assist me in figuring out the issue? Thank you
F is function.
Argument *arg = &*F.arg_begin();
Value *argValue = arg;
// Create the if statement to check if the argument is zero
BasicBlock *entryBB = &F.getEntryBlock();
IRBuilder<> builder(entryBB, entryBB->getFirstInsertionPt());
Value *zeroValue = ConstantInt::get(argValue->getType(), 0);
Value *condition = builder.CreateICmpEQ(argValue, zeroValue, "isZero");
// Split the block to create two new basic blocks for "if" and "else"
BasicBlock *ifBB = BasicBlock::Create(F.getContext(), "if", &F);
BasicBlock *elseBB = BasicBlock::Create(F.getContext(), "else", &F);
// Set insertion point to the "if" block
builder.SetInsertPoint(ifBB);
builder.CreateCall(F.getParent()->getFunction("printf"),
{buildesr.CreateGlobalStringPtr("The value is zero.\n")});
// Create the unconditional branch from "if" block to "else" block
builder.CreateBr(elseBB);
// Set insertion point to the "else" block
builder.SetInsertPoint(elseBB);