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

; 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
declare dso_local noundef i64 @_Z3maxxx(i64 noundef, i64 noundef) #0

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i64 @_Z8knapSackxPxS_x(i64 noundef %0, ptr noundef %1, ptr noundef %2, i64 noundef %3) #0 {
  %5 = alloca i64, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i64, align 8
  %9 = alloca ptr, align 8
  %10 = alloca i64, align 8
  %11 = alloca i32, align 4
  %12 = alloca i64, align 8
  %13 = alloca i64, align 8
  store i64 %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !9
  store ptr %2, ptr %7, align 8, !tbaa !9
  store i64 %3, ptr %8, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #4
  %14 = load i64, ptr %5, align 8, !tbaa !5
  %15 = add nsw i64 %14, 1
  %16 = call noalias ptr @calloc(i64 noundef %15, i64 noundef 8) #5
  store ptr %16, ptr %9, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #4
  store i64 1, ptr %10, align 8, !tbaa !5
  br label %17

17:                                               ; preds = %66, %4
  %18 = load i64, ptr %10, align 8, !tbaa !5
  %19 = load i64, ptr %8, align 8, !tbaa !5
  %20 = icmp sle i64 %18, %19
  br i1 %20, label %22, label %21

21:                                               ; preds = %17
  store i32 2, ptr %11, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #4
  br label %69

22:                                               ; preds = %17
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #4
  %23 = load i64, ptr %5, align 8, !tbaa !5
  store i64 %23, ptr %12, align 8, !tbaa !5
  br label %24

24:                                               ; preds = %62, %22
  %25 = load i64, ptr %12, align 8, !tbaa !5
  %26 = icmp sgt i64 %25, 0
  br i1 %26, label %28, label %27

27:                                               ; preds = %24
  store i32 5, ptr %11, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #4
  br label %65

28:                                               ; preds = %24
  %29 = load ptr, ptr %6, align 8, !tbaa !9
  %30 = load i64, ptr %10, align 8, !tbaa !5
  %31 = sub nsw i64 %30, 1
  %32 = getelementptr inbounds i64, ptr %29, i64 %31
  %33 = load i64, ptr %32, align 8, !tbaa !5
  %34 = load i64, ptr %12, align 8, !tbaa !5
  %35 = icmp sle i64 %33, %34
  br i1 %35, label %36, label %61

36:                                               ; preds = %28
  %37 = load ptr, ptr %7, align 8, !tbaa !9
  %38 = load i64, ptr %10, align 8, !tbaa !5
  %39 = sub nsw i64 %38, 1
  %40 = getelementptr inbounds i64, ptr %37, i64 %39
  %41 = load i64, ptr %40, align 8, !tbaa !5
  %42 = load ptr, ptr %9, align 8, !tbaa !9
  %43 = load i64, ptr %12, align 8, !tbaa !5
  %44 = load ptr, ptr %6, align 8, !tbaa !9
  %45 = load i64, ptr %10, align 8, !tbaa !5
  %46 = sub nsw i64 %45, 1
  %47 = getelementptr inbounds i64, ptr %44, i64 %46
  %48 = load i64, ptr %47, align 8, !tbaa !5
  %49 = sub nsw i64 %43, %48
  %50 = getelementptr inbounds i64, ptr %42, i64 %49
  %51 = load i64, ptr %50, align 8, !tbaa !5
  %52 = add nsw i64 %41, %51
  %53 = load ptr, ptr %9, align 8, !tbaa !9
  %54 = load i64, ptr %12, align 8, !tbaa !5
  %55 = getelementptr inbounds i64, ptr %53, i64 %54
  %56 = load i64, ptr %55, align 8, !tbaa !5
  %57 = call noundef i64 @_Z3maxxx(i64 noundef %52, i64 noundef %56)
  %58 = load ptr, ptr %9, align 8, !tbaa !9
  %59 = load i64, ptr %12, align 8, !tbaa !5
  %60 = getelementptr inbounds i64, ptr %58, i64 %59
  store i64 %57, ptr %60, align 8, !tbaa !5
  br label %61

61:                                               ; preds = %36, %28
  br label %62

62:                                               ; preds = %61
  %63 = load i64, ptr %12, align 8, !tbaa !5
  %64 = add nsw i64 %63, -1
  store i64 %64, ptr %12, align 8, !tbaa !5
  br label %24, !llvm.loop !11

65:                                               ; preds = %27
  br label %66

66:                                               ; preds = %65
  %67 = load i64, ptr %10, align 8, !tbaa !5
  %68 = add nsw i64 %67, 1
  store i64 %68, ptr %10, align 8, !tbaa !5
  br label %17, !llvm.loop !13

69:                                               ; preds = %21
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #4
  %70 = load ptr, ptr %9, align 8, !tbaa !9
  %71 = load i64, ptr %5, align 8, !tbaa !5
  %72 = getelementptr inbounds i64, ptr %70, i64 %71
  %73 = load i64, ptr %72, align 8, !tbaa !5
  store i64 %73, ptr %13, align 8, !tbaa !5
  %74 = load ptr, ptr %9, align 8, !tbaa !9
  call void @free(ptr noundef %74) #4
  %75 = load i64, ptr %13, align 8, !tbaa !5
  store i32 1, ptr %11, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #4
  ret i64 %75
}

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

; Function Attrs: nounwind allocsize(0,1)
declare noalias ptr @calloc(i64 noundef, i64 noundef) #2

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

; Function Attrs: nounwind
declare void @free(ptr noundef) #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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nounwind allocsize(0,1) "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 = { nounwind "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 }
attributes #5 = { nounwind allocsize(0,1) }

!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 = !{!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
declare dso_local noundef i64 @_Z3maxxx(i64 noundef, i64 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i64 @_Z8knapSackxPxS_x(i64 noundef %0, ptr nocapture noundef readonly %1, ptr nocapture noundef readonly %2, i64 noundef %3) local_unnamed_addr #0 {
  %5 = add nsw i64 %0, 1
  %6 = tail call noalias ptr @calloc(i64 noundef %5, i64 noundef 8) #3
  %.not27 = icmp sgt i64 %3, 0
  %7 = icmp sgt i64 %0, 0
  %or.cond = and i1 %.not27, %7
  br i1 %or.cond, label %.preheader.us, label %._crit_edge29

.preheader.us:                                    ; preds = %4, %._crit_edge.us
  %.028.us = phi i64 [ %25, %._crit_edge.us ], [ 1, %4 ]
  %8 = add nsw i64 %.028.us, -1
  %9 = getelementptr inbounds i64, ptr %1, i64 %8
  %10 = getelementptr inbounds i64, ptr %2, i64 %8
  br label %11

11:                                               ; preds = %.preheader.us, %22
  %.02326.us = phi i64 [ %0, %.preheader.us ], [ %23, %22 ]
  %12 = load i64, ptr %9, align 8, !tbaa !5
  %.not25.us = icmp sgt i64 %12, %.02326.us
  br i1 %.not25.us, label %22, label %13

13:                                               ; preds = %11
  %14 = load i64, ptr %10, align 8, !tbaa !5
  %15 = sub nsw i64 %.02326.us, %12
  %16 = getelementptr inbounds i64, ptr %6, i64 %15
  %17 = load i64, ptr %16, align 8, !tbaa !5
  %18 = add nsw i64 %17, %14
  %19 = getelementptr inbounds i64, ptr %6, i64 %.02326.us
  %20 = load i64, ptr %19, align 8, !tbaa !5
  %21 = tail call noundef i64 @_Z3maxxx(i64 noundef %18, i64 noundef %20)
  store i64 %21, ptr %19, align 8, !tbaa !5
  br label %22

22:                                               ; preds = %13, %11
  %23 = add nsw i64 %.02326.us, -1
  %24 = icmp sgt i64 %.02326.us, 1
  br i1 %24, label %11, label %._crit_edge.us, !llvm.loop !9

._crit_edge.us:                                   ; preds = %22
  %25 = add nuw i64 %.028.us, 1
  %exitcond.not = icmp eq i64 %.028.us, %3
  br i1 %exitcond.not, label %._crit_edge29.loopexit, label %.preheader.us, !llvm.loop !11

._crit_edge29.loopexit:                           ; preds = %._crit_edge.us
  %.phi.trans.insert = getelementptr inbounds i64, ptr %6, i64 %0
  %.pre = load i64, ptr %.phi.trans.insert, align 8, !tbaa !5
  br label %._crit_edge29

._crit_edge29:                                    ; preds = %._crit_edge29.loopexit, %4
  %26 = phi i64 [ %.pre, %._crit_edge29.loopexit ], [ 0, %4 ]
  tail call void @free(ptr noundef %6) #4
  ret i64 %26
}

; Function Attrs: mustprogress nofree nounwind willreturn allockind("alloc,zeroed") allocsize(0,1) memory(inaccessiblemem: readwrite)
declare noalias noundef ptr @calloc(i64 noundef, i64 noundef) local_unnamed_addr #1

; Function Attrs: mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite)
declare void @free(ptr allocptr nocapture noundef) local_unnamed_addr #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 = { mustprogress nofree nounwind willreturn allockind("alloc,zeroed") allocsize(0,1) memory(inaccessiblemem: readwrite) "alloc-family"="malloc" "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 #2 = { mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite) "alloc-family"="malloc" "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 = { nounwind allocsize(0,1) }
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}


; Effective O3 passes: BDCEPass,EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopRotatePass,LoopSimplifyCFGPass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass