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

; 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 [1001 x i32], align 16
@.str = external hidden unnamed_addr constant [4 x i8], align 1

; Function Attrs: mustprogress norecurse uwtable
define dso_local noundef i32 @main() #0 {
  %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
  %6 = alloca i32, align 4
  store i32 0, ptr %1, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #4
  store i32 1, ptr getelementptr inbounds ([1001 x i32], ptr @f, i64 0, i64 1), align 4, !tbaa !5
  store i32 1, ptr %2, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #4
  %7 = call i32 @getchar()
  store i32 %7, ptr %3, align 4, !tbaa !5
  br label %8

8:                                                ; preds = %78, %0
  %9 = load i32, ptr %2, align 4, !tbaa !5
  %10 = icmp sgt i32 %9, 0
  br i1 %10, label %11, label %14

11:                                               ; preds = %8
  %12 = load i32, ptr %3, align 4, !tbaa !5
  %13 = icmp eq i32 %12, 40
  br i1 %13, label %17, label %14

14:                                               ; preds = %11, %8
  %15 = load i32, ptr %3, align 4, !tbaa !5
  %16 = icmp eq i32 %15, 41
  br label %17

17:                                               ; preds = %14, %11
  %18 = phi i1 [ true, %11 ], [ %16, %14 ]
  br i1 %18, label %20, label %19

19:                                               ; preds = %17
  store i32 2, ptr %4, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #4
  br label %80

20:                                               ; preds = %17
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #4
  %21 = load i32, ptr %3, align 4, !tbaa !5
  %22 = icmp eq i32 %21, 40
  %23 = zext i1 %22 to i64
  %24 = select i1 %22, i32 1, i32 -1
  store i32 %24, ptr %5, align 4, !tbaa !5
  %25 = load i32, ptr %5, align 4, !tbaa !5
  %26 = load i32, ptr %2, align 4, !tbaa !5
  %27 = add nsw i32 %26, %25
  store i32 %27, ptr %2, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #4
  %28 = load i32, ptr %5, align 4, !tbaa !5
  %29 = icmp slt i32 %28, 0
  br i1 %29, label %30, label %31

30:                                               ; preds = %20
  br label %33

31:                                               ; preds = %20
  %32 = load i32, ptr %2, align 4, !tbaa !5
  br label %33

33:                                               ; preds = %31, %30
  %34 = phi i32 [ 1, %30 ], [ %32, %31 ]
  store i32 %34, ptr %6, align 4, !tbaa !5
  br label %35

35:                                               ; preds = %69, %33
  %36 = load i32, ptr %6, align 4, !tbaa !5
  %37 = icmp sle i32 1, %36
  br i1 %37, label %38, label %42

38:                                               ; preds = %35
  %39 = load i32, ptr %6, align 4, !tbaa !5
  %40 = load i32, ptr %2, align 4, !tbaa !5
  %41 = icmp sle i32 %39, %40
  br label %42

42:                                               ; preds = %38, %35
  %43 = phi i1 [ false, %35 ], [ %41, %38 ]
  br i1 %43, label %45, label %44

44:                                               ; preds = %42
  store i32 5, ptr %4, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #4
  br label %73

45:                                               ; preds = %42
  %46 = load i32, ptr %6, align 4, !tbaa !5
  %47 = load i32, ptr %5, align 4, !tbaa !5
  %48 = sub nsw i32 %46, %47
  %49 = sext i32 %48 to i64
  %50 = getelementptr inbounds [1001 x i32], ptr @f, i64 0, i64 %49
  %51 = load i32, ptr %50, align 4, !tbaa !5
  %52 = load i32, ptr %6, align 4, !tbaa !5
  %53 = sext i32 %52 to i64
  %54 = getelementptr inbounds [1001 x i32], ptr @f, i64 0, i64 %53
  %55 = load i32, ptr %54, align 4, !tbaa !5
  %56 = add nsw i32 %55, %51
  store i32 %56, ptr %54, align 4, !tbaa !5
  %57 = load i32, ptr %6, align 4, !tbaa !5
  %58 = sext i32 %57 to i64
  %59 = getelementptr inbounds [1001 x i32], ptr @f, i64 0, i64 %58
  %60 = load i32, ptr %59, align 4, !tbaa !5
  %61 = icmp sge i32 %60, 2012
  br i1 %61, label %62, label %68

62:                                               ; preds = %45
  %63 = load i32, ptr %6, align 4, !tbaa !5
  %64 = sext i32 %63 to i64
  %65 = getelementptr inbounds [1001 x i32], ptr @f, i64 0, i64 %64
  %66 = load i32, ptr %65, align 4, !tbaa !5
  %67 = sub nsw i32 %66, 2012
  store i32 %67, ptr %65, align 4, !tbaa !5
  br label %68

68:                                               ; preds = %62, %45
  br label %69

69:                                               ; preds = %68
  %70 = load i32, ptr %5, align 4, !tbaa !5
  %71 = load i32, ptr %6, align 4, !tbaa !5
  %72 = sub nsw i32 %71, %70
  store i32 %72, ptr %6, align 4, !tbaa !5
  br label %35, !llvm.loop !9

73:                                               ; preds = %44
  %74 = load i32, ptr %2, align 4, !tbaa !5
  %75 = add nsw i32 %74, 1
  %76 = sext i32 %75 to i64
  %77 = getelementptr inbounds [1001 x i32], ptr @f, i64 0, i64 %76
  store i32 0, ptr %77, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #4
  br label %78

78:                                               ; preds = %73
  %79 = call i32 @getchar()
  store i32 %79, ptr %3, align 4, !tbaa !5
  br label %8, !llvm.loop !11

80:                                               ; preds = %19
  %81 = load i32, ptr %2, align 4, !tbaa !5
  %82 = icmp eq i32 %81, 1
  br i1 %82, label %83, label %85

83:                                               ; preds = %80
  %84 = load i32, ptr getelementptr inbounds ([1001 x i32], ptr @f, i64 0, i64 1), align 4, !tbaa !5
  br label %86

85:                                               ; preds = %80
  br label %86

86:                                               ; preds = %85, %83
  %87 = phi i32 [ %84, %83 ], [ 0, %85 ]
  %88 = call i32 (ptr, ...) @printf(ptr noundef @.str, i32 noundef %87)
  store i32 0, ptr %1, align 4
  store i32 1, ptr %4, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #4
  %89 = load i32, ptr %1, align 4
  ret i32 %89
}

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

; Function Attrs: inlinehint mustprogress uwtable
declare i32 @getchar() #2

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

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

attributes #0 = { 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 #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { inlinehint 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 #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"

@f = external dso_local local_unnamed_addr global [1001 x i32], align 16
@.str = external hidden unnamed_addr constant [4 x i8], align 1

; Function Attrs: mustprogress nofree norecurse nounwind uwtable
define dso_local noundef i32 @main() local_unnamed_addr #0 {
  store i32 1, ptr getelementptr inbounds (i8, ptr @f, i64 4), align 4, !tbaa !5
  %1 = tail call i32 @getchar()
  %2 = and i32 %1, -2
  %switch = icmp eq i32 %2, 40
  br i1 %switch, label %.critedge.preheader, label %._crit_edge30.thread

._crit_edge30.thread:                             ; preds = %0
  %3 = load i32, ptr getelementptr inbounds (i8, ptr @f, i64 4), align 4
  br label %7

.critedge.preheader:                              ; preds = %0
  %4 = icmp eq i32 %1, 40
  br label %.critedge

._crit_edge30:                                    ; preds = %._crit_edge
  %5 = icmp eq i32 %12, 1
  %6 = load i32, ptr getelementptr inbounds (i8, ptr @f, i64 4), align 4
  %spec.select33 = select i1 %5, i32 %6, i32 0
  br label %7

7:                                                ; preds = %._crit_edge30, %._crit_edge30.thread
  %8 = phi i32 [ %3, %._crit_edge30.thread ], [ %spec.select33, %._crit_edge30 ]
  %9 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str, i32 noundef %8)
  ret i32 0

.critedge:                                        ; preds = %.critedge.preheader, %._crit_edge
  %10 = phi i1 [ %22, %._crit_edge ], [ %4, %.critedge.preheader ]
  %.02529 = phi i32 [ %12, %._crit_edge ], [ 1, %.critedge.preheader ]
  %11 = select i1 %10, i32 1, i32 -1
  %12 = add i32 %11, %.02529
  %13 = select i1 %10, i32 %12, i32 1
  %14 = icmp sgt i32 %13, 0
  %15 = icmp sle i32 %13, %12
  %16 = select i1 %14, i1 %15, i1 false
  br i1 %16, label %.lr.ph, label %._crit_edge

._crit_edge:                                      ; preds = %.lr.ph, %.critedge
  %17 = add nsw i32 %12, 1
  %18 = sext i32 %17 to i64
  %19 = getelementptr inbounds [1001 x i32], ptr @f, i64 0, i64 %18
  store i32 0, ptr %19, align 4, !tbaa !5
  %20 = tail call i32 @getchar()
  %21 = icmp sgt i32 %12, 0
  %22 = icmp eq i32 %20, 40
  %or.cond = and i1 %21, %22
  %23 = icmp eq i32 %20, 41
  %or.cond3 = or i1 %23, %or.cond
  br i1 %or.cond3, label %.critedge, label %._crit_edge30, !llvm.loop !9

.lr.ph:                                           ; preds = %.critedge, %.lr.ph
  %.027 = phi i32 [ %24, %.lr.ph ], [ %13, %.critedge ]
  %24 = sub nsw i32 %.027, %11
  %25 = zext nneg i32 %24 to i64
  %26 = getelementptr inbounds [1001 x i32], ptr @f, i64 0, i64 %25
  %27 = load i32, ptr %26, align 4, !tbaa !5
  %28 = zext nneg i32 %.027 to i64
  %29 = getelementptr inbounds [1001 x i32], ptr @f, i64 0, i64 %28
  %30 = load i32, ptr %29, align 4, !tbaa !5
  %31 = add nsw i32 %30, %27
  %32 = icmp sgt i32 %31, 2011
  %33 = add nsw i32 %31, -2012
  %spec.select = select i1 %32, i32 %33, i32 %31
  store i32 %spec.select, ptr %29, align 4, !tbaa !5
  %34 = icmp sgt i32 %24, 0
  %35 = icmp sle i32 %24, %12
  %36 = select i1 %34, i1 %35, i1 false
  br i1 %36, label %.lr.ph, label %._crit_edge, !llvm.loop !11
}

; Function Attrs: inlinehint mustprogress nofree nounwind uwtable
declare noundef i32 @getchar() local_unnamed_addr #1

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

attributes #0 = { mustprogress nofree 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 #1 = { inlinehint mustprogress nofree 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 = !{!"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: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass