; LLVM IR from UtkarshPathrabe/Competitive-Coding :: Model Algorithms/Dynamic Programming/Knapsack (Unbounded 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 nounwind uwtable
define dso_local noundef i32 @_Z17UnboundedKnapSackRSt6vectorIiSaIiEES2_ii(ptr noundef nonnull align 8 dereferenceable(24) %0, ptr noundef nonnull align 8 dereferenceable(24) %1, i32 noundef %2, i32 noundef %3) #0 {
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca ptr, align 8
  %10 = alloca i64, align 8
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  %13 = alloca i32, align 4
  %14 = alloca i32, align 4
  %15 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store i32 %2, ptr %7, align 4, !tbaa !9
  store i32 %3, ptr %8, align 4, !tbaa !9
  %16 = load i32, ptr %8, align 4, !tbaa !9
  %17 = add nsw i32 %16, 1
  %18 = zext i32 %17 to i64
  %19 = call ptr @llvm.stacksave.p0()
  store ptr %19, ptr %9, align 8
  %20 = alloca i32, i64 %18, align 16
  store i64 %18, ptr %10, align 8
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #3
  store i32 0, ptr %12, align 4, !tbaa !9
  br label %21

21:                                               ; preds = %30, %4
  %22 = load i32, ptr %12, align 4, !tbaa !9
  %23 = load i32, ptr %8, align 4, !tbaa !9
  %24 = icmp sle i32 %22, %23
  br i1 %24, label %26, label %25

25:                                               ; preds = %21
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #3
  br label %33

26:                                               ; preds = %21
  %27 = load i32, ptr %12, align 4, !tbaa !9
  %28 = sext i32 %27 to i64
  %29 = getelementptr inbounds i32, ptr %20, i64 %28
  store i32 0, ptr %29, align 4, !tbaa !9
  br label %30

30:                                               ; preds = %26
  %31 = load i32, ptr %12, align 4, !tbaa !9
  %32 = add nsw i32 %31, 1
  store i32 %32, ptr %12, align 4, !tbaa !9
  br label %21, !llvm.loop !11

33:                                               ; preds = %25
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #3
  store i32 1, ptr %13, align 4, !tbaa !9
  br label %34

34:                                               ; preds = %95, %33
  %35 = load i32, ptr %13, align 4, !tbaa !9
  %36 = load i32, ptr %8, align 4, !tbaa !9
  %37 = icmp sle i32 %35, %36
  br i1 %37, label %39, label %38

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

39:                                               ; preds = %34
  %40 = load i32, ptr %13, align 4, !tbaa !9
  %41 = sub nsw i32 %40, 1
  %42 = sext i32 %41 to i64
  %43 = getelementptr inbounds i32, ptr %20, i64 %42
  %44 = load i32, ptr %43, align 4, !tbaa !9
  %45 = load i32, ptr %13, align 4, !tbaa !9
  %46 = sext i32 %45 to i64
  %47 = getelementptr inbounds i32, ptr %20, i64 %46
  store i32 %44, ptr %47, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %15) #3
  store i32 0, ptr %15, align 4, !tbaa !9
  br label %48

48:                                               ; preds = %91, %39
  %49 = load i32, ptr %15, align 4, !tbaa !9
  %50 = load i32, ptr %7, align 4, !tbaa !9
  %51 = icmp slt i32 %49, %50
  br i1 %51, label %53, label %52

52:                                               ; preds = %48
  store i32 8, ptr %14, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %15) #3
  br label %94

53:                                               ; preds = %48
  %54 = load i32, ptr %13, align 4, !tbaa !9
  %55 = load ptr, ptr %6, align 8, !tbaa !5
  %56 = load i32, ptr %15, align 4, !tbaa !9
  %57 = sext i32 %56 to i64
  %58 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt6vectorIiSaIiEEixEm(ptr noundef nonnull align 8 dereferenceable(24) %55, i64 noundef %57) #3
  %59 = load i32, ptr %58, align 4, !tbaa !9
  %60 = icmp sge i32 %54, %59
  br i1 %60, label %61, label %78

61:                                               ; preds = %53
  %62 = load i32, ptr %13, align 4, !tbaa !9
  %63 = load ptr, ptr %6, align 8, !tbaa !5
  %64 = load i32, ptr %15, align 4, !tbaa !9
  %65 = sext i32 %64 to i64
  %66 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt6vectorIiSaIiEEixEm(ptr noundef nonnull align 8 dereferenceable(24) %63, i64 noundef %65) #3
  %67 = load i32, ptr %66, align 4, !tbaa !9
  %68 = sub nsw i32 %62, %67
  %69 = sext i32 %68 to i64
  %70 = getelementptr inbounds i32, ptr %20, i64 %69
  %71 = load i32, ptr %70, align 4, !tbaa !9
  %72 = load ptr, ptr %5, align 8, !tbaa !5
  %73 = load i32, ptr %15, align 4, !tbaa !9
  %74 = sext i32 %73 to i64
  %75 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt6vectorIiSaIiEEixEm(ptr noundef nonnull align 8 dereferenceable(24) %72, i64 noundef %74) #3
  %76 = load i32, ptr %75, align 4, !tbaa !9
  %77 = add nsw i32 %71, %76
  store i32 %77, ptr %11, align 4, !tbaa !9
  br label %78

78:                                               ; preds = %61, %53
  %79 = load i32, ptr %11, align 4, !tbaa !9
  %80 = load i32, ptr %13, align 4, !tbaa !9
  %81 = sext i32 %80 to i64
  %82 = getelementptr inbounds i32, ptr %20, i64 %81
  %83 = load i32, ptr %82, align 4, !tbaa !9
  %84 = icmp sgt i32 %79, %83
  br i1 %84, label %85, label %90

85:                                               ; preds = %78
  %86 = load i32, ptr %11, align 4, !tbaa !9
  %87 = load i32, ptr %13, align 4, !tbaa !9
  %88 = sext i32 %87 to i64
  %89 = getelementptr inbounds i32, ptr %20, i64 %88
  store i32 %86, ptr %89, align 4, !tbaa !9
  br label %90

90:                                               ; preds = %85, %78
  br label %91

91:                                               ; preds = %90
  %92 = load i32, ptr %15, align 4, !tbaa !9
  %93 = add nsw i32 %92, 1
  store i32 %93, ptr %15, align 4, !tbaa !9
  br label %48, !llvm.loop !13

94:                                               ; preds = %52
  br label %95

95:                                               ; preds = %94
  %96 = load i32, ptr %13, align 4, !tbaa !9
  %97 = add nsw i32 %96, 1
  store i32 %97, ptr %13, align 4, !tbaa !9
  br label %34, !llvm.loop !14

98:                                               ; preds = %38
  %99 = load i32, ptr %8, align 4, !tbaa !9
  %100 = sext i32 %99 to i64
  %101 = getelementptr inbounds i32, ptr %20, i64 %100
  %102 = load i32, ptr %101, align 4, !tbaa !9
  store i32 1, ptr %14, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #3
  %103 = load ptr, ptr %9, align 8
  call void @llvm.stackrestore.p0(ptr %103)
  ret i32 %102
}

; 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: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.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) #0 align 2

; Function Attrs: nocallback nofree nosync nounwind willreturn
declare void @llvm.stackrestore.p0(ptr) #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 }
attributes #2 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12}
!14 = distinct !{!14, !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 @_Z17UnboundedKnapSackRSt6vectorIiSaIiEES2_ii(ptr noundef nonnull align 8 dereferenceable(24) %0, ptr noundef nonnull align 8 dereferenceable(24) %1, i32 noundef %2, i32 noundef %3) local_unnamed_addr #0 {
  %5 = add i32 %3, 1
  %6 = zext i32 %5 to i64
  %7 = alloca i32, i64 %6, align 16
  %.not32 = icmp slt i32 %3, 0
  br i1 %.not32, label %._crit_edge41, label %.preheader

.preheader:                                       ; preds = %4
  %8 = shl nuw nsw i64 %6, 2
  call void @llvm.memset.p0.i64(ptr nonnull align 16 %7, i8 0, i64 %8, i1 false), !tbaa !5
  %.not3037 = icmp eq i32 %3, 0
  br i1 %.not3037, label %._crit_edge41, label %.lr.ph40

.lr.ph40:                                         ; preds = %.preheader
  %9 = icmp sgt i32 %2, 0
  br i1 %9, label %.lr.ph36.us.preheader, label %.lr.ph40.split.preheader

.lr.ph40.split.preheader:                         ; preds = %.lr.ph40
  %load_initial = load i32, ptr %7, align 16
  %10 = add nsw i64 %6, -1
  %11 = add nsw i64 %6, -2
  %xtraiter = and i64 %10, 7
  %12 = icmp ult i64 %11, 7
  br i1 %12, label %._crit_edge41.loopexit58.unr-lcssa, label %.lr.ph40.split.preheader.new

.lr.ph40.split.preheader.new:                     ; preds = %.lr.ph40.split.preheader
  %unroll_iter = and i64 %10, -8
  %invariant.gep = getelementptr i8, ptr %7, i64 4
  %invariant.gep59 = getelementptr i8, ptr %7, i64 8
  %invariant.gep61 = getelementptr i8, ptr %7, i64 12
  %invariant.gep63 = getelementptr i8, ptr %7, i64 16
  %invariant.gep65 = getelementptr i8, ptr %7, i64 20
  %invariant.gep67 = getelementptr i8, ptr %7, i64 24
  %invariant.gep69 = getelementptr i8, ptr %7, i64 28
  br label %.lr.ph40.split

.lr.ph36.us.preheader:                            ; preds = %.lr.ph40
  %wide.trip.count48 = zext nneg i32 %2 to i64
  br label %.lr.ph36.us

.lr.ph36.us:                                      ; preds = %.lr.ph36.us.preheader, %._crit_edge.us
  %indvars.iv50 = phi i64 [ 1, %.lr.ph36.us.preheader ], [ %indvars.iv.next51, %._crit_edge.us ]
  %.02838.us = phi i32 [ undef, %.lr.ph36.us.preheader ], [ %.2.us, %._crit_edge.us ]
  %13 = getelementptr i32, ptr %7, i64 %indvars.iv50
  %14 = getelementptr i8, ptr %13, i64 -4
  %15 = load i32, ptr %14, align 4, !tbaa !5
  store i32 %15, ptr %13, align 4, !tbaa !5
  %16 = trunc nuw nsw i64 %indvars.iv50 to i32
  br label %17

17:                                               ; preds = %.lr.ph36.us, %32
  %18 = phi i32 [ %15, %.lr.ph36.us ], [ %spec.store.select.us, %32 ]
  %indvars.iv45 = phi i64 [ 0, %.lr.ph36.us ], [ %indvars.iv.next46, %32 ]
  %.134.us = phi i32 [ %.02838.us, %.lr.ph36.us ], [ %.2.us, %32 ]
  %19 = tail call noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt6vectorIiSaIiEEixEm(ptr noundef nonnull align 8 dereferenceable(24) %1, i64 noundef %indvars.iv45) #3
  %20 = load i32, ptr %19, align 4, !tbaa !5
  %21 = sext i32 %20 to i64
  %.not31.us = icmp slt i64 %indvars.iv50, %21
  br i1 %.not31.us, label %32, label %22

22:                                               ; preds = %17
  %23 = tail call noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt6vectorIiSaIiEEixEm(ptr noundef nonnull align 8 dereferenceable(24) %1, i64 noundef %indvars.iv45) #3
  %24 = load i32, ptr %23, align 4, !tbaa !5
  %25 = sub nsw i32 %16, %24
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds i32, ptr %7, i64 %26
  %28 = load i32, ptr %27, align 4, !tbaa !5
  %29 = tail call noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt6vectorIiSaIiEEixEm(ptr noundef nonnull align 8 dereferenceable(24) %0, i64 noundef %indvars.iv45) #3
  %30 = load i32, ptr %29, align 4, !tbaa !5
  %31 = add nsw i32 %30, %28
  br label %32

32:                                               ; preds = %22, %17
  %.2.us = phi i32 [ %31, %22 ], [ %.134.us, %17 ]
  %spec.store.select.us = tail call i32 @llvm.smax.i32(i32 %.2.us, i32 %18)
  store i32 %spec.store.select.us, ptr %13, align 4
  %indvars.iv.next46 = add nuw nsw i64 %indvars.iv45, 1
  %exitcond49.not = icmp eq i64 %indvars.iv.next46, %wide.trip.count48
  br i1 %exitcond49.not, label %._crit_edge.us, label %17, !llvm.loop !9

._crit_edge.us:                                   ; preds = %32
  %indvars.iv.next51 = add nuw nsw i64 %indvars.iv50, 1
  %exitcond54.not = icmp eq i64 %indvars.iv.next51, %6
  br i1 %exitcond54.not, label %._crit_edge41, label %.lr.ph36.us, !llvm.loop !11

._crit_edge41.loopexit58.unr-lcssa:               ; preds = %.lr.ph40.split, %.lr.ph40.split.preheader
  %indvars.iv.unr = phi i64 [ 1, %.lr.ph40.split.preheader ], [ %indvars.iv.next.7, %.lr.ph40.split ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge41, label %.lr.ph40.split.epil

.lr.ph40.split.epil:                              ; preds = %._crit_edge41.loopexit58.unr-lcssa, %.lr.ph40.split.epil
  %indvars.iv.epil = phi i64 [ %indvars.iv.next.epil, %.lr.ph40.split.epil ], [ %indvars.iv.unr, %._crit_edge41.loopexit58.unr-lcssa ]
  %epil.iter = phi i64 [ %epil.iter.next, %.lr.ph40.split.epil ], [ 0, %._crit_edge41.loopexit58.unr-lcssa ]
  %33 = getelementptr i32, ptr %7, i64 %indvars.iv.epil
  store i32 %load_initial, ptr %33, align 4, !tbaa !5
  %indvars.iv.next.epil = add nuw nsw i64 %indvars.iv.epil, 1
  %epil.iter.next = add i64 %epil.iter, 1
  %epil.iter.cmp.not = icmp eq i64 %epil.iter.next, %xtraiter
  br i1 %epil.iter.cmp.not, label %._crit_edge41, label %.lr.ph40.split.epil, !llvm.loop !12

._crit_edge41:                                    ; preds = %._crit_edge41.loopexit58.unr-lcssa, %.lr.ph40.split.epil, %._crit_edge.us, %4, %.preheader
  %34 = sext i32 %3 to i64
  %35 = getelementptr inbounds i32, ptr %7, i64 %34
  %36 = load i32, ptr %35, align 4, !tbaa !5
  ret i32 %36

.lr.ph40.split:                                   ; preds = %.lr.ph40.split, %.lr.ph40.split.preheader.new
  %indvars.iv = phi i64 [ 1, %.lr.ph40.split.preheader.new ], [ %indvars.iv.next.7, %.lr.ph40.split ]
  %niter = phi i64 [ 0, %.lr.ph40.split.preheader.new ], [ %niter.next.7, %.lr.ph40.split ]
  %37 = getelementptr i32, ptr %7, i64 %indvars.iv
  store i32 %load_initial, ptr %37, align 4, !tbaa !5
  %gep = getelementptr i32, ptr %invariant.gep, i64 %indvars.iv
  store i32 %load_initial, ptr %gep, align 4, !tbaa !5
  %gep60 = getelementptr i32, ptr %invariant.gep59, i64 %indvars.iv
  store i32 %load_initial, ptr %gep60, align 4, !tbaa !5
  %gep62 = getelementptr i32, ptr %invariant.gep61, i64 %indvars.iv
  store i32 %load_initial, ptr %gep62, align 4, !tbaa !5
  %gep64 = getelementptr i32, ptr %invariant.gep63, i64 %indvars.iv
  store i32 %load_initial, ptr %gep64, align 4, !tbaa !5
  %gep66 = getelementptr i32, ptr %invariant.gep65, i64 %indvars.iv
  store i32 %load_initial, ptr %gep66, align 4, !tbaa !5
  %gep68 = getelementptr i32, ptr %invariant.gep67, i64 %indvars.iv
  store i32 %load_initial, ptr %gep68, align 4, !tbaa !5
  %gep70 = getelementptr i32, ptr %invariant.gep69, i64 %indvars.iv
  store i32 %load_initial, ptr %gep70, align 4, !tbaa !5
  %indvars.iv.next.7 = add nuw nsw i64 %indvars.iv, 8
  %niter.next.7 = add i64 %niter, 8
  %niter.ncmp.7 = icmp eq i64 %niter.next.7, %unroll_iter
  br i1 %niter.ncmp.7, label %._crit_edge41.loopexit58.unr-lcssa, label %.lr.ph40.split, !llvm.loop !11
}

; 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: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.smax.i32(i32, i32) #1

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

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 speculatable willreturn memory(none) }
attributes #2 = { nocallback nofree nounwind willreturn memory(argmem: write) }
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 = !{!"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 = distinct !{!12, !13}
!13 = !{!"llvm.loop.unroll.disable"}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopIdiomRecognizePass,LoopInstSimplifyPass,LoopLoadEliminationPass,LoopRotatePass,LoopSimplifyCFGPass,LoopSimplifyPass,LoopUnrollPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass