; LLVM IR from Algo-Phantoms/Algo-Tree :: Code/C++/unbounded_knapsack.ll (Z8KnapsackPiS_ii)

; 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 @_Z8KnapsackPiS_ii(ptr noundef %0, ptr noundef %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 i64, align 8
  %12 = alloca i32, align 4
  %13 = alloca i32, align 4
  %14 = alloca i32, align 4
  %15 = alloca i32, align 4
  %16 = alloca i32, align 4
  %17 = 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
  %18 = load i32, ptr %7, align 4, !tbaa !9
  %19 = add nsw i32 %18, 1
  %20 = zext i32 %19 to i64
  %21 = load i32, ptr %8, align 4, !tbaa !9
  %22 = add nsw i32 %21, 1
  %23 = zext i32 %22 to i64
  %24 = call ptr @llvm.stacksave.p0()
  store ptr %24, ptr %9, align 8
  %25 = mul nuw i64 %20, %23
  %26 = alloca i32, i64 %25, align 16
  store i64 %20, ptr %10, align 8
  store i64 %23, ptr %11, align 8
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #4
  store i32 0, ptr %12, align 4, !tbaa !9
  br label %27

27:                                               ; preds = %59, %4
  %28 = load i32, ptr %12, align 4, !tbaa !9
  %29 = load i32, ptr %7, align 4, !tbaa !9
  %30 = add nsw i32 %29, 1
  %31 = icmp slt i32 %28, %30
  br i1 %31, label %33, label %32

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

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

34:                                               ; preds = %55, %33
  %35 = load i32, ptr %14, align 4, !tbaa !9
  %36 = load i32, ptr %8, align 4, !tbaa !9
  %37 = add nsw i32 %36, 1
  %38 = icmp slt i32 %35, %37
  br i1 %38, label %40, label %39

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

40:                                               ; preds = %34
  %41 = load i32, ptr %12, align 4, !tbaa !9
  %42 = icmp eq i32 %41, 0
  br i1 %42, label %46, label %43

43:                                               ; preds = %40
  %44 = load i32, ptr %14, align 4, !tbaa !9
  %45 = icmp eq i32 %44, 0
  br i1 %45, label %46, label %54

46:                                               ; preds = %43, %40
  %47 = load i32, ptr %12, align 4, !tbaa !9
  %48 = sext i32 %47 to i64
  %49 = mul nsw i64 %48, %23
  %50 = getelementptr inbounds i32, ptr %26, i64 %49
  %51 = load i32, ptr %14, align 4, !tbaa !9
  %52 = sext i32 %51 to i64
  %53 = getelementptr inbounds i32, ptr %50, i64 %52
  store i32 0, ptr %53, align 4, !tbaa !9
  br label %54

54:                                               ; preds = %46, %43
  br label %55

55:                                               ; preds = %54
  %56 = load i32, ptr %14, align 4, !tbaa !9
  %57 = add nsw i32 %56, 1
  store i32 %57, ptr %14, align 4, !tbaa !9
  br label %34, !llvm.loop !11

58:                                               ; preds = %39
  br label %59

59:                                               ; preds = %58
  %60 = load i32, ptr %12, align 4, !tbaa !9
  %61 = add nsw i32 %60, 1
  store i32 %61, ptr %12, align 4, !tbaa !9
  br label %27, !llvm.loop !13

62:                                               ; preds = %32
  call void @llvm.lifetime.start.p0(i64 4, ptr %15) #4
  store i32 1, ptr %15, align 4, !tbaa !9
  br label %63

63:                                               ; preds = %147, %62
  %64 = load i32, ptr %15, align 4, !tbaa !9
  %65 = load i32, ptr %7, align 4, !tbaa !9
  %66 = add nsw i32 %65, 1
  %67 = icmp slt i32 %64, %66
  br i1 %67, label %69, label %68

68:                                               ; preds = %63
  store i32 8, ptr %13, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %15) #4
  br label %150

69:                                               ; preds = %63
  call void @llvm.lifetime.start.p0(i64 4, ptr %16) #4
  store i32 1, ptr %16, align 4, !tbaa !9
  br label %70

70:                                               ; preds = %143, %69
  %71 = load i32, ptr %16, align 4, !tbaa !9
  %72 = load i32, ptr %8, align 4, !tbaa !9
  %73 = add nsw i32 %72, 1
  %74 = icmp slt i32 %71, %73
  br i1 %74, label %76, label %75

75:                                               ; preds = %70
  store i32 11, ptr %13, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %16) #4
  br label %146

76:                                               ; preds = %70
  %77 = load ptr, ptr %5, align 8, !tbaa !5
  %78 = load i32, ptr %15, align 4, !tbaa !9
  %79 = sub nsw i32 %78, 1
  %80 = sext i32 %79 to i64
  %81 = getelementptr inbounds i32, ptr %77, i64 %80
  %82 = load i32, ptr %81, align 4, !tbaa !9
  %83 = load i32, ptr %16, align 4, !tbaa !9
  %84 = icmp sle i32 %82, %83
  br i1 %84, label %85, label %125

85:                                               ; preds = %76
  call void @llvm.lifetime.start.p0(i64 4, ptr %17) #4
  %86 = load ptr, ptr %6, align 8, !tbaa !5
  %87 = load i32, ptr %15, align 4, !tbaa !9
  %88 = sub nsw i32 %87, 1
  %89 = sext i32 %88 to i64
  %90 = getelementptr inbounds i32, ptr %86, i64 %89
  %91 = load i32, ptr %90, align 4, !tbaa !9
  %92 = load i32, ptr %15, align 4, !tbaa !9
  %93 = sext i32 %92 to i64
  %94 = mul nsw i64 %93, %23
  %95 = getelementptr inbounds i32, ptr %26, i64 %94
  %96 = load i32, ptr %16, align 4, !tbaa !9
  %97 = load ptr, ptr %5, align 8, !tbaa !5
  %98 = load i32, ptr %15, align 4, !tbaa !9
  %99 = sub nsw i32 %98, 1
  %100 = sext i32 %99 to i64
  %101 = getelementptr inbounds i32, ptr %97, i64 %100
  %102 = load i32, ptr %101, align 4, !tbaa !9
  %103 = sub nsw i32 %96, %102
  %104 = sext i32 %103 to i64
  %105 = getelementptr inbounds i32, ptr %95, i64 %104
  %106 = load i32, ptr %105, align 4, !tbaa !9
  %107 = add nsw i32 %91, %106
  store i32 %107, ptr %17, align 4, !tbaa !9
  %108 = load i32, ptr %15, align 4, !tbaa !9
  %109 = sub nsw i32 %108, 1
  %110 = sext i32 %109 to i64
  %111 = mul nsw i64 %110, %23
  %112 = getelementptr inbounds i32, ptr %26, i64 %111
  %113 = load i32, ptr %16, align 4, !tbaa !9
  %114 = sext i32 %113 to i64
  %115 = getelementptr inbounds i32, ptr %112, i64 %114
  %116 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3maxIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4) %17, ptr noundef nonnull align 4 dereferenceable(4) %115)
  %117 = load i32, ptr %116, align 4, !tbaa !9
  %118 = load i32, ptr %15, align 4, !tbaa !9
  %119 = sext i32 %118 to i64
  %120 = mul nsw i64 %119, %23
  %121 = getelementptr inbounds i32, ptr %26, i64 %120
  %122 = load i32, ptr %16, align 4, !tbaa !9
  %123 = sext i32 %122 to i64
  %124 = getelementptr inbounds i32, ptr %121, i64 %123
  store i32 %117, ptr %124, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 4, ptr %17) #4
  br label %142

125:                                              ; preds = %76
  %126 = load i32, ptr %15, align 4, !tbaa !9
  %127 = sub nsw i32 %126, 1
  %128 = sext i32 %127 to i64
  %129 = mul nsw i64 %128, %23
  %130 = getelementptr inbounds i32, ptr %26, i64 %129
  %131 = load i32, ptr %16, align 4, !tbaa !9
  %132 = sext i32 %131 to i64
  %133 = getelementptr inbounds i32, ptr %130, i64 %132
  %134 = load i32, ptr %133, align 4, !tbaa !9
  %135 = load i32, ptr %15, align 4, !tbaa !9
  %136 = sext i32 %135 to i64
  %137 = mul nsw i64 %136, %23
  %138 = getelementptr inbounds i32, ptr %26, i64 %137
  %139 = load i32, ptr %16, align 4, !tbaa !9
  %140 = sext i32 %139 to i64
  %141 = getelementptr inbounds i32, ptr %138, i64 %140
  store i32 %134, ptr %141, align 4, !tbaa !9
  br label %142

142:                                              ; preds = %125, %85
  br label %143

143:                                              ; preds = %142
  %144 = load i32, ptr %16, align 4, !tbaa !9
  %145 = add nsw i32 %144, 1
  store i32 %145, ptr %16, align 4, !tbaa !9
  br label %70, !llvm.loop !14

146:                                              ; preds = %75
  br label %147

147:                                              ; preds = %146
  %148 = load i32, ptr %15, align 4, !tbaa !9
  %149 = add nsw i32 %148, 1
  store i32 %149, ptr %15, align 4, !tbaa !9
  br label %63, !llvm.loop !15

150:                                              ; preds = %68
  %151 = load i32, ptr %7, align 4, !tbaa !9
  %152 = sext i32 %151 to i64
  %153 = mul nsw i64 %152, %23
  %154 = getelementptr inbounds i32, ptr %26, i64 %153
  %155 = load i32, ptr %8, align 4, !tbaa !9
  %156 = sext i32 %155 to i64
  %157 = getelementptr inbounds i32, ptr %154, i64 %156
  %158 = load i32, ptr %157, align 4, !tbaa !9
  store i32 1, ptr %13, align 4
  %159 = load ptr, ptr %9, align 8
  call void @llvm.stackrestore.p0(ptr %159)
  ret i32 %158
}

; 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: 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
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 = !{!"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}
!15 = distinct !{!15, !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 @_Z8KnapsackPiS_ii(ptr nocapture noundef readonly %0, ptr nocapture noundef readonly %1, i32 noundef %2, i32 noundef %3) local_unnamed_addr #0 {
  %5 = alloca i32, align 4
  %6 = add i32 %2, 1
  %7 = zext i32 %6 to i64
  %8 = add i32 %3, 1
  %9 = zext i32 %8 to i64
  %10 = mul nuw i64 %9, %7
  %11 = alloca i32, i64 %10, align 16
  %.not50 = icmp slt i32 %2, 0
  br i1 %.not50, label %._crit_edge57, label %.preheader47.lr.ph

.preheader47.lr.ph:                               ; preds = %4
  %.not4548 = icmp slt i32 %3, 0
  br i1 %.not4548, label %.preheader46, label %.preheader47.preheader

.preheader47.preheader:                           ; preds = %.preheader47.lr.ph
  %12 = shl nuw nsw i64 %9, 2
  %smax = tail call i32 @llvm.smax.i32(i32 %6, i32 1)
  %wide.trip.count67 = zext nneg i32 %smax to i64
  call void @llvm.memset.p0.i64(ptr nonnull align 16 %11, i8 0, i64 %12, i1 false), !tbaa !5
  %exitcond68.peel = icmp slt i32 %6, 2
  br i1 %exitcond68.peel, label %.preheader46, label %.preheader47

.preheader47:                                     ; preds = %.preheader47.preheader, %.preheader47
  %indvar = phi i64 [ %indvar.next, %.preheader47 ], [ 1, %.preheader47.preheader ]
  %13 = mul nuw nsw i64 %indvar, %9
  %14 = getelementptr inbounds i32, ptr %11, i64 %13
  store i32 0, ptr %14, align 4, !tbaa !5
  %indvar.next = add nuw nsw i64 %indvar, 1
  %exitcond68 = icmp eq i64 %indvar.next, %wide.trip.count67
  br i1 %exitcond68, label %.preheader46, label %.preheader47, !llvm.loop !9

.preheader46:                                     ; preds = %.preheader47.preheader, %.preheader47, %.preheader47.lr.ph
  %.not4255 = icmp eq i32 %2, 0
  %.not4352 = icmp slt i32 %3, 1
  %or.cond = or i1 %.not4255, %.not4352
  br i1 %or.cond, label %._crit_edge57, label %.preheader

.preheader:                                       ; preds = %.preheader46, %._crit_edge54
  %indvars.iv74 = phi i64 [ %indvars.iv.next75, %._crit_edge54 ], [ 1, %.preheader46 ]
  %15 = add nsw i64 %indvars.iv74, -1
  %16 = getelementptr inbounds i32, ptr %0, i64 %15
  %17 = getelementptr inbounds i32, ptr %1, i64 %15
  %18 = mul nuw nsw i64 %indvars.iv74, %9
  %19 = getelementptr inbounds i32, ptr %11, i64 %18
  %20 = mul nuw nsw i64 %15, %9
  %21 = getelementptr inbounds i32, ptr %11, i64 %20
  br label %28

._crit_edge57:                                    ; preds = %._crit_edge54, %4, %.preheader46
  %22 = sext i32 %2 to i64
  %23 = mul nsw i64 %9, %22
  %24 = getelementptr inbounds i32, ptr %11, i64 %23
  %25 = sext i32 %3 to i64
  %26 = getelementptr inbounds i32, ptr %24, i64 %25
  %27 = load i32, ptr %26, align 4, !tbaa !5
  ret i32 %27

._crit_edge54:                                    ; preds = %47
  %indvars.iv.next75 = add nuw nsw i64 %indvars.iv74, 1
  %exitcond78.not = icmp eq i64 %indvars.iv.next75, %7
  br i1 %exitcond78.not, label %._crit_edge57, label %.preheader, !llvm.loop !12

28:                                               ; preds = %.preheader, %47
  %indvars.iv69 = phi i64 [ 1, %.preheader ], [ %indvars.iv.next70, %47 ]
  %29 = load i32, ptr %16, align 4, !tbaa !5
  %30 = sext i32 %29 to i64
  %.not44 = icmp slt i64 %indvars.iv69, %30
  br i1 %.not44, label %43, label %31

31:                                               ; preds = %28
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %5) #5
  %32 = load i32, ptr %17, align 4, !tbaa !5
  %33 = trunc nuw nsw i64 %indvars.iv69 to i32
  %34 = sub nsw i32 %33, %29
  %35 = sext i32 %34 to i64
  %36 = getelementptr inbounds i32, ptr %19, i64 %35
  %37 = load i32, ptr %36, align 4, !tbaa !5
  %38 = add nsw i32 %37, %32
  store i32 %38, ptr %5, align 4, !tbaa !5
  %39 = getelementptr inbounds i32, ptr %21, i64 %indvars.iv69
  %40 = 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) %39)
  %41 = load i32, ptr %40, align 4, !tbaa !5
  %42 = getelementptr inbounds i32, ptr %19, i64 %indvars.iv69
  store i32 %41, ptr %42, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %5) #5
  br label %47

43:                                               ; preds = %28
  %44 = getelementptr inbounds i32, ptr %21, i64 %indvars.iv69
  %45 = load i32, ptr %44, align 4, !tbaa !5
  %46 = getelementptr inbounds i32, ptr %19, i64 %indvars.iv69
  store i32 %45, ptr %46, align 4, !tbaa !5
  br label %47

47:                                               ; preds = %31, %43
  %indvars.iv.next70 = add nuw nsw i64 %indvars.iv69, 1
  %exitcond73.not = icmp eq i64 %indvars.iv.next70, %9
  br i1 %exitcond73.not, label %._crit_edge54, label %28, !llvm.loop !13
}

; 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 nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.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: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #3

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.smax.i32(i32, i32) #4

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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
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 = distinct !{!9, !10, !11}
!10 = !{!"llvm.loop.mustprogress"}
!11 = !{!"llvm.loop.peeled.count", i32 1}
!12 = distinct !{!12, !10}
!13 = distinct !{!13, !10}


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