Skip to content

Instantly share code, notes, and snippets.

@jFransham
Created April 25, 2017 09:02
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 jFransham/72bd7b4f5999b0b7e78e22469e70a997 to your computer and use it in GitHub Desktop.
Save jFransham/72bd7b4f5999b0b7e78e22469e70a997 to your computer and use it in GitHub Desktop.
@out-str = private unnamed_addr constant [4 x i8] c"%u\0A\00", align 1
declare i32 @printf(i8*, ...)
declare i32 @atoi(i8*)
define i32 @main(i32 %argc, i8** %argv) {
%cur = alloca i128, align 8
store i128 1, i128* %cur, align 8
%last = alloca i128, align 8
store i128 1, i128* %last, align 8
%has-enough-vars = icmp ugt i32 %argc, 1
br i1 %has-enough-vars, label %continue, label %error
continue:
%pointer-to-n = getelementptr inbounds i8*, i8** %argv, i32 1
%input-n = load i8*, i8** %pointer-to-n
%n-val = call i32 (i8*) @atoi(i8* %input-n)
%n = alloca i32, align 4
store i32 %n-val, i32* %n, align 4
br label %loop
loop:
%loop-cond-cur-n = load i32, i32* %n, align 2
%loop-cond = icmp eq i32 %loop-cond-cur-n, 0
br i1 %loop-cond, label %end, label %loop.body
loop.body:
%cur-cur = load i128, i128* %cur, align 8
%cur-last = load i128, i128* %last, align 8
%added = add i128 %cur-cur, %cur-last
store i128 %added, i128* %cur
store i128 %cur-cur, i128* %last
%cur-n = load i32, i32* %n, align 2
%next-n = sub i32 %cur-n, 1
store i32 %next-n, i32* %n
br label %loop
end:
%final-val = load i128, i128* %cur, align 8
call i32 (i8*, ...) @printf(
i8* getelementptr inbounds ([4 x i8], [4 x i8]* @out-str, i32 0, i32 0),
i128 %final-val
)
br label %success
success:
ret i32 0
error:
ret i32 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment