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

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

@dp = external dso_local global [1000 x i64], align 16

; Function Attrs: mustprogress uwtable
define dso_local noundef i64 @_Z15Zero_N_KnapsackxPxS_x(i64 noundef %0, ptr noundef %1, ptr noundef %2, i64 noundef %3) #0 {
  %5 = alloca i64, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i64, align 8
  %9 = alloca i64, align 8
  %10 = alloca i32, align 4
  %11 = alloca i64, align 8
  %12 = alloca i64, align 8
  store i64 %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !9
  store ptr %2, ptr %7, align 8, !tbaa !9
  store i64 %3, ptr %8, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  store i64 0, ptr %9, align 8, !tbaa !5
  br label %13

13:                                               ; preds = %56, %4
  %14 = load i64, ptr %9, align 8, !tbaa !5
  %15 = load i64, ptr %5, align 8, !tbaa !5
  %16 = icmp sle i64 %14, %15
  br i1 %16, label %18, label %17

17:                                               ; preds = %13
  store i32 2, ptr %10, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  br label %59

18:                                               ; preds = %13
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #3
  store i64 0, ptr %11, align 8, !tbaa !5
  br label %19

19:                                               ; preds = %52, %18
  %20 = load i64, ptr %11, align 8, !tbaa !5
  %21 = load i64, ptr %8, align 8, !tbaa !5
  %22 = icmp slt i64 %20, %21
  br i1 %22, label %24, label %23

23:                                               ; preds = %19
  store i32 5, ptr %10, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #3
  br label %55

24:                                               ; preds = %19
  %25 = load ptr, ptr %6, align 8, !tbaa !9
  %26 = load i64, ptr %11, align 8, !tbaa !5
  %27 = getelementptr inbounds i64, ptr %25, i64 %26
  %28 = load i64, ptr %27, align 8, !tbaa !5
  %29 = load i64, ptr %9, align 8, !tbaa !5
  %30 = icmp sle i64 %28, %29
  br i1 %30, label %31, label %51

31:                                               ; preds = %24
  %32 = load i64, ptr %9, align 8, !tbaa !5
  %33 = getelementptr inbounds [1000 x i64], ptr @dp, i64 0, i64 %32
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #3
  %34 = load i64, ptr %9, align 8, !tbaa !5
  %35 = load ptr, ptr %6, align 8, !tbaa !9
  %36 = load i64, ptr %11, align 8, !tbaa !5
  %37 = getelementptr inbounds i64, ptr %35, i64 %36
  %38 = load i64, ptr %37, align 8, !tbaa !5
  %39 = sub nsw i64 %34, %38
  %40 = getelementptr inbounds [1000 x i64], ptr @dp, i64 0, i64 %39
  %41 = load i64, ptr %40, align 8, !tbaa !5
  %42 = load ptr, ptr %7, align 8, !tbaa !9
  %43 = load i64, ptr %11, align 8, !tbaa !5
  %44 = getelementptr inbounds i64, ptr %42, i64 %43
  %45 = load i64, ptr %44, align 8, !tbaa !5
  %46 = add nsw i64 %41, %45
  store i64 %46, ptr %12, align 8, !tbaa !5
  %47 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3maxIxERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8) %33, ptr noundef nonnull align 8 dereferenceable(8) %12)
  %48 = load i64, ptr %47, align 8, !tbaa !5
  %49 = load i64, ptr %9, align 8, !tbaa !5
  %50 = getelementptr inbounds [1000 x i64], ptr @dp, i64 0, i64 %49
  store i64 %48, ptr %50, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #3
  br label %51

51:                                               ; preds = %31, %24
  br label %52

52:                                               ; preds = %51
  %53 = load i64, ptr %11, align 8, !tbaa !5
  %54 = add nsw i64 %53, 1
  store i64 %54, ptr %11, align 8, !tbaa !5
  br label %19, !llvm.loop !11

55:                                               ; preds = %23
  br label %56

56:                                               ; preds = %55
  %57 = load i64, ptr %9, align 8, !tbaa !5
  %58 = add nsw i64 %57, 1
  store i64 %58, ptr %9, align 8, !tbaa !5
  br label %13, !llvm.loop !13

59:                                               ; preds = %17
  %60 = load i64, ptr %5, align 8, !tbaa !5
  %61 = getelementptr inbounds [1000 x i64], ptr @dp, i64 0, i64 %60
  %62 = load i64, ptr %61, align 8, !tbaa !5
  ret i64 %62
}

; 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 8 dereferenceable(8) ptr @_ZSt3maxIxERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8), ptr noundef nonnull align 8 dereferenceable(8)) #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 = !{!"long long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !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"

@dp = external dso_local global [1000 x i64], align 16

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i64 @_Z15Zero_N_KnapsackxPxS_x(i64 noundef %0, ptr nocapture noundef readonly %1, ptr nocapture noundef readonly %2, i64 noundef %3) local_unnamed_addr #0 {
  %5 = alloca i64, align 8
  %.not20 = icmp sgt i64 %0, -1
  %6 = icmp sgt i64 %3, 0
  %or.cond = and i1 %.not20, %6
  br i1 %or.cond, label %.preheader.us, label %._crit_edge22

.preheader.us:                                    ; preds = %4, %._crit_edge.us
  %.01621.us = phi i64 [ %22, %._crit_edge.us ], [ 0, %4 ]
  %7 = getelementptr inbounds [1000 x i64], ptr @dp, i64 0, i64 %.01621.us
  br label %8

8:                                                ; preds = %.preheader.us, %20
  %.019.us = phi i64 [ 0, %.preheader.us ], [ %21, %20 ]
  %9 = getelementptr inbounds i64, ptr %1, i64 %.019.us
  %10 = load i64, ptr %9, align 8, !tbaa !5
  %.not18.us = icmp sgt i64 %10, %.01621.us
  br i1 %.not18.us, label %20, label %11

11:                                               ; preds = %8
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %5) #3
  %12 = sub nsw i64 %.01621.us, %10
  %13 = getelementptr inbounds [1000 x i64], ptr @dp, i64 0, i64 %12
  %14 = load i64, ptr %13, align 8, !tbaa !5
  %15 = getelementptr inbounds i64, ptr %2, i64 %.019.us
  %16 = load i64, ptr %15, align 8, !tbaa !5
  %17 = add nsw i64 %16, %14
  store i64 %17, ptr %5, align 8, !tbaa !5
  %18 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3maxIxERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8) %7, ptr noundef nonnull align 8 dereferenceable(8) %5)
  %19 = load i64, ptr %18, align 8, !tbaa !5
  store i64 %19, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %5) #3
  br label %20

20:                                               ; preds = %11, %8
  %21 = add nuw nsw i64 %.019.us, 1
  %exitcond.not = icmp eq i64 %21, %3
  br i1 %exitcond.not, label %._crit_edge.us, label %8, !llvm.loop !9

._crit_edge.us:                                   ; preds = %20
  %22 = add nuw i64 %.01621.us, 1
  %exitcond24.not = icmp eq i64 %.01621.us, %0
  br i1 %exitcond24.not, label %._crit_edge22, label %.preheader.us, !llvm.loop !11

._crit_edge22:                                    ; preds = %._crit_edge.us, %4
  %23 = getelementptr inbounds [1000 x i64], ptr @dp, i64 0, i64 %0
  %24 = load i64, ptr %23, align 8, !tbaa !5
  ret i64 %24
}

; 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 8 dereferenceable(8) ptr @_ZSt3maxIxERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8), ptr noundef nonnull align 8 dereferenceable(8)) 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, !6, i64 0}
!6 = !{!"long long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopRotatePass,LoopSimplifyCFGPass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass