; LLVM IR from CactiLab/Sherloc-Cortex-M-CFVD :: Sherloc_runtime/evaluation/src/sglib_arraybinsearch (arraybinsearch)

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

@array = external dso_local global [100 x i32], align 16
@found = external dso_local global i32, align 4

; Function Attrs: nounwind uwtable
define dso_local i32 @benchmark_sg_binsearch() #0 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr %1) #2
  store volatile i32 0, ptr %1, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #2
  store i32 0, ptr %3, align 4, !tbaa !5
  store i32 0, ptr %4, align 4, !tbaa !5
  br label %10

10:                                               ; preds = %77, %0
  %11 = load i32, ptr %4, align 4, !tbaa !5
  %12 = icmp slt i32 %11, 100
  br i1 %12, label %13, label %80

13:                                               ; preds = %10
  %14 = load i32, ptr %4, align 4, !tbaa !5
  %15 = sext i32 %14 to i64
  %16 = getelementptr inbounds [100 x i32], ptr @array, i64 0, i64 %15
  %17 = load i32, ptr %16, align 4, !tbaa !5
  store i32 %17, ptr %2, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #2
  store i32 0, ptr %7, align 4, !tbaa !5
  %18 = load i32, ptr %4, align 4, !tbaa !5
  store i32 %18, ptr %8, align 4, !tbaa !5
  store i32 0, ptr %9, align 4, !tbaa !5
  br label %19

19:                                               ; preds = %65, %13
  %20 = load i32, ptr %7, align 4, !tbaa !5
  %21 = load i32, ptr %8, align 4, !tbaa !5
  %22 = icmp sle i32 %20, %21
  br i1 %22, label %23, label %26

23:                                               ; preds = %19
  %24 = load i32, ptr %9, align 4, !tbaa !5
  %25 = icmp eq i32 %24, 0
  br label %26

26:                                               ; preds = %23, %19
  %27 = phi i1 [ false, %19 ], [ %25, %23 ]
  br i1 %27, label %28, label %66

28:                                               ; preds = %26
  %29 = load i32, ptr %8, align 4, !tbaa !5
  %30 = load i32, ptr %7, align 4, !tbaa !5
  %31 = add nsw i32 %29, %30
  %32 = sdiv i32 %31, 2
  store i32 %32, ptr %5, align 4, !tbaa !5
  %33 = load i32, ptr %5, align 4, !tbaa !5
  %34 = sext i32 %33 to i64
  %35 = getelementptr inbounds [100 x i32], ptr @array, i64 0, i64 %34
  %36 = load i32, ptr %35, align 4, !tbaa !5
  %37 = load i32, ptr %2, align 4, !tbaa !5
  %38 = icmp sgt i32 %36, %37
  br i1 %38, label %39, label %40

39:                                               ; preds = %28
  br label %49

40:                                               ; preds = %28
  %41 = load i32, ptr %5, align 4, !tbaa !5
  %42 = sext i32 %41 to i64
  %43 = getelementptr inbounds [100 x i32], ptr @array, i64 0, i64 %42
  %44 = load i32, ptr %43, align 4, !tbaa !5
  %45 = load i32, ptr %2, align 4, !tbaa !5
  %46 = icmp slt i32 %44, %45
  %47 = zext i1 %46 to i64
  %48 = select i1 %46, i32 -1, i32 0
  br label %49

49:                                               ; preds = %40, %39
  %50 = phi i32 [ 1, %39 ], [ %48, %40 ]
  store i32 %50, ptr %6, align 4, !tbaa !5
  %51 = load i32, ptr %6, align 4, !tbaa !5
  %52 = icmp eq i32 %51, 0
  br i1 %52, label %53, label %55

53:                                               ; preds = %49
  %54 = load i32, ptr %5, align 4, !tbaa !5
  store i32 %54, ptr %3, align 4, !tbaa !5
  store i32 1, ptr %9, align 4, !tbaa !5
  br label %65

55:                                               ; preds = %49
  %56 = load i32, ptr %6, align 4, !tbaa !5
  %57 = icmp slt i32 %56, 0
  br i1 %57, label %58, label %61

58:                                               ; preds = %55
  %59 = load i32, ptr %5, align 4, !tbaa !5
  %60 = add nsw i32 %59, 1
  store i32 %60, ptr %7, align 4, !tbaa !5
  br label %64

61:                                               ; preds = %55
  %62 = load i32, ptr %5, align 4, !tbaa !5
  %63 = sub nsw i32 %62, 1
  store i32 %63, ptr %8, align 4, !tbaa !5
  br label %64

64:                                               ; preds = %61, %58
  br label %65

65:                                               ; preds = %64, %53
  br label %19, !llvm.loop !9

66:                                               ; preds = %26
  %67 = load i32, ptr %9, align 4, !tbaa !5
  %68 = icmp eq i32 %67, 0
  br i1 %68, label %69, label %72

69:                                               ; preds = %66
  %70 = load i32, ptr %8, align 4, !tbaa !5
  %71 = add nsw i32 %70, 1
  store i32 %71, ptr %3, align 4, !tbaa !5
  br label %72

72:                                               ; preds = %69, %66
  %73 = load i32, ptr %9, align 4, !tbaa !5
  store volatile i32 %73, ptr @found, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  %74 = load i32, ptr %3, align 4, !tbaa !5
  %75 = load volatile i32, ptr %1, align 4, !tbaa !5
  %76 = add nsw i32 %75, %74
  store volatile i32 %76, ptr %1, align 4, !tbaa !5
  br label %77

77:                                               ; preds = %72
  %78 = load i32, ptr %4, align 4, !tbaa !5
  %79 = add nsw i32 %78, 1
  store i32 %79, ptr %4, align 4, !tbaa !5
  br label %10, !llvm.loop !11

80:                                               ; preds = %10
  %81 = load volatile i32, ptr %1, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #2
  ret i32 %81
}

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

; 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 = { 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"}
!11 = distinct !{!11, !10}


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

@array = external dso_local local_unnamed_addr global [100 x i32], align 16
@found = external dso_local global i32, align 4

; Function Attrs: nofree norecurse nounwind memory(readwrite, argmem: none) uwtable
define dso_local i32 @benchmark_sg_binsearch() local_unnamed_addr #0 {
  %1 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %1)
  store volatile i32 0, ptr %1, align 4, !tbaa !5
  br label %2

2:                                                ; preds = %0, %.thread
  %indvars.iv = phi i64 [ 0, %0 ], [ %indvars.iv.next, %.thread ]
  %.02737 = phi i32 [ 0, %0 ], [ %spec.select, %.thread ]
  %3 = getelementptr inbounds [100 x i32], ptr @array, i64 0, i64 %indvars.iv
  %4 = load i32, ptr %3, align 4, !tbaa !5
  %5 = trunc nuw nsw i64 %indvars.iv to i32
  br label %6

6:                                                ; preds = %2, %13
  %.02335 = phi i32 [ %5, %2 ], [ %.124, %13 ]
  %.02534 = phi i32 [ 0, %2 ], [ %.126, %13 ]
  %7 = add nsw i32 %.02335, %.02534
  %8 = sdiv i32 %7, 2
  %9 = sext i32 %8 to i64
  %10 = getelementptr inbounds [100 x i32], ptr @array, i64 0, i64 %9
  %11 = load i32, ptr %10, align 4, !tbaa !5
  %12 = icmp ne i32 %11, %4
  br i1 %12, label %13, label %.thread

13:                                               ; preds = %6
  %14 = icmp slt i32 %11, %4
  %15 = add nsw i32 %8, 1
  %16 = add nsw i32 %8, -1
  %.126 = select i1 %14, i32 %15, i32 %.02534
  %.124 = select i1 %14, i32 %.02335, i32 %16
  %17 = icmp sle i32 %.126, %.124
  %18 = and i1 %17, %12
  br i1 %18, label %6, label %.thread, !llvm.loop !9

.thread:                                          ; preds = %6, %13
  %.145 = phi i32 [ 0, %13 ], [ 1, %6 ]
  %.12444 = phi i32 [ %.124, %13 ], [ %.02335, %6 ]
  %.243 = phi i32 [ %.02737, %13 ], [ %8, %6 ]
  %19 = add nsw i32 %.12444, 1
  %spec.select = select i1 %12, i32 %19, i32 %.243
  store volatile i32 %.145, ptr @found, align 4, !tbaa !5
  %.0..0..0..0. = load volatile i32, ptr %1, align 4, !tbaa !5
  %20 = add nsw i32 %.0..0..0..0., %spec.select
  store volatile i32 %20, ptr %1, align 4, !tbaa !5
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, 100
  br i1 %exitcond.not, label %21, label %2, !llvm.loop !11

21:                                               ; preds = %.thread
  %.0..0..0..0.22 = load volatile i32, ptr %1, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %1)
  ret i32 %.0..0..0..0.22
}

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

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

attributes #0 = { nofree norecurse nounwind memory(readwrite, argmem: none) 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 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }

!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"}
!11 = distinct !{!11, !10}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass