; LLVM IR from ahr9n/psolving-paradigms :: 4.dynamic-programming/04.knapsack/CodeForces 1287C - Garland.ll (Z2dpiiii)

; 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"

@n = external dso_local global i64, align 8
@p = external dso_local global [105 x i64], align 16
@mem = external dso_local global [105 x [105 x [105 x [2 x i64]]]], align 16

; Function Attrs: mustprogress uwtable
define dso_local noundef i64 @_Z2dpiiii(i32 noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %3) #0 {
  %5 = alloca i64, align 8
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca ptr, align 8
  %11 = alloca i32, align 4
  %12 = alloca i64, align 8
  %13 = alloca i64, align 8
  store i32 %0, ptr %6, align 4, !tbaa !5
  store i32 %1, ptr %7, align 4, !tbaa !5
  store i32 %2, ptr %8, align 4, !tbaa !5
  store i32 %3, ptr %9, align 4, !tbaa !5
  %14 = load i32, ptr %6, align 4, !tbaa !5
  %15 = sext i32 %14 to i64
  %16 = load i64, ptr @n, align 8, !tbaa !9
  %17 = icmp eq i64 %15, %16
  br i1 %17, label %18, label %19

18:                                               ; preds = %4
  store i64 0, ptr %5, align 8
  br label %104

19:                                               ; preds = %4
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #3
  %20 = load i32, ptr %6, align 4, !tbaa !5
  %21 = sext i32 %20 to i64
  %22 = getelementptr inbounds [105 x [105 x [105 x [2 x i64]]]], ptr @mem, i64 0, i64 %21
  %23 = load i32, ptr %7, align 4, !tbaa !5
  %24 = sext i32 %23 to i64
  %25 = getelementptr inbounds [105 x [105 x [2 x i64]]], ptr %22, i64 0, i64 %24
  %26 = load i32, ptr %8, align 4, !tbaa !5
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds [105 x [2 x i64]], ptr %25, i64 0, i64 %27
  %29 = load i32, ptr %9, align 4, !tbaa !5
  %30 = sext i32 %29 to i64
  %31 = getelementptr inbounds [2 x i64], ptr %28, i64 0, i64 %30
  store ptr %31, ptr %10, align 8, !tbaa !11
  %32 = load ptr, ptr %10, align 8, !tbaa !11
  %33 = load i64, ptr %32, align 8, !tbaa !9
  %34 = xor i64 %33, -1
  %35 = icmp ne i64 %34, 0
  br i1 %35, label %36, label %39

36:                                               ; preds = %19
  %37 = load ptr, ptr %10, align 8, !tbaa !11
  %38 = load i64, ptr %37, align 8, !tbaa !9
  store i64 %38, ptr %5, align 8
  store i32 1, ptr %11, align 4
  br label %103

39:                                               ; preds = %19
  %40 = load i32, ptr %6, align 4, !tbaa !5
  %41 = sext i32 %40 to i64
  %42 = getelementptr inbounds [105 x i64], ptr @p, i64 0, i64 %41
  %43 = load i64, ptr %42, align 8, !tbaa !9
  %44 = icmp ne i64 %43, 0
  br i1 %44, label %45, label %68

45:                                               ; preds = %39
  %46 = load i32, ptr %9, align 4, !tbaa !5
  %47 = sext i32 %46 to i64
  %48 = load i32, ptr %6, align 4, !tbaa !5
  %49 = sext i32 %48 to i64
  %50 = getelementptr inbounds [105 x i64], ptr @p, i64 0, i64 %49
  %51 = load i64, ptr %50, align 8, !tbaa !9
  %52 = srem i64 %51, 2
  %53 = icmp ne i64 %47, %52
  %54 = zext i1 %53 to i64
  %55 = load i32, ptr %6, align 4, !tbaa !5
  %56 = add nsw i32 %55, 1
  %57 = load i32, ptr %7, align 4, !tbaa !5
  %58 = load i32, ptr %8, align 4, !tbaa !5
  %59 = load i32, ptr %6, align 4, !tbaa !5
  %60 = sext i32 %59 to i64
  %61 = getelementptr inbounds [105 x i64], ptr @p, i64 0, i64 %60
  %62 = load i64, ptr %61, align 8, !tbaa !9
  %63 = srem i64 %62, 2
  %64 = trunc i64 %63 to i32
  %65 = call noundef i64 @_Z2dpiiii(i32 noundef %56, i32 noundef %57, i32 noundef %58, i32 noundef %64)
  %66 = add nsw i64 %54, %65
  %67 = load ptr, ptr %10, align 8, !tbaa !11
  store i64 %66, ptr %67, align 8, !tbaa !9
  br label %100

68:                                               ; preds = %39
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #3
  store i64 1000000000000000000, ptr %12, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #3
  store i64 1000000000000000000, ptr %13, align 8, !tbaa !9
  %69 = load i32, ptr %8, align 4, !tbaa !5
  %70 = icmp sgt i32 %69, 0
  br i1 %70, label %71, label %82

71:                                               ; preds = %68
  %72 = load i32, ptr %9, align 4, !tbaa !5
  %73 = icmp ne i32 %72, 0
  %74 = zext i1 %73 to i64
  %75 = load i32, ptr %6, align 4, !tbaa !5
  %76 = add nsw i32 %75, 1
  %77 = load i32, ptr %7, align 4, !tbaa !5
  %78 = load i32, ptr %8, align 4, !tbaa !5
  %79 = sub nsw i32 %78, 1
  %80 = call noundef i64 @_Z2dpiiii(i32 noundef %76, i32 noundef %77, i32 noundef %79, i32 noundef 0)
  %81 = add nsw i64 %74, %80
  store i64 %81, ptr %12, align 8, !tbaa !9
  br label %82

82:                                               ; preds = %71, %68
  %83 = load i32, ptr %7, align 4, !tbaa !5
  %84 = icmp sgt i32 %83, 0
  br i1 %84, label %85, label %96

85:                                               ; preds = %82
  %86 = load i32, ptr %9, align 4, !tbaa !5
  %87 = icmp ne i32 %86, 1
  %88 = zext i1 %87 to i64
  %89 = load i32, ptr %6, align 4, !tbaa !5
  %90 = add nsw i32 %89, 1
  %91 = load i32, ptr %7, align 4, !tbaa !5
  %92 = sub nsw i32 %91, 1
  %93 = load i32, ptr %8, align 4, !tbaa !5
  %94 = call noundef i64 @_Z2dpiiii(i32 noundef %90, i32 noundef %92, i32 noundef %93, i32 noundef 1)
  %95 = add nsw i64 %88, %94
  store i64 %95, ptr %13, align 8, !tbaa !9
  br label %96

96:                                               ; preds = %85, %82
  %97 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3minIxERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8) %12, ptr noundef nonnull align 8 dereferenceable(8) %13)
  %98 = load i64, ptr %97, align 8, !tbaa !9
  %99 = load ptr, ptr %10, align 8, !tbaa !11
  store i64 %98, ptr %99, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #3
  br label %100

100:                                              ; preds = %96, %45
  %101 = load ptr, ptr %10, align 8, !tbaa !11
  %102 = load i64, ptr %101, align 8, !tbaa !9
  store i64 %102, ptr %5, align 8
  store i32 1, ptr %11, align 4
  br label %103

103:                                              ; preds = %100, %36
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  br label %104

104:                                              ; preds = %103, %18
  %105 = load i64, ptr %5, align 8
  ret i64 %105
}

; Function Attrs: 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 @_ZSt3minIxERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8), ptr noundef nonnull align 8 dereferenceable(8)) #2

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #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 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 = { 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 = !{!"long long", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"any pointer", !7, i64 0}


; 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"

@n = external dso_local local_unnamed_addr global i64, align 8
@p = external dso_local local_unnamed_addr global [105 x i64], align 16
@mem = external dso_local local_unnamed_addr global [105 x [105 x [105 x [2 x i64]]]], align 16

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i64 @_Z2dpiiii(i32 noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %3) local_unnamed_addr #0 {
  %5 = alloca i64, align 8
  %6 = alloca i64, align 8
  %7 = sext i32 %0 to i64
  %8 = load i64, ptr @n, align 8, !tbaa !5
  %9 = icmp eq i64 %8, %7
  br i1 %9, label %common.ret, label %10

10:                                               ; preds = %4
  %11 = sext i32 %1 to i64
  %12 = sext i32 %2 to i64
  %13 = sext i32 %3 to i64
  %14 = getelementptr inbounds [105 x [105 x [105 x [2 x i64]]]], ptr @mem, i64 0, i64 %7, i64 %11, i64 %12, i64 %13
  %15 = load i64, ptr %14, align 8, !tbaa !5
  %.not = icmp eq i64 %15, -1
  br i1 %.not, label %16, label %common.ret

16:                                               ; preds = %10
  %17 = getelementptr inbounds [105 x i64], ptr @p, i64 0, i64 %7
  %18 = load i64, ptr %17, align 8, !tbaa !5
  %.not31 = icmp eq i64 %18, 0
  br i1 %.not31, label %27, label %19

common.ret:                                       ; preds = %4, %10, %45, %19
  %common.ret.op = phi i64 [ %26, %19 ], [ 0, %4 ], [ %15, %10 ], [ %47, %45 ]
  ret i64 %common.ret.op

19:                                               ; preds = %16
  %20 = srem i64 %18, 2
  %21 = icmp ne i64 %20, %13
  %22 = zext i1 %21 to i64
  %23 = add nsw i32 %0, 1
  %24 = trunc nsw i64 %20 to i32
  %25 = tail call noundef i64 @_Z2dpiiii(i32 noundef %23, i32 noundef %1, i32 noundef %2, i32 noundef %24)
  %26 = add nsw i64 %25, %22
  store i64 %26, ptr %14, align 8, !tbaa !5
  br label %common.ret

27:                                               ; preds = %16
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %5) #3
  store i64 1000000000000000000, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %6) #3
  store i64 1000000000000000000, ptr %6, align 8, !tbaa !5
  %28 = icmp sgt i32 %2, 0
  br i1 %28, label %29, label %36

29:                                               ; preds = %27
  %30 = icmp ne i32 %3, 0
  %31 = zext i1 %30 to i64
  %32 = add nsw i32 %0, 1
  %33 = add nsw i32 %2, -1
  %34 = tail call noundef i64 @_Z2dpiiii(i32 noundef %32, i32 noundef %1, i32 noundef %33, i32 noundef 0)
  %35 = add nsw i64 %34, %31
  store i64 %35, ptr %5, align 8, !tbaa !5
  br label %36

36:                                               ; preds = %29, %27
  %37 = icmp sgt i32 %1, 0
  br i1 %37, label %38, label %45

38:                                               ; preds = %36
  %39 = icmp ne i32 %3, 1
  %40 = zext i1 %39 to i64
  %41 = add nsw i32 %0, 1
  %42 = add nsw i32 %1, -1
  %43 = tail call noundef i64 @_Z2dpiiii(i32 noundef %41, i32 noundef %42, i32 noundef %2, i32 noundef 1)
  %44 = add nsw i64 %43, %40
  store i64 %44, ptr %6, align 8, !tbaa !5
  br label %45

45:                                               ; preds = %38, %36
  %46 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3minIxERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8) %5, ptr noundef nonnull align 8 dereferenceable(8) %6)
  %47 = load i64, ptr %46, align 8, !tbaa !5
  store i64 %47, ptr %14, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %6) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %5) #3
  br label %common.ret
}

; 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 @_ZSt3minIxERKT_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

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 = { 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"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass