; LLVM IR from UtkarshPathrabe/Competitive-Coding :: Model Algorithms/Dynamic Programming/Binomial Coefficient.ll (Z13binomialCoeffxx)

; 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
declare dso_local noundef i64 @_Z3minxx(i64 noundef, i64 noundef) #0

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i64 @_Z13binomialCoeffxx(i64 noundef %0, i64 noundef %1) #0 {
  %3 = alloca i64, align 8
  %4 = alloca i64, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i64, align 8
  %7 = alloca i32, align 4
  %8 = alloca i64, align 8
  %9 = alloca i64, align 8
  store i64 %0, ptr %3, align 8, !tbaa !5
  store i64 %1, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #4
  %10 = load i64, ptr %4, align 8, !tbaa !5
  %11 = add nsw i64 %10, 1
  %12 = call noalias ptr @calloc(i64 noundef %11, i64 noundef 8) #5
  store ptr %12, ptr %5, align 8, !tbaa !9
  %13 = load ptr, ptr %5, align 8, !tbaa !9
  %14 = getelementptr inbounds i64, ptr %13, i64 0
  store i64 1, ptr %14, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #4
  store i64 1, ptr %6, align 8, !tbaa !5
  br label %15

15:                                               ; preds = %46, %2
  %16 = load i64, ptr %6, align 8, !tbaa !5
  %17 = load i64, ptr %3, align 8, !tbaa !5
  %18 = icmp sle i64 %16, %17
  br i1 %18, label %20, label %19

19:                                               ; preds = %15
  store i32 2, ptr %7, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #4
  br label %49

20:                                               ; preds = %15
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #4
  %21 = load i64, ptr %6, align 8, !tbaa !5
  %22 = load i64, ptr %4, align 8, !tbaa !5
  %23 = call noundef i64 @_Z3minxx(i64 noundef %21, i64 noundef %22)
  store i64 %23, ptr %8, align 8, !tbaa !5
  br label %24

24:                                               ; preds = %42, %20
  %25 = load i64, ptr %8, align 8, !tbaa !5
  %26 = icmp sgt i64 %25, 0
  br i1 %26, label %28, label %27

27:                                               ; preds = %24
  store i32 5, ptr %7, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #4
  br label %45

28:                                               ; preds = %24
  %29 = load ptr, ptr %5, align 8, !tbaa !9
  %30 = load i64, ptr %8, align 8, !tbaa !5
  %31 = getelementptr inbounds i64, ptr %29, i64 %30
  %32 = load i64, ptr %31, align 8, !tbaa !5
  %33 = load ptr, ptr %5, align 8, !tbaa !9
  %34 = load i64, ptr %8, align 8, !tbaa !5
  %35 = sub nsw i64 %34, 1
  %36 = getelementptr inbounds i64, ptr %33, i64 %35
  %37 = load i64, ptr %36, align 8, !tbaa !5
  %38 = add nsw i64 %32, %37
  %39 = load ptr, ptr %5, align 8, !tbaa !9
  %40 = load i64, ptr %8, align 8, !tbaa !5
  %41 = getelementptr inbounds i64, ptr %39, i64 %40
  store i64 %38, ptr %41, align 8, !tbaa !5
  br label %42

42:                                               ; preds = %28
  %43 = load i64, ptr %8, align 8, !tbaa !5
  %44 = add nsw i64 %43, -1
  store i64 %44, ptr %8, align 8, !tbaa !5
  br label %24, !llvm.loop !11

45:                                               ; preds = %27
  br label %46

46:                                               ; preds = %45
  %47 = load i64, ptr %6, align 8, !tbaa !5
  %48 = add nsw i64 %47, 1
  store i64 %48, ptr %6, align 8, !tbaa !5
  br label %15, !llvm.loop !13

49:                                               ; preds = %19
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #4
  %50 = load ptr, ptr %5, align 8, !tbaa !9
  %51 = load i64, ptr %4, align 8, !tbaa !5
  %52 = getelementptr inbounds i64, ptr %50, i64 %51
  %53 = load i64, ptr %52, align 8, !tbaa !5
  store i64 %53, ptr %9, align 8, !tbaa !5
  %54 = load ptr, ptr %5, align 8, !tbaa !9
  call void @free(ptr noundef %54) #4
  %55 = load i64, ptr %9, align 8, !tbaa !5
  store i32 1, ptr %7, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #4
  ret i64 %55
}

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

; Function Attrs: nounwind allocsize(0,1)
declare noalias ptr @calloc(i64 noundef, i64 noundef) #2

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

; Function Attrs: nounwind
declare void @free(ptr noundef) #3

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 allocsize(0,1) "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 #3 = { nounwind "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 #4 = { nounwind }
attributes #5 = { nounwind allocsize(0,1) }

!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 = !{!"any pointer", !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 nounwind uwtable
declare dso_local noundef i64 @_Z3minxx(i64 noundef, i64 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i64 @_Z13binomialCoeffxx(i64 noundef %0, i64 noundef %1) local_unnamed_addr #0 {
  %3 = add nsw i64 %1, 1
  %4 = tail call noalias ptr @calloc(i64 noundef %3, i64 noundef 8) #3
  store i64 1, ptr %4, align 8, !tbaa !5
  %.not20 = icmp slt i64 %0, 1
  br i1 %.not20, label %._crit_edge24, label %.lr.ph23

._crit_edge24:                                    ; preds = %._crit_edge, %2
  %5 = getelementptr inbounds i64, ptr %4, i64 %1
  %6 = load i64, ptr %5, align 8, !tbaa !5
  tail call void @free(ptr noundef %4) #4
  ret i64 %6

.lr.ph23:                                         ; preds = %2, %._crit_edge
  %.021 = phi i64 [ %17, %._crit_edge ], [ 1, %2 ]
  %7 = tail call noundef i64 @_Z3minxx(i64 noundef %.021, i64 noundef %1)
  %8 = icmp sgt i64 %7, 0
  br i1 %8, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %.lr.ph23
  %min.iters.check = icmp ult i64 %7, 4
  br i1 %min.iters.check, label %.lr.ph, label %vector.ph

vector.ph:                                        ; preds = %.lr.ph.preheader
  %n.vec = and i64 %7, 9223372036854775804
  %ind.end = and i64 %7, 3
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %offset.idx = sub i64 %7, %index
  %9 = getelementptr inbounds i64, ptr %4, i64 %offset.idx
  %10 = getelementptr inbounds i8, ptr %9, i64 -8
  %11 = getelementptr inbounds i8, ptr %9, i64 -24
  %wide.load = load <2 x i64>, ptr %10, align 8, !tbaa !5
  %wide.load25 = load <2 x i64>, ptr %11, align 8, !tbaa !5
  %12 = getelementptr i8, ptr %9, i64 -16
  %13 = getelementptr i8, ptr %9, i64 -32
  %wide.load27 = load <2 x i64>, ptr %12, align 8, !tbaa !5
  %wide.load29 = load <2 x i64>, ptr %13, align 8, !tbaa !5
  %14 = add nsw <2 x i64> %wide.load27, %wide.load
  %15 = add nsw <2 x i64> %wide.load29, %wide.load25
  store <2 x i64> %14, ptr %10, align 8, !tbaa !5
  store <2 x i64> %15, ptr %11, align 8, !tbaa !5
  %index.next = add nuw i64 %index, 4
  %16 = icmp eq i64 %index.next, %n.vec
  br i1 %16, label %middle.block, label %vector.body, !llvm.loop !9

middle.block:                                     ; preds = %vector.body
  %cmp.n = icmp eq i64 %7, %n.vec
  br i1 %cmp.n, label %._crit_edge, label %.lr.ph

._crit_edge:                                      ; preds = %.lr.ph, %.lr.ph.1, %.lr.ph.2, %middle.block, %.lr.ph23
  %17 = add nuw i64 %.021, 1
  %exitcond.not = icmp eq i64 %.021, %0
  br i1 %exitcond.not, label %._crit_edge24, label %.lr.ph23, !llvm.loop !13

.lr.ph:                                           ; preds = %.lr.ph.preheader, %middle.block
  %.01819.ph = phi i64 [ %7, %.lr.ph.preheader ], [ %ind.end, %middle.block ]
  %18 = getelementptr inbounds i64, ptr %4, i64 %.01819.ph
  %19 = load i64, ptr %18, align 8, !tbaa !5
  %20 = getelementptr i8, ptr %18, i64 -8
  %21 = load i64, ptr %20, align 8, !tbaa !5
  %22 = add nsw i64 %21, %19
  store i64 %22, ptr %18, align 8, !tbaa !5
  %23 = icmp ugt i64 %.01819.ph, 1
  br i1 %23, label %.lr.ph.1, label %._crit_edge, !llvm.loop !14

.lr.ph.1:                                         ; preds = %.lr.ph
  %24 = add nsw i64 %.01819.ph, -1
  %25 = getelementptr inbounds i64, ptr %4, i64 %24
  %26 = load i64, ptr %25, align 8, !tbaa !5
  %27 = getelementptr i8, ptr %25, i64 -8
  %28 = load i64, ptr %27, align 8, !tbaa !5
  %29 = add nsw i64 %28, %26
  store i64 %29, ptr %25, align 8, !tbaa !5
  %30 = icmp ugt i64 %24, 1
  br i1 %30, label %.lr.ph.2, label %._crit_edge, !llvm.loop !14

.lr.ph.2:                                         ; preds = %.lr.ph.1
  %31 = getelementptr i64, ptr %4, i64 %.01819.ph
  %32 = getelementptr i8, ptr %31, i64 -16
  %33 = load i64, ptr %32, align 8, !tbaa !5
  %34 = getelementptr i8, ptr %31, i64 -24
  %35 = load i64, ptr %34, align 8, !tbaa !5
  %36 = add nsw i64 %35, %33
  store i64 %36, ptr %32, align 8, !tbaa !5
  br label %._crit_edge
}

; Function Attrs: mustprogress nofree nounwind willreturn allockind("alloc,zeroed") allocsize(0,1) memory(inaccessiblemem: readwrite)
declare noalias noundef ptr @calloc(i64 noundef, i64 noundef) local_unnamed_addr #1

; Function Attrs: mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite)
declare void @free(ptr allocptr nocapture noundef) local_unnamed_addr #2

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 = { mustprogress nofree nounwind willreturn allockind("alloc,zeroed") allocsize(0,1) memory(inaccessiblemem: readwrite) "alloc-family"="malloc" "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 = { mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite) "alloc-family"="malloc" "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 #3 = { nounwind allocsize(0,1) }
attributes #4 = { 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 = distinct !{!9, !10, !11, !12}
!10 = !{!"llvm.loop.mustprogress"}
!11 = !{!"llvm.loop.isvectorized", i32 1}
!12 = !{!"llvm.loop.unroll.runtime.disable"}
!13 = distinct !{!13, !10}
!14 = distinct !{!14, !10, !12, !11}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopLoadEliminationPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,LoopVectorizePass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass