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

; 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
@f = external dso_local global [200010 x i32], align 16
@g = external dso_local global [200010 x i32], align 16
@sum = external dso_local global i64, align 8
@.str = external hidden unnamed_addr constant [3 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [6 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
  %7 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef @n)
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #4
  store i32 1, ptr %2, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #4
  br label %8

8:                                                ; preds = %19, %0
  %9 = load i32, ptr %2, align 4, !tbaa !5
  %10 = load i32, ptr @n, align 4, !tbaa !5
  %11 = icmp sle i32 %9, %10
  br i1 %11, label %13, label %12

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

13:                                               ; preds = %8
  %14 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef %3)
  %15 = load i32, ptr %2, align 4, !tbaa !5
  %16 = load i32, ptr %3, align 4, !tbaa !5
  %17 = sext i32 %16 to i64
  %18 = getelementptr inbounds [200010 x i32], ptr @f, i64 0, i64 %17
  store i32 %15, ptr %18, align 4, !tbaa !5
  br label %19

19:                                               ; preds = %13
  %20 = load i32, ptr %2, align 4, !tbaa !5
  %21 = add nsw i32 %20, 1
  store i32 %21, ptr %2, align 4, !tbaa !5
  br label %8, !llvm.loop !9

22:                                               ; preds = %12
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #4
  store i32 1, ptr %4, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #4
  br label %23

23:                                               ; preds = %34, %22
  %24 = load i32, ptr %4, align 4, !tbaa !5
  %25 = load i32, ptr @n, align 4, !tbaa !5
  %26 = icmp sle i32 %24, %25
  br i1 %26, label %28, label %27

27:                                               ; preds = %23
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #4
  br label %37

28:                                               ; preds = %23
  %29 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef %5)
  %30 = load i32, ptr %4, align 4, !tbaa !5
  %31 = load i32, ptr %5, align 4, !tbaa !5
  %32 = sext i32 %31 to i64
  %33 = getelementptr inbounds [200010 x i32], ptr @g, i64 0, i64 %32
  store i32 %30, ptr %33, align 4, !tbaa !5
  br label %34

34:                                               ; preds = %28
  %35 = load i32, ptr %4, align 4, !tbaa !5
  %36 = add nsw i32 %35, 1
  store i32 %36, ptr %4, align 4, !tbaa !5
  br label %23, !llvm.loop !11

37:                                               ; preds = %27
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #4
  %38 = load i32, ptr @n, align 4, !tbaa !5
  store i32 %38, ptr %6, align 4, !tbaa !5
  br label %39

39:                                               ; preds = %57, %37
  %40 = load i32, ptr %6, align 4, !tbaa !5
  %41 = icmp sge i32 %40, 1
  br i1 %41, label %43, label %42

42:                                               ; preds = %39
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #4
  br label %60

43:                                               ; preds = %39
  %44 = load i32, ptr %6, align 4, !tbaa !5
  %45 = sext i32 %44 to i64
  %46 = getelementptr inbounds [200010 x i32], ptr @f, i64 0, i64 %45
  %47 = load i32, ptr %46, align 4, !tbaa !5
  %48 = load i32, ptr %6, align 4, !tbaa !5
  %49 = sext i32 %48 to i64
  %50 = getelementptr inbounds [200010 x i32], ptr @g, i64 0, i64 %49
  %51 = load i32, ptr %50, align 4, !tbaa !5
  %52 = sub nsw i32 %47, %51
  %53 = call i32 @llvm.abs.i32(i32 %52, i1 true)
  %54 = sext i32 %53 to i64
  %55 = load i64, ptr @sum, align 8, !tbaa !12
  %56 = add nsw i64 %55, %54
  store i64 %56, ptr @sum, align 8, !tbaa !12
  br label %57

57:                                               ; preds = %43
  %58 = load i32, ptr %6, align 4, !tbaa !5
  %59 = add nsw i32 %58, -1
  store i32 %59, ptr %6, align 4, !tbaa !5
  br label %39, !llvm.loop !14

60:                                               ; preds = %42
  %61 = load i64, ptr @sum, align 8, !tbaa !12
  %62 = ashr i64 %61, 1
  %63 = call i32 (ptr, ...) @printf(ptr noundef @.str.1, i64 noundef %62)
  ret i32 0
}

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

; 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

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.abs.i32(i32, i1 immarg) #3

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

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 = { "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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
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 = !{!13, !13, i64 0}
!13 = !{!"long long", !7, i64 0}
!14 = distinct !{!14, !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
@f = external dso_local local_unnamed_addr global [200010 x i32], align 16
@g = external dso_local local_unnamed_addr global [200010 x i32], align 16
@sum = external dso_local local_unnamed_addr global i64, align 8
@.str = external hidden unnamed_addr constant [3 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [6 x i8], align 1

; Function Attrs: mustprogress nofree norecurse nounwind uwtable
define dso_local noundef i32 @main() local_unnamed_addr #0 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull @n)
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %1) #5
  %4 = load i32, ptr @n, align 4, !tbaa !5
  %.not13 = icmp slt i32 %4, 1
  br i1 %.not13, label %._crit_edge.thread, label %.lr.ph

._crit_edge.thread:                               ; preds = %0
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %1) #5
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %2) #5
  br label %._crit_edge19.thread

._crit_edge:                                      ; preds = %.lr.ph
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %1) #5
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %2) #5
  %.not1215 = icmp slt i32 %10, 1
  br i1 %.not1215, label %._crit_edge19.thread, label %.lr.ph18

.lr.ph:                                           ; preds = %0, %.lr.ph
  %.01114 = phi i32 [ %9, %.lr.ph ], [ 1, %0 ]
  %5 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull %1)
  %6 = load i32, ptr %1, align 4, !tbaa !5
  %7 = sext i32 %6 to i64
  %8 = getelementptr inbounds [200010 x i32], ptr @f, i64 0, i64 %7
  store i32 %.01114, ptr %8, align 4, !tbaa !5
  %9 = add nuw nsw i32 %.01114, 1
  %10 = load i32, ptr @n, align 4, !tbaa !5
  %.not.not = icmp slt i32 %.01114, %10
  br i1 %.not.not, label %.lr.ph, label %._crit_edge, !llvm.loop !9

._crit_edge19.thread:                             ; preds = %._crit_edge, %._crit_edge.thread
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %2) #5
  %sum.promoted30 = load i64, ptr @sum, align 8, !tbaa !11
  br label %38

._crit_edge19:                                    ; preds = %.lr.ph18
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %2) #5
  %sum.promoted = load i64, ptr @sum, align 8, !tbaa !11
  %11 = icmp sgt i32 %37, 0
  br i1 %11, label %.lr.ph22.preheader, label %38

.lr.ph22.preheader:                               ; preds = %._crit_edge19
  %12 = zext nneg i32 %37 to i64
  %min.iters.check = icmp ult i32 %37, 4
  br i1 %min.iters.check, label %.lr.ph22.preheader42, label %vector.ph

.lr.ph22.preheader42:                             ; preds = %middle.block, %.lr.ph22.preheader
  %indvars.iv.ph = phi i64 [ %12, %.lr.ph22.preheader ], [ %ind.end, %middle.block ]
  %.ph = phi i64 [ %sum.promoted, %.lr.ph22.preheader ], [ %31, %middle.block ]
  br label %.lr.ph22

vector.ph:                                        ; preds = %.lr.ph22.preheader
  %n.vec = and i64 %12, 2147483644
  %ind.end = and i64 %12, 3
  %13 = insertelement <2 x i64> <i64 poison, i64 0>, i64 %sum.promoted, i64 0
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %vec.phi = phi <2 x i64> [ %13, %vector.ph ], [ %28, %vector.body ]
  %vec.phi35 = phi <2 x i64> [ zeroinitializer, %vector.ph ], [ %29, %vector.body ]
  %offset.idx = sub i64 %12, %index
  %14 = getelementptr inbounds [200010 x i32], ptr @f, i64 0, i64 %offset.idx
  %15 = getelementptr inbounds i8, ptr %14, i64 -4
  %16 = getelementptr inbounds i8, ptr %14, i64 -12
  %wide.load = load <2 x i32>, ptr %15, align 4, !tbaa !5
  %wide.load36 = load <2 x i32>, ptr %16, align 4, !tbaa !5
  %17 = getelementptr inbounds [200010 x i32], ptr @g, i64 0, i64 %offset.idx
  %18 = getelementptr inbounds i8, ptr %17, i64 -4
  %19 = getelementptr inbounds i8, ptr %17, i64 -12
  %wide.load38 = load <2 x i32>, ptr %18, align 4, !tbaa !5
  %wide.load40 = load <2 x i32>, ptr %19, align 4, !tbaa !5
  %20 = sub nsw <2 x i32> %wide.load, %wide.load38
  %21 = shufflevector <2 x i32> %20, <2 x i32> poison, <2 x i32> <i32 1, i32 0>
  %22 = sub nsw <2 x i32> %wide.load36, %wide.load40
  %23 = shufflevector <2 x i32> %22, <2 x i32> poison, <2 x i32> <i32 1, i32 0>
  %24 = call <2 x i32> @llvm.abs.v2i32(<2 x i32> %21, i1 true)
  %25 = call <2 x i32> @llvm.abs.v2i32(<2 x i32> %23, i1 true)
  %26 = zext nneg <2 x i32> %24 to <2 x i64>
  %27 = zext nneg <2 x i32> %25 to <2 x i64>
  %28 = add <2 x i64> %vec.phi, %26
  %29 = add <2 x i64> %vec.phi35, %27
  %index.next = add nuw i64 %index, 4
  %30 = icmp eq i64 %index.next, %n.vec
  br i1 %30, label %middle.block, label %vector.body, !llvm.loop !13

middle.block:                                     ; preds = %vector.body
  %bin.rdx = add <2 x i64> %29, %28
  %31 = call i64 @llvm.vector.reduce.add.v2i64(<2 x i64> %bin.rdx)
  %cmp.n = icmp eq i64 %n.vec, %12
  br i1 %cmp.n, label %._crit_edge23, label %.lr.ph22.preheader42

.lr.ph18:                                         ; preds = %._crit_edge, %.lr.ph18
  %.01016 = phi i32 [ %36, %.lr.ph18 ], [ 1, %._crit_edge ]
  %32 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull %2)
  %33 = load i32, ptr %2, align 4, !tbaa !5
  %34 = sext i32 %33 to i64
  %35 = getelementptr inbounds [200010 x i32], ptr @g, i64 0, i64 %34
  store i32 %.01016, ptr %35, align 4, !tbaa !5
  %36 = add nuw nsw i32 %.01016, 1
  %37 = load i32, ptr @n, align 4, !tbaa !5
  %.not12.not = icmp slt i32 %.01016, %37
  br i1 %.not12.not, label %.lr.ph18, label %._crit_edge19, !llvm.loop !16

._crit_edge23:                                    ; preds = %.lr.ph22, %middle.block
  %.lcssa = phi i64 [ %31, %middle.block ], [ %50, %.lr.ph22 ]
  store i64 %.lcssa, ptr @sum, align 8, !tbaa !11
  br label %38

38:                                               ; preds = %._crit_edge19.thread, %._crit_edge23, %._crit_edge19
  %39 = phi i64 [ %.lcssa, %._crit_edge23 ], [ %sum.promoted, %._crit_edge19 ], [ %sum.promoted30, %._crit_edge19.thread ]
  %40 = ashr i64 %39, 1
  %41 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1, i64 noundef %40)
  ret i32 0

.lr.ph22:                                         ; preds = %.lr.ph22.preheader42, %.lr.ph22
  %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph22 ], [ %indvars.iv.ph, %.lr.ph22.preheader42 ]
  %42 = phi i64 [ %50, %.lr.ph22 ], [ %.ph, %.lr.ph22.preheader42 ]
  %43 = getelementptr inbounds [200010 x i32], ptr @f, i64 0, i64 %indvars.iv
  %44 = load i32, ptr %43, align 4, !tbaa !5
  %45 = getelementptr inbounds [200010 x i32], ptr @g, i64 0, i64 %indvars.iv
  %46 = load i32, ptr %45, align 4, !tbaa !5
  %47 = sub nsw i32 %44, %46
  %48 = call i32 @llvm.abs.i32(i32 %47, i1 true)
  %49 = zext nneg i32 %48 to i64
  %50 = add nsw i64 %42, %49
  %indvars.iv.next = add nsw i64 %indvars.iv, -1
  %51 = icmp ugt i64 %indvars.iv, 1
  br i1 %51, label %.lr.ph22, label %._crit_edge23, !llvm.loop !17
}

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

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

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

; Function Attrs: mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.abs.i32(i32, i1 immarg) #3

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

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare <2 x i32> @llvm.abs.v2i32(<2 x i32>, i1 immarg) #4

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.vector.reduce.add.v2i64(<2 x i64>) #4

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 = { 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" }
attributes #2 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #4 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #5 = { 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 = !{!12, !12, i64 0}
!12 = !{!"long long", !7, i64 0}
!13 = distinct !{!13, !10, !14, !15}
!14 = !{!"llvm.loop.isvectorized", i32 1}
!15 = !{!"llvm.loop.unroll.runtime.disable"}
!16 = distinct !{!16, !10}
!17 = distinct !{!17, !10, !15, !14}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopLoadEliminationPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,LoopVectorizePass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass