; LLVM IR from kenjin/Awesome-LC-C-Cracker :: leetcode-c-sol/0441_arranging_coins (0441_arranging_coins)

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

9:                                                ; preds = %1
  %10 = load i32, ptr %3, align 4, !tbaa !5
  store i32 %10, ptr %2, align 4
  br label %43

11:                                               ; preds = %1
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #2
  store i64 1, ptr %4, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #2
  %12 = load i32, ptr %3, align 4, !tbaa !5
  %13 = sext i32 %12 to i64
  store i64 %13, ptr %5, align 8, !tbaa !9
  br label %14

14:                                               ; preds = %38, %11
  %15 = load i64, ptr %4, align 8, !tbaa !9
  %16 = load i64, ptr %5, align 8, !tbaa !9
  %17 = icmp slt i64 %15, %16
  br i1 %17, label %18, label %39

18:                                               ; preds = %14
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #2
  %19 = load i64, ptr %4, align 8, !tbaa !9
  %20 = load i64, ptr %5, align 8, !tbaa !9
  %21 = load i64, ptr %4, align 8, !tbaa !9
  %22 = sub nsw i64 %20, %21
  %23 = ashr i64 %22, 1
  %24 = add nsw i64 %19, %23
  store i64 %24, ptr %6, align 8, !tbaa !9
  %25 = load i64, ptr %6, align 8, !tbaa !9
  %26 = load i64, ptr %6, align 8, !tbaa !9
  %27 = add nsw i64 %26, 1
  %28 = mul nsw i64 %25, %27
  %29 = ashr i64 %28, 1
  %30 = load i32, ptr %3, align 4, !tbaa !5
  %31 = sext i32 %30 to i64
  %32 = icmp sle i64 %29, %31
  br i1 %32, label %33, label %36

33:                                               ; preds = %18
  %34 = load i64, ptr %6, align 8, !tbaa !9
  %35 = add nsw i64 %34, 1
  store i64 %35, ptr %4, align 8, !tbaa !9
  br label %38

36:                                               ; preds = %18
  %37 = load i64, ptr %6, align 8, !tbaa !9
  store i64 %37, ptr %5, align 8, !tbaa !9
  br label %38

38:                                               ; preds = %36, %33
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #2
  br label %14, !llvm.loop !11

39:                                               ; preds = %14
  %40 = load i64, ptr %4, align 8, !tbaa !9
  %41 = sub nsw i64 %40, 1
  %42 = trunc i64 %41 to i32
  store i32 %42, ptr %2, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #2
  br label %43

43:                                               ; preds = %39, %9
  %44 = load i32, ptr %2, align 4
  ret i32 %44
}

; 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 = { 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 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/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"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"

; Function Attrs: nofree norecurse nosync nounwind memory(none) uwtable
define dso_local i32 @arrangeCoins(i32 noundef %0) local_unnamed_addr #0 {
  %2 = icmp slt i32 %0, 2
  br i1 %2, label %16, label %3

3:                                                ; preds = %1
  %4 = zext nneg i32 %0 to i64
  br label %5

5:                                                ; preds = %3, %5
  %.01521 = phi i64 [ %4, %3 ], [ %..015, %5 ]
  %.01620 = phi i64 [ 1, %3 ], [ %.016., %5 ]
  %6 = sub nsw i64 %.01521, %.01620
  %7 = ashr i64 %6, 1
  %8 = add nsw i64 %7, %.01620
  %9 = add nsw i64 %8, 1
  %10 = mul nsw i64 %9, %8
  %11 = ashr i64 %10, 1
  %.not = icmp sgt i64 %11, %4
  %.016. = select i1 %.not, i64 %.01620, i64 %9
  %..015 = select i1 %.not, i64 %8, i64 %.01521
  %12 = icmp slt i64 %.016., %..015
  br i1 %12, label %5, label %13, !llvm.loop !5

13:                                               ; preds = %5
  %14 = trunc i64 %.016. to i32
  %15 = add i32 %14, -1
  br label %16

16:                                               ; preds = %1, %13
  %.0 = phi i32 [ %15, %13 ], [ %0, %1 ]
  ret i32 %.0
}

attributes #0 = { nofree norecurse nosync nounwind memory(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 = distinct !{!5, !6}
!6 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,LCSSAPass,LoopRotatePass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass