; LLVM IR from Muhammad-Magdi/problem-solving :: Dynamic Programming/CoinChange.ll (Z5solveii)

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

@Coins = external dso_local global [5 x i32], align 16
@mem = external dso_local global [5 x [1048576 x i64]], align 16

; Function Attrs: mustprogress uwtable
define dso_local noundef i64 @_Z5solveii(i32 noundef %0, i32 noundef %1) #0 {
  %3 = alloca i64, align 8
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  store i32 %0, ptr %4, align 4, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !5
  %8 = load i32, ptr %4, align 4, !tbaa !5
  %9 = icmp eq i32 %8, 5
  br i1 %9, label %10, label %14

10:                                               ; preds = %2
  %11 = load i32, ptr %5, align 4, !tbaa !5
  %12 = icmp eq i32 %11, 0
  %13 = zext i1 %12 to i64
  store i64 %13, ptr %3, align 8
  br label %54

14:                                               ; preds = %2
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #2
  %15 = load i32, ptr %4, align 4, !tbaa !5
  %16 = sext i32 %15 to i64
  %17 = getelementptr inbounds [5 x [1048576 x i64]], ptr @mem, i64 0, i64 %16
  %18 = load i32, ptr %5, align 4, !tbaa !5
  %19 = sext i32 %18 to i64
  %20 = getelementptr inbounds [1048576 x i64], ptr %17, i64 0, i64 %19
  store ptr %20, ptr %6, align 8, !tbaa !9
  %21 = load ptr, ptr %6, align 8, !tbaa !9
  %22 = load i64, ptr %21, align 8, !tbaa !11
  %23 = xor i64 %22, -1
  %24 = icmp ne i64 %23, 0
  br i1 %24, label %25, label %28

25:                                               ; preds = %14
  %26 = load ptr, ptr %6, align 8, !tbaa !9
  %27 = load i64, ptr %26, align 8, !tbaa !11
  store i64 %27, ptr %3, align 8
  store i32 1, ptr %7, align 4
  br label %53

28:                                               ; preds = %14
  %29 = load i32, ptr %5, align 4, !tbaa !5
  %30 = load i32, ptr %4, align 4, !tbaa !5
  %31 = sext i32 %30 to i64
  %32 = getelementptr inbounds [5 x i32], ptr @Coins, i64 0, i64 %31
  %33 = load i32, ptr %32, align 4, !tbaa !5
  %34 = icmp sge i32 %29, %33
  br i1 %34, label %35, label %44

35:                                               ; preds = %28
  %36 = load i32, ptr %4, align 4, !tbaa !5
  %37 = load i32, ptr %5, align 4, !tbaa !5
  %38 = load i32, ptr %4, align 4, !tbaa !5
  %39 = sext i32 %38 to i64
  %40 = getelementptr inbounds [5 x i32], ptr @Coins, i64 0, i64 %39
  %41 = load i32, ptr %40, align 4, !tbaa !5
  %42 = sub nsw i32 %37, %41
  %43 = call noundef i64 @_Z5solveii(i32 noundef %36, i32 noundef %42)
  br label %45

44:                                               ; preds = %28
  br label %45

45:                                               ; preds = %44, %35
  %46 = phi i64 [ %43, %35 ], [ 0, %44 ]
  %47 = load i32, ptr %4, align 4, !tbaa !5
  %48 = add nsw i32 %47, 1
  %49 = load i32, ptr %5, align 4, !tbaa !5
  %50 = call noundef i64 @_Z5solveii(i32 noundef %48, i32 noundef %49)
  %51 = add nsw i64 %46, %50
  %52 = load ptr, ptr %6, align 8, !tbaa !9
  store i64 %51, ptr %52, align 8, !tbaa !11
  store i64 %51, ptr %3, align 8
  store i32 1, ptr %7, align 4
  br label %53

53:                                               ; preds = %45, %25
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #2
  br label %54

54:                                               ; preds = %53, %10
  %55 = load i64, ptr %3, align 8
  ret i64 %55
}

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

; 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 = { 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 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"long long", !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"

@Coins = external dso_local local_unnamed_addr global [5 x i32], align 16
@mem = external dso_local local_unnamed_addr global [5 x [1048576 x i64]], align 16

; Function Attrs: mustprogress nofree nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) uwtable
define dso_local noundef i64 @_Z5solveii(i32 noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = icmp eq i32 %0, 5
  br i1 %3, label %4, label %7

4:                                                ; preds = %2
  %5 = icmp eq i32 %1, 0
  %6 = zext i1 %5 to i64
  br label %common.ret18

7:                                                ; preds = %2
  %8 = sext i32 %0 to i64
  %9 = sext i32 %1 to i64
  %10 = getelementptr inbounds [5 x [1048576 x i64]], ptr @mem, i64 0, i64 %8, i64 %9
  %11 = load i64, ptr %10, align 8, !tbaa !5
  %.not = icmp eq i64 %11, -1
  br i1 %.not, label %12, label %common.ret18

12:                                               ; preds = %7
  %13 = getelementptr inbounds [5 x i32], ptr @Coins, i64 0, i64 %8
  %14 = load i32, ptr %13, align 4, !tbaa !9
  %.not17 = icmp sgt i32 %14, %1
  br i1 %.not17, label %18, label %15

15:                                               ; preds = %12
  %16 = sub nsw i32 %1, %14
  %17 = tail call noundef i64 @_Z5solveii(i32 noundef %0, i32 noundef %16)
  br label %18

common.ret18:                                     ; preds = %7, %4, %18
  %common.ret18.op = phi i64 [ %22, %18 ], [ %6, %4 ], [ %11, %7 ]
  ret i64 %common.ret18.op

18:                                               ; preds = %12, %15
  %19 = phi i64 [ %17, %15 ], [ 0, %12 ]
  %20 = add nsw i32 %0, 1
  %21 = tail call noundef i64 @_Z5solveii(i32 noundef %20, i32 noundef %1)
  %22 = add nsw i64 %21, %19
  store i64 %22, ptr %10, align 8, !tbaa !5
  br label %common.ret18
}

attributes #0 = { mustprogress nofree nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) 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" }

!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 = !{!"int", !7, i64 0}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass