; LLVM IR from UtkarshPathrabe/Competitive-Coding :: Model Algorithms/Dynamic Programming/Knapsack (0-1 KnapSack)

; 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: mustprogress uwtable
define dso_local noundef i32 @_Z8KnapSackiRSt6vectorIiSaIiEES2_i(i32 noundef %0, ptr noundef nonnull align 8 dereferenceable(24) %1, ptr noundef nonnull align 8 dereferenceable(24) %2, i32 noundef %3) #0 {
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  %9 = alloca ptr, align 8
  %10 = alloca i64, align 8
  %11 = alloca i64, align 8
  %12 = alloca i32, align 4
  %13 = alloca i32, align 4
  %14 = alloca i32, align 4
  %15 = alloca i32, align 4
  store i32 %0, ptr %5, align 4, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !9
  store ptr %2, ptr %7, align 8, !tbaa !9
  store i32 %3, ptr %8, align 4, !tbaa !5
  %16 = load i32, ptr %8, align 4, !tbaa !5
  %17 = add nsw i32 %16, 1
  %18 = zext i32 %17 to i64
  %19 = load i32, ptr %5, align 4, !tbaa !5
  %20 = add nsw i32 %19, 1
  %21 = zext i32 %20 to i64
  %22 = call ptr @llvm.stacksave.p0()
  store ptr %22, ptr %9, align 8
  %23 = mul nuw i64 %18, %21
  %24 = alloca i32, i64 %23, align 16
  store i64 %18, ptr %10, align 8
  store i64 %21, ptr %11, align 8
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #5
  store i32 0, ptr %12, align 4, !tbaa !5
  br label %25

25:                                               ; preds = %123, %4
  %26 = load i32, ptr %12, align 4, !tbaa !5
  %27 = load i32, ptr %8, align 4, !tbaa !5
  %28 = icmp sle i32 %26, %27
  br i1 %28, label %30, label %29

29:                                               ; preds = %25
  store i32 2, ptr %13, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #5
  br label %126

30:                                               ; preds = %25
  call void @llvm.lifetime.start.p0(i64 4, ptr %14) #5
  store i32 0, ptr %14, align 4, !tbaa !5
  br label %31

31:                                               ; preds = %119, %30
  %32 = load i32, ptr %14, align 4, !tbaa !5
  %33 = load i32, ptr %5, align 4, !tbaa !5
  %34 = icmp sle i32 %32, %33
  br i1 %34, label %36, label %35

35:                                               ; preds = %31
  store i32 5, ptr %13, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %14) #5
  br label %122

36:                                               ; preds = %31
  %37 = load i32, ptr %12, align 4, !tbaa !5
  %38 = icmp eq i32 %37, 0
  br i1 %38, label %42, label %39

39:                                               ; preds = %36
  %40 = load i32, ptr %14, align 4, !tbaa !5
  %41 = icmp eq i32 %40, 0
  br i1 %41, label %42, label %50

42:                                               ; preds = %39, %36
  %43 = load i32, ptr %12, align 4, !tbaa !5
  %44 = sext i32 %43 to i64
  %45 = mul nsw i64 %44, %21
  %46 = getelementptr inbounds i32, ptr %24, i64 %45
  %47 = load i32, ptr %14, align 4, !tbaa !5
  %48 = sext i32 %47 to i64
  %49 = getelementptr inbounds i32, ptr %46, i64 %48
  store i32 0, ptr %49, align 4, !tbaa !5
  br label %118

50:                                               ; preds = %39
  %51 = load ptr, ptr %7, align 8, !tbaa !9
  %52 = load i32, ptr %12, align 4, !tbaa !5
  %53 = sub nsw i32 %52, 1
  %54 = sext i32 %53 to i64
  %55 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt6vectorIiSaIiEEixEm(ptr noundef nonnull align 8 dereferenceable(24) %51, i64 noundef %54) #5
  %56 = load i32, ptr %55, align 4, !tbaa !5
  %57 = load i32, ptr %5, align 4, !tbaa !5
  %58 = icmp sgt i32 %56, %57
  br i1 %58, label %59, label %76

59:                                               ; preds = %50
  %60 = load i32, ptr %12, align 4, !tbaa !5
  %61 = sub nsw i32 %60, 1
  %62 = sext i32 %61 to i64
  %63 = mul nsw i64 %62, %21
  %64 = getelementptr inbounds i32, ptr %24, i64 %63
  %65 = load i32, ptr %14, align 4, !tbaa !5
  %66 = sext i32 %65 to i64
  %67 = getelementptr inbounds i32, ptr %64, i64 %66
  %68 = load i32, ptr %67, align 4, !tbaa !5
  %69 = load i32, ptr %12, align 4, !tbaa !5
  %70 = sext i32 %69 to i64
  %71 = mul nsw i64 %70, %21
  %72 = getelementptr inbounds i32, ptr %24, i64 %71
  %73 = load i32, ptr %14, align 4, !tbaa !5
  %74 = sext i32 %73 to i64
  %75 = getelementptr inbounds i32, ptr %72, i64 %74
  store i32 %68, ptr %75, align 4, !tbaa !5
  br label %117

76:                                               ; preds = %50
  call void @llvm.lifetime.start.p0(i64 4, ptr %15) #5
  %77 = load ptr, ptr %6, align 8, !tbaa !9
  %78 = load i32, ptr %12, align 4, !tbaa !5
  %79 = sub nsw i32 %78, 1
  %80 = sext i32 %79 to i64
  %81 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt6vectorIiSaIiEEixEm(ptr noundef nonnull align 8 dereferenceable(24) %77, i64 noundef %80) #5
  %82 = load i32, ptr %81, align 4, !tbaa !5
  %83 = load i32, ptr %12, align 4, !tbaa !5
  %84 = sub nsw i32 %83, 1
  %85 = sext i32 %84 to i64
  %86 = mul nsw i64 %85, %21
  %87 = getelementptr inbounds i32, ptr %24, i64 %86
  %88 = load i32, ptr %14, align 4, !tbaa !5
  %89 = load ptr, ptr %7, align 8, !tbaa !9
  %90 = load i32, ptr %12, align 4, !tbaa !5
  %91 = sub nsw i32 %90, 1
  %92 = sext i32 %91 to i64
  %93 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt6vectorIiSaIiEEixEm(ptr noundef nonnull align 8 dereferenceable(24) %89, i64 noundef %92) #5
  %94 = load i32, ptr %93, align 4, !tbaa !5
  %95 = sub nsw i32 %88, %94
  %96 = sext i32 %95 to i64
  %97 = getelementptr inbounds i32, ptr %87, i64 %96
  %98 = load i32, ptr %97, align 4, !tbaa !5
  %99 = add nsw i32 %82, %98
  store i32 %99, ptr %15, align 4, !tbaa !5
  %100 = load i32, ptr %12, align 4, !tbaa !5
  %101 = sub nsw i32 %100, 1
  %102 = sext i32 %101 to i64
  %103 = mul nsw i64 %102, %21
  %104 = getelementptr inbounds i32, ptr %24, i64 %103
  %105 = load i32, ptr %14, align 4, !tbaa !5
  %106 = sext i32 %105 to i64
  %107 = getelementptr inbounds i32, ptr %104, i64 %106
  %108 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3maxIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4) %15, ptr noundef nonnull align 4 dereferenceable(4) %107)
  %109 = load i32, ptr %108, align 4, !tbaa !5
  %110 = load i32, ptr %12, align 4, !tbaa !5
  %111 = sext i32 %110 to i64
  %112 = mul nsw i64 %111, %21
  %113 = getelementptr inbounds i32, ptr %24, i64 %112
  %114 = load i32, ptr %14, align 4, !tbaa !5
  %115 = sext i32 %114 to i64
  %116 = getelementptr inbounds i32, ptr %113, i64 %115
  store i32 %109, ptr %116, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %15) #5
  br label %117

117:                                              ; preds = %76, %59
  br label %118

118:                                              ; preds = %117, %42
  br label %119

119:                                              ; preds = %118
  %120 = load i32, ptr %14, align 4, !tbaa !5
  %121 = add nsw i32 %120, 1
  store i32 %121, ptr %14, align 4, !tbaa !5
  br label %31, !llvm.loop !11

122:                                              ; preds = %35
  br label %123

123:                                              ; preds = %122
  %124 = load i32, ptr %12, align 4, !tbaa !5
  %125 = add nsw i32 %124, 1
  store i32 %125, ptr %12, align 4, !tbaa !5
  br label %25, !llvm.loop !13

126:                                              ; preds = %29
  %127 = load i32, ptr %8, align 4, !tbaa !5
  %128 = sext i32 %127 to i64
  %129 = mul nsw i64 %128, %21
  %130 = getelementptr inbounds i32, ptr %24, i64 %129
  %131 = load i32, ptr %5, align 4, !tbaa !5
  %132 = sext i32 %131 to i64
  %133 = getelementptr inbounds i32, ptr %130, i64 %132
  %134 = load i32, ptr %133, align 4, !tbaa !5
  store i32 1, ptr %13, align 4
  %135 = load ptr, ptr %9, align 8
  call void @llvm.stackrestore.p0(ptr %135)
  ret i32 %134
}

; Function Attrs: nocallback nofree nosync nounwind willreturn
declare ptr @llvm.stacksave.p0() #1

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt6vectorIiSaIiEEixEm(ptr noundef nonnull align 8 dereferenceable(24), i64 noundef) #3 align 2

; 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)) #4

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #2

; Function Attrs: nocallback nofree nosync nounwind willreturn
declare void @llvm.stackrestore.p0(ptr) #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 }
attributes #2 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { 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 #4 = { 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 #5 = { 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, !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"

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i32 @_Z8KnapSackiRSt6vectorIiSaIiEES2_i(i32 noundef %0, ptr noundef nonnull align 8 dereferenceable(24) %1, ptr noundef nonnull align 8 dereferenceable(24) %2, i32 noundef %3) local_unnamed_addr #0 {
  %5 = alloca i32, align 4
  %6 = add nsw i32 %3, 1
  %7 = zext i32 %6 to i64
  %8 = add i32 %0, 1
  %9 = zext i32 %8 to i64
  %10 = mul nuw i64 %7, %9
  %11 = alloca i32, i64 %10, align 16
  %12 = or i32 %3, %0
  %or.cond.not = icmp sgt i32 %12, -1
  br i1 %or.cond.not, label %.preheader.preheader, label %._crit_edge40

.preheader.preheader:                             ; preds = %4
  %13 = shl nuw nsw i64 %9, 2
  %exitcond.peel.not = icmp eq i32 %0, 0
  br label %.preheader

.preheader:                                       ; preds = %.preheader.preheader, %._crit_edge
  %.03239 = phi i32 [ %30, %._crit_edge ], [ 0, %.preheader.preheader ]
  %14 = zext i32 %.03239 to i64
  %15 = icmp eq i32 %.03239, 0
  %16 = add nsw i32 %.03239, -1
  %17 = sext i32 %16 to i64
  %18 = mul nsw i64 %17, %9
  %19 = getelementptr inbounds i32, ptr %11, i64 %18
  %20 = mul nuw nsw i64 %14, %9
  %21 = getelementptr inbounds i32, ptr %11, i64 %20
  br i1 %15, label %.lr.ph.split.us.preheader, label %22

22:                                               ; preds = %.preheader
  store i32 0, ptr %21, align 4, !tbaa !5
  br i1 %exitcond.peel.not, label %._crit_edge, label %.lr.ph.split.peel.next

.lr.ph.split.us.preheader:                        ; preds = %.preheader
  %23 = mul nuw nsw i64 %13, %14
  %scevgep = getelementptr i8, ptr %11, i64 %23
  call void @llvm.memset.p0.i64(ptr align 4 %scevgep, i8 0, i64 %13, i1 false), !tbaa !5
  br label %._crit_edge

._crit_edge40:                                    ; preds = %._crit_edge, %4
  %24 = sext i32 %3 to i64
  %25 = mul nsw i64 %24, %9
  %26 = getelementptr inbounds i32, ptr %11, i64 %25
  %27 = sext i32 %0 to i64
  %28 = getelementptr inbounds i32, ptr %26, i64 %27
  %29 = load i32, ptr %28, align 4, !tbaa !5
  ret i32 %29

._crit_edge:                                      ; preds = %53, %22, %.lr.ph.split.us.preheader
  %30 = add i32 %.03239, 1
  %.not = icmp sgt i32 %30, %3
  br i1 %.not, label %._crit_edge40, label %.preheader, !llvm.loop !9

.lr.ph.split.peel.next:                           ; preds = %22, %53
  %indvars.iv = phi i64 [ %indvars.iv.next, %53 ], [ 1, %22 ]
  %31 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt6vectorIiSaIiEEixEm(ptr noundef nonnull align 8 dereferenceable(24) %2, i64 noundef %17) #4
  %32 = load i32, ptr %31, align 4, !tbaa !5
  %33 = icmp sgt i32 %32, %0
  br i1 %33, label %34, label %38

34:                                               ; preds = %.lr.ph.split.peel.next
  %35 = getelementptr inbounds i32, ptr %19, i64 %indvars.iv
  %36 = load i32, ptr %35, align 4, !tbaa !5
  %37 = getelementptr inbounds i32, ptr %21, i64 %indvars.iv
  store i32 %36, ptr %37, align 4, !tbaa !5
  br label %53

38:                                               ; preds = %.lr.ph.split.peel.next
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %5) #4
  %39 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt6vectorIiSaIiEEixEm(ptr noundef nonnull align 8 dereferenceable(24) %1, i64 noundef %17) #4
  %40 = load i32, ptr %39, align 4, !tbaa !5
  %41 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt6vectorIiSaIiEEixEm(ptr noundef nonnull align 8 dereferenceable(24) %2, i64 noundef %17) #4
  %42 = load i32, ptr %41, align 4, !tbaa !5
  %43 = trunc nuw nsw i64 %indvars.iv to i32
  %44 = sub nsw i32 %43, %42
  %45 = sext i32 %44 to i64
  %46 = getelementptr inbounds i32, ptr %19, i64 %45
  %47 = load i32, ptr %46, align 4, !tbaa !5
  %48 = add nsw i32 %47, %40
  store i32 %48, ptr %5, align 4, !tbaa !5
  %49 = getelementptr inbounds i32, ptr %19, i64 %indvars.iv
  %50 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3maxIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4) %5, ptr noundef nonnull align 4 dereferenceable(4) %49)
  %51 = load i32, ptr %50, align 4, !tbaa !5
  %52 = getelementptr inbounds i32, ptr %21, i64 %indvars.iv
  store i32 %51, ptr %52, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %5) #4
  br label %53

53:                                               ; preds = %38, %34
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %9
  br i1 %exitcond.not, label %._crit_edge, label %.lr.ph.split.peel.next, !llvm.loop !11
}

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt6vectorIiSaIiEEixEm(ptr noundef nonnull align 8 dereferenceable(24), i64 noundef) local_unnamed_addr #0 align 2

; 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

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #3

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 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #4 = { 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 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10, !12}
!12 = !{!"llvm.loop.peeled.count", i32 1}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopFullUnrollPass,LoopIdiomRecognizePass,LoopInstSimplifyPass,LoopRotatePass,LoopSimplifyCFGPass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SCCPPass,SROAPass,SimplifyCFGPass