I am trying to convert a string to an integer in LLVM assembly code. The code works fine with atoi but I want to switch to strtol.
This is the code:
; initialise a number
@number0 = private unnamed_addr constant [2 x i8] c"5\00"
%str = getelementptr [2 x i8]* @number0, i64 0, i64 0
; the endpointer that indicates an error
%endptr = alloca i8*
; the actual call of strtol
%addr = getelementptr i8* %str, i64 0
%new_long = call i64 @strtol(i8* %addr, i8** %endptr)
; debug printing
%after_casting = getelementptr [18 x i8]* @after_casting, i64 0, i64 0
call i64(i8*, ...)* @printf(i8* %after_casting, i64 %new_long)
Now, the debug printf message prints 0. I guess something wrong with the endptr passing. What am I doing wrong?
When wondering about things like this, just run Clang with LLVM IR emission. For example this C code:
int main ()
{
char szNumbers[] = "2001";
char * pEnd;
long int li1;
li1 = strtol (szNumbers,&pEnd,10);
printf ("%ld\n", li1);
return 0;
}
Turns into this IR:
@main.szNumbers = private unnamed_addr constant [5 x i8] c"2001\00", align 1
@.str = private unnamed_addr constant [5 x i8] c"%ld\0A\00", align 1
; Function Attrs: nounwind uwtable
define i32 @main() #0 {
entry:
%szNumbers = alloca [5 x i8], align 1
%pEnd = alloca i8*, align 8
%0 = getelementptr inbounds [5 x i8]* %szNumbers, i64 0, i64 0
call void @llvm.memcpy.p0i8.p0i8.i64(i8* %0, i8* getelementptr inbounds ([5 x i8]* @main.szNumbers, i64 0, i64 0), i64 5, i32 1, i1 false)
%call = call i64 @strtol(i8* %0, i8** %pEnd, i32 10) #1
%call1 = call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([5 x i8]* @.str, i64 0, i64 0), i64 %call) #1
ret i32 0
}