; LLVM IR from smv1999/CompetitiveProgrammingQuestionBank :: Dynamic Programming/knapsack_01.ll (Z8knapSackiPiS_i)

; 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 @_Z8knapSackiPiS_i(i32 noundef %0, ptr noundef %1, ptr noundef %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) #4
  store i32 0, ptr %12, align 4, !tbaa !5
  br label %25

25:                                               ; preds = %125, %4
  %26 = load i32, ptr %12, align 4, !tbaa !5
  %27 = load i32, ptr %8, align 4, !tbaa !5
  %28 = add nsw i32 %27, 1
  %29 = icmp slt i32 %26, %28
  br i1 %29, label %31, label %30

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

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

32:                                               ; preds = %121, %31
  %33 = load i32, ptr %14, align 4, !tbaa !5
  %34 = load i32, ptr %5, align 4, !tbaa !5
  %35 = add nsw i32 %34, 1
  %36 = icmp slt i32 %33, %35
  br i1 %36, label %38, label %37

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

38:                                               ; preds = %32
  %39 = load i32, ptr %12, align 4, !tbaa !5
  %40 = icmp eq i32 %39, 0
  br i1 %40, label %44, label %41

41:                                               ; preds = %38
  %42 = load i32, ptr %14, align 4, !tbaa !5
  %43 = icmp eq i32 %42, 0
  br i1 %43, label %44, label %52

44:                                               ; preds = %41, %38
  %45 = load i32, ptr %12, align 4, !tbaa !5
  %46 = sext i32 %45 to i64
  %47 = mul nsw i64 %46, %21
  %48 = getelementptr inbounds i32, ptr %24, i64 %47
  %49 = load i32, ptr %14, align 4, !tbaa !5
  %50 = sext i32 %49 to i64
  %51 = getelementptr inbounds i32, ptr %48, i64 %50
  store i32 0, ptr %51, align 4, !tbaa !5
  br label %120

52:                                               ; preds = %41
  %53 = load ptr, ptr %6, align 8, !tbaa !9
  %54 = load i32, ptr %12, align 4, !tbaa !5
  %55 = sub nsw i32 %54, 1
  %56 = sext i32 %55 to i64
  %57 = getelementptr inbounds i32, ptr %53, i64 %56
  %58 = load i32, ptr %57, align 4, !tbaa !5
  %59 = load i32, ptr %14, align 4, !tbaa !5
  %60 = icmp sle i32 %58, %59
  br i1 %60, label %61, label %102

61:                                               ; preds = %52
  call void @llvm.lifetime.start.p0(i64 4, ptr %15) #4
  %62 = load ptr, ptr %7, align 8, !tbaa !9
  %63 = load i32, ptr %12, align 4, !tbaa !5
  %64 = sub nsw i32 %63, 1
  %65 = sext i32 %64 to i64
  %66 = getelementptr inbounds i32, ptr %62, i64 %65
  %67 = load i32, ptr %66, align 4, !tbaa !5
  %68 = load i32, ptr %12, align 4, !tbaa !5
  %69 = sub nsw i32 %68, 1
  %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 = load ptr, ptr %6, align 8, !tbaa !9
  %75 = load i32, ptr %12, align 4, !tbaa !5
  %76 = sub nsw i32 %75, 1
  %77 = sext i32 %76 to i64
  %78 = getelementptr inbounds i32, ptr %74, i64 %77
  %79 = load i32, ptr %78, align 4, !tbaa !5
  %80 = sub nsw i32 %73, %79
  %81 = sext i32 %80 to i64
  %82 = getelementptr inbounds i32, ptr %72, i64 %81
  %83 = load i32, ptr %82, align 4, !tbaa !5
  %84 = add nsw i32 %67, %83
  store i32 %84, ptr %15, align 4, !tbaa !5
  %85 = load i32, ptr %12, align 4, !tbaa !5
  %86 = sub nsw i32 %85, 1
  %87 = sext i32 %86 to i64
  %88 = mul nsw i64 %87, %21
  %89 = getelementptr inbounds i32, ptr %24, i64 %88
  %90 = load i32, ptr %14, align 4, !tbaa !5
  %91 = sext i32 %90 to i64
  %92 = getelementptr inbounds i32, ptr %89, i64 %91
  %93 = 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) %92)
  %94 = load i32, ptr %93, align 4, !tbaa !5
  %95 = load i32, ptr %12, align 4, !tbaa !5
  %96 = sext i32 %95 to i64
  %97 = mul nsw i64 %96, %21
  %98 = getelementptr inbounds i32, ptr %24, i64 %97
  %99 = load i32, ptr %14, align 4, !tbaa !5
  %100 = sext i32 %99 to i64
  %101 = getelementptr inbounds i32, ptr %98, i64 %100
  store i32 %94, ptr %101, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %15) #4
  br label %119

102:                                              ; preds = %52
  %103 = load i32, ptr %12, align 4, !tbaa !5
  %104 = sub nsw i32 %103, 1
  %105 = sext i32 %104 to i64
  %106 = mul nsw i64 %105, %21
  %107 = getelementptr inbounds i32, ptr %24, i64 %106
  %108 = load i32, ptr %14, align 4, !tbaa !5
  %109 = sext i32 %108 to i64
  %110 = getelementptr inbounds i32, ptr %107, i64 %109
  %111 = load i32, ptr %110, align 4, !tbaa !5
  %112 = load i32, ptr %12, align 4, !tbaa !5
  %113 = sext i32 %112 to i64
  %114 = mul nsw i64 %113, %21
  %115 = getelementptr inbounds i32, ptr %24, i64 %114
  %116 = load i32, ptr %14, align 4, !tbaa !5
  %117 = sext i32 %116 to i64
  %118 = getelementptr inbounds i32, ptr %115, i64 %117
  store i32 %111, ptr %118, align 4, !tbaa !5
  br label %119

119:                                              ; preds = %102, %61
  br label %120

120:                                              ; preds = %119, %44
  br label %121

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

124:                                              ; preds = %37
  br label %125

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

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

; 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: 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)) #3

; 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 = { 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 #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 = !{!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 @_Z8knapSackiPiS_i(i32 noundef %0, ptr nocapture noundef readonly %1, ptr nocapture noundef readonly %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_edge39

.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
  %.03238 = phi i32 [ %32, %._crit_edge ], [ 0, %.preheader.preheader ]
  %14 = zext i32 %.03238 to i64
  %15 = icmp eq i32 %.03238, 0
  %16 = add nsw i32 %.03238, -1
  %17 = sext i32 %16 to i64
  %18 = getelementptr inbounds i32, ptr %1, i64 %17
  %19 = getelementptr inbounds i32, ptr %2, i64 %17
  %20 = mul nsw i64 %17, %9
  %21 = getelementptr inbounds i32, ptr %11, i64 %20
  %22 = mul nuw nsw i64 %14, %9
  %23 = getelementptr inbounds i32, ptr %11, i64 %22
  br i1 %15, label %.lr.ph.split.us.preheader, label %24

24:                                               ; preds = %.preheader
  store i32 0, ptr %23, 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
  %25 = mul nuw nsw i64 %13, %14
  %scevgep = getelementptr i8, ptr %11, i64 %25
  call void @llvm.memset.p0.i64(ptr align 4 %scevgep, i8 0, i64 %13, i1 false), !tbaa !5
  br label %._crit_edge

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

._crit_edge:                                      ; preds = %51, %24, %.lr.ph.split.us.preheader
  %32 = add i32 %.03238, 1
  %.not = icmp sgt i32 %32, %3
  br i1 %.not, label %._crit_edge39, label %.preheader, !llvm.loop !9

.lr.ph.split.peel.next:                           ; preds = %24, %51
  %indvars.iv = phi i64 [ %indvars.iv.next, %51 ], [ 1, %24 ]
  %33 = load i32, ptr %18, align 4, !tbaa !5
  %34 = sext i32 %33 to i64
  %.not34 = icmp slt i64 %indvars.iv, %34
  br i1 %.not34, label %47, label %35

35:                                               ; preds = %.lr.ph.split.peel.next
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %5) #4
  %36 = load i32, ptr %19, align 4, !tbaa !5
  %37 = trunc nuw nsw i64 %indvars.iv to i32
  %38 = sub nsw i32 %37, %33
  %39 = sext i32 %38 to i64
  %40 = getelementptr inbounds i32, ptr %21, i64 %39
  %41 = load i32, ptr %40, align 4, !tbaa !5
  %42 = add nsw i32 %41, %36
  store i32 %42, ptr %5, align 4, !tbaa !5
  %43 = getelementptr inbounds i32, ptr %21, i64 %indvars.iv
  %44 = 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) %43)
  %45 = load i32, ptr %44, align 4, !tbaa !5
  %46 = getelementptr inbounds i32, ptr %23, i64 %indvars.iv
  store i32 %45, ptr %46, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %5) #4
  br label %51

47:                                               ; preds = %.lr.ph.split.peel.next
  %48 = getelementptr inbounds i32, ptr %21, i64 %indvars.iv
  %49 = load i32, ptr %48, align 4, !tbaa !5
  %50 = getelementptr inbounds i32, ptr %23, i64 %indvars.iv
  store i32 %49, ptr %50, align 4, !tbaa !5
  br label %51

51:                                               ; preds = %47, %35
  %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: 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,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopFullUnrollPass,LoopIdiomRecognizePass,LoopInstSimplifyPass,LoopRotatePass,LoopSimplifyCFGPass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SCCPPass,SROAPass,SimplifyCFGPass