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

; 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"

@wireframe = external dso_local global i32, align 4
@.str.1 = external hidden unnamed_addr constant [12 x i8], align 1
@gl_polygon_mode = external global i32, align 4

; Function Attrs: nounwind uwtable
declare dso_local void @spi_reset() #0

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #1

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

; Function Attrs: nounwind uwtable
declare dso_local void @print_frame(i32 noundef) #0

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

; Function Attrs: nounwind uwtable
define dso_local i32 @main() #0 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  store i32 0, ptr %1, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #3
  call void @GL_init(i32 noundef -1)
  call void (...) @GL_clear()
  call void (...) @MAX7219_tty_init()
  %3 = call i32 (ptr, ...) @printf(ptr noundef @.str.1)
  call void @GL_tty_init(i32 noundef -1)
  store i32 0, ptr @wireframe, align 4, !tbaa !5
  br label %4

4:                                                ; preds = %16, %0
  store i32 0, ptr %2, align 4, !tbaa !5
  call void @spi_reset()
  %5 = load i32, ptr @wireframe, align 4, !tbaa !5
  %6 = icmp ne i32 %5, 0
  %7 = zext i1 %6 to i64
  %8 = select i1 %6, i32 1, i32 2
  store i32 %8, ptr @gl_polygon_mode, align 4, !tbaa !5
  br label %9

9:                                                ; preds = %12, %4
  %10 = call i32 @read_frame()
  %11 = icmp ne i32 %10, 0
  br i1 %11, label %12, label %16

12:                                               ; preds = %9
  %13 = load i32, ptr %2, align 4, !tbaa !5
  call void @print_frame(i32 noundef %13)
  %14 = load i32, ptr %2, align 4, !tbaa !5
  %15 = add nsw i32 %14, 1
  store i32 %15, ptr %2, align 4, !tbaa !5
  br label %9, !llvm.loop !9

16:                                               ; preds = %9
  %17 = load i32, ptr @wireframe, align 4, !tbaa !5
  %18 = icmp ne i32 %17, 0
  %19 = xor i1 %18, true
  %20 = zext i1 %19 to i32
  store i32 %20, ptr @wireframe, align 4, !tbaa !5
  br label %4
}

declare void @GL_init(i32 noundef) #2

declare void @GL_clear(...) #2

declare void @MAX7219_tty_init(...) #2

declare void @GL_tty_init(i32 noundef) #2

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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { "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 = distinct !{!9, !10}
!10 = !{!"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"

@wireframe = external dso_local local_unnamed_addr global i32, align 4
@.str.1 = external hidden unnamed_addr constant [12 x i8], align 1
@gl_polygon_mode = external local_unnamed_addr global i32, align 4

; Function Attrs: nounwind uwtable
declare dso_local void @spi_reset() local_unnamed_addr #0

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

; Function Attrs: nounwind uwtable
declare dso_local void @print_frame(i32 noundef) local_unnamed_addr #0

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

; Function Attrs: noreturn nounwind uwtable
define dso_local noundef i32 @main() local_unnamed_addr #2 {
  tail call void @GL_init(i32 noundef -1) #4
  tail call void (...) @GL_clear() #4
  tail call void (...) @MAX7219_tty_init() #4
  %1 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1)
  tail call void @GL_tty_init(i32 noundef -1) #4
  br label %2

2:                                                ; preds = %._crit_edge, %0
  %storemerge = phi i32 [ 0, %0 ], [ %9, %._crit_edge ]
  store i32 %storemerge, ptr @wireframe, align 4, !tbaa !5
  tail call void @spi_reset()
  %3 = load i32, ptr @wireframe, align 4, !tbaa !5
  %.not = icmp eq i32 %3, 0
  %4 = select i1 %.not, i32 2, i32 1
  store i32 %4, ptr @gl_polygon_mode, align 4, !tbaa !5
  %5 = tail call i32 @read_frame()
  %.not24 = icmp eq i32 %5, 0
  br i1 %.not24, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %2, %.lr.ph
  %.05 = phi i32 [ %6, %.lr.ph ], [ 0, %2 ]
  tail call void @print_frame(i32 noundef %.05)
  %6 = add nuw nsw i32 %.05, 1
  %7 = tail call i32 @read_frame()
  %.not2 = icmp eq i32 %7, 0
  br i1 %.not2, label %._crit_edge, label %.lr.ph, !llvm.loop !9

._crit_edge:                                      ; preds = %.lr.ph, %2
  %8 = load i32, ptr @wireframe, align 4, !tbaa !5
  %.not3 = icmp eq i32 %8, 0
  %9 = zext i1 %.not3 to i32
  br label %2
}

declare void @GL_init(i32 noundef) local_unnamed_addr #3

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

declare void @MAX7219_tty_init(...) local_unnamed_addr #3

declare void @GL_tty_init(i32 noundef) local_unnamed_addr #3

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 = { 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 #2 = { noreturn 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 #3 = { "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 #4 = { 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 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}


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