; LLVM IR from VastoLorde95/Competitive-Programming :: CodeChef/Random/NWAYS (A)

; 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 [2500001 x i64], align 16
@MOD = external dso_local global i64, align 8
@t = external dso_local global i32, align 4
@n = external dso_local global i32, align 4
@k = external dso_local global i32, align 4
@.str = external hidden unnamed_addr constant [3 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [5 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [6 x i8], align 1

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

; Function Attrs: mustprogress norecurse uwtable
define dso_local noundef i32 @main() #2 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca i64, align 8
  store i32 0, ptr %1, align 4
  store i64 1, ptr @fact, align 16, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #4
  store i32 1, ptr %2, align 4, !tbaa !9
  br label %4

4:                                                ; preds = %22, %0
  %5 = load i32, ptr %2, align 4, !tbaa !9
  %6 = icmp sle i32 %5, 2500000
  br i1 %6, label %8, label %7

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

8:                                                ; preds = %4
  %9 = load i32, ptr %2, align 4, !tbaa !9
  %10 = sub nsw i32 %9, 1
  %11 = sext i32 %10 to i64
  %12 = getelementptr inbounds [2500001 x i64], ptr @fact, i64 0, i64 %11
  %13 = load i64, ptr %12, align 8, !tbaa !5
  %14 = load i32, ptr %2, align 4, !tbaa !9
  %15 = sext i32 %14 to i64
  %16 = mul nsw i64 %13, %15
  %17 = load i64, ptr @MOD, align 8, !tbaa !5
  %18 = srem i64 %16, %17
  %19 = load i32, ptr %2, align 4, !tbaa !9
  %20 = sext i32 %19 to i64
  %21 = getelementptr inbounds [2500001 x i64], ptr @fact, i64 0, i64 %20
  store i64 %18, ptr %21, align 8, !tbaa !5
  br label %22

22:                                               ; preds = %8
  %23 = load i32, ptr %2, align 4, !tbaa !9
  %24 = add nsw i32 %23, 1
  store i32 %24, ptr %2, align 4, !tbaa !9
  br label %4, !llvm.loop !11

25:                                               ; preds = %7
  %26 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef @t)
  br label %27

27:                                               ; preds = %31, %25
  %28 = load i32, ptr @t, align 4, !tbaa !9
  %29 = add nsw i32 %28, -1
  store i32 %29, ptr @t, align 4, !tbaa !9
  %30 = icmp ne i32 %28, 0
  br i1 %30, label %31, label %55

31:                                               ; preds = %27
  %32 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str.1, ptr noundef @n, ptr noundef @k)
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #4
  %33 = load i32, ptr @k, align 4, !tbaa !9
  %34 = load i32, ptr @n, align 4, !tbaa !9
  %35 = add nsw i32 %33, %34
  %36 = add nsw i32 %35, 1
  %37 = sext i32 %36 to i64
  %38 = load i32, ptr @k, align 4, !tbaa !9
  %39 = add nsw i32 %38, 2
  %40 = sext i32 %39 to i64
  %41 = call noundef i64 @_Z3nCrxx(i64 noundef %37, i64 noundef %40)
  %42 = mul nsw i64 2, %41
  %43 = load i64, ptr @MOD, align 8, !tbaa !5
  %44 = srem i64 %42, %43
  store i64 %44, ptr %3, align 8, !tbaa !5
  %45 = load i64, ptr %3, align 8, !tbaa !5
  %46 = load i32, ptr @n, align 4, !tbaa !9
  %47 = sext i32 %46 to i64
  %48 = sub nsw i64 %45, %47
  %49 = load i64, ptr @MOD, align 8, !tbaa !5
  %50 = add nsw i64 %48, %49
  %51 = load i64, ptr @MOD, align 8, !tbaa !5
  %52 = srem i64 %50, %51
  store i64 %52, ptr %3, align 8, !tbaa !5
  %53 = load i64, ptr %3, align 8, !tbaa !5
  %54 = call i32 (ptr, ...) @printf(ptr noundef @.str.2, i64 noundef %53)
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #4
  br label %27, !llvm.loop !13

55:                                               ; preds = %27
  ret i32 0
}

declare i32 @__isoc99_scanf(ptr noundef, ...) #3

declare i32 @printf(ptr noundef, ...) #3

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 = { mustprogress norecurse 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 #3 = { "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 }

!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"}
!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"

@fact = external dso_local local_unnamed_addr global [2500001 x i64], align 16
@MOD = external dso_local local_unnamed_addr global i64, align 8
@t = external dso_local global i32, align 4
@n = external dso_local global i32, align 4
@k = external dso_local global i32, align 4
@.str = external hidden unnamed_addr constant [3 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [5 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [6 x i8], align 1

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i64 @_Z3nCrxx(i64 noundef, i64 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress norecurse nounwind uwtable
define dso_local noundef i32 @main() local_unnamed_addr #1 {
  store i64 1, ptr @fact, align 16, !tbaa !5
  %1 = load i64, ptr @MOD, align 8, !tbaa !5
  br label %6

2:                                                ; preds = %6
  %3 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull @t)
  %4 = load i32, ptr @t, align 4, !tbaa !9
  %5 = add nsw i32 %4, -1
  store i32 %5, ptr @t, align 4, !tbaa !9
  %.not8 = icmp eq i32 %4, 0
  br i1 %.not8, label %._crit_edge, label %.lr.ph

6:                                                ; preds = %6, %0
  %7 = phi i64 [ 1, %0 ], [ %12, %6 ]
  %indvars.iv = phi i64 [ 1, %0 ], [ %indvars.iv.next.1, %6 ]
  %8 = mul nsw i64 %7, %indvars.iv
  %9 = srem i64 %8, %1
  %10 = getelementptr inbounds [2500001 x i64], ptr @fact, i64 0, i64 %indvars.iv
  store i64 %9, ptr %10, align 8, !tbaa !5
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %11 = mul nsw i64 %9, %indvars.iv.next
  %12 = srem i64 %11, %1
  %13 = getelementptr inbounds [2500001 x i64], ptr @fact, i64 0, i64 %indvars.iv.next
  store i64 %12, ptr %13, align 8, !tbaa !5
  %indvars.iv.next.1 = add nuw nsw i64 %indvars.iv, 2
  %exitcond.not.1 = icmp eq i64 %indvars.iv.next.1, 2500001
  br i1 %exitcond.not.1, label %2, label %6, !llvm.loop !11

.lr.ph:                                           ; preds = %2, %.lr.ph
  %14 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str.1, ptr noundef nonnull @n, ptr noundef nonnull @k)
  %15 = load i32, ptr @k, align 4, !tbaa !9
  %16 = load i32, ptr @n, align 4, !tbaa !9
  %17 = add i32 %15, 1
  %18 = add i32 %17, %16
  %19 = sext i32 %18 to i64
  %20 = add nsw i32 %15, 2
  %21 = sext i32 %20 to i64
  %22 = tail call noundef i64 @_Z3nCrxx(i64 noundef %19, i64 noundef %21)
  %23 = shl nsw i64 %22, 1
  %24 = load i64, ptr @MOD, align 8, !tbaa !5
  %25 = srem i64 %23, %24
  %26 = load i32, ptr @n, align 4, !tbaa !9
  %27 = sext i32 %26 to i64
  %28 = add i64 %25, %24
  %29 = sub i64 %28, %27
  %30 = srem i64 %29, %24
  %31 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.2, i64 noundef %30)
  %32 = load i32, ptr @t, align 4, !tbaa !9
  %33 = add nsw i32 %32, -1
  store i32 %33, ptr @t, align 4, !tbaa !9
  %.not = icmp eq i32 %32, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !13

._crit_edge:                                      ; preds = %.lr.ph, %2
  ret i32 0
}

; Function Attrs: nofree nounwind
declare noundef i32 @__isoc99_scanf(ptr nocapture noundef readonly, ...) local_unnamed_addr #2

; Function Attrs: nofree nounwind
declare noundef i32 @printf(ptr nocapture noundef readonly, ...) 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 norecurse 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 = { nofree 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" }

!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"}
!13 = distinct !{!13, !12}


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