Skip to content

Instantly share code, notes, and snippets.

@richiejp
Created September 2, 2013 21:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save richiejp/6417262 to your computer and use it in GitHub Desktop.
Save richiejp/6417262 to your computer and use it in GitHub Desktop.
Fibonachi sequence in LLVM IR
@resultmsg = internal constant [14 x i8] c"fib(%u) = %u\0A\00"
declare i32 @printf(i8 *, ...)
declare i32 @atoi(i8 *)
define fastcc i32 @fib (i32 %n) {
entry:
%leone = icmp sle i32 %n, 1
br i1 %leone, label %exitzero, label %checkiftwo
checkiftwo:
%letwo = icmp sle i32 %n, 2
br i1 %letwo, label %exitone, label %continue
exitzero:
ret i32 0
exitone:
ret i32 1
continue:
%n.1 = sub i32 %n, 1
%n.2 = sub i32 %n.1, 1
%next1 = call fastcc i32 @fib(i32 %n.1)
%next2 = call fastcc i32 @fib(i32 %n.2)
%result = add i32 %next1, %next2
ret i32 %result
}
define fastcc i32 @main (i32 %argc, i8** %argv) {
%rmsg = getelementptr [14 x i8]* @resultmsg, i64 0, i64 0
%n.1 = getelementptr i8** %argv, i64 1
%n.2 = load i8** %n.1
%n.3 = call i32 @atoi(i8* %n.2)
%fibres = call fastcc i32 @fib(i32 %n.3)
call i32 (i8 *, ...)* @printf(i8* %rmsg, i32 %n.3, i32 %fibres)
ret i32 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment