; LLVM IR from tangjz/acm-icpc :: 51nod/1857.ll (Z4calcv)

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

@e = external dso_local global [403 x [403 x i32]], align 16
@m = external dso_local global i32, align 4
@ord = external dso_local global [403 x i32], align 16
@match = external dso_local global [403 x i32], align 16

; 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
define dso_local noundef i64 @_Z4calcv() #1 {
  %1 = alloca i64, align 8
  %2 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 8, ptr %1) #2
  store i64 0, ptr %1, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #2
  store i32 0, ptr %2, align 4, !tbaa !9
  br label %3

3:                                                ; preds = %42, %0
  %4 = load i32, ptr %2, align 4, !tbaa !9
  %5 = load i32, ptr @m, align 4, !tbaa !9
  %6 = icmp slt i32 %4, %5
  br i1 %6, label %8, label %7

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

8:                                                ; preds = %3
  %9 = load i32, ptr %2, align 4, !tbaa !9
  %10 = sext i32 %9 to i64
  %11 = getelementptr inbounds [403 x i32], ptr @ord, i64 0, i64 %10
  %12 = load i32, ptr %11, align 4, !tbaa !9
  %13 = load i32, ptr %2, align 4, !tbaa !9
  %14 = sext i32 %13 to i64
  %15 = getelementptr inbounds [403 x i32], ptr @ord, i64 0, i64 %14
  %16 = load i32, ptr %15, align 4, !tbaa !9
  %17 = sext i32 %16 to i64
  %18 = getelementptr inbounds [403 x i32], ptr @match, i64 0, i64 %17
  %19 = load i32, ptr %18, align 4, !tbaa !9
  %20 = icmp slt i32 %12, %19
  br i1 %20, label %21, label %41

21:                                               ; preds = %8
  %22 = load i32, ptr %2, align 4, !tbaa !9
  %23 = sext i32 %22 to i64
  %24 = getelementptr inbounds [403 x i32], ptr @ord, i64 0, i64 %23
  %25 = load i32, ptr %24, align 4, !tbaa !9
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds [403 x [403 x i32]], ptr @e, i64 0, i64 %26
  %28 = load i32, ptr %2, align 4, !tbaa !9
  %29 = sext i32 %28 to i64
  %30 = getelementptr inbounds [403 x i32], ptr @ord, i64 0, i64 %29
  %31 = load i32, ptr %30, align 4, !tbaa !9
  %32 = sext i32 %31 to i64
  %33 = getelementptr inbounds [403 x i32], ptr @match, i64 0, i64 %32
  %34 = load i32, ptr %33, align 4, !tbaa !9
  %35 = sext i32 %34 to i64
  %36 = getelementptr inbounds [403 x i32], ptr %27, i64 0, i64 %35
  %37 = load i32, ptr %36, align 4, !tbaa !9
  %38 = sext i32 %37 to i64
  %39 = load i64, ptr %1, align 8, !tbaa !5
  %40 = add nsw i64 %39, %38
  store i64 %40, ptr %1, align 8, !tbaa !5
  br label %41

41:                                               ; preds = %21, %8
  br label %42

42:                                               ; preds = %41
  %43 = load i32, ptr %2, align 4, !tbaa !9
  %44 = add nsw i32 %43, 1
  store i32 %44, ptr %2, align 4, !tbaa !9
  br label %3, !llvm.loop !11

45:                                               ; preds = %7
  %46 = load i64, ptr %1, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %1) #2
  ret i64 %46
}

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 = { 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"}


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

@e = external dso_local local_unnamed_addr global [403 x [403 x i32]], align 16
@m = external dso_local local_unnamed_addr global i32, align 4
@ord = external dso_local local_unnamed_addr global [403 x i32], align 16
@match = external dso_local local_unnamed_addr global [403 x i32], align 16

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(read, argmem: none, inaccessiblemem: none) uwtable
define dso_local noundef i64 @_Z4calcv() local_unnamed_addr #0 {
  %1 = load i32, ptr @m, align 4, !tbaa !5
  %2 = icmp sgt i32 %1, 0
  br i1 %2, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %0
  %wide.trip.count = zext nneg i32 %1 to i64
  %xtraiter = and i64 %wide.trip.count, 1
  %3 = icmp eq i32 %1, 1
  br i1 %3, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph.preheader.new

.lr.ph.preheader.new:                             ; preds = %.lr.ph.preheader
  %unroll_iter = and i64 %wide.trip.count, 2147483646
  br label %.lr.ph

._crit_edge.loopexit.unr-lcssa:                   ; preds = %40, %.lr.ph.preheader
  %.1.lcssa.ph = phi i64 [ poison, %.lr.ph.preheader ], [ %.1.1, %40 ]
  %indvars.iv.unr = phi i64 [ 0, %.lr.ph.preheader ], [ %indvars.iv.next.1, %40 ]
  %.0810.unr = phi i64 [ 0, %.lr.ph.preheader ], [ %.1.1, %40 ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge, label %.lr.ph.epil

.lr.ph.epil:                                      ; preds = %._crit_edge.loopexit.unr-lcssa
  %4 = getelementptr inbounds [403 x i32], ptr @ord, i64 0, i64 %indvars.iv.unr
  %5 = load i32, ptr %4, align 4, !tbaa !5
  %6 = sext i32 %5 to i64
  %7 = getelementptr inbounds [403 x i32], ptr @match, i64 0, i64 %6
  %8 = load i32, ptr %7, align 4, !tbaa !5
  %9 = icmp slt i32 %5, %8
  br i1 %9, label %10, label %._crit_edge

10:                                               ; preds = %.lr.ph.epil
  %11 = sext i32 %8 to i64
  %12 = getelementptr inbounds [403 x [403 x i32]], ptr @e, i64 0, i64 %6, i64 %11
  %13 = load i32, ptr %12, align 4, !tbaa !5
  %14 = sext i32 %13 to i64
  %15 = add nsw i64 %.0810.unr, %14
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit.unr-lcssa, %10, %.lr.ph.epil, %0
  %.08.lcssa = phi i64 [ 0, %0 ], [ %.1.lcssa.ph, %._crit_edge.loopexit.unr-lcssa ], [ %15, %10 ], [ %.0810.unr, %.lr.ph.epil ]
  ret i64 %.08.lcssa

.lr.ph:                                           ; preds = %40, %.lr.ph.preheader.new
  %indvars.iv = phi i64 [ 0, %.lr.ph.preheader.new ], [ %indvars.iv.next.1, %40 ]
  %.0810 = phi i64 [ 0, %.lr.ph.preheader.new ], [ %.1.1, %40 ]
  %niter = phi i64 [ 0, %.lr.ph.preheader.new ], [ %niter.next.1, %40 ]
  %16 = getelementptr inbounds [403 x i32], ptr @ord, i64 0, i64 %indvars.iv
  %17 = load i32, ptr %16, align 8, !tbaa !5
  %18 = sext i32 %17 to i64
  %19 = getelementptr inbounds [403 x i32], ptr @match, i64 0, i64 %18
  %20 = load i32, ptr %19, align 4, !tbaa !5
  %21 = icmp slt i32 %17, %20
  br i1 %21, label %22, label %.lr.ph.1

22:                                               ; preds = %.lr.ph
  %23 = sext i32 %20 to i64
  %24 = getelementptr inbounds [403 x [403 x i32]], ptr @e, i64 0, i64 %18, i64 %23
  %25 = load i32, ptr %24, align 4, !tbaa !5
  %26 = sext i32 %25 to i64
  %27 = add nsw i64 %.0810, %26
  br label %.lr.ph.1

.lr.ph.1:                                         ; preds = %.lr.ph, %22
  %.1 = phi i64 [ %27, %22 ], [ %.0810, %.lr.ph ]
  %indvars.iv.next = or disjoint i64 %indvars.iv, 1
  %28 = getelementptr inbounds [403 x i32], ptr @ord, i64 0, i64 %indvars.iv.next
  %29 = load i32, ptr %28, align 4, !tbaa !5
  %30 = sext i32 %29 to i64
  %31 = getelementptr inbounds [403 x i32], ptr @match, i64 0, i64 %30
  %32 = load i32, ptr %31, align 4, !tbaa !5
  %33 = icmp slt i32 %29, %32
  br i1 %33, label %34, label %40

34:                                               ; preds = %.lr.ph.1
  %35 = sext i32 %32 to i64
  %36 = getelementptr inbounds [403 x [403 x i32]], ptr @e, i64 0, i64 %30, i64 %35
  %37 = load i32, ptr %36, align 4, !tbaa !5
  %38 = sext i32 %37 to i64
  %39 = add nsw i64 %.1, %38
  br label %40

40:                                               ; preds = %34, %.lr.ph.1
  %.1.1 = phi i64 [ %39, %34 ], [ %.1, %.lr.ph.1 ]
  %indvars.iv.next.1 = add nuw nsw i64 %indvars.iv, 2
  %niter.next.1 = add i64 %niter, 2
  %niter.ncmp.1 = icmp eq i64 %niter.next.1, %unroll_iter
  br i1 %niter.ncmp.1, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph, !llvm.loop !9
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(read, argmem: none, inaccessiblemem: none) 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" }

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


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