I want to print the value of a floating point variable to the screen. I am declaring printf() function in the LLVM IR code, and it is linking in successfully.
Whenever I print an integer or a character data type, or a string, printf() prints them normally to the screen as it prints them in the C code. However, if I pass a float to printf(), instead of printing the floating point number, it prints 0.000000. I checked the source code multiple times and it seems that the syntax is correct. It should be printing 2.75! I am looking at this code and I absolutely do not understand how code has a different behavior than what I wrote it.
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"
@obj1 = global {i32, float, i8} zeroinitializer
@format_string = constant [10 x i8] c"%i %f %c\0A\00"
declare i32 @printf(i8*, ...)
define i32 @main() {
entry:
    %obj1 = load {i32, float, i8}, {i32, float, i8}* @obj1
    %obj2 = insertvalue {i32, float, i8} %obj1, i32 44, 0
    %obj3 = insertvalue {i32, float, i8} %obj2, float 2.75, 1
    %obj4 = insertvalue {i32, float, i8} %obj3, i8 36, 2
    store {i32, float, i8} %obj4, {i32, float, i8}* @obj1
    %ptr.i32 = getelementptr {i32, float, i8}, {i32, float, i8}* @obj1, i32 0, i32 0
    %0 = load i32, i32* %ptr.i32
    %ptr.float = getelementptr {i32, float, i8}, {i32, float, i8}* @obj1, i32 0, i32 1
    %1 = load float, float* %ptr.float
    %ptr.i8 = getelementptr {i32, float, i8}, {i32, float, i8}* @obj1, i32 0, i32 2
    %2 = load i8, i8* %ptr.i8
    %format_ptr = getelementptr [10 x i8], [10 x i8]* @format_string, i64 0, i64 0
    call i32 (i8*, ...) @printf(i8* %format_ptr, i32 %0, float %1, i8 %2)
    ret i32 0
}
When I compile the LLVM IR code, this is the output:
$ llvm-as code.ll -o code.bc
$ lli code.bc
44 0.000000 $
It successfully printed the integer and the character, but not the floating point number!
 
    