; LLVM IR from AlgoGenesis/C :: Dynamic Programming/Coin Change Problem (Program)

; 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: nounwind uwtable
define dso_local i32 @coinChange(ptr noundef %0, i32 noundef %1, i32 noundef %2) #0 {
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  %8 = alloca i64, align 8
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !9
  store i32 %2, ptr %6, align 4, !tbaa !9
  %13 = load i32, ptr %6, align 4, !tbaa !9
  %14 = add nsw i32 %13, 1
  %15 = zext i32 %14 to i64
  %16 = call ptr @llvm.stacksave.p0()
  store ptr %16, ptr %7, align 8
  %17 = alloca i32, i64 %15, align 16
  store i64 %15, ptr %8, align 8
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #3
  store i32 0, ptr %9, align 4, !tbaa !9
  br label %18

18:                                               ; preds = %27, %3
  %19 = load i32, ptr %9, align 4, !tbaa !9
  %20 = load i32, ptr %6, align 4, !tbaa !9
  %21 = icmp sle i32 %19, %20
  br i1 %21, label %23, label %22

22:                                               ; preds = %18
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  br label %30

23:                                               ; preds = %18
  %24 = load i32, ptr %9, align 4, !tbaa !9
  %25 = sext i32 %24 to i64
  %26 = getelementptr inbounds i32, ptr %17, i64 %25
  store i32 0, ptr %26, align 4, !tbaa !9
  br label %27

27:                                               ; preds = %23
  %28 = load i32, ptr %9, align 4, !tbaa !9
  %29 = add nsw i32 %28, 1
  store i32 %29, ptr %9, align 4, !tbaa !9
  br label %18, !llvm.loop !11

30:                                               ; preds = %22
  %31 = getelementptr inbounds i32, ptr %17, i64 0
  store i32 1, ptr %31, align 16, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #3
  store i32 0, ptr %10, align 4, !tbaa !9
  br label %32

32:                                               ; preds = %68, %30
  %33 = load i32, ptr %10, align 4, !tbaa !9
  %34 = load i32, ptr %5, align 4, !tbaa !9
  %35 = icmp slt i32 %33, %34
  br i1 %35, label %37, label %36

36:                                               ; preds = %32
  store i32 5, ptr %11, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #3
  br label %71

37:                                               ; preds = %32
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #3
  %38 = load ptr, ptr %4, align 8, !tbaa !5
  %39 = load i32, ptr %10, align 4, !tbaa !9
  %40 = sext i32 %39 to i64
  %41 = getelementptr inbounds i32, ptr %38, i64 %40
  %42 = load i32, ptr %41, align 4, !tbaa !9
  store i32 %42, ptr %12, align 4, !tbaa !9
  br label %43

43:                                               ; preds = %64, %37
  %44 = load i32, ptr %12, align 4, !tbaa !9
  %45 = load i32, ptr %6, align 4, !tbaa !9
  %46 = icmp sle i32 %44, %45
  br i1 %46, label %48, label %47

47:                                               ; preds = %43
  store i32 8, ptr %11, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #3
  br label %67

48:                                               ; preds = %43
  %49 = load i32, ptr %12, align 4, !tbaa !9
  %50 = load ptr, ptr %4, align 8, !tbaa !5
  %51 = load i32, ptr %10, align 4, !tbaa !9
  %52 = sext i32 %51 to i64
  %53 = getelementptr inbounds i32, ptr %50, i64 %52
  %54 = load i32, ptr %53, align 4, !tbaa !9
  %55 = sub nsw i32 %49, %54
  %56 = sext i32 %55 to i64
  %57 = getelementptr inbounds i32, ptr %17, i64 %56
  %58 = load i32, ptr %57, align 4, !tbaa !9
  %59 = load i32, ptr %12, align 4, !tbaa !9
  %60 = sext i32 %59 to i64
  %61 = getelementptr inbounds i32, ptr %17, i64 %60
  %62 = load i32, ptr %61, align 4, !tbaa !9
  %63 = add nsw i32 %62, %58
  store i32 %63, ptr %61, align 4, !tbaa !9
  br label %64

64:                                               ; preds = %48
  %65 = load i32, ptr %12, align 4, !tbaa !9
  %66 = add nsw i32 %65, 1
  store i32 %66, ptr %12, align 4, !tbaa !9
  br label %43, !llvm.loop !13

67:                                               ; preds = %47
  br label %68

68:                                               ; preds = %67
  %69 = load i32, ptr %10, align 4, !tbaa !9
  %70 = add nsw i32 %69, 1
  store i32 %70, ptr %10, align 4, !tbaa !9
  br label %32, !llvm.loop !14

71:                                               ; preds = %36
  %72 = load i32, ptr %6, align 4, !tbaa !9
  %73 = sext i32 %72 to i64
  %74 = getelementptr inbounds i32, ptr %17, i64 %73
  %75 = load i32, ptr %74, align 4, !tbaa !9
  store i32 1, ptr %11, align 4
  %76 = load ptr, ptr %7, align 8
  call void @llvm.stackrestore.p0(ptr %76)
  ret i32 %75
}

; Function Attrs: nocallback nofree nosync nounwind willreturn
declare ptr @llvm.stacksave.p0() #1

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

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

; Function Attrs: nocallback nofree nosync nounwind willreturn
declare void @llvm.stackrestore.p0(ptr) #1

attributes #0 = { 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 }
attributes #2 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12}
!14 = distinct !{!14, !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: nofree norecurse nosync nounwind memory(argmem: read) uwtable
define dso_local i32 @coinChange(ptr nocapture noundef readonly %0, i32 noundef %1, i32 noundef %2) local_unnamed_addr #0 {
  %4 = add i32 %2, 1
  %5 = zext i32 %4 to i64
  %6 = alloca i32, i64 %5, align 16
  %.not22 = icmp slt i32 %2, 0
  br i1 %.not22, label %._crit_edge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %3
  %7 = shl nuw nsw i64 %5, 2
  call void @llvm.memset.p0.i64(ptr nonnull align 16 %6, i8 0, i64 %7, i1 false), !tbaa !5
  br label %._crit_edge

._crit_edge:                                      ; preds = %.lr.ph.preheader, %3
  store i32 1, ptr %6, align 16, !tbaa !5
  %8 = icmp sgt i32 %1, 0
  br i1 %8, label %.lr.ph31.preheader, label %._crit_edge32

.lr.ph31.preheader:                               ; preds = %._crit_edge
  %wide.trip.count = zext nneg i32 %1 to i64
  %scevgep39 = getelementptr i8, ptr %6, i64 4
  %scevgep41 = getelementptr i8, ptr %6, i64 4
  br label %.lr.ph31

._crit_edge32:                                    ; preds = %._crit_edge28, %._crit_edge
  %9 = sext i32 %2 to i64
  %10 = getelementptr inbounds i32, ptr %6, i64 %9
  %11 = load i32, ptr %10, align 4, !tbaa !5
  ret i32 %11

.lr.ph31:                                         ; preds = %.lr.ph31.preheader, %._crit_edge28
  %indvars.iv35 = phi i64 [ 0, %.lr.ph31.preheader ], [ %indvars.iv.next36, %._crit_edge28 ]
  %12 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv35
  %13 = load i32, ptr %12, align 4, !tbaa !5
  %.not2124 = icmp sgt i32 %13, %2
  br i1 %.not2124, label %._crit_edge28, label %.lr.ph27.preheader

.lr.ph27.preheader:                               ; preds = %.lr.ph31
  %14 = sext i32 %13 to i64
  %15 = sub i32 %2, %13
  %16 = zext i32 %15 to i64
  %17 = add nuw nsw i64 %16, 1
  %min.iters.check = icmp ult i32 %15, 7
  br i1 %min.iters.check, label %.lr.ph27.preheader46, label %vector.memcheck

.lr.ph27.preheader46:                             ; preds = %middle.block, %vector.memcheck, %.lr.ph27.preheader
  %indvars.iv.ph = phi i64 [ %14, %vector.memcheck ], [ %14, %.lr.ph27.preheader ], [ %ind.end, %middle.block ]
  %18 = trunc i64 %indvars.iv.ph to i32
  %19 = add i32 %18, %2
  %20 = and i32 %19, 1
  %lcmp.mod.not.not = icmp eq i32 %20, 0
  br i1 %lcmp.mod.not.not, label %.lr.ph27.prol, label %.lr.ph27.prol.loopexit

.lr.ph27.prol:                                    ; preds = %.lr.ph27.preheader46
  %21 = sub nsw i64 %indvars.iv.ph, %14
  %22 = getelementptr inbounds i32, ptr %6, i64 %21
  %23 = load i32, ptr %22, align 4, !tbaa !5
  %24 = getelementptr inbounds i32, ptr %6, i64 %indvars.iv.ph
  %25 = load i32, ptr %24, align 4, !tbaa !5
  %26 = add nsw i32 %25, %23
  store i32 %26, ptr %24, align 4, !tbaa !5
  %indvars.iv.next.prol = add nsw i64 %indvars.iv.ph, 1
  br label %.lr.ph27.prol.loopexit

.lr.ph27.prol.loopexit:                           ; preds = %.lr.ph27.prol, %.lr.ph27.preheader46
  %indvars.iv.unr = phi i64 [ %indvars.iv.ph, %.lr.ph27.preheader46 ], [ %indvars.iv.next.prol, %.lr.ph27.prol ]
  %27 = icmp eq i32 %18, %2
  br i1 %27, label %._crit_edge28, label %.lr.ph27

vector.memcheck:                                  ; preds = %.lr.ph27.preheader
  %28 = shl nsw i64 %14, 2
  %scevgep = getelementptr i8, ptr %6, i64 %28
  %29 = sub i32 %2, %13
  %30 = zext i32 %29 to i64
  %31 = shl nuw nsw i64 %30, 2
  %32 = getelementptr i8, ptr %scevgep39, i64 %28
  %scevgep40 = getelementptr i8, ptr %32, i64 %31
  %scevgep42 = getelementptr i8, ptr %scevgep41, i64 %31
  %bound0 = icmp ult ptr %scevgep, %scevgep42
  %bound1 = icmp ult ptr %6, %scevgep40
  %found.conflict = and i1 %bound0, %bound1
  br i1 %found.conflict, label %.lr.ph27.preheader46, label %vector.ph

vector.ph:                                        ; preds = %vector.memcheck
  %n.vec = and i64 %17, 8589934584
  %ind.end = add nsw i64 %n.vec, %14
  %invariant.gep = getelementptr i32, ptr %6, i64 %14
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %33 = getelementptr inbounds i32, ptr %6, i64 %index
  %34 = getelementptr inbounds i8, ptr %33, i64 16
  %wide.load = load <4 x i32>, ptr %33, align 16, !tbaa !5, !alias.scope !9
  %wide.load43 = load <4 x i32>, ptr %34, align 16, !tbaa !5, !alias.scope !9
  %gep = getelementptr i32, ptr %invariant.gep, i64 %index
  %35 = getelementptr inbounds i8, ptr %gep, i64 16
  %wide.load44 = load <4 x i32>, ptr %gep, align 4, !tbaa !5, !alias.scope !12, !noalias !9
  %wide.load45 = load <4 x i32>, ptr %35, align 4, !tbaa !5, !alias.scope !12, !noalias !9
  %36 = add nsw <4 x i32> %wide.load44, %wide.load
  %37 = add nsw <4 x i32> %wide.load45, %wide.load43
  store <4 x i32> %36, ptr %gep, align 4, !tbaa !5, !alias.scope !12, !noalias !9
  store <4 x i32> %37, ptr %35, align 4, !tbaa !5, !alias.scope !12, !noalias !9
  %index.next = add nuw i64 %index, 8
  %38 = icmp eq i64 %index.next, %n.vec
  br i1 %38, label %middle.block, label %vector.body, !llvm.loop !14

middle.block:                                     ; preds = %vector.body
  %cmp.n = icmp eq i64 %17, %n.vec
  br i1 %cmp.n, label %._crit_edge28, label %.lr.ph27.preheader46

._crit_edge28:                                    ; preds = %.lr.ph27.prol.loopexit, %.lr.ph27, %middle.block, %.lr.ph31
  %indvars.iv.next36 = add nuw nsw i64 %indvars.iv35, 1
  %exitcond38.not = icmp eq i64 %indvars.iv.next36, %wide.trip.count
  br i1 %exitcond38.not, label %._crit_edge32, label %.lr.ph31, !llvm.loop !18

.lr.ph27:                                         ; preds = %.lr.ph27.prol.loopexit, %.lr.ph27
  %indvars.iv = phi i64 [ %indvars.iv.next.1, %.lr.ph27 ], [ %indvars.iv.unr, %.lr.ph27.prol.loopexit ]
  %39 = sub nsw i64 %indvars.iv, %14
  %40 = getelementptr inbounds i32, ptr %6, i64 %39
  %41 = load i32, ptr %40, align 4, !tbaa !5
  %42 = getelementptr inbounds i32, ptr %6, i64 %indvars.iv
  %43 = load i32, ptr %42, align 4, !tbaa !5
  %44 = add nsw i32 %43, %41
  store i32 %44, ptr %42, align 4, !tbaa !5
  %indvars.iv.next = add nsw i64 %indvars.iv, 1
  %45 = sub nsw i64 %indvars.iv.next, %14
  %46 = getelementptr inbounds i32, ptr %6, i64 %45
  %47 = load i32, ptr %46, align 4, !tbaa !5
  %48 = getelementptr inbounds i32, ptr %6, i64 %indvars.iv.next
  %49 = load i32, ptr %48, align 4, !tbaa !5
  %50 = add nsw i32 %49, %47
  store i32 %50, ptr %48, align 4, !tbaa !5
  %indvars.iv.next.1 = add nsw i64 %indvars.iv, 2
  %lftr.wideiv.1 = trunc i64 %indvars.iv.next.1 to i32
  %exitcond.not.1 = icmp eq i32 %4, %lftr.wideiv.1
  br i1 %exitcond.not.1, label %._crit_edge28, label %.lr.ph27, !llvm.loop !19
}

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #1

attributes #0 = { nofree norecurse nosync nounwind 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" }
attributes #1 = { nocallback nofree nounwind willreturn memory(argmem: write) }

!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/C++ TBAA"}
!9 = !{!10}
!10 = distinct !{!10, !11}
!11 = distinct !{!11, !"LVerDomain"}
!12 = !{!13}
!13 = distinct !{!13, !11}
!14 = distinct !{!14, !15, !16, !17}
!15 = !{!"llvm.loop.mustprogress"}
!16 = !{!"llvm.loop.isvectorized", i32 1}
!17 = !{!"llvm.loop.unroll.runtime.disable"}
!18 = distinct !{!18, !15}
!19 = distinct !{!19, !15, !16}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopIdiomRecognizePass,LoopLoadEliminationPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,LoopVectorizePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass