; LLVM IR from tangjz/acm-icpc :: 51nod (1120)

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

@n = external dso_local global i32, align 4
@fact = external dso_local global [10007 x i32], align 16
@inv = external dso_local global [10007 x i32], align 16
@.str = external hidden unnamed_addr constant [3 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [4 x i8], align 1

; Function Attrs: mustprogress uwtable
declare dso_local noundef i32 @_Z1Cii(i32 noundef, i32 noundef) #0

; Function Attrs: mustprogress norecurse uwtable
define dso_local noundef i32 @main() #1 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  store i32 0, ptr %1, align 4
  %6 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef @n)
  store i32 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 !5
  br label %7

7:                                                ; preds = %23, %0
  %8 = load i32, ptr %2, align 4, !tbaa !5
  %9 = icmp slt i32 %8, 10007
  br i1 %9, label %11, label %10

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

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

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

26:                                               ; preds = %10
  store i32 1, ptr getelementptr inbounds ([10007 x i32], ptr @inv, i64 0, i64 1), align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #4
  store i32 2, ptr %3, align 4, !tbaa !5
  br label %27

27:                                               ; preds = %45, %26
  %28 = load i32, ptr %3, align 4, !tbaa !5
  %29 = icmp slt i32 %28, 10007
  br i1 %29, label %31, label %30

30:                                               ; preds = %27
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #4
  br label %48

31:                                               ; preds = %27
  %32 = load i32, ptr %3, align 4, !tbaa !5
  %33 = srem i32 10007, %32
  %34 = sext i32 %33 to i64
  %35 = getelementptr inbounds [10007 x i32], ptr @inv, i64 0, i64 %34
  %36 = load i32, ptr %35, align 4, !tbaa !5
  %37 = load i32, ptr %3, align 4, !tbaa !5
  %38 = sdiv i32 10007, %37
  %39 = sub nsw i32 10007, %38
  %40 = mul nsw i32 %36, %39
  %41 = srem i32 %40, 10007
  %42 = load i32, ptr %3, align 4, !tbaa !5
  %43 = sext i32 %42 to i64
  %44 = getelementptr inbounds [10007 x i32], ptr @inv, i64 0, i64 %43
  store i32 %41, ptr %44, align 4, !tbaa !5
  br label %45

45:                                               ; preds = %31
  %46 = load i32, ptr %3, align 4, !tbaa !5
  %47 = add nsw i32 %46, 1
  store i32 %47, ptr %3, align 4, !tbaa !5
  br label %27, !llvm.loop !11

48:                                               ; preds = %30
  store i32 1, ptr @inv, align 16, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #4
  store i32 1, ptr %4, align 4, !tbaa !5
  br label %49

49:                                               ; preds = %68, %48
  %50 = load i32, ptr %4, align 4, !tbaa !5
  %51 = icmp slt i32 %50, 10007
  br i1 %51, label %53, label %52

52:                                               ; preds = %49
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #4
  br label %71

53:                                               ; preds = %49
  %54 = load i32, ptr %4, align 4, !tbaa !5
  %55 = sub nsw i32 %54, 1
  %56 = sext i32 %55 to i64
  %57 = getelementptr inbounds [10007 x i32], ptr @inv, i64 0, i64 %56
  %58 = load i32, ptr %57, align 4, !tbaa !5
  %59 = load i32, ptr %4, align 4, !tbaa !5
  %60 = sext i32 %59 to i64
  %61 = getelementptr inbounds [10007 x i32], ptr @inv, i64 0, i64 %60
  %62 = load i32, ptr %61, align 4, !tbaa !5
  %63 = mul nsw i32 %58, %62
  %64 = srem i32 %63, 10007
  %65 = load i32, ptr %4, align 4, !tbaa !5
  %66 = sext i32 %65 to i64
  %67 = getelementptr inbounds [10007 x i32], ptr @inv, i64 0, i64 %66
  store i32 %64, ptr %67, align 4, !tbaa !5
  br label %68

68:                                               ; preds = %53
  %69 = load i32, ptr %4, align 4, !tbaa !5
  %70 = add nsw i32 %69, 1
  store i32 %70, ptr %4, align 4, !tbaa !5
  br label %49, !llvm.loop !12

71:                                               ; preds = %52
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #4
  %72 = load i32, ptr @n, align 4, !tbaa !5
  %73 = mul nsw i32 2, %72
  %74 = load i32, ptr @n, align 4, !tbaa !5
  %75 = call noundef i32 @_Z1Cii(i32 noundef %73, i32 noundef %74)
  %76 = load i32, ptr @n, align 4, !tbaa !5
  %77 = mul nsw i32 2, %76
  %78 = load i32, ptr @n, align 4, !tbaa !5
  %79 = sub nsw i32 %78, 1
  %80 = call noundef i32 @_Z1Cii(i32 noundef %77, i32 noundef %79)
  %81 = sub nsw i32 %75, %80
  store i32 %81, ptr %5, align 4, !tbaa !5
  %82 = load i32, ptr %5, align 4, !tbaa !5
  %83 = icmp slt i32 %82, 0
  br i1 %83, label %84, label %87

84:                                               ; preds = %71
  %85 = load i32, ptr %5, align 4, !tbaa !5
  %86 = add nsw i32 %85, 10007
  store i32 %86, ptr %5, align 4, !tbaa !5
  br label %87

87:                                               ; preds = %84, %71
  %88 = load i32, ptr %5, align 4, !tbaa !5
  %89 = call i32 (ptr, ...) @printf(ptr noundef @.str.1, i32 noundef %88)
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #4
  ret i32 0
}

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

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

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

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

attributes #0 = { mustprogress 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 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 = { "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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = !{!"int", !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"

@n = external dso_local global i32, align 4
@fact = external dso_local local_unnamed_addr global [10007 x i32], align 16
@inv = external dso_local local_unnamed_addr global [10007 x i32], align 16
@.str = external hidden unnamed_addr constant [3 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [4 x i8], align 1

; Function Attrs: mustprogress uwtable
declare dso_local noundef i32 @_Z1Cii(i32 noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress norecurse uwtable
define dso_local noundef i32 @main() local_unnamed_addr #1 {
  %1 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull @n)
  store i32 1, ptr @fact, align 16, !tbaa !5
  br label %3

2:                                                ; preds = %3
  store i32 1, ptr getelementptr inbounds (i8, ptr @inv, i64 4), align 4, !tbaa !5
  br label %14

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

13:                                               ; preds = %14
  store i32 1, ptr @inv, align 16, !tbaa !5
  br label %36

14:                                               ; preds = %2, %14
  %indvars.iv28 = phi i64 [ 2, %2 ], [ %indvars.iv.next29, %14 ]
  %.rhs.trunc = trunc i64 %indvars.iv28 to i16
  %15 = urem i16 10007, %.rhs.trunc
  %16 = zext nneg i16 %15 to i64
  %17 = getelementptr inbounds [10007 x i32], ptr @inv, i64 0, i64 %16
  %18 = load i32, ptr %17, align 4, !tbaa !5
  %19 = udiv i16 10007, %.rhs.trunc
  %narrow = sub nuw nsw i16 10007, %19
  %20 = zext nneg i16 %narrow to i32
  %21 = mul nsw i32 %18, %20
  %22 = srem i32 %21, 10007
  %23 = getelementptr inbounds [10007 x i32], ptr @inv, i64 0, i64 %indvars.iv28
  store i32 %22, ptr %23, align 4, !tbaa !5
  %indvars.iv.next29 = add nuw nsw i64 %indvars.iv28, 1
  %exitcond31.not = icmp eq i64 %indvars.iv.next29, 10007
  br i1 %exitcond31.not, label %13, label %14, !llvm.loop !11

24:                                               ; preds = %36
  %25 = load i32, ptr @n, align 4, !tbaa !5
  %26 = shl nsw i32 %25, 1
  %27 = tail call noundef i32 @_Z1Cii(i32 noundef %26, i32 noundef %25)
  %28 = load i32, ptr @n, align 4, !tbaa !5
  %29 = shl nsw i32 %28, 1
  %30 = add nsw i32 %28, -1
  %31 = tail call noundef i32 @_Z1Cii(i32 noundef %29, i32 noundef %30)
  %32 = sub nsw i32 %27, %31
  %33 = icmp slt i32 %32, 0
  %34 = add nsw i32 %32, 10007
  %spec.select = select i1 %33, i32 %34, i32 %32
  %35 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1, i32 noundef %spec.select)
  ret i32 0

36:                                               ; preds = %36, %13
  %37 = phi i32 [ 1, %13 ], [ %45, %36 ]
  %indvars.iv32 = phi i64 [ 1, %13 ], [ %indvars.iv.next33.1, %36 ]
  %38 = getelementptr inbounds [10007 x i32], ptr @inv, i64 0, i64 %indvars.iv32
  %39 = load i32, ptr %38, align 4, !tbaa !5
  %40 = mul nsw i32 %39, %37
  %41 = srem i32 %40, 10007
  store i32 %41, ptr %38, align 4, !tbaa !5
  %indvars.iv.next33 = add nuw nsw i64 %indvars.iv32, 1
  %42 = getelementptr inbounds [10007 x i32], ptr @inv, i64 0, i64 %indvars.iv.next33
  %43 = load i32, ptr %42, align 4, !tbaa !5
  %44 = mul nsw i32 %43, %41
  %45 = srem i32 %44, 10007
  store i32 %45, ptr %42, align 4, !tbaa !5
  %indvars.iv.next33.1 = add nuw nsw i64 %indvars.iv32, 2
  %exitcond35.not.1 = icmp eq i64 %indvars.iv.next33.1, 10007
  br i1 %exitcond35.not.1, label %24, label %36, !llvm.loop !12
}

; 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 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 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 = !{!"int", !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,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,LoopRotatePass,LoopUnrollPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass