; LLVM IR from bhrigukansra/algorithms-in-C-Cplusplus-Java-Python-JavaScript :: Algorithms/Dynamic Programming/C++/coin_change.ll (Z5countPiii)

; 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 i32 @_Z5countPiii(ptr noundef %0, i32 noundef %1, i32 noundef %2) #0 {
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store i32 %1, ptr %6, align 4, !tbaa !9
  store i32 %2, ptr %7, align 4, !tbaa !9
  %8 = load i32, ptr %7, align 4, !tbaa !9
  %9 = icmp eq i32 %8, 0
  br i1 %9, label %10, label %11

10:                                               ; preds = %3
  store i32 1, ptr %4, align 4
  br label %40

11:                                               ; preds = %3
  %12 = load i32, ptr %7, align 4, !tbaa !9
  %13 = icmp slt i32 %12, 0
  br i1 %13, label %14, label %15

14:                                               ; preds = %11
  store i32 0, ptr %4, align 4
  br label %40

15:                                               ; preds = %11
  %16 = load i32, ptr %6, align 4, !tbaa !9
  %17 = icmp sle i32 %16, 0
  br i1 %17, label %18, label %22

18:                                               ; preds = %15
  %19 = load i32, ptr %7, align 4, !tbaa !9
  %20 = icmp sge i32 %19, 1
  br i1 %20, label %21, label %22

21:                                               ; preds = %18
  store i32 0, ptr %4, align 4
  br label %40

22:                                               ; preds = %18, %15
  %23 = load ptr, ptr %5, align 8, !tbaa !5
  %24 = load i32, ptr %6, align 4, !tbaa !9
  %25 = sub nsw i32 %24, 1
  %26 = load i32, ptr %7, align 4, !tbaa !9
  %27 = call noundef i32 @_Z5countPiii(ptr noundef %23, i32 noundef %25, i32 noundef %26)
  %28 = load ptr, ptr %5, align 8, !tbaa !5
  %29 = load i32, ptr %6, align 4, !tbaa !9
  %30 = load i32, ptr %7, align 4, !tbaa !9
  %31 = load ptr, ptr %5, align 8, !tbaa !5
  %32 = load i32, ptr %6, align 4, !tbaa !9
  %33 = sub nsw i32 %32, 1
  %34 = sext i32 %33 to i64
  %35 = getelementptr inbounds i32, ptr %31, i64 %34
  %36 = load i32, ptr %35, align 4, !tbaa !9
  %37 = sub nsw i32 %30, %36
  %38 = call noundef i32 @_Z5countPiii(ptr noundef %28, i32 noundef %29, i32 noundef %37)
  %39 = add nsw i32 %27, %38
  store i32 %39, ptr %4, align 4
  br label %40

40:                                               ; preds = %22, %21, %14, %10
  %41 = load i32, ptr %4, align 4
  ret i32 %41
}

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

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

; Function Attrs: mustprogress nofree nosync nounwind willreturn memory(argmem: read) uwtable
define dso_local noundef i32 @_Z5countPiii(ptr nocapture noundef readonly %0, i32 noundef %1, i32 noundef %2) local_unnamed_addr #0 {
  %4 = icmp eq i32 %2, 0
  br i1 %4, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %3
  %5 = icmp slt i32 %1, 1
  %6 = add nsw i32 %1, -1
  %7 = zext nneg i32 %6 to i64
  %8 = getelementptr inbounds i32, ptr %0, i64 %7
  br i1 %5, label %._crit_edge, label %.lr.ph.split

.lr.ph.split:                                     ; preds = %.lr.ph, %tailrecurse
  %.tr1517 = phi i32 [ %12, %tailrecurse ], [ %2, %.lr.ph ]
  %accumulator.tr16 = phi i32 [ %13, %tailrecurse ], [ 0, %.lr.ph ]
  %9 = icmp slt i32 %.tr1517, 0
  br i1 %9, label %._crit_edge, label %tailrecurse

tailrecurse:                                      ; preds = %.lr.ph.split
  %10 = tail call noundef i32 @_Z5countPiii(ptr noundef %0, i32 noundef %6, i32 noundef %.tr1517)
  %11 = load i32, ptr %8, align 4, !tbaa !5
  %12 = sub nsw i32 %.tr1517, %11
  %13 = add nsw i32 %10, %accumulator.tr16
  %14 = icmp eq i32 %12, 0
  br i1 %14, label %._crit_edge, label %.lr.ph.split

._crit_edge:                                      ; preds = %tailrecurse, %.lr.ph.split, %.lr.ph, %3
  %accumulator.tr.lcssa = phi i32 [ 0, %3 ], [ 0, %.lr.ph ], [ %accumulator.tr16, %.lr.ph.split ], [ %13, %tailrecurse ]
  %.0 = phi i32 [ 1, %3 ], [ 0, %.lr.ph ], [ 0, %.lr.ph.split ], [ 1, %tailrecurse ]
  %accumulator.ret.tr = add nsw i32 %.0, %accumulator.tr.lcssa
  ret i32 %accumulator.ret.tr
}

attributes #0 = { mustprogress nofree nosync nounwind willreturn memory(argmem: read) 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GlobalOptPass,IPSCCPPass,InstCombinePass,JumpThreadingPass,LCSSAPass,LICMPass,LoopInstSimplifyPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass