; LLVM IR from AdarshGaur/Competitive-Programming :: Algorithm Library/Number Theory/nCr.ll (Z10factorialsv)

; 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"

@fact = external dso_local global [100007 x i64], align 16
@invf = external dso_local global [100007 x i64], align 16

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_Z10factorialsv() #0 {
  %1 = alloca i64, align 8
  %2 = alloca i64, align 8
  %3 = alloca i64, align 8
  store i64 1, ptr getelementptr inbounds ([100007 x i64], ptr @invf, i64 0, i64 1), align 8, !tbaa !5
  store i64 1, ptr @invf, align 16, !tbaa !5
  store i64 1, ptr @fact, align 16, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %1) #2
  store i64 1, ptr %1, align 8, !tbaa !5
  br label %4

4:                                                ; preds = %18, %0
  %5 = load i64, ptr %1, align 8, !tbaa !5
  %6 = icmp slt i64 %5, 100007
  br i1 %6, label %8, label %7

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

8:                                                ; preds = %4
  %9 = load i64, ptr %1, align 8, !tbaa !5
  %10 = sub nsw i64 %9, 1
  %11 = getelementptr inbounds [100007 x i64], ptr @fact, i64 0, i64 %10
  %12 = load i64, ptr %11, align 8, !tbaa !5
  %13 = load i64, ptr %1, align 8, !tbaa !5
  %14 = mul nsw i64 %12, %13
  %15 = srem i64 %14, 1000000007
  %16 = load i64, ptr %1, align 8, !tbaa !5
  %17 = getelementptr inbounds [100007 x i64], ptr @fact, i64 0, i64 %16
  store i64 %15, ptr %17, align 8, !tbaa !5
  br label %18

18:                                               ; preds = %8
  %19 = load i64, ptr %1, align 8, !tbaa !5
  %20 = add nsw i64 %19, 1
  store i64 %20, ptr %1, align 8, !tbaa !5
  br label %4, !llvm.loop !9

21:                                               ; preds = %7
  call void @llvm.lifetime.start.p0(i64 8, ptr %2) #2
  store i64 2, ptr %2, align 8, !tbaa !5
  br label %22

22:                                               ; preds = %38, %21
  %23 = load i64, ptr %2, align 8, !tbaa !5
  %24 = icmp slt i64 %23, 100007
  br i1 %24, label %26, label %25

25:                                               ; preds = %22
  call void @llvm.lifetime.end.p0(i64 8, ptr %2) #2
  br label %41

26:                                               ; preds = %22
  %27 = load i64, ptr %2, align 8, !tbaa !5
  %28 = sdiv i64 1000000007, %27
  %29 = sub nsw i64 1000000007, %28
  %30 = load i64, ptr %2, align 8, !tbaa !5
  %31 = srem i64 1000000007, %30
  %32 = getelementptr inbounds [100007 x i64], ptr @invf, i64 0, i64 %31
  %33 = load i64, ptr %32, align 8, !tbaa !5
  %34 = mul nsw i64 %29, %33
  %35 = srem i64 %34, 1000000007
  %36 = load i64, ptr %2, align 8, !tbaa !5
  %37 = getelementptr inbounds [100007 x i64], ptr @invf, i64 0, i64 %36
  store i64 %35, ptr %37, align 8, !tbaa !5
  br label %38

38:                                               ; preds = %26
  %39 = load i64, ptr %2, align 8, !tbaa !5
  %40 = add nsw i64 %39, 1
  store i64 %40, ptr %2, align 8, !tbaa !5
  br label %22, !llvm.loop !11

41:                                               ; preds = %25
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #2
  store i64 1, ptr %3, align 8, !tbaa !5
  br label %42

42:                                               ; preds = %58, %41
  %43 = load i64, ptr %3, align 8, !tbaa !5
  %44 = icmp slt i64 %43, 100007
  br i1 %44, label %46, label %45

45:                                               ; preds = %42
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #2
  br label %61

46:                                               ; preds = %42
  %47 = load i64, ptr %3, align 8, !tbaa !5
  %48 = getelementptr inbounds [100007 x i64], ptr @invf, i64 0, i64 %47
  %49 = load i64, ptr %48, align 8, !tbaa !5
  %50 = load i64, ptr %3, align 8, !tbaa !5
  %51 = sub nsw i64 %50, 1
  %52 = getelementptr inbounds [100007 x i64], ptr @invf, i64 0, i64 %51
  %53 = load i64, ptr %52, align 8, !tbaa !5
  %54 = mul nsw i64 %49, %53
  %55 = srem i64 %54, 1000000007
  %56 = load i64, ptr %3, align 8, !tbaa !5
  %57 = getelementptr inbounds [100007 x i64], ptr @invf, i64 0, i64 %56
  store i64 %55, ptr %57, align 8, !tbaa !5
  br label %58

58:                                               ; preds = %46
  %59 = load i64, ptr %3, align 8, !tbaa !5
  %60 = add nsw i64 %59, 1
  store i64 %60, ptr %3, align 8, !tbaa !5
  br label %42, !llvm.loop !12

61:                                               ; preds = %45
  ret void
}

; 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 = !{!"long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10}
!12 = distinct !{!12, !10}


; 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"

@fact = external dso_local local_unnamed_addr global [100007 x i64], align 16
@invf = external dso_local local_unnamed_addr global [100007 x i64], align 16

; Function Attrs: mustprogress nofree norecurse nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) uwtable
define dso_local void @_Z10factorialsv() local_unnamed_addr #0 {
  store i64 1, ptr getelementptr inbounds (i8, ptr @invf, i64 8), align 8, !tbaa !5
  store i64 1, ptr @invf, align 16, !tbaa !5
  store i64 1, ptr @fact, align 16, !tbaa !5
  br label %1

1:                                                ; preds = %1, %0
  %2 = phi i64 [ 1, %0 ], [ %8, %1 ]
  %.01620 = phi i64 [ 1, %0 ], [ %10, %1 ]
  %3 = mul nsw i64 %2, %.01620
  %4 = srem i64 %3, 1000000007
  %5 = getelementptr inbounds [100007 x i64], ptr @fact, i64 0, i64 %.01620
  store i64 %4, ptr %5, align 8, !tbaa !5
  %6 = add nuw nsw i64 %.01620, 1
  %7 = mul nsw i64 %4, %6
  %8 = srem i64 %7, 1000000007
  %9 = getelementptr inbounds [100007 x i64], ptr @fact, i64 0, i64 %6
  store i64 %8, ptr %9, align 8, !tbaa !5
  %10 = add nuw nsw i64 %.01620, 2
  %exitcond.not.1 = icmp eq i64 %10, 100007
  br i1 %exitcond.not.1, label %.preheader19, label %1, !llvm.loop !9

.preheader19:                                     ; preds = %1, %.preheader19
  %.01521 = phi i64 [ %19, %.preheader19 ], [ 2, %1 ]
  %.rhs.trunc = trunc nuw i64 %.01521 to i32
  %11 = udiv i32 1000000007, %.rhs.trunc
  %narrow = sub nuw nsw i32 1000000007, %11
  %12 = zext nneg i32 %narrow to i64
  %13 = urem i32 1000000007, %.rhs.trunc
  %.zext18 = zext nneg i32 %13 to i64
  %14 = getelementptr inbounds [100007 x i64], ptr @invf, i64 0, i64 %.zext18
  %15 = load i64, ptr %14, align 8, !tbaa !5
  %16 = mul nsw i64 %15, %12
  %17 = srem i64 %16, 1000000007
  %18 = getelementptr inbounds [100007 x i64], ptr @invf, i64 0, i64 %.01521
  store i64 %17, ptr %18, align 8, !tbaa !5
  %19 = add nuw nsw i64 %.01521, 1
  %exitcond23.not = icmp eq i64 %19, 100007
  br i1 %exitcond23.not, label %.preheader, label %.preheader19, !llvm.loop !11

20:                                               ; preds = %.preheader
  ret void

.preheader:                                       ; preds = %.preheader19, %.preheader
  %21 = phi i64 [ %30, %.preheader ], [ 1, %.preheader19 ]
  %.022 = phi i64 [ %31, %.preheader ], [ 1, %.preheader19 ]
  %22 = getelementptr inbounds [100007 x i64], ptr @invf, i64 0, i64 %.022
  %23 = load i64, ptr %22, align 8, !tbaa !5
  %24 = mul nsw i64 %21, %23
  %25 = srem i64 %24, 1000000007
  store i64 %25, ptr %22, align 8, !tbaa !5
  %26 = add nuw nsw i64 %.022, 1
  %27 = getelementptr inbounds [100007 x i64], ptr @invf, i64 0, i64 %26
  %28 = load i64, ptr %27, align 8, !tbaa !5
  %29 = mul nsw i64 %25, %28
  %30 = srem i64 %29, 1000000007
  store i64 %30, ptr %27, align 8, !tbaa !5
  %31 = add nuw nsw i64 %.022, 2
  %exitcond24.not.1 = icmp eq i64 %31, 100007
  br i1 %exitcond24.not.1, label %20, label %.preheader, !llvm.loop !12
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: 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 = !{!6, !6, i64 0}
!6 = !{!"long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10}
!12 = distinct !{!12, !10}


; Effective O3 passes: BDCEPass,CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass