; LLVM IR from VAR-solutions/Algorithms :: Dynamic Programming/WeightedJobScheduling/cpp (weighted_job_scheduling)

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

@jobs = external dso_local global [100005 x %struct.Job], align 16

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

9:                                                ; preds = %36, %1
  %10 = load i32, ptr %3, align 4, !tbaa !5
  %11 = load i32, ptr %4, align 4, !tbaa !5
  %12 = icmp sle i32 %10, %11
  br i1 %12, label %13, label %37

13:                                               ; preds = %9
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  %14 = load i32, ptr %3, align 4, !tbaa !5
  %15 = load i32, ptr %4, align 4, !tbaa !5
  %16 = add nsw i32 %14, %15
  %17 = sdiv i32 %16, 2
  store i32 %17, ptr %6, align 4, !tbaa !5
  %18 = load i32, ptr %6, align 4, !tbaa !5
  %19 = sext i32 %18 to i64
  %20 = getelementptr inbounds [100005 x %struct.Job], ptr @jobs, i64 0, i64 %19
  %21 = getelementptr inbounds %struct.Job, ptr %20, i32 0, i32 1
  %22 = load i32, ptr %21, align 4, !tbaa !9
  %23 = load i32, ptr %2, align 4, !tbaa !5
  %24 = sext i32 %23 to i64
  %25 = getelementptr inbounds [100005 x %struct.Job], ptr @jobs, i64 0, i64 %24
  %26 = getelementptr inbounds %struct.Job, ptr %25, i32 0, i32 0
  %27 = load i32, ptr %26, align 4, !tbaa !11
  %28 = icmp sle i32 %22, %27
  br i1 %28, label %29, label %33

29:                                               ; preds = %13
  %30 = load i32, ptr %6, align 4, !tbaa !5
  store i32 %30, ptr %5, align 4, !tbaa !5
  %31 = load i32, ptr %6, align 4, !tbaa !5
  %32 = add nsw i32 %31, 1
  store i32 %32, ptr %3, align 4, !tbaa !5
  br label %36

33:                                               ; preds = %13
  %34 = load i32, ptr %6, align 4, !tbaa !5
  %35 = sub nsw i32 %34, 1
  store i32 %35, ptr %4, align 4, !tbaa !5
  br label %36

36:                                               ; preds = %33, %29
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  br label %9, !llvm.loop !12

37:                                               ; preds = %9
  %38 = load i32, ptr %5, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #2
  ret i32 %38
}

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

@jobs = external dso_local local_unnamed_addr global [100005 x %struct.Job], align 16

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(read, argmem: none, inaccessiblemem: none) uwtable
define dso_local noundef range(i32 -1, 1073741824) i32 @_Z12binarySearchi(i32 noundef %0) local_unnamed_addr #0 {
  %.not16 = icmp slt i32 %0, 1
  br i1 %.not16, label %._crit_edge, label %.lr.ph

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

6:                                                ; preds = %.lr.ph, %6
  %.019 = phi i32 [ 0, %.lr.ph ], [ %.1, %6 ]
  %.01118 = phi i32 [ -1, %.lr.ph ], [ %.112, %6 ]
  %.01317 = phi i32 [ %2, %.lr.ph ], [ %.114, %6 ]
  %7 = add nuw nsw i32 %.019, %.01317
  %8 = lshr i32 %7, 1
  %9 = zext nneg i32 %8 to i64
  %10 = getelementptr inbounds [100005 x %struct.Job], ptr @jobs, i64 0, i64 %9, i32 1
  %11 = load i32, ptr %10, align 4, !tbaa !10
  %.not15 = icmp sgt i32 %11, %5
  %12 = add nuw nsw i32 %8, 1
  %13 = add nsw i32 %8, -1
  %.114 = select i1 %.not15, i32 %13, i32 %.01317
  %.112 = select i1 %.not15, i32 %.01118, i32 %8
  %.1 = select i1 %.not15, i32 %.019, i32 %12
  %.not = icmp sgt i32 %.1, %.114
  br i1 %.not, label %._crit_edge, label %6, !llvm.loop !11

._crit_edge:                                      ; preds = %6, %1
  %.011.lcssa = phi i32 [ -1, %1 ], [ %.112, %6 ]
  ret i32 %.011.lcssa
}

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

!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: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass