; LLVM IR from BrunoLevy/learn-fpga :: FemtoRV/FIRMWARE/EXAMPLES (ST_NICCC)

; Unoptimized IR (clang -disable-llvm-passes):
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

@F = external dso_local global ptr, align 8
@cur_byte_address = external dso_local global i32, align 4
@wireframe = external dso_local global i32, align 4
@colormapped = external dso_local global i32, align 4
@FGA_mode = external global i32, align 4
@.str = external hidden unnamed_addr constant [12 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [2 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [27 x i8], align 1
@gl_polygon_mode = external global i32, align 4

; Function Attrs: nounwind uwtable
declare dso_local i32 @read_frame() #0

declare void @GL_clear(...) #1

; Function Attrs: nounwind uwtable
define dso_local i32 @main() #0 {
  %1 = alloca i32, align 4
  store i32 0, ptr %1, align 4
  call void @GL_init(i32 noundef -2)
  call void (...) @GL_clear()
  %2 = load i32, ptr @FGA_mode, align 4, !tbaa !5
  %3 = icmp eq i32 %2, 1
  br i1 %3, label %7, label %4

4:                                                ; preds = %0
  %5 = load i32, ptr @FGA_mode, align 4, !tbaa !5
  %6 = icmp eq i32 %5, 2
  br label %7

7:                                                ; preds = %4, %0
  %8 = phi i1 [ true, %0 ], [ %6, %4 ]
  %9 = zext i1 %8 to i32
  store i32 %9, ptr @colormapped, align 4, !tbaa !5
  %10 = call i32 (...) @filesystem_init()
  %11 = icmp ne i32 %10, 0
  br i1 %11, label %12, label %13

12:                                               ; preds = %7
  store i32 -1, ptr %1, align 4
  br label %35

13:                                               ; preds = %7
  store i32 0, ptr @wireframe, align 4, !tbaa !5
  br label %14

14:                                               ; preds = %29, %13
  store i32 0, ptr @cur_byte_address, align 4, !tbaa !5
  %15 = call ptr @fl_fopen(ptr noundef @.str, ptr noundef @.str.1)
  store ptr %15, ptr @F, align 8, !tbaa !9
  %16 = load ptr, ptr @F, align 8, !tbaa !9
  %17 = icmp ne ptr %16, null
  br i1 %17, label %20, label %18

18:                                               ; preds = %14
  %19 = call i32 (ptr, ...) @printf(ptr noundef @.str.2)
  store i32 -1, ptr %1, align 4
  br label %35

20:                                               ; preds = %14
  call void (...) @GL_clear()
  %21 = load i32, ptr @wireframe, align 4, !tbaa !5
  %22 = icmp ne i32 %21, 0
  %23 = zext i1 %22 to i64
  %24 = select i1 %22, i32 1, i32 2
  store i32 %24, ptr @gl_polygon_mode, align 4, !tbaa !5
  br label %25

25:                                               ; preds = %28, %20
  %26 = call i32 @read_frame()
  %27 = icmp ne i32 %26, 0
  br i1 %27, label %28, label %29

28:                                               ; preds = %25
  br label %25, !llvm.loop !11

29:                                               ; preds = %25
  %30 = load i32, ptr @wireframe, align 4, !tbaa !5
  %31 = icmp ne i32 %30, 0
  %32 = xor i1 %31, true
  %33 = zext i1 %32 to i32
  store i32 %33, ptr @wireframe, align 4, !tbaa !5
  %34 = load ptr, ptr @F, align 8, !tbaa !9
  call void @fl_fclose(ptr noundef %34)
  br label %14

35:                                               ; preds = %18, %12
  %36 = load i32, ptr %1, align 4
  ret i32 %36
}

declare void @GL_init(i32 noundef) #1

declare i32 @filesystem_init(...) #1

declare ptr @fl_fopen(ptr noundef, ptr noundef) #1

declare i32 @printf(ptr noundef, ...) #1

declare void @fl_fclose(ptr noundef) #1

attributes #0 = { nounwind uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #1 = { "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }

!llvm.module.flags = !{!0, !1, !2, !3}
!llvm.ident = !{!4}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 8, !"PIC Level", i32 2}
!2 = !{i32 7, !"PIE Level", i32 2}
!3 = !{i32 7, !"uwtable", i32 2}
!4 = !{!"clang version 19.1.0 (git@github.com:llvm/llvm-project.git a4bf6cd7cfb1a1421ba92bca9d017b49936c55e4)"}
!5 = !{!6, !6, i64 0}
!6 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}


; Optimized IR (opt -passes='default<O3>'):
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

@F = external dso_local local_unnamed_addr global ptr, align 8
@cur_byte_address = external dso_local local_unnamed_addr global i32, align 4
@wireframe = external dso_local local_unnamed_addr global i32, align 4
@colormapped = external dso_local local_unnamed_addr global i32, align 4
@FGA_mode = external local_unnamed_addr global i32, align 4
@.str = external hidden unnamed_addr constant [12 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [2 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [27 x i8], align 1
@gl_polygon_mode = external local_unnamed_addr global i32, align 4

; Function Attrs: nounwind uwtable
declare dso_local i32 @read_frame() local_unnamed_addr #0

declare void @GL_clear(...) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local noundef i32 @main() local_unnamed_addr #0 {
  tail call void @GL_init(i32 noundef -2) #3
  tail call void (...) @GL_clear() #3
  %1 = load i32, ptr @FGA_mode, align 4
  %2 = add i32 %1, -1
  %3 = icmp ult i32 %2, 2
  %4 = zext i1 %3 to i32
  store i32 %4, ptr @colormapped, align 4, !tbaa !5
  %5 = tail call i32 (...) @filesystem_init() #3
  %.not = icmp eq i32 %5, 0
  br i1 %.not, label %6, label %18

6:                                                ; preds = %0
  store i32 0, ptr @wireframe, align 4, !tbaa !5
  store i32 0, ptr @cur_byte_address, align 4, !tbaa !5
  %7 = tail call ptr @fl_fopen(ptr noundef nonnull @.str, ptr noundef nonnull @.str.1) #3
  store ptr %7, ptr @F, align 8, !tbaa !9
  %.not15 = icmp eq ptr %7, null
  br i1 %.not15, label %._crit_edge, label %.lr.ph

._crit_edge:                                      ; preds = %13, %6
  %8 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.2)
  br label %18

.lr.ph:                                           ; preds = %6, %13
  tail call void (...) @GL_clear() #3
  %9 = load i32, ptr @wireframe, align 4, !tbaa !5
  %.not2 = icmp eq i32 %9, 0
  %10 = select i1 %.not2, i32 2, i32 1
  store i32 %10, ptr @gl_polygon_mode, align 4, !tbaa !5
  br label %11

11:                                               ; preds = %11, %.lr.ph
  %12 = tail call i32 @read_frame()
  %.not3 = icmp eq i32 %12, 0
  br i1 %.not3, label %13, label %11, !llvm.loop !11

13:                                               ; preds = %11
  %14 = load i32, ptr @wireframe, align 4, !tbaa !5
  %.not4 = icmp eq i32 %14, 0
  %15 = zext i1 %.not4 to i32
  store i32 %15, ptr @wireframe, align 4, !tbaa !5
  %16 = load ptr, ptr @F, align 8, !tbaa !9
  tail call void @fl_fclose(ptr noundef %16) #3
  store i32 0, ptr @cur_byte_address, align 4, !tbaa !5
  %17 = tail call ptr @fl_fopen(ptr noundef nonnull @.str, ptr noundef nonnull @.str.1) #3
  store ptr %17, ptr @F, align 8, !tbaa !9
  %.not1 = icmp eq ptr %17, null
  br i1 %.not1, label %._crit_edge, label %.lr.ph

18:                                               ; preds = %0, %._crit_edge
  ret i32 -1
}

declare void @GL_init(i32 noundef) local_unnamed_addr #1

declare i32 @filesystem_init(...) local_unnamed_addr #1

declare ptr @fl_fopen(ptr noundef, ptr noundef) local_unnamed_addr #1

; Function Attrs: nofree nounwind
declare noundef i32 @printf(ptr nocapture noundef readonly, ...) local_unnamed_addr #2

declare void @fl_fclose(ptr noundef) local_unnamed_addr #1

attributes #0 = { nounwind uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #1 = { "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #2 = { nofree nounwind "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #3 = { nounwind }

!llvm.module.flags = !{!0, !1, !2, !3}
!llvm.ident = !{!4}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 8, !"PIC Level", i32 2}
!2 = !{i32 7, !"PIE Level", i32 2}
!3 = !{i32 7, !"uwtable", i32 2}
!4 = !{!"clang version 19.1.0 (git@github.com:llvm/llvm-project.git a4bf6cd7cfb1a1421ba92bca9d017b49936c55e4)"}
!5 = !{!6, !6, i64 0}
!6 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass