; LLVM IR from AdarshGaur/Competitive-Programming :: Codechef/Random Problems/Binary XOR.ll (Z9factorialv)

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

@f = external dso_local global [1000005 x i64], align 16
@invf = external dso_local global [1000005 x i64], align 16

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_Z9factorialv() #0 {
  %1 = alloca i64, align 8
  %2 = alloca i64, align 8
  %3 = alloca i64, align 8
  store i64 1, ptr @f, align 16, !tbaa !5
  store i64 1, ptr getelementptr inbounds ([1000005 x i64], ptr @invf, i64 0, i64 1), align 8, !tbaa !5
  store i64 1, ptr @invf, 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 sle i64 %5, 1000005
  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 [1000005 x i64], ptr @f, i64 0, i64 %10
  %12 = load i64, ptr %11, align 8, !tbaa !5
  %13 = load i64, ptr %1, align 8, !tbaa !5
  %14 = mul i64 %12, %13
  %15 = urem i64 %14, 1000000007
  %16 = load i64, ptr %1, align 8, !tbaa !5
  %17 = getelementptr inbounds [1000005 x i64], ptr @f, 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, 1000005
  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 [1000005 x i64], ptr @invf, i64 0, i64 %31
  %33 = load i64, ptr %32, align 8, !tbaa !5
  %34 = mul i64 %29, %33
  %35 = urem i64 %34, 1000000007
  %36 = load i64, ptr %2, align 8, !tbaa !5
  %37 = getelementptr inbounds [1000005 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, 1000005
  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 = sub nsw i64 %47, 1
  %49 = getelementptr inbounds [1000005 x i64], ptr @invf, i64 0, i64 %48
  %50 = load i64, ptr %49, align 8, !tbaa !5
  %51 = load i64, ptr %3, align 8, !tbaa !5
  %52 = getelementptr inbounds [1000005 x i64], ptr @invf, i64 0, i64 %51
  %53 = load i64, ptr %52, align 8, !tbaa !5
  %54 = mul i64 %50, %53
  %55 = urem i64 %54, 1000000007
  %56 = load i64, ptr %3, align 8, !tbaa !5
  %57 = getelementptr inbounds [1000005 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 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"

@f = external dso_local local_unnamed_addr global [1000005 x i64], align 16
@invf = external dso_local local_unnamed_addr global [1000005 x i64], align 16

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

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

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

24:                                               ; preds = %.preheader
  ret void

.preheader:                                       ; preds = %.preheader19, %.preheader
  %25 = phi i64 [ %34, %.preheader ], [ 1, %.preheader19 ]
  %.022 = phi i64 [ %35, %.preheader ], [ 1, %.preheader19 ]
  %26 = getelementptr inbounds [1000005 x i64], ptr @invf, i64 0, i64 %.022
  %27 = load i64, ptr %26, align 8, !tbaa !5
  %28 = mul i64 %27, %25
  %29 = urem i64 %28, 1000000007
  store i64 %29, ptr %26, align 8, !tbaa !5
  %30 = add nuw nsw i64 %.022, 1
  %31 = getelementptr inbounds [1000005 x i64], ptr @invf, i64 0, i64 %30
  %32 = load i64, ptr %31, align 8, !tbaa !5
  %33 = mul i64 %32, %29
  %34 = urem i64 %33, 1000000007
  store i64 %34, ptr %31, align 8, !tbaa !5
  %35 = add nuw nsw i64 %.022, 2
  %exitcond24.not.1 = icmp eq i64 %35, 1000005
  br i1 %exitcond24.not.1, label %24, 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 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