; LLVM IR from smv1999/CompetitiveProgrammingQuestionBank :: CodeVita Problems/consecutive_prime_sum.ll (Z12binarySearchPiii)

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

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef zeroext i1 @_Z12binarySearchPiii(ptr noundef %0, i32 noundef %1, i32 noundef %2) #1 {
  %4 = alloca i1, align 1
  %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
  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) #2
  store i32 0, ptr %8, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #2
  %12 = load i32, ptr %6, align 4, !tbaa !9
  %13 = sub nsw i32 %12, 1
  store i32 %13, ptr %9, align 4, !tbaa !9
  br label %14

14:                                               ; preds = %58, %3
  %15 = load i32, ptr %8, align 4, !tbaa !9
  %16 = load i32, ptr %9, align 4, !tbaa !9
  %17 = icmp sle i32 %15, %16
  br i1 %17, label %18, label %59

18:                                               ; preds = %14
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #2
  %19 = load i32, ptr %8, align 4, !tbaa !9
  %20 = load i32, ptr %9, align 4, !tbaa !9
  %21 = add nsw i32 %19, %20
  %22 = sdiv i32 %21, 2
  store i32 %22, ptr %10, align 4, !tbaa !9
  %23 = load ptr, ptr %5, align 8, !tbaa !5
  %24 = load i32, ptr %10, align 4, !tbaa !9
  %25 = sext i32 %24 to i64
  %26 = getelementptr inbounds i32, ptr %23, i64 %25
  %27 = load i32, ptr %26, align 4, !tbaa !9
  %28 = load i32, ptr %7, align 4, !tbaa !9
  %29 = icmp sgt i32 %27, %28
  br i1 %29, label %30, label %33

30:                                               ; preds = %18
  %31 = load i32, ptr %9, align 4, !tbaa !9
  %32 = sub nsw i32 %31, 1
  store i32 %32, ptr %9, align 4, !tbaa !9
  br label %55

33:                                               ; preds = %18
  %34 = load ptr, ptr %5, align 8, !tbaa !5
  %35 = load i32, ptr %10, align 4, !tbaa !9
  %36 = sext i32 %35 to i64
  %37 = getelementptr inbounds i32, ptr %34, i64 %36
  %38 = load i32, ptr %37, align 4, !tbaa !9
  %39 = load i32, ptr %7, align 4, !tbaa !9
  %40 = icmp slt i32 %38, %39
  br i1 %40, label %41, label %44

41:                                               ; preds = %33
  %42 = load i32, ptr %8, align 4, !tbaa !9
  %43 = add nsw i32 %42, 1
  store i32 %43, ptr %8, align 4, !tbaa !9
  br label %54

44:                                               ; preds = %33
  %45 = load ptr, ptr %5, align 8, !tbaa !5
  %46 = load i32, ptr %10, align 4, !tbaa !9
  %47 = sext i32 %46 to i64
  %48 = getelementptr inbounds i32, ptr %45, i64 %47
  %49 = load i32, ptr %48, align 4, !tbaa !9
  %50 = load i32, ptr %7, align 4, !tbaa !9
  %51 = icmp eq i32 %49, %50
  br i1 %51, label %52, label %53

52:                                               ; preds = %44
  store i1 true, ptr %4, align 1
  store i32 1, ptr %11, align 4
  br label %56

53:                                               ; preds = %44
  br label %54

54:                                               ; preds = %53, %41
  br label %55

55:                                               ; preds = %54, %30
  store i32 0, ptr %11, align 4
  br label %56

56:                                               ; preds = %55, %52
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #2
  %57 = load i32, ptr %11, align 4
  switch i32 %57, label %60 [
    i32 0, label %58
  ]

58:                                               ; preds = %56
  br label %14, !llvm.loop !11

59:                                               ; preds = %14
  store i1 false, ptr %4, align 1
  store i32 1, ptr %11, align 4
  br label %60

60:                                               ; preds = %59, %56
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  %61 = load i1, ptr %4, align 1
  ret i1 %61
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #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 = !{!"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"}


; 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 nofree norecurse nosync nounwind willreturn memory(argmem: read) uwtable
define dso_local noundef zeroext i1 @_Z12binarySearchPiii(ptr nocapture noundef readonly %0, i32 noundef %1, i32 noundef %2) local_unnamed_addr #0 {
  %.not4044 = icmp sgt i32 %1, 0
  br i1 %.not4044, label %.outer27.split.lr.ph.preheader, label %.split

.outer27.split.lr.ph.preheader:                   ; preds = %3
  %4 = add nsw i32 %1, -1
  br label %.outer27.split.lr.ph

.outer:                                           ; preds = %.outer27.split.split
  %5 = add nuw nsw i32 %.021.ph45, 1
  %.not40 = icmp slt i32 %.021.ph45, %.018.ph2841
  %6 = add nsw i32 %.018.ph2841, %5
  br i1 %.not40, label %.outer27.split.lr.ph, label %.split, !llvm.loop !5

.outer27.split.lr.ph:                             ; preds = %.outer27.split.lr.ph.preheader, %.outer
  %.pn.in.in = phi i32 [ %6, %.outer ], [ %4, %.outer27.split.lr.ph.preheader ]
  %.018.ph46 = phi i32 [ %.018.ph2841, %.outer ], [ %4, %.outer27.split.lr.ph.preheader ]
  %.021.ph45 = phi i32 [ %5, %.outer ], [ 0, %.outer27.split.lr.ph.preheader ]
  br label %.outer27.split

.outer27.split:                                   ; preds = %.outer27.split.lr.ph, %.outer27
  %.pn.pn.in.in = phi i32 [ %.pn.in.in, %.outer27.split.lr.ph ], [ %12, %.outer27 ]
  %.018.ph2841 = phi i32 [ %.018.ph46, %.outer27.split.lr.ph ], [ %11, %.outer27 ]
  %.pn.pn.in = sdiv i32 %.pn.pn.in.in, 2
  %.pn.pn = sext i32 %.pn.pn.in to i64
  %7 = getelementptr inbounds i32, ptr %0, i64 %.pn.pn
  %8 = load i32, ptr %7, align 4, !tbaa !7
  %9 = icmp sgt i32 %8, %2
  br i1 %9, label %.outer27, label %.outer27.split.split

.outer27.split.split:                             ; preds = %.outer27.split
  %10 = icmp slt i32 %8, %2
  br i1 %10, label %.outer, label %.outer27.split.split.split

.outer27.split.split.split:                       ; preds = %.outer27.split.split
  %.not26.le.le = icmp eq i32 %8, %2
  tail call void @llvm.assume(i1 %.not26.le.le)
  br label %.split

.outer27:                                         ; preds = %.outer27.split
  %11 = add nsw i32 %.018.ph2841, -1
  %.not = icmp slt i32 %.021.ph45, %.018.ph2841
  %12 = add nsw i32 %11, %.021.ph45
  br i1 %.not, label %.outer27.split, label %.split, !llvm.loop !5

.split:                                           ; preds = %.outer, %.outer27, %.outer27.split.split.split, %3
  %.not.lcssa.split = phi i1 [ true, %.outer27.split.split.split ], [ false, %3 ], [ false, %.outer27 ], [ false, %.outer ]
  ret i1 %.not.lcssa.split
}

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(inaccessiblemem: write)
declare void @llvm.assume(i1 noundef) #1

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(argmem: read) 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(inaccessiblemem: write) }

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


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