; LLVM IR from ravireddy07/cpp-book :: src/Code-Chef/BESTBATS.ll (Z8NChooseKii)

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

13:                                               ; preds = %23, %2
  %14 = load i64, ptr %6, align 8, !tbaa !9
  %15 = load i32, ptr %3, align 4, !tbaa !5
  %16 = sext i32 %15 to i64
  %17 = icmp ule i64 %14, %16
  br i1 %17, label %19, label %18

18:                                               ; preds = %13
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #2
  br label %26

19:                                               ; preds = %13
  %20 = load i64, ptr %6, align 8, !tbaa !9
  %21 = load i64, ptr %5, align 8, !tbaa !9
  %22 = mul i64 %21, %20
  store i64 %22, ptr %5, align 8, !tbaa !9
  br label %23

23:                                               ; preds = %19
  %24 = load i64, ptr %6, align 8, !tbaa !9
  %25 = add i64 %24, 1
  store i64 %25, ptr %6, align 8, !tbaa !9
  br label %13, !llvm.loop !11

26:                                               ; preds = %18
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #2
  store i64 1, ptr %7, align 8, !tbaa !9
  br label %27

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

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

33:                                               ; preds = %27
  %34 = load i64, ptr %7, align 8, !tbaa !9
  %35 = load i64, ptr %5, align 8, !tbaa !9
  %36 = udiv i64 %35, %34
  store i64 %36, ptr %5, align 8, !tbaa !9
  br label %37

37:                                               ; preds = %33
  %38 = load i64, ptr %7, align 8, !tbaa !9
  %39 = add i64 %38, 1
  store i64 %39, ptr %7, align 8, !tbaa !9
  br label %27, !llvm.loop !13

40:                                               ; preds = %32
  %41 = load i64, ptr %5, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #2
  ret i64 %41
}

; 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 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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !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: mustprogress nofree norecurse nosync nounwind willreturn memory(none) uwtable
define dso_local noundef i64 @_Z8NChooseKii(i32 noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = add i32 %0, 1
  %4 = sub i32 %3, %1
  %5 = sext i32 %0 to i64
  %.not16 = icmp ugt i32 %4, %0
  br i1 %.not16, label %.preheader, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %2
  %6 = sext i32 %4 to i64
  br label %.lr.ph

.preheader:                                       ; preds = %.lr.ph, %2
  %.014.lcssa = phi i64 [ 1, %2 ], [ %12, %.lr.ph ]
  %.not1519 = icmp eq i32 %1, 0
  br i1 %.not1519, label %._crit_edge, label %.lr.ph22.preheader

.lr.ph22.preheader:                               ; preds = %.preheader
  %7 = sext i32 %1 to i64
  %8 = add nsw i64 %7, 1
  %umax = tail call i64 @llvm.umax.i64(i64 %8, i64 2)
  %9 = add i64 %umax, -1
  %10 = add i64 %umax, -2
  %xtraiter = and i64 %9, 3
  %11 = icmp ult i64 %10, 3
  br i1 %11, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph22.preheader.new

.lr.ph22.preheader.new:                           ; preds = %.lr.ph22.preheader
  %unroll_iter = and i64 %9, -4
  br label %.lr.ph22

.lr.ph:                                           ; preds = %.lr.ph.preheader, %.lr.ph
  %.01318 = phi i64 [ %13, %.lr.ph ], [ %6, %.lr.ph.preheader ]
  %.01417 = phi i64 [ %12, %.lr.ph ], [ 1, %.lr.ph.preheader ]
  %12 = mul i64 %.01318, %.01417
  %13 = add i64 %.01318, 1
  %.not = icmp ugt i64 %13, %5
  br i1 %.not, label %.preheader, label %.lr.ph, !llvm.loop !5

._crit_edge.loopexit.unr-lcssa:                   ; preds = %.lr.ph22, %.lr.ph22.preheader
  %.lcssa.ph = phi i64 [ poison, %.lr.ph22.preheader ], [ %22, %.lr.ph22 ]
  %.021.unr = phi i64 [ 1, %.lr.ph22.preheader ], [ %23, %.lr.ph22 ]
  %.120.unr = phi i64 [ %.014.lcssa, %.lr.ph22.preheader ], [ %22, %.lr.ph22 ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge, label %.lr.ph22.epil

.lr.ph22.epil:                                    ; preds = %._crit_edge.loopexit.unr-lcssa, %.lr.ph22.epil
  %.021.epil = phi i64 [ %15, %.lr.ph22.epil ], [ %.021.unr, %._crit_edge.loopexit.unr-lcssa ]
  %.120.epil = phi i64 [ %14, %.lr.ph22.epil ], [ %.120.unr, %._crit_edge.loopexit.unr-lcssa ]
  %epil.iter = phi i64 [ %epil.iter.next, %.lr.ph22.epil ], [ 0, %._crit_edge.loopexit.unr-lcssa ]
  %14 = udiv i64 %.120.epil, %.021.epil
  %15 = add nuw i64 %.021.epil, 1
  %epil.iter.next = add i64 %epil.iter, 1
  %epil.iter.cmp.not = icmp eq i64 %epil.iter.next, %xtraiter
  br i1 %epil.iter.cmp.not, label %._crit_edge, label %.lr.ph22.epil, !llvm.loop !7

._crit_edge:                                      ; preds = %._crit_edge.loopexit.unr-lcssa, %.lr.ph22.epil, %.preheader
  %.1.lcssa = phi i64 [ %.014.lcssa, %.preheader ], [ %.lcssa.ph, %._crit_edge.loopexit.unr-lcssa ], [ %14, %.lr.ph22.epil ]
  ret i64 %.1.lcssa

.lr.ph22:                                         ; preds = %.lr.ph22, %.lr.ph22.preheader.new
  %.021 = phi i64 [ 1, %.lr.ph22.preheader.new ], [ %23, %.lr.ph22 ]
  %.120 = phi i64 [ %.014.lcssa, %.lr.ph22.preheader.new ], [ %22, %.lr.ph22 ]
  %niter = phi i64 [ 0, %.lr.ph22.preheader.new ], [ %niter.next.3, %.lr.ph22 ]
  %16 = udiv i64 %.120, %.021
  %17 = add nuw nsw i64 %.021, 1
  %18 = udiv i64 %16, %17
  %19 = add nuw nsw i64 %.021, 2
  %20 = udiv i64 %18, %19
  %21 = add nuw i64 %.021, 3
  %22 = udiv i64 %20, %21
  %23 = add nuw i64 %.021, 4
  %niter.next.3 = add i64 %niter, 4
  %niter.ncmp.3 = icmp eq i64 %niter.next.3, %unroll_iter
  br i1 %niter.ncmp.3, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph22, !llvm.loop !9
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.umax.i64(i64, i64) #1

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" }
attributes #1 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }

!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"}
!7 = distinct !{!7, !8}
!8 = !{!"llvm.loop.unroll.disable"}
!9 = distinct !{!9, !6}


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