; LLVM IR from graycatlabs/serbus :: examples (spi_ad7390)

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

@running = external hidden global i8, align 1
@.str = external hidden unnamed_addr constant [28 x i8], align 1

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

; Function Attrs: nounwind uwtable
declare dso_local void @AD7390_setValue(i32 noundef, i16 noundef zeroext) #0

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

; Function Attrs: nounwind uwtable
define dso_local i32 @main() #0 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  store i32 0, ptr %1, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #5
  %4 = call i32 @SPI_open(i8 noundef zeroext 1, i8 noundef zeroext 0)
  store i32 %4, ptr %2, align 4, !tbaa !5
  %5 = load i32, ptr %2, align 4, !tbaa !5
  %6 = icmp slt i32 %5, 0
  br i1 %6, label %7, label %9

7:                                                ; preds = %0
  %8 = call i32 (ptr, ...) @printf(ptr noundef @.str, i32 noundef 1)
  call void @exit(i32 noundef 0) #6
  unreachable

9:                                                ; preds = %0
  %10 = load i32, ptr %2, align 4, !tbaa !5
  call void @AD7390_SPIConfig(i32 noundef %10)
  store i8 1, ptr @running, align 1, !tbaa !9
  %11 = call ptr @signal(i32 noundef 2, ptr noundef @stopHandler) #5
  br label %12

12:                                               ; preds = %37, %9
  %13 = load i8, ptr @running, align 1, !tbaa !9
  %14 = icmp ne i8 %13, 0
  br i1 %14, label %15, label %38

15:                                               ; preds = %12
  store i32 0, ptr %3, align 4, !tbaa !5
  br label %16

16:                                               ; preds = %23, %15
  %17 = load i32, ptr %3, align 4, !tbaa !5
  %18 = icmp slt i32 %17, 4095
  br i1 %18, label %19, label %26

19:                                               ; preds = %16
  %20 = load i32, ptr %2, align 4, !tbaa !5
  %21 = load i32, ptr %3, align 4, !tbaa !5
  %22 = trunc i32 %21 to i16
  call void @AD7390_setValue(i32 noundef %20, i16 noundef zeroext %22)
  br label %23

23:                                               ; preds = %19
  %24 = load i32, ptr %3, align 4, !tbaa !5
  %25 = add nsw i32 %24, 1
  store i32 %25, ptr %3, align 4, !tbaa !5
  br label %16, !llvm.loop !10

26:                                               ; preds = %16
  store i32 4096, ptr %3, align 4, !tbaa !5
  br label %27

27:                                               ; preds = %34, %26
  %28 = load i32, ptr %3, align 4, !tbaa !5
  %29 = icmp sgt i32 %28, 0
  br i1 %29, label %30, label %37

30:                                               ; preds = %27
  %31 = load i32, ptr %2, align 4, !tbaa !5
  %32 = load i32, ptr %3, align 4, !tbaa !5
  %33 = trunc i32 %32 to i16
  call void @AD7390_setValue(i32 noundef %31, i16 noundef zeroext %33)
  br label %34

34:                                               ; preds = %30
  %35 = load i32, ptr %3, align 4, !tbaa !5
  %36 = add nsw i32 %35, -1
  store i32 %36, ptr %3, align 4, !tbaa !5
  br label %27, !llvm.loop !12

37:                                               ; preds = %27
  br label %12, !llvm.loop !13

38:                                               ; preds = %12
  %39 = load i32, ptr %2, align 4, !tbaa !5
  call void @AD7390_setValue(i32 noundef %39, i16 noundef zeroext 0)
  %40 = load i32, ptr %2, align 4, !tbaa !5
  call void @SPI_close(i32 noundef %40)
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #5
  ret i32 0
}

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

declare i32 @SPI_open(i8 noundef zeroext, i8 noundef zeroext) #2

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

; Function Attrs: noreturn nounwind
declare void @exit(i32 noundef) #3

; Function Attrs: nounwind
declare ptr @signal(i32 noundef, ptr noundef) #4

declare void @SPI_close(i32 noundef) #2

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #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 = { 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 = { noreturn 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 #4 = { 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 #5 = { nounwind }
attributes #6 = { noreturn 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 = !{!7, !7, i64 0}
!10 = distinct !{!10, !11}
!11 = !{!"llvm.loop.mustprogress"}
!12 = distinct !{!12, !11}
!13 = distinct !{!13, !11}


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

@running = external hidden local_unnamed_addr global i8, align 1
@.str = external hidden unnamed_addr constant [28 x i8], align 1

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

; Function Attrs: nounwind uwtable
declare dso_local void @AD7390_setValue(i32 noundef, i16 noundef zeroext) local_unnamed_addr #0

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

; Function Attrs: nounwind uwtable
define dso_local noundef i32 @main() local_unnamed_addr #0 {
  %1 = tail call i32 @SPI_open(i8 noundef zeroext 1, i8 noundef zeroext 0) #5
  %2 = icmp slt i32 %1, 0
  br i1 %2, label %3, label %5

3:                                                ; preds = %0
  %4 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str, i32 noundef 1)
  tail call void @exit(i32 noundef 0) #6
  unreachable

5:                                                ; preds = %0
  tail call void @AD7390_SPIConfig(i32 noundef %1)
  store i8 1, ptr @running, align 1, !tbaa !5
  %6 = tail call ptr @signal(i32 noundef 2, ptr noundef nonnull @stopHandler) #5
  %7 = load i8, ptr @running, align 1, !tbaa !5
  %.not16 = icmp eq i8 %7, 0
  br i1 %.not16, label %._crit_edge, label %.preheader13

.loopexit:                                        ; preds = %.preheader
  %8 = load i8, ptr @running, align 1, !tbaa !5
  %.not = icmp eq i8 %8, 0
  br i1 %.not, label %._crit_edge, label %.preheader13.backedge

.preheader13:                                     ; preds = %5, %.preheader13.backedge
  %.014 = phi i32 [ %.014.be, %.preheader13.backedge ], [ 0, %5 ]
  %9 = trunc nuw i32 %.014 to i16
  tail call void @AD7390_setValue(i32 noundef %1, i16 noundef zeroext %9)
  %10 = add nuw nsw i32 %.014, 1
  %exitcond.not = icmp eq i32 %10, 4095
  br i1 %exitcond.not, label %.preheader, label %.preheader13.backedge

.preheader13.backedge:                            ; preds = %.preheader13, %.loopexit
  %.014.be = phi i32 [ %10, %.preheader13 ], [ 0, %.loopexit ]
  br label %.preheader13, !llvm.loop !8

.preheader:                                       ; preds = %.preheader13, %.preheader
  %.115 = phi i32 [ %12, %.preheader ], [ 4096, %.preheader13 ]
  %11 = trunc i32 %.115 to i16
  tail call void @AD7390_setValue(i32 noundef %1, i16 noundef zeroext %11)
  %12 = add nsw i32 %.115, -1
  %13 = icmp ugt i32 %.115, 1
  br i1 %13, label %.preheader, label %.loopexit, !llvm.loop !10

._crit_edge:                                      ; preds = %.loopexit, %5
  tail call void @AD7390_setValue(i32 noundef %1, i16 noundef zeroext 0)
  tail call void @SPI_close(i32 noundef %1) #5
  ret i32 0
}

declare i32 @SPI_open(i8 noundef zeroext, i8 noundef zeroext) local_unnamed_addr #1

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

; Function Attrs: noreturn nounwind
declare void @exit(i32 noundef) local_unnamed_addr #3

; Function Attrs: nounwind
declare ptr @signal(i32 noundef, ptr noundef) local_unnamed_addr #4

declare void @SPI_close(i32 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 = { noreturn 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 #4 = { 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 #5 = { nounwind }
attributes #6 = { noreturn 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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = distinct !{!8, !9}
!9 = !{!"llvm.loop.mustprogress"}
!10 = distinct !{!10, !9}


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