; LLVM IR from codeIIEST/Algorithms :: Competitive Coding/Greedy/Knapsack/knapsack.ll (Z8knapsackPxS_xx)

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

23:                                               ; preds = %106, %4
  %24 = load i64, ptr %12, align 8, !tbaa !9
  %25 = load i64, ptr %7, align 8, !tbaa !9
  %26 = icmp sle i64 %24, %25
  br i1 %26, label %28, label %27

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

28:                                               ; preds = %23
  call void @llvm.lifetime.start.p0(i64 8, ptr %14) #4
  store i64 0, ptr %14, align 8, !tbaa !9
  br label %29

29:                                               ; preds = %102, %28
  %30 = load i64, ptr %14, align 8, !tbaa !9
  %31 = load i64, ptr %8, align 8, !tbaa !9
  %32 = icmp sle i64 %30, %31
  br i1 %32, label %34, label %33

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

34:                                               ; preds = %29
  %35 = load i64, ptr %12, align 8, !tbaa !9
  %36 = icmp eq i64 %35, 0
  br i1 %36, label %40, label %37

37:                                               ; preds = %34
  %38 = load i64, ptr %14, align 8, !tbaa !9
  %39 = icmp eq i64 %38, 0
  br i1 %39, label %40, label %46

40:                                               ; preds = %37, %34
  %41 = load i64, ptr %12, align 8, !tbaa !9
  %42 = mul nsw i64 %41, %19
  %43 = getelementptr inbounds i64, ptr %22, i64 %42
  %44 = load i64, ptr %14, align 8, !tbaa !9
  %45 = getelementptr inbounds i64, ptr %43, i64 %44
  store i64 0, ptr %45, align 8, !tbaa !9
  br label %101

46:                                               ; preds = %37
  %47 = load ptr, ptr %6, align 8, !tbaa !5
  %48 = load i64, ptr %12, align 8, !tbaa !9
  %49 = sub nsw i64 %48, 1
  %50 = getelementptr inbounds i64, ptr %47, i64 %49
  %51 = load i64, ptr %50, align 8, !tbaa !9
  %52 = load i64, ptr %14, align 8, !tbaa !9
  %53 = icmp sle i64 %51, %52
  br i1 %53, label %54, label %87

54:                                               ; preds = %46
  call void @llvm.lifetime.start.p0(i64 8, ptr %15) #4
  %55 = load ptr, ptr %5, align 8, !tbaa !5
  %56 = load i64, ptr %12, align 8, !tbaa !9
  %57 = sub nsw i64 %56, 1
  %58 = getelementptr inbounds i64, ptr %55, i64 %57
  %59 = load i64, ptr %58, align 8, !tbaa !9
  %60 = load i64, ptr %12, align 8, !tbaa !9
  %61 = sub nsw i64 %60, 1
  %62 = mul nsw i64 %61, %19
  %63 = getelementptr inbounds i64, ptr %22, i64 %62
  %64 = load i64, ptr %14, align 8, !tbaa !9
  %65 = load ptr, ptr %6, align 8, !tbaa !5
  %66 = load i64, ptr %12, align 8, !tbaa !9
  %67 = sub nsw i64 %66, 1
  %68 = getelementptr inbounds i64, ptr %65, i64 %67
  %69 = load i64, ptr %68, align 8, !tbaa !9
  %70 = sub nsw i64 %64, %69
  %71 = getelementptr inbounds i64, ptr %63, i64 %70
  %72 = load i64, ptr %71, align 8, !tbaa !9
  %73 = add nsw i64 %59, %72
  store i64 %73, ptr %15, align 8, !tbaa !9
  %74 = load i64, ptr %12, align 8, !tbaa !9
  %75 = sub nsw i64 %74, 1
  %76 = mul nsw i64 %75, %19
  %77 = getelementptr inbounds i64, ptr %22, i64 %76
  %78 = load i64, ptr %14, align 8, !tbaa !9
  %79 = getelementptr inbounds i64, ptr %77, i64 %78
  %80 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3maxIxERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8) %15, ptr noundef nonnull align 8 dereferenceable(8) %79)
  %81 = load i64, ptr %80, align 8, !tbaa !9
  %82 = load i64, ptr %12, align 8, !tbaa !9
  %83 = mul nsw i64 %82, %19
  %84 = getelementptr inbounds i64, ptr %22, i64 %83
  %85 = load i64, ptr %14, align 8, !tbaa !9
  %86 = getelementptr inbounds i64, ptr %84, i64 %85
  store i64 %81, ptr %86, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %15) #4
  br label %100

87:                                               ; preds = %46
  %88 = load i64, ptr %12, align 8, !tbaa !9
  %89 = sub nsw i64 %88, 1
  %90 = mul nsw i64 %89, %19
  %91 = getelementptr inbounds i64, ptr %22, i64 %90
  %92 = load i64, ptr %14, align 8, !tbaa !9
  %93 = getelementptr inbounds i64, ptr %91, i64 %92
  %94 = load i64, ptr %93, align 8, !tbaa !9
  %95 = load i64, ptr %12, align 8, !tbaa !9
  %96 = mul nsw i64 %95, %19
  %97 = getelementptr inbounds i64, ptr %22, i64 %96
  %98 = load i64, ptr %14, align 8, !tbaa !9
  %99 = getelementptr inbounds i64, ptr %97, i64 %98
  store i64 %94, ptr %99, align 8, !tbaa !9
  br label %100

100:                                              ; preds = %87, %54
  br label %101

101:                                              ; preds = %100, %40
  br label %102

102:                                              ; preds = %101
  %103 = load i64, ptr %14, align 8, !tbaa !9
  %104 = add nsw i64 %103, 1
  store i64 %104, ptr %14, align 8, !tbaa !9
  br label %29, !llvm.loop !11

105:                                              ; preds = %33
  br label %106

106:                                              ; preds = %105
  %107 = load i64, ptr %12, align 8, !tbaa !9
  %108 = add nsw i64 %107, 1
  store i64 %108, ptr %12, align 8, !tbaa !9
  br label %23, !llvm.loop !13

109:                                              ; preds = %27
  %110 = load i64, ptr %7, align 8, !tbaa !9
  %111 = mul nsw i64 %110, %19
  %112 = getelementptr inbounds i64, ptr %22, i64 %111
  %113 = load i64, ptr %8, align 8, !tbaa !9
  %114 = getelementptr inbounds i64, ptr %112, i64 %113
  %115 = load i64, ptr %114, align 8, !tbaa !9
  store i32 1, ptr %13, align 4
  %116 = load ptr, ptr %9, align 8
  call void @llvm.stackrestore.p0(ptr %116)
  ret i64 %115
}

; 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 8 dereferenceable(8) ptr @_ZSt3maxIxERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8), ptr noundef nonnull align 8 dereferenceable(8)) #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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long long", !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 i64 @_Z8knapsackPxS_xx(ptr nocapture noundef readonly %0, ptr nocapture noundef readonly %1, i64 noundef %2, i64 noundef %3) local_unnamed_addr #0 {
  %5 = alloca i64, align 8
  %6 = add nsw i64 %2, 1
  %7 = add i64 %3, 1
  %8 = mul nuw i64 %7, %6
  %9 = alloca i64, i64 %8, align 16
  %10 = or i64 %2, %3
  %or.cond.not = icmp sgt i64 %10, -1
  br i1 %or.cond.not, label %.preheader.preheader, label %._crit_edge40

.preheader.preheader:                             ; preds = %4
  %11 = shl i64 %3, 3
  %12 = add i64 %11, 8
  %exitcond.peel.not = icmp eq i64 %3, 0
  br label %.preheader

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

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

.lr.ph.split.us.preheader:                        ; preds = %.preheader
  %22 = mul nuw nsw i64 %12, %.03239
  %scevgep = getelementptr i8, ptr %9, i64 %22
  call void @llvm.memset.p0.i64(ptr align 8 %scevgep, i8 0, i64 %12, i1 false), !tbaa !5
  br label %._crit_edge

._crit_edge40:                                    ; preds = %._crit_edge, %4
  %23 = mul nsw i64 %7, %2
  %24 = getelementptr inbounds i64, ptr %9, i64 %23
  %25 = getelementptr inbounds i64, ptr %24, i64 %3
  %26 = load i64, ptr %25, align 8, !tbaa !5
  ret i64 %26

._crit_edge:                                      ; preds = %43, %21, %.lr.ph.split.us.preheader
  %27 = add i64 %.03239, 1
  %.not = icmp sgt i64 %27, %2
  br i1 %.not, label %._crit_edge40, label %.preheader, !llvm.loop !9

.lr.ph.split.peel.next:                           ; preds = %21, %43
  %.037 = phi i64 [ %44, %43 ], [ 1, %21 ]
  %28 = load i64, ptr %15, align 8, !tbaa !5
  %.not35 = icmp sgt i64 %28, %.037
  br i1 %.not35, label %39, label %29

29:                                               ; preds = %.lr.ph.split.peel.next
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %5) #4
  %30 = load i64, ptr %16, align 8, !tbaa !5
  %31 = sub nsw i64 %.037, %28
  %32 = getelementptr inbounds i64, ptr %18, i64 %31
  %33 = load i64, ptr %32, align 8, !tbaa !5
  %34 = add nsw i64 %33, %30
  store i64 %34, ptr %5, align 8, !tbaa !5
  %35 = getelementptr inbounds i64, ptr %18, i64 %.037
  %36 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3maxIxERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8) %5, ptr noundef nonnull align 8 dereferenceable(8) %35)
  %37 = load i64, ptr %36, align 8, !tbaa !5
  %38 = getelementptr inbounds i64, ptr %20, i64 %.037
  store i64 %37, ptr %38, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %5) #4
  br label %43

39:                                               ; preds = %.lr.ph.split.peel.next
  %40 = getelementptr inbounds i64, ptr %18, i64 %.037
  %41 = load i64, ptr %40, align 8, !tbaa !5
  %42 = getelementptr inbounds i64, ptr %20, i64 %.037
  store i64 %41, ptr %42, align 8, !tbaa !5
  br label %43

43:                                               ; preds = %39, %29
  %44 = add nuw i64 %.037, 1
  %exitcond.not = icmp eq i64 %.037, %3
  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 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

; 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 = !{!"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, !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