Skip to content

Instantly share code, notes, and snippets.

@cchudant
Created January 20, 2022 17:42
Show Gist options
  • Save cchudant/201287db2feba62d1486aaf802a7017e to your computer and use it in GitHub Desktop.
Save cchudant/201287db2feba62d1486aaf802a7017e to your computer and use it in GitHub Desktop.
clang -S -emit-llvm main.c
#include <stdio.h>
int factorial(int n, int p) {
int res = 1;
for (int i = 0; i < p; i++) {
res *= n;
}
return res;
}
int main() {
printf("Hello\n");
int res = factorial(3, 3);
printf("%d\n", res);
return 1;
}
; ModuleID = 'main.c'
source_filename = "main.c"
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-pc-linux-gnu"
@.str = private unnamed_addr constant [7 x i8] c"Hello\0A\00", align 1
@.str.1 = private unnamed_addr constant [4 x i8] c"%d\0A\00", align 1
; Function Attrs: noinline nounwind optnone sspstrong uwtable
define dso_local i32 @factorial(i32 %0, i32 %1) #0 {
%3 = alloca i32, align 4
%4 = alloca i32, align 4
%5 = alloca i32, align 4
%6 = alloca i32, align 4
store i32 %0, i32* %3, align 4
store i32 %1, i32* %4, align 4
store i32 1, i32* %5, align 4
store i32 0, i32* %6, align 4
br label %7
7: ; preds = %15, %2
%8 = load i32, i32* %6, align 4
%9 = load i32, i32* %4, align 4
%10 = icmp slt i32 %8, %9
br i1 %10, label %11, label %18
11: ; preds = %7
%12 = load i32, i32* %3, align 4
%13 = load i32, i32* %5, align 4
%14 = mul nsw i32 %13, %12
store i32 %14, i32* %5, align 4
br label %15
15: ; preds = %11
%16 = load i32, i32* %6, align 4
%17 = add nsw i32 %16, 1
store i32 %17, i32* %6, align 4
br label %7, !llvm.loop !6
18: ; preds = %7
%19 = load i32, i32* %5, align 4
ret i32 %19
}
; Function Attrs: noinline nounwind optnone sspstrong uwtable
define dso_local i32 @main() #0 {
%1 = alloca i32, align 4
%2 = alloca i32, align 4
store i32 0, i32* %1, align 4
%3 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%4 = call i32 @factorial(i32 3, i32 3)
store i32 %4, i32* %2, align 4
%5 = load i32, i32* %2, align 4
%6 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %5)
ret i32 1
}
declare i32 @printf(i8*, ...) #1
attributes #0 = { noinline nounwind optnone sspstrong uwtable "frame-pointer"="all" "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #1 = { "frame-pointer"="all" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
!llvm.module.flags = !{!0, !1, !2, !3, !4}
!llvm.ident = !{!5}
!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 7, !"PIC Level", i32 2}
!2 = !{i32 7, !"PIE Level", i32 2}
!3 = !{i32 7, !"uwtable", i32 1}
!4 = !{i32 7, !"frame-pointer", i32 2}
!5 = !{!"clang version 13.0.0"}
!6 = distinct !{!6, !7}
!7 = !{!"llvm.loop.mustprogress"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment