; LLVM IR from tangjz/acm-icpc :: lydsy (4403)

; 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 [1000003 x i32], align 16
@inv = external dso_local global [1000003 x i32], align 16
@t = external dso_local global i32, align 4
@n = external dso_local global i32, align 4
@l = external dso_local global i32, align 4
@r = external dso_local global i32, align 4
@ans = 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 [7 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [4 x i8], align 1

; Function Attrs: mustprogress uwtable
declare dso_local noundef i32 @_Z8mod_factii(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
  store i32 0, ptr %1, align 4
  store i32 1, ptr getelementptr inbounds ([1000003 x i32], ptr @inv, i64 0, i64 1), align 4, !tbaa !5
  store i32 1, ptr getelementptr inbounds ([1000003 x i32], ptr @fact, i64 0, i64 1), align 4, !tbaa !5
  store i32 1, ptr @fact, align 16, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #4
  store i32 2, ptr %2, align 4, !tbaa !5
  br label %3

3:                                                ; preds = %38, %0
  %4 = load i32, ptr %2, align 4, !tbaa !5
  %5 = icmp slt i32 %4, 1000003
  br i1 %5, label %7, label %6

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

7:                                                ; preds = %3
  %8 = load i32, ptr %2, align 4, !tbaa !5
  %9 = sub nsw i32 %8, 1
  %10 = sext i32 %9 to i64
  %11 = getelementptr inbounds [1000003 x i32], ptr @fact, i64 0, i64 %10
  %12 = load i32, ptr %11, align 4, !tbaa !5
  %13 = sext i32 %12 to i64
  %14 = load i32, ptr %2, align 4, !tbaa !5
  %15 = sext i32 %14 to i64
  %16 = mul nsw i64 %13, %15
  %17 = srem i64 %16, 1000003
  %18 = trunc i64 %17 to i32
  %19 = load i32, ptr %2, align 4, !tbaa !5
  %20 = sext i32 %19 to i64
  %21 = getelementptr inbounds [1000003 x i32], ptr @fact, i64 0, i64 %20
  store i32 %18, ptr %21, align 4, !tbaa !5
  %22 = load i32, ptr %2, align 4, !tbaa !5
  %23 = sdiv i32 1000003, %22
  %24 = sext i32 %23 to i64
  %25 = load i32, ptr %2, align 4, !tbaa !5
  %26 = srem i32 1000003, %25
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds [1000003 x i32], ptr @inv, i64 0, i64 %27
  %29 = load i32, ptr %28, align 4, !tbaa !5
  %30 = sext i32 %29 to i64
  %31 = mul nsw i64 %24, %30
  %32 = srem i64 %31, 1000003
  %33 = sub nsw i64 1000003, %32
  %34 = trunc i64 %33 to i32
  %35 = load i32, ptr %2, align 4, !tbaa !5
  %36 = sext i32 %35 to i64
  %37 = getelementptr inbounds [1000003 x i32], ptr @inv, i64 0, i64 %36
  store i32 %34, ptr %37, align 4, !tbaa !5
  br label %38

38:                                               ; preds = %7
  %39 = load i32, ptr %2, align 4, !tbaa !5
  %40 = add nsw i32 %39, 1
  store i32 %40, ptr %2, align 4, !tbaa !5
  br label %3, !llvm.loop !9

41:                                               ; preds = %6
  %42 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef @t)
  br label %43

43:                                               ; preds = %63, %41
  %44 = load i32, ptr @t, align 4, !tbaa !5
  %45 = add nsw i32 %44, -1
  store i32 %45, ptr @t, align 4, !tbaa !5
  %46 = icmp ne i32 %44, 0
  br i1 %46, label %47, label %66

47:                                               ; preds = %43
  %48 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str.1, ptr noundef @n, ptr noundef @l, ptr noundef @r)
  %49 = load i32, ptr @r, align 4, !tbaa !5
  %50 = load i32, ptr @l, align 4, !tbaa !5
  %51 = sub nsw i32 %49, %50
  %52 = add nsw i32 %51, 1
  %53 = load i32, ptr @n, align 4, !tbaa !5
  %54 = add nsw i32 %52, %53
  %55 = load i32, ptr @n, align 4, !tbaa !5
  %56 = call noundef i32 @_Z8mod_factii(i32 noundef %54, i32 noundef %55)
  %57 = sub nsw i32 %56, 1
  store i32 %57, ptr @ans, align 4, !tbaa !5
  %58 = load i32, ptr @ans, align 4, !tbaa !5
  %59 = icmp slt i32 %58, 0
  br i1 %59, label %60, label %63

60:                                               ; preds = %47
  %61 = load i32, ptr @ans, align 4, !tbaa !5
  %62 = add nsw i32 %61, 1000003
  store i32 %62, ptr @ans, align 4, !tbaa !5
  br label %63

63:                                               ; preds = %60, %47
  %64 = load i32, ptr @ans, align 4, !tbaa !5
  %65 = call i32 (ptr, ...) @printf(ptr noundef @.str.2, i32 noundef %64)
  br label %43, !llvm.loop !11

66:                                               ; preds = %43
  ret i32 0
}

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

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

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

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

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


; 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 [1000003 x i32], align 16
@inv = external dso_local local_unnamed_addr global [1000003 x i32], align 16
@t = external dso_local global i32, align 4
@n = external dso_local global i32, align 4
@l = external dso_local global i32, align 4
@r = external dso_local global i32, align 4
@ans = external dso_local local_unnamed_addr global i32, align 4
@.str = external hidden unnamed_addr constant [3 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [7 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [4 x i8], align 1

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

; Function Attrs: mustprogress norecurse uwtable
define dso_local noundef i32 @main() local_unnamed_addr #1 {
  store i32 1, ptr getelementptr inbounds (i8, ptr @inv, i64 4), align 4, !tbaa !5
  store i32 1, ptr getelementptr inbounds (i8, ptr @fact, i64 4), align 4, !tbaa !5
  store i32 1, ptr @fact, align 16, !tbaa !5
  br label %5

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

5:                                                ; preds = %0, %5
  %6 = phi i64 [ 1, %0 ], [ %8, %5 ]
  %indvars.iv = phi i64 [ 2, %0 ], [ %indvars.iv.next, %5 ]
  %7 = mul nsw i64 %6, %indvars.iv
  %8 = srem i64 %7, 1000003
  %9 = trunc nsw i64 %8 to i32
  %10 = getelementptr inbounds [1000003 x i32], ptr @fact, i64 0, i64 %indvars.iv
  store i32 %9, ptr %10, align 4, !tbaa !5
  %11 = trunc nuw nsw i64 %indvars.iv to i32
  %12 = udiv i32 1000003, %11
  %13 = zext nneg i32 %12 to i64
  %14 = urem i32 1000003, %11
  %15 = zext nneg i32 %14 to i64
  %16 = getelementptr inbounds [1000003 x i32], ptr @inv, i64 0, i64 %15
  %17 = load i32, ptr %16, align 4, !tbaa !5
  %18 = sext i32 %17 to i64
  %19 = mul nsw i64 %18, %13
  %20 = srem i64 %19, 1000003
  %21 = trunc nsw i64 %20 to i32
  %22 = sub nsw i32 1000003, %21
  %23 = getelementptr inbounds [1000003 x i32], ptr @inv, i64 0, i64 %indvars.iv
  store i32 %22, ptr %23, align 4, !tbaa !5
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, 1000003
  br i1 %exitcond.not, label %1, label %5, !llvm.loop !9

.lr.ph:                                           ; preds = %1, %.lr.ph
  %24 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str.1, ptr noundef nonnull @n, ptr noundef nonnull @l, ptr noundef nonnull @r)
  %25 = load i32, ptr @r, align 4, !tbaa !5
  %26 = load i32, ptr @l, align 4, !tbaa !5
  %27 = load i32, ptr @n, align 4, !tbaa !5
  %28 = add i32 %25, 1
  %29 = sub i32 %28, %26
  %30 = add nsw i32 %29, %27
  %31 = tail call noundef i32 @_Z8mod_factii(i32 noundef %30, i32 noundef %27)
  %32 = icmp slt i32 %31, 1
  %spec.select.v = select i1 %32, i32 1000002, i32 -1
  %spec.select = add nsw i32 %spec.select.v, %31
  store i32 %spec.select, ptr @ans, align 4, !tbaa !5
  %33 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.2, i32 noundef %spec.select)
  %34 = load i32, ptr @t, align 4, !tbaa !5
  %35 = add nsw i32 %34, -1
  store i32 %35, ptr @t, align 4, !tbaa !5
  %.not = icmp eq i32 %34, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !11

._crit_edge:                                      ; preds = %.lr.ph, %1
  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 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}


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