; LLVM IR from abeaumont/competitive-programming :: hackerrank/algorithms/coin-change.ll (Z1fii)

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

@a = external dso_local global [51 x i32], align 16
@m = external dso_local global i32, align 4
@c = external dso_local global [251 x [51 x i64]], align 16

; Function Attrs: mustprogress uwtable
define dso_local noundef i64 @_Z1fii(i32 noundef %0, i32 noundef %1) #0 {
  %3 = alloca i64, align 8
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i64, 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, 0
  br i1 %9, label %10, label %11

10:                                               ; preds = %2
  store i64 1, ptr %3, align 8
  br label %66

11:                                               ; preds = %2
  %12 = load i32, ptr %4, align 4, !tbaa !5
  %13 = sext i32 %12 to i64
  %14 = getelementptr inbounds [251 x [51 x i64]], ptr @c, i64 0, i64 %13
  %15 = load i32, ptr %5, align 4, !tbaa !5
  %16 = sext i32 %15 to i64
  %17 = getelementptr inbounds [51 x i64], ptr %14, i64 0, i64 %16
  %18 = load i64, ptr %17, align 8, !tbaa !9
  %19 = icmp ne i64 %18, -1
  br i1 %19, label %20, label %28

20:                                               ; preds = %11
  %21 = load i32, ptr %4, align 4, !tbaa !5
  %22 = sext i32 %21 to i64
  %23 = getelementptr inbounds [251 x [51 x i64]], ptr @c, i64 0, i64 %22
  %24 = load i32, ptr %5, align 4, !tbaa !5
  %25 = sext i32 %24 to i64
  %26 = getelementptr inbounds [51 x i64], ptr %23, i64 0, i64 %25
  %27 = load i64, ptr %26, align 8, !tbaa !9
  store i64 %27, ptr %3, align 8
  br label %66

28:                                               ; preds = %11
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #2
  store i64 0, ptr %6, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  %29 = load i32, ptr %5, align 4, !tbaa !5
  store i32 %29, ptr %7, align 4, !tbaa !5
  br label %30

30:                                               ; preds = %54, %28
  %31 = load i32, ptr %7, align 4, !tbaa !5
  %32 = load i32, ptr @m, align 4, !tbaa !5
  %33 = icmp slt i32 %31, %32
  br i1 %33, label %35, label %34

34:                                               ; preds = %30
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  br label %57

35:                                               ; preds = %30
  %36 = load i32, ptr %7, align 4, !tbaa !5
  %37 = sext i32 %36 to i64
  %38 = getelementptr inbounds [51 x i32], ptr @a, i64 0, i64 %37
  %39 = load i32, ptr %38, align 4, !tbaa !5
  %40 = load i32, ptr %4, align 4, !tbaa !5
  %41 = icmp sle i32 %39, %40
  br i1 %41, label %42, label %53

42:                                               ; preds = %35
  %43 = load i32, ptr %4, align 4, !tbaa !5
  %44 = load i32, ptr %7, align 4, !tbaa !5
  %45 = sext i32 %44 to i64
  %46 = getelementptr inbounds [51 x i32], ptr @a, i64 0, i64 %45
  %47 = load i32, ptr %46, align 4, !tbaa !5
  %48 = sub nsw i32 %43, %47
  %49 = load i32, ptr %7, align 4, !tbaa !5
  %50 = call noundef i64 @_Z1fii(i32 noundef %48, i32 noundef %49)
  %51 = load i64, ptr %6, align 8, !tbaa !9
  %52 = add nsw i64 %51, %50
  store i64 %52, ptr %6, align 8, !tbaa !9
  br label %53

53:                                               ; preds = %42, %35
  br label %54

54:                                               ; preds = %53
  %55 = load i32, ptr %7, align 4, !tbaa !5
  %56 = add nsw i32 %55, 1
  store i32 %56, ptr %7, align 4, !tbaa !5
  br label %30, !llvm.loop !11

57:                                               ; preds = %34
  %58 = load i64, ptr %6, align 8, !tbaa !9
  %59 = load i32, ptr %4, align 4, !tbaa !5
  %60 = sext i32 %59 to i64
  %61 = getelementptr inbounds [251 x [51 x i64]], ptr @c, i64 0, i64 %60
  %62 = load i32, ptr %5, align 4, !tbaa !5
  %63 = sext i32 %62 to i64
  %64 = getelementptr inbounds [51 x i64], ptr %61, i64 0, i64 %63
  store i64 %58, ptr %64, align 8, !tbaa !9
  %65 = load i64, ptr %6, align 8, !tbaa !9
  store i64 %65, ptr %3, align 8
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #2
  br label %66

66:                                               ; preds = %57, %20, %10
  %67 = load i64, ptr %3, align 8
  ret i64 %67
}

; 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 = !{!"long long", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}


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

@a = external dso_local local_unnamed_addr global [51 x i32], align 16
@m = external dso_local local_unnamed_addr global i32, align 4
@c = external dso_local local_unnamed_addr global [251 x [51 x i64]], align 16

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

4:                                                ; preds = %2
  %5 = sext i32 %0 to i64
  %6 = sext i32 %1 to i64
  %7 = getelementptr inbounds [251 x [51 x i64]], ptr @c, i64 0, i64 %5, i64 %6
  %8 = load i64, ptr %7, align 8, !tbaa !5
  %.not = icmp eq i64 %8, -1
  br i1 %.not, label %.preheader, label %23

.preheader:                                       ; preds = %4
  %9 = load i32, ptr @m, align 4, !tbaa !9
  %10 = icmp sgt i32 %9, %1
  br i1 %10, label %.lr.ph, label %._crit_edge

._crit_edge:                                      ; preds = %19, %.preheader
  %.019.lcssa = phi i64 [ 0, %.preheader ], [ %.1, %19 ]
  store i64 %.019.lcssa, ptr %7, align 8, !tbaa !5
  br label %23

.lr.ph:                                           ; preds = %.preheader, %19
  %11 = phi i32 [ %20, %19 ], [ %9, %.preheader ]
  %indvars.iv = phi i64 [ %indvars.iv.next, %19 ], [ %6, %.preheader ]
  %.01925 = phi i64 [ %.1, %19 ], [ 0, %.preheader ]
  %12 = getelementptr inbounds [51 x i32], ptr @a, i64 0, i64 %indvars.iv
  %13 = load i32, ptr %12, align 4, !tbaa !9
  %.not24 = icmp sgt i32 %13, %0
  br i1 %.not24, label %19, label %14

14:                                               ; preds = %.lr.ph
  %15 = sub nsw i32 %0, %13
  %16 = trunc nsw i64 %indvars.iv to i32
  %17 = tail call noundef i64 @_Z1fii(i32 noundef %15, i32 noundef %16)
  %18 = add nsw i64 %17, %.01925
  %.pre = load i32, ptr @m, align 4, !tbaa !9
  br label %19

19:                                               ; preds = %.lr.ph, %14
  %20 = phi i32 [ %.pre, %14 ], [ %11, %.lr.ph ]
  %.1 = phi i64 [ %18, %14 ], [ %.01925, %.lr.ph ]
  %indvars.iv.next = add nsw i64 %indvars.iv, 1
  %21 = sext i32 %20 to i64
  %22 = icmp slt i64 %indvars.iv.next, %21
  br i1 %22, label %.lr.ph, label %._crit_edge, !llvm.loop !11

23:                                               ; preds = %4, %2, %._crit_edge
  %.020 = phi i64 [ %.019.lcssa, %._crit_edge ], [ 1, %2 ], [ %8, %4 ]
  ret i64 %.020
}

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}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}


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