; LLVM IR from div-bargali/Data-Structures-and-Algorithms :: C++/Algorithms/Greedy-Algorithms/WeightedJobScheduling.ll (Z12binarySearchP3Jobi)

; 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.Job = type { i32, i32, i32 }

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

12:                                               ; preds = %61, %2
  %13 = load i32, ptr %6, align 4, !tbaa !9
  %14 = load i32, ptr %7, align 4, !tbaa !9
  %15 = icmp sle i32 %13, %14
  br i1 %15, label %16, label %62

16:                                               ; preds = %12
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #2
  %17 = load i32, ptr %6, align 4, !tbaa !9
  %18 = load i32, ptr %7, align 4, !tbaa !9
  %19 = add nsw i32 %17, %18
  %20 = sdiv i32 %19, 2
  store i32 %20, ptr %8, align 4, !tbaa !9
  %21 = load ptr, ptr %4, align 8, !tbaa !5
  %22 = load i32, ptr %8, align 4, !tbaa !9
  %23 = sext i32 %22 to i64
  %24 = getelementptr inbounds %struct.Job, ptr %21, i64 %23
  %25 = getelementptr inbounds %struct.Job, ptr %24, i32 0, i32 1
  %26 = load i32, ptr %25, align 4, !tbaa !11
  %27 = load ptr, ptr %4, align 8, !tbaa !5
  %28 = load i32, ptr %5, align 4, !tbaa !9
  %29 = sext i32 %28 to i64
  %30 = getelementptr inbounds %struct.Job, ptr %27, i64 %29
  %31 = getelementptr inbounds %struct.Job, ptr %30, i32 0, i32 0
  %32 = load i32, ptr %31, align 4, !tbaa !13
  %33 = icmp sle i32 %26, %32
  br i1 %33, label %34, label %55

34:                                               ; preds = %16
  %35 = load ptr, ptr %4, align 8, !tbaa !5
  %36 = load i32, ptr %8, align 4, !tbaa !9
  %37 = add nsw i32 %36, 1
  %38 = sext i32 %37 to i64
  %39 = getelementptr inbounds %struct.Job, ptr %35, i64 %38
  %40 = getelementptr inbounds %struct.Job, ptr %39, i32 0, i32 1
  %41 = load i32, ptr %40, align 4, !tbaa !11
  %42 = load ptr, ptr %4, align 8, !tbaa !5
  %43 = load i32, ptr %5, align 4, !tbaa !9
  %44 = sext i32 %43 to i64
  %45 = getelementptr inbounds %struct.Job, ptr %42, i64 %44
  %46 = getelementptr inbounds %struct.Job, ptr %45, i32 0, i32 0
  %47 = load i32, ptr %46, align 4, !tbaa !13
  %48 = icmp sle i32 %41, %47
  br i1 %48, label %49, label %52

49:                                               ; preds = %34
  %50 = load i32, ptr %8, align 4, !tbaa !9
  %51 = add nsw i32 %50, 1
  store i32 %51, ptr %6, align 4, !tbaa !9
  br label %54

52:                                               ; preds = %34
  %53 = load i32, ptr %8, align 4, !tbaa !9
  store i32 %53, ptr %3, align 4
  store i32 1, ptr %9, align 4
  br label %59

54:                                               ; preds = %49
  br label %58

55:                                               ; preds = %16
  %56 = load i32, ptr %8, align 4, !tbaa !9
  %57 = sub nsw i32 %56, 1
  store i32 %57, ptr %7, align 4, !tbaa !9
  br label %58

58:                                               ; preds = %55, %54
  store i32 0, ptr %9, align 4
  br label %59

59:                                               ; preds = %58, %52
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  %60 = load i32, ptr %9, align 4
  switch i32 %60, label %63 [
    i32 0, label %61
  ]

61:                                               ; preds = %59
  br label %12, !llvm.loop !14

62:                                               ; preds = %12
  store i32 -1, ptr %3, align 4
  store i32 1, ptr %9, align 4
  br label %63

63:                                               ; preds = %62, %59
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  %64 = load i32, ptr %3, align 4
  ret i32 %64
}

; 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 = { 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 = { 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 = !{!"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 = !{!12, !10, i64 4}
!12 = !{!"_ZTS3Job", !10, i64 0, !10, i64 4, !10, i64 8}
!13 = !{!12, !10, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"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"

%struct.Job = type { i32, i32, i32 }

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(argmem: read) uwtable
define dso_local noundef i32 @_Z12binarySearchP3Jobi(ptr nocapture noundef readonly %0, i32 noundef %1) local_unnamed_addr #0 {
  %.not31 = icmp slt i32 %1, 1
  br i1 %.not31, label %.thread, label %.lr.ph

.lr.ph:                                           ; preds = %2
  %3 = add nsw i32 %1, -1
  %4 = zext nneg i32 %1 to i64
  %5 = getelementptr inbounds %struct.Job, ptr %0, i64 %4
  %6 = load i32, ptr %5, align 4, !tbaa !5
  br label %7

7:                                                ; preds = %.lr.ph, %20
  %.01833 = phi i32 [ %3, %.lr.ph ], [ %.220, %20 ]
  %.02132 = phi i32 [ 0, %.lr.ph ], [ %.223, %20 ]
  %8 = add nuw nsw i32 %.01833, %.02132
  %9 = lshr i32 %8, 1
  %10 = zext nneg i32 %9 to i64
  %11 = getelementptr inbounds %struct.Job, ptr %0, i64 %10, i32 1
  %12 = load i32, ptr %11, align 4, !tbaa !10
  %.not25 = icmp sgt i32 %12, %6
  br i1 %.not25, label %18, label %13

13:                                               ; preds = %7
  %14 = add nuw nsw i32 %9, 1
  %15 = zext nneg i32 %14 to i64
  %16 = getelementptr inbounds %struct.Job, ptr %0, i64 %15, i32 1
  %17 = load i32, ptr %16, align 4, !tbaa !10
  %.not26 = icmp sgt i32 %17, %6
  br i1 %.not26, label %.thread, label %20

18:                                               ; preds = %7
  %19 = add nsw i32 %9, -1
  br label %20

20:                                               ; preds = %13, %18
  %.223 = phi i32 [ %.02132, %18 ], [ %14, %13 ]
  %.220 = phi i32 [ %19, %18 ], [ %.01833, %13 ]
  %.not = icmp sgt i32 %.223, %.220
  br i1 %.not, label %.thread, label %7, !llvm.loop !11

.thread:                                          ; preds = %20, %13, %2
  %.2 = phi i32 [ -1, %2 ], [ %9, %13 ], [ -1, %20 ]
  ret i32 %.2
}

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

!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, !7, i64 0}
!6 = !{!"_ZTS3Job", !7, i64 0, !7, i64 4, !7, i64 8}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!6, !7, i64 4}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass