; LLVM IR from HarshCasper/NeoAlgo :: C-Plus-Plus/search/fibonacci_search.ll (Z15fibonacciSearchPiii)

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

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_Z15fibonacciSearchPiii(ptr noundef %0, i32 noundef %1, i32 noundef %2) #0 {
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %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 = alloca i32, align 4
  %13 = alloca i32, align 4
  %14 = alloca i32, align 4
  %15 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store i32 %1, ptr %6, align 4, !tbaa !9
  store i32 %2, ptr %7, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  store i32 0, ptr %8, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #3
  store i32 1, ptr %9, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #3
  %16 = load i32, ptr %9, align 4, !tbaa !9
  %17 = load i32, ptr %8, align 4, !tbaa !9
  %18 = add nsw i32 %16, %17
  store i32 %18, ptr %10, align 4, !tbaa !9
  br label %19

19:                                               ; preds = %23, %3
  %20 = load i32, ptr %10, align 4, !tbaa !9
  %21 = load i32, ptr %6, align 4, !tbaa !9
  %22 = icmp slt i32 %20, %21
  br i1 %22, label %23, label %29

23:                                               ; preds = %19
  %24 = load i32, ptr %9, align 4, !tbaa !9
  store i32 %24, ptr %8, align 4, !tbaa !9
  %25 = load i32, ptr %10, align 4, !tbaa !9
  store i32 %25, ptr %9, align 4, !tbaa !9
  %26 = load i32, ptr %9, align 4, !tbaa !9
  %27 = load i32, ptr %8, align 4, !tbaa !9
  %28 = add nsw i32 %26, %27
  store i32 %28, ptr %10, align 4, !tbaa !9
  br label %19, !llvm.loop !11

29:                                               ; preds = %19
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #3
  store i32 -1, ptr %11, align 4, !tbaa !9
  br label %30

30:                                               ; preds = %77, %29
  %31 = load i32, ptr %10, align 4, !tbaa !9
  %32 = icmp sgt i32 %31, 1
  br i1 %32, label %33, label %78

33:                                               ; preds = %30
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #3
  %34 = load i32, ptr %11, align 4, !tbaa !9
  %35 = load i32, ptr %8, align 4, !tbaa !9
  %36 = add nsw i32 %34, %35
  store i32 %36, ptr %13, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %14) #3
  %37 = load i32, ptr %6, align 4, !tbaa !9
  %38 = sub nsw i32 %37, 1
  store i32 %38, ptr %14, align 4, !tbaa !9
  %39 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3minIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4) %13, ptr noundef nonnull align 4 dereferenceable(4) %14)
  %40 = load i32, ptr %39, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 4, ptr %14) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #3
  store i32 %40, ptr %12, align 4, !tbaa !9
  %41 = load ptr, ptr %5, align 8, !tbaa !5
  %42 = load i32, ptr %12, align 4, !tbaa !9
  %43 = sext i32 %42 to i64
  %44 = getelementptr inbounds i32, ptr %41, i64 %43
  %45 = load i32, ptr %44, align 4, !tbaa !9
  %46 = load i32, ptr %7, align 4, !tbaa !9
  %47 = icmp slt i32 %45, %46
  br i1 %47, label %48, label %55

48:                                               ; preds = %33
  %49 = load i32, ptr %9, align 4, !tbaa !9
  store i32 %49, ptr %10, align 4, !tbaa !9
  %50 = load i32, ptr %8, align 4, !tbaa !9
  store i32 %50, ptr %9, align 4, !tbaa !9
  %51 = load i32, ptr %10, align 4, !tbaa !9
  %52 = load i32, ptr %9, align 4, !tbaa !9
  %53 = sub nsw i32 %51, %52
  store i32 %53, ptr %8, align 4, !tbaa !9
  %54 = load i32, ptr %12, align 4, !tbaa !9
  store i32 %54, ptr %11, align 4, !tbaa !9
  br label %74

55:                                               ; preds = %33
  %56 = load ptr, ptr %5, align 8, !tbaa !5
  %57 = load i32, ptr %12, align 4, !tbaa !9
  %58 = sext i32 %57 to i64
  %59 = getelementptr inbounds i32, ptr %56, i64 %58
  %60 = load i32, ptr %59, align 4, !tbaa !9
  %61 = load i32, ptr %7, align 4, !tbaa !9
  %62 = icmp sgt i32 %60, %61
  br i1 %62, label %63, label %71

63:                                               ; preds = %55
  %64 = load i32, ptr %8, align 4, !tbaa !9
  store i32 %64, ptr %10, align 4, !tbaa !9
  %65 = load i32, ptr %9, align 4, !tbaa !9
  %66 = load i32, ptr %8, align 4, !tbaa !9
  %67 = sub nsw i32 %65, %66
  store i32 %67, ptr %9, align 4, !tbaa !9
  %68 = load i32, ptr %10, align 4, !tbaa !9
  %69 = load i32, ptr %9, align 4, !tbaa !9
  %70 = sub nsw i32 %68, %69
  store i32 %70, ptr %8, align 4, !tbaa !9
  br label %73

71:                                               ; preds = %55
  %72 = load i32, ptr %12, align 4, !tbaa !9
  store i32 %72, ptr %4, align 4
  store i32 1, ptr %15, align 4
  br label %75

73:                                               ; preds = %63
  br label %74

74:                                               ; preds = %73, %48
  store i32 0, ptr %15, align 4
  br label %75

75:                                               ; preds = %74, %71
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #3
  %76 = load i32, ptr %15, align 4
  switch i32 %76, label %94 [
    i32 0, label %77
  ]

77:                                               ; preds = %75
  br label %30, !llvm.loop !13

78:                                               ; preds = %30
  %79 = load i32, ptr %9, align 4, !tbaa !9
  %80 = icmp ne i32 %79, 0
  br i1 %80, label %81, label %93

81:                                               ; preds = %78
  %82 = load ptr, ptr %5, align 8, !tbaa !5
  %83 = load i32, ptr %11, align 4, !tbaa !9
  %84 = add nsw i32 %83, 1
  %85 = sext i32 %84 to i64
  %86 = getelementptr inbounds i32, ptr %82, i64 %85
  %87 = load i32, ptr %86, align 4, !tbaa !9
  %88 = load i32, ptr %7, align 4, !tbaa !9
  %89 = icmp eq i32 %87, %88
  br i1 %89, label %90, label %93

90:                                               ; preds = %81
  %91 = load i32, ptr %11, align 4, !tbaa !9
  %92 = add nsw i32 %91, 1
  store i32 %92, ptr %4, align 4
  store i32 1, ptr %15, align 4
  br label %94

93:                                               ; preds = %81, %78
  store i32 -1, ptr %4, align 4
  store i32 1, ptr %15, align 4
  br label %94

94:                                               ; preds = %93, %90, %75
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  %95 = load i32, ptr %4, align 4
  ret i32 %95
}

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

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3minIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4), ptr noundef nonnull align 4 dereferenceable(4)) #2

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

attributes #0 = { mustprogress 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 = { inlinehint mustprogress 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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12}


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

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i32 @_Z15fibonacciSearchPiii(ptr nocapture noundef readonly %0, i32 noundef %1, i32 noundef %2) local_unnamed_addr #0 {
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  br label %6

6:                                                ; preds = %6, %3
  %.046 = phi i32 [ 0, %3 ], [ %.042, %6 ]
  %.042 = phi i32 [ 1, %3 ], [ %.039, %6 ]
  %.039 = phi i32 [ 1, %3 ], [ %8, %6 ]
  %7 = icmp slt i32 %.039, %1
  %8 = add nsw i32 %.039, %.042
  br i1 %7, label %6, label %.preheader, !llvm.loop !5

.preheader:                                       ; preds = %6
  %9 = icmp sgt i32 %.039, 1
  br i1 %9, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %.preheader
  %10 = add nsw i32 %1, -1
  br label %11

11:                                               ; preds = %.lr.ph, %26
  %.03663 = phi i32 [ -1, %.lr.ph ], [ %.238, %26 ]
  %.14362 = phi i32 [ %.042, %.lr.ph ], [ %.345, %26 ]
  %.14761 = phi i32 [ %.046, %.lr.ph ], [ %.349, %26 ]
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %4) #3
  %12 = add nsw i32 %.03663, %.14761
  store i32 %12, ptr %4, align 4, !tbaa !7
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %5) #3
  store i32 %10, ptr %5, align 4, !tbaa !7
  %13 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3minIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4) %4, ptr noundef nonnull align 4 dereferenceable(4) %5)
  %14 = load i32, ptr %13, align 4, !tbaa !7
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %5) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %4) #3
  %15 = sext i32 %14 to i64
  %16 = getelementptr inbounds i32, ptr %0, i64 %15
  %17 = load i32, ptr %16, align 4, !tbaa !7
  %18 = icmp slt i32 %17, %2
  br i1 %18, label %19, label %21

19:                                               ; preds = %11
  %20 = sub nsw i32 %.14362, %.14761
  br label %26

21:                                               ; preds = %11
  %22 = icmp sgt i32 %17, %2
  br i1 %22, label %23, label %.thread

23:                                               ; preds = %21
  %24 = sub nsw i32 %.14362, %.14761
  %25 = sub nsw i32 %.14761, %24
  br label %26

26:                                               ; preds = %19, %23
  %.349 = phi i32 [ %20, %19 ], [ %25, %23 ]
  %.345 = phi i32 [ %.14761, %19 ], [ %24, %23 ]
  %.3 = phi i32 [ %.14362, %19 ], [ %.14761, %23 ]
  %.238 = phi i32 [ %14, %19 ], [ %.03663, %23 ]
  %27 = icmp sgt i32 %.3, 1
  br i1 %27, label %11, label %._crit_edge.loopexit, !llvm.loop !11

._crit_edge.loopexit:                             ; preds = %26
  %28 = add nsw i32 %.238, 1
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit, %.preheader
  %.143.lcssa = phi i32 [ %.042, %.preheader ], [ %.345, %._crit_edge.loopexit ]
  %.036.lcssa = phi i32 [ 0, %.preheader ], [ %28, %._crit_edge.loopexit ]
  %.not = icmp eq i32 %.143.lcssa, 0
  br i1 %.not, label %34, label %29

29:                                               ; preds = %._crit_edge
  %30 = sext i32 %.036.lcssa to i64
  %31 = getelementptr inbounds i32, ptr %0, i64 %30
  %32 = load i32, ptr %31, align 4, !tbaa !7
  %33 = icmp eq i32 %32, %2
  br i1 %33, label %.thread, label %34

34:                                               ; preds = %29, %._crit_edge
  br label %.thread

.thread:                                          ; preds = %21, %29, %34
  %.2 = phi i32 [ -1, %34 ], [ %.036.lcssa, %29 ], [ %14, %21 ]
  ret i32 %.2
}

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

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3minIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4), ptr noundef nonnull align 4 dereferenceable(4)) local_unnamed_addr #2

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

attributes #0 = { mustprogress 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 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { inlinehint mustprogress 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 = distinct !{!5, !6}
!6 = !{!"llvm.loop.mustprogress"}
!7 = !{!8, !8, i64 0}
!8 = !{!"int", !9, i64 0}
!9 = !{!"omnipotent char", !10, i64 0}
!10 = !{!"Simple C++ TBAA"}
!11 = distinct !{!11, !6}


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