Skip to content

Instantly share code, notes, and snippets.

@scaryghost
Created June 10, 2019 10:31
Show Gist options
  • Save scaryghost/4aa329dbcf13899823cd9e7c2b8f51de to your computer and use it in GitHub Desktop.
Save scaryghost/4aa329dbcf13899823cd9e7c2b8f51de to your computer and use it in GitHub Desktop.
random llvm-ir files
declare i32 @atoi(i8* nocapture)
declare i32 @printf(i8* noalias nocapture, ...) nounwind
@.result_fmt = private unnamed_addr constant [18 x i8] c"pascal(%d,%d)=%d\0A\00"
define i32 @pascal(i32 %n, i32 %k) {
%n_eq_0 = icmp eq i32 %n, 0
br i1 %n_eq_0, label %base_case_1, label %n_is_not_base
n_is_not_base:
%k_eq_0 = icmp eq i32 %k, 0
br i1 %k_eq_0, label %base_case_1, label %k_is_not_base
k_is_not_base:
%k_eq_n = icmp eq i32 %k, %n
br i1 %k_eq_n, label %base_case_1, label %k_n_is_not_base
k_n_is_not_base:
%k_gt_n = icmp ugt i32 %k, %n
br i1 %k_gt_n, label %base_case_0, label %recurse
recurse:
%n_dec = sub i32 %n, 1
%k_dec = sub i32 %k, 1
%rec_1 = call i32 @pascal(i32 %n_dec, i32 %k)
%rec_2 = call i32 @pascal(i32 %n_dec, i32 %k_dec)
%result = add i32 %rec_1, %rec_2
ret i32 %result
base_case_1:
ret i32 1
base_case_0:
ret i32 0
}
; Definition of main function
define i32 @main(i32 %argc, i8** %argv) { ; i32()*
%argv_1 = getelementptr i8*, i8** %argv, i32 1
%argv_2 = getelementptr i8*, i8** %argv, i32 2
%n_ptr = load i8*, i8** %argv_1
%k_ptr = load i8*, i8** %argv_2
%n = call i32 @atoi(i8* %n_ptr)
%k = call i32 @atoi(i8* %k_ptr)
%result = call i32 @pascal(i32 %n, i32 %k)
%cast210 = getelementptr [18 x i8], [18 x i8]* @.result_fmt, i32 0, i32 0
call i32 (i8*, ...) @printf(i8* %cast210, i32 %n, i32 %k, i32 %result)
ret i32 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment