; LLVM IR from HarshCasper/NeoAlgo :: C-Plus-Plus/dp/JobSchedulingWithMaxProfit.ll (Z18Optimal_profit_RecP4Jobsi)

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

%class.Jobs = type { i32, i32, i32 }

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_Z18Optimal_profit_RecP4Jobsi(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
  %10 = load i32, ptr %5, align 4, !tbaa !9
  %11 = icmp slt i32 %10, 0
  br i1 %11, label %12, label %13

12:                                               ; preds = %2
  store i32 0, ptr %3, align 4
  br label %74

13:                                               ; preds = %2
  %14 = load i32, ptr %5, align 4, !tbaa !9
  %15 = icmp eq i32 %14, 1
  br i1 %15, label %16, label %24

16:                                               ; preds = %13
  %17 = load ptr, ptr %4, align 8, !tbaa !5
  %18 = load i32, ptr %5, align 4, !tbaa !9
  %19 = sub nsw i32 %18, 1
  %20 = sext i32 %19 to i64
  %21 = getelementptr inbounds %class.Jobs, ptr %17, i64 %20
  %22 = getelementptr inbounds %class.Jobs, ptr %21, i32 0, i32 2
  %23 = load i32, ptr %22, align 4, !tbaa !11
  store i32 %23, ptr %3, align 4
  br label %74

24:                                               ; preds = %13
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  store i32 -1, ptr %8, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #3
  %25 = load i32, ptr %5, align 4, !tbaa !9
  %26 = sub nsw i32 %25, 1
  store i32 %26, ptr %9, align 4, !tbaa !9
  br label %27

27:                                               ; preds = %47, %24
  %28 = load i32, ptr %9, align 4, !tbaa !9
  %29 = icmp sge i32 %28, 0
  br i1 %29, label %30, label %50

30:                                               ; preds = %27
  %31 = load ptr, ptr %4, align 8, !tbaa !5
  %32 = load i32, ptr %9, align 4, !tbaa !9
  %33 = sext i32 %32 to i64
  %34 = getelementptr inbounds %class.Jobs, ptr %31, i64 %33
  %35 = getelementptr inbounds %class.Jobs, ptr %34, i32 0, i32 1
  %36 = load i32, ptr %35, align 4, !tbaa !13
  %37 = load ptr, ptr %4, align 8, !tbaa !5
  %38 = load i32, ptr %5, align 4, !tbaa !9
  %39 = sub nsw i32 %38, 1
  %40 = sext i32 %39 to i64
  %41 = getelementptr inbounds %class.Jobs, ptr %37, i64 %40
  %42 = getelementptr inbounds %class.Jobs, ptr %41, i32 0, i32 0
  %43 = load i32, ptr %42, align 4, !tbaa !14
  %44 = icmp sle i32 %36, %43
  br i1 %44, label %45, label %47

45:                                               ; preds = %30
  %46 = load i32, ptr %9, align 4, !tbaa !9
  store i32 %46, ptr %8, align 4, !tbaa !9
  br label %50

47:                                               ; preds = %30
  %48 = load i32, ptr %9, align 4, !tbaa !9
  %49 = add nsw i32 %48, -1
  store i32 %49, ptr %9, align 4, !tbaa !9
  br label %27, !llvm.loop !15

50:                                               ; preds = %45, %27
  %51 = load ptr, ptr %4, align 8, !tbaa !5
  %52 = load i32, ptr %5, align 4, !tbaa !9
  %53 = sub nsw i32 %52, 1
  %54 = sext i32 %53 to i64
  %55 = getelementptr inbounds %class.Jobs, ptr %51, i64 %54
  %56 = getelementptr inbounds %class.Jobs, ptr %55, i32 0, i32 2
  %57 = load i32, ptr %56, align 4, !tbaa !11
  store i32 %57, ptr %6, align 4, !tbaa !9
  %58 = load i32, ptr %8, align 4, !tbaa !9
  %59 = icmp ne i32 %58, -1
  br i1 %59, label %60, label %67

60:                                               ; preds = %50
  %61 = load i32, ptr %6, align 4, !tbaa !9
  %62 = load ptr, ptr %4, align 8, !tbaa !5
  %63 = load i32, ptr %8, align 4, !tbaa !9
  %64 = add nsw i32 %63, 1
  %65 = call noundef i32 @_Z18Optimal_profit_RecP4Jobsi(ptr noundef %62, i32 noundef %64)
  %66 = add nsw i32 %61, %65
  store i32 %66, ptr %6, align 4, !tbaa !9
  br label %67

67:                                               ; preds = %60, %50
  %68 = load ptr, ptr %4, align 8, !tbaa !5
  %69 = load i32, ptr %5, align 4, !tbaa !9
  %70 = sub nsw i32 %69, 1
  %71 = call noundef i32 @_Z18Optimal_profit_RecP4Jobsi(ptr noundef %68, i32 noundef %70)
  store i32 %71, ptr %7, align 4, !tbaa !9
  %72 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3maxIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4) %6, ptr noundef nonnull align 4 dereferenceable(4) %7)
  %73 = load i32, ptr %72, align 4, !tbaa !9
  store i32 %73, ptr %3, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  br label %74

74:                                               ; preds = %67, %16, %12
  %75 = load i32, ptr %3, align 4
  ret i32 %75
}

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

%class.Jobs = type { i32, i32, i32 }

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i32 @_Z18Optimal_profit_RecP4Jobsi(ptr noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = icmp slt i32 %1, 0
  br i1 %5, label %common.ret33, label %6

6:                                                ; preds = %2
  %7 = icmp eq i32 %1, 1
  br i1 %7, label %8, label %11

8:                                                ; preds = %6
  %9 = getelementptr inbounds i8, ptr %0, i64 8
  %10 = load i32, ptr %9, align 4, !tbaa !5
  br label %common.ret33

11:                                               ; preds = %6
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %3) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %4) #3
  %12 = add nsw i32 %1, -1
  %.not27 = icmp eq i32 %1, 0
  br i1 %.not27, label %.thread, label %.lr.ph

.lr.ph:                                           ; preds = %11
  %13 = zext nneg i32 %12 to i64
  %14 = getelementptr inbounds %class.Jobs, ptr %0, i64 %13
  %15 = load i32, ptr %14, align 4, !tbaa !10
  br label %18

.thread:                                          ; preds = %22, %11
  %.pre-phi = phi i64 [ -1, %11 ], [ %13, %22 ]
  %16 = getelementptr inbounds %class.Jobs, ptr %0, i64 %.pre-phi, i32 2
  %17 = load i32, ptr %16, align 4, !tbaa !5
  br label %31

18:                                               ; preds = %.lr.ph, %22
  %.026 = phi i32 [ %12, %.lr.ph ], [ %23, %22 ]
  %19 = zext nneg i32 %.026 to i64
  %20 = getelementptr inbounds %class.Jobs, ptr %0, i64 %19, i32 1
  %21 = load i32, ptr %20, align 4, !tbaa !11
  %.not = icmp sgt i32 %21, %15
  br i1 %.not, label %22, label %25

22:                                               ; preds = %18
  %23 = add nsw i32 %.026, -1
  %24 = icmp sgt i32 %.026, 0
  br i1 %24, label %18, label %.thread, !llvm.loop !12

25:                                               ; preds = %18
  %26 = getelementptr inbounds %class.Jobs, ptr %0, i64 %13, i32 2
  %27 = load i32, ptr %26, align 4, !tbaa !5
  %28 = add nuw nsw i32 %.026, 1
  %29 = tail call noundef i32 @_Z18Optimal_profit_RecP4Jobsi(ptr noundef nonnull %0, i32 noundef %28)
  %30 = add nsw i32 %29, %27
  br label %31

common.ret33:                                     ; preds = %2, %8, %31
  %common.ret33.op = phi i32 [ %34, %31 ], [ %10, %8 ], [ 0, %2 ]
  ret i32 %common.ret33.op

31:                                               ; preds = %.thread, %25
  %storemerge = phi i32 [ %17, %.thread ], [ %30, %25 ]
  store i32 %storemerge, ptr %3, align 4, !tbaa !14
  %32 = tail call noundef i32 @_Z18Optimal_profit_RecP4Jobsi(ptr noundef nonnull %0, i32 noundef %12)
  store i32 %32, ptr %4, align 4, !tbaa !14
  %33 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3maxIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4) %3, ptr noundef nonnull align 4 dereferenceable(4) %4)
  %34 = load i32, ptr %33, align 4, !tbaa !14
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %4) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %3) #3
  br label %common.ret33
}

; 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 @_ZSt3maxIiERKT_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 = !{!6, !7, i64 8}
!6 = !{!"_ZTS4Jobs", !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 0}
!11 = !{!6, !7, i64 4}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}
!14 = !{!7, !7, i64 0}


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