; LLVM IR from codemistic/Data-Structures-and-Algorithms :: Arrays (31_Menu_for_Array)

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

%struct.array = type { ptr, i32, i32 }

@.str.7 = external hidden unnamed_addr constant [37 x i8], align 1
@.str.11 = external hidden unnamed_addr constant [26 x i8], align 1

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

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

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

; Function Attrs: nounwind uwtable
define dso_local void @RevBinSearch(ptr %0, i64 %1, i32 noundef %2, i32 noundef %3, i32 noundef %4) #2 {
  %6 = alloca %struct.array, align 8
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  %12 = getelementptr inbounds { ptr, i64 }, ptr %6, i32 0, i32 0
  store ptr %0, ptr %12, align 8
  %13 = getelementptr inbounds { ptr, i64 }, ptr %6, i32 0, i32 1
  store i64 %1, ptr %13, align 8
  store i32 %2, ptr %7, align 4, !tbaa !5
  store i32 %3, ptr %8, align 4, !tbaa !5
  store i32 %4, ptr %9, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #3
  store i32 0, ptr %11, align 4, !tbaa !5
  %14 = load i32, ptr %7, align 4, !tbaa !5
  %15 = load i32, ptr %8, align 4, !tbaa !5
  %16 = icmp sle i32 %14, %15
  br i1 %16, label %17, label %72

17:                                               ; preds = %5
  %18 = load i32, ptr %7, align 4, !tbaa !5
  %19 = load i32, ptr %8, align 4, !tbaa !5
  %20 = add nsw i32 %18, %19
  %21 = sdiv i32 %20, 2
  store i32 %21, ptr %10, align 4, !tbaa !5
  %22 = getelementptr inbounds %struct.array, ptr %6, i32 0, i32 0
  %23 = load ptr, ptr %22, align 8, !tbaa !9
  %24 = load i32, ptr %10, align 4, !tbaa !5
  %25 = sext i32 %24 to i64
  %26 = getelementptr inbounds i32, ptr %23, i64 %25
  %27 = load i32, ptr %26, align 4, !tbaa !5
  %28 = load i32, ptr %9, align 4, !tbaa !5
  %29 = icmp eq i32 %27, %28
  br i1 %29, label %30, label %33

30:                                               ; preds = %17
  %31 = load i32, ptr %10, align 4, !tbaa !5
  %32 = call i32 (ptr, ...) @printf(ptr noundef @.str.7, i32 noundef %31)
  store i32 1, ptr %11, align 4, !tbaa !5
  br label %71

33:                                               ; preds = %17
  %34 = load i32, ptr %9, align 4, !tbaa !5
  %35 = getelementptr inbounds %struct.array, ptr %6, i32 0, i32 0
  %36 = load ptr, ptr %35, align 8, !tbaa !9
  %37 = load i32, ptr %10, align 4, !tbaa !5
  %38 = sext i32 %37 to i64
  %39 = getelementptr inbounds i32, ptr %36, i64 %38
  %40 = load i32, ptr %39, align 4, !tbaa !5
  %41 = icmp slt i32 %34, %40
  br i1 %41, label %42, label %51

42:                                               ; preds = %33
  %43 = load i32, ptr %7, align 4, !tbaa !5
  %44 = load i32, ptr %10, align 4, !tbaa !5
  %45 = sub nsw i32 %44, 1
  %46 = load i32, ptr %9, align 4, !tbaa !5
  %47 = getelementptr inbounds { ptr, i64 }, ptr %6, i32 0, i32 0
  %48 = load ptr, ptr %47, align 8
  %49 = getelementptr inbounds { ptr, i64 }, ptr %6, i32 0, i32 1
  %50 = load i64, ptr %49, align 8
  call void @RevBinSearch(ptr %48, i64 %50, i32 noundef %43, i32 noundef %45, i32 noundef %46)
  br label %70

51:                                               ; preds = %33
  %52 = load i32, ptr %9, align 4, !tbaa !5
  %53 = getelementptr inbounds %struct.array, ptr %6, i32 0, i32 0
  %54 = load ptr, ptr %53, align 8, !tbaa !9
  %55 = load i32, ptr %10, align 4, !tbaa !5
  %56 = sext i32 %55 to i64
  %57 = getelementptr inbounds i32, ptr %54, i64 %56
  %58 = load i32, ptr %57, align 4, !tbaa !5
  %59 = icmp sgt i32 %52, %58
  br i1 %59, label %60, label %69

60:                                               ; preds = %51
  %61 = load i32, ptr %10, align 4, !tbaa !5
  %62 = add nsw i32 %61, 1
  %63 = load i32, ptr %8, align 4, !tbaa !5
  %64 = load i32, ptr %9, align 4, !tbaa !5
  %65 = getelementptr inbounds { ptr, i64 }, ptr %6, i32 0, i32 0
  %66 = load ptr, ptr %65, align 8
  %67 = getelementptr inbounds { ptr, i64 }, ptr %6, i32 0, i32 1
  %68 = load i64, ptr %67, align 8
  call void @RevBinSearch(ptr %66, i64 %68, i32 noundef %62, i32 noundef %63, i32 noundef %64)
  br label %69

69:                                               ; preds = %60, %51
  br label %70

70:                                               ; preds = %69, %42
  br label %71

71:                                               ; preds = %70, %30
  br label %74

72:                                               ; preds = %5
  %73 = call i32 (ptr, ...) @printf(ptr noundef @.str.11)
  br label %74

74:                                               ; preds = %72, %71
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #3
  ret void
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { 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 = { 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, !11, i64 0}
!10 = !{!"array", !11, i64 0, !6, i64 8, !6, i64 12}
!11 = !{!"any pointer", !7, i64 0}


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

@.str.7 = external hidden unnamed_addr constant [37 x i8], align 1
@.str.11 = external hidden unnamed_addr constant [26 x i8], align 1

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

; Function Attrs: nofree nounwind uwtable
define dso_local void @RevBinSearch(ptr nocapture readonly %0, i64 %1, i32 noundef %2, i32 noundef %3, i32 noundef %4) local_unnamed_addr #1 {
  %.not3436 = icmp sgt i32 %2, %3
  br i1 %.not3436, label %tailrecurse.outer._crit_edge, label %.lr.ph

tailrecurse.outer:                                ; preds = %19
  %6 = add nsw i32 %9, 1
  %.not34.not = icmp slt i32 %9, %.tr2735
  br i1 %.not34.not, label %.lr.ph, label %tailrecurse.outer._crit_edge

.lr.ph:                                           ; preds = %5, %tailrecurse.outer
  %.tr27.ph38 = phi i32 [ %.tr2735, %tailrecurse.outer ], [ %3, %5 ]
  %.tr26.ph37 = phi i32 [ %6, %tailrecurse.outer ], [ %2, %5 ]
  br label %7

7:                                                ; preds = %.lr.ph, %tailrecurse
  %.tr2735 = phi i32 [ %.tr27.ph38, %.lr.ph ], [ %18, %tailrecurse ]
  %8 = add nsw i32 %.tr2735, %.tr26.ph37
  %9 = sdiv i32 %8, 2
  %10 = sext i32 %9 to i64
  %11 = getelementptr inbounds i32, ptr %0, i64 %10
  %12 = load i32, ptr %11, align 4, !tbaa !5
  %13 = icmp eq i32 %12, %4
  br i1 %13, label %14, label %16

14:                                               ; preds = %7
  %15 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.7, i32 noundef %9)
  br label %.loopexit

16:                                               ; preds = %7
  %17 = icmp sgt i32 %12, %4
  br i1 %17, label %tailrecurse, label %19

tailrecurse:                                      ; preds = %16
  %18 = add nsw i32 %9, -1
  %.not.not = icmp slt i32 %.tr26.ph37, %9
  br i1 %.not.not, label %7, label %tailrecurse.outer._crit_edge

19:                                               ; preds = %16
  %20 = icmp slt i32 %12, %4
  br i1 %20, label %tailrecurse.outer, label %.loopexit

tailrecurse.outer._crit_edge:                     ; preds = %tailrecurse.outer, %tailrecurse, %5
  %21 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.11)
  br label %.loopexit

.loopexit:                                        ; preds = %19, %14, %tailrecurse.outer._crit_edge
  ret void
}

attributes #0 = { 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 #1 = { nofree 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" }

!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"}


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