; LLVM IR from upupming/algorithm :: leetcode/5557.ll (Z8nChoosekxx)

; 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: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #0

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

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

11:                                               ; preds = %2
  store i64 0, ptr %3, align 8
  br label %50

12:                                               ; preds = %2
  %13 = load i64, ptr %5, align 8, !tbaa !5
  %14 = mul nsw i64 %13, 2
  %15 = load i64, ptr %4, align 8, !tbaa !5
  %16 = icmp sgt i64 %14, %15
  br i1 %16, label %17, label %21

17:                                               ; preds = %12
  %18 = load i64, ptr %4, align 8, !tbaa !5
  %19 = load i64, ptr %5, align 8, !tbaa !5
  %20 = sub nsw i64 %18, %19
  store i64 %20, ptr %5, align 8, !tbaa !5
  br label %21

21:                                               ; preds = %17, %12
  %22 = load i64, ptr %5, align 8, !tbaa !5
  %23 = icmp eq i64 %22, 0
  br i1 %23, label %24, label %25

24:                                               ; preds = %21
  store i64 1, ptr %3, align 8
  br label %50

25:                                               ; preds = %21
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #2
  %26 = load i64, ptr %4, align 8, !tbaa !5
  store i64 %26, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  store i32 2, ptr %7, align 4, !tbaa !9
  br label %27

27:                                               ; preds = %45, %25
  %28 = load i32, ptr %7, align 4, !tbaa !9
  %29 = sext i32 %28 to i64
  %30 = load i64, ptr %5, align 8, !tbaa !5
  %31 = icmp sle i64 %29, %30
  br i1 %31, label %33, label %32

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

33:                                               ; preds = %27
  %34 = load i64, ptr %4, align 8, !tbaa !5
  %35 = load i32, ptr %7, align 4, !tbaa !9
  %36 = sext i32 %35 to i64
  %37 = sub nsw i64 %34, %36
  %38 = add nsw i64 %37, 1
  %39 = load i64, ptr %6, align 8, !tbaa !5
  %40 = mul nsw i64 %39, %38
  store i64 %40, ptr %6, align 8, !tbaa !5
  %41 = load i32, ptr %7, align 4, !tbaa !9
  %42 = sext i32 %41 to i64
  %43 = load i64, ptr %6, align 8, !tbaa !5
  %44 = sdiv i64 %43, %42
  store i64 %44, ptr %6, align 8, !tbaa !5
  br label %45

45:                                               ; preds = %33
  %46 = load i32, ptr %7, align 4, !tbaa !9
  %47 = add nsw i32 %46, 1
  store i32 %47, ptr %7, align 4, !tbaa !9
  br label %27, !llvm.loop !11

48:                                               ; preds = %32
  %49 = load i64, ptr %6, align 8, !tbaa !5
  store i64 %49, ptr %3, align 8
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #2
  br label %50

50:                                               ; preds = %48, %24, %11
  %51 = load i64, ptr %3, align 8
  ret i64 %51
}

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


; 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 norecurse nosync nounwind willreturn memory(none) uwtable
define dso_local noundef i64 @_Z8nChoosekxx(i64 noundef %0, i64 noundef %1) local_unnamed_addr #0 {
  %3 = icmp sgt i64 %1, %0
  br i1 %3, label %.loopexit, label %4

4:                                                ; preds = %2
  %5 = shl nsw i64 %1, 1
  %6 = icmp sgt i64 %5, %0
  %7 = sub nsw i64 %0, %1
  %spec.select = select i1 %6, i64 %7, i64 %1
  %8 = icmp eq i64 %spec.select, 0
  br i1 %8, label %.loopexit, label %.preheader

.preheader:                                       ; preds = %4
  %.not24 = icmp slt i64 %spec.select, 2
  br i1 %.not24, label %.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %.preheader
  %9 = add i64 %0, 1
  br label %10

10:                                               ; preds = %.lr.ph, %10
  %indvars.iv = phi i64 [ 2, %.lr.ph ], [ %indvars.iv.next, %10 ]
  %.01825 = phi i64 [ %0, %.lr.ph ], [ %13, %10 ]
  %11 = sub i64 %9, %indvars.iv
  %12 = mul nsw i64 %11, %.01825
  %13 = sdiv i64 %12, %indvars.iv
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %.not.not = icmp sgt i64 %spec.select, %indvars.iv
  br i1 %.not.not, label %10, label %.loopexit, !llvm.loop !5

.loopexit:                                        ; preds = %10, %.preheader, %4, %2
  %.020 = phi i64 [ 0, %2 ], [ 1, %4 ], [ %0, %.preheader ], [ %13, %10 ]
  ret i64 %.020
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn 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,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass