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

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

@size = external dso_local global [3 x i32], align 4
@dist1 = external dso_local global [1000001 x i64], align 16
@val1 = external dso_local global [1000001 x i64], 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 uwtable
declare dso_local void @_Z4pfs2i(i32 noundef) #1

; Function Attrs: mustprogress uwtable
declare dso_local void @_Z4dfs2i(i32 noundef) #1

; Function Attrs: mustprogress uwtable
define dso_local noundef i64 @_Z5checkv() #1 {
  %1 = alloca i64, align 8
  %2 = alloca i32, align 4
  call void @_Z4pfs2i(i32 noundef 0)
  call void @_Z4dfs2i(i32 noundef 0)
  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 = %43, %0
  %4 = load i32, ptr %2, align 4, !tbaa !9
  %5 = load i32, ptr getelementptr inbounds ([3 x i32], ptr @size, i64 0, i64 1), 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 %46

8:                                                ; preds = %3
  %9 = load i64, ptr %1, align 8, !tbaa !5
  %10 = load i32, ptr @size, align 4, !tbaa !9
  %11 = sext i32 %10 to i64
  %12 = load i32, ptr %2, align 4, !tbaa !9
  %13 = sext i32 %12 to i64
  %14 = getelementptr inbounds [1000001 x i64], ptr @dist1, i64 0, i64 %13
  %15 = load i64, ptr %14, align 8, !tbaa !5
  %16 = mul nsw i64 %11, %15
  %17 = load i32, ptr getelementptr inbounds ([3 x i32], ptr @size, i64 0, i64 2), align 4, !tbaa !9
  %18 = sext i32 %17 to i64
  %19 = load i32, ptr %2, align 4, !tbaa !9
  %20 = sext i32 %19 to i64
  %21 = getelementptr inbounds [1000001 x i64], ptr @val1, i64 0, i64 %20
  %22 = load i64, ptr %21, align 8, !tbaa !5
  %23 = mul nsw i64 %18, %22
  %24 = add nsw i64 %16, %23
  %25 = icmp slt i64 %9, %24
  br i1 %25, label %26, label %42

26:                                               ; preds = %8
  %27 = load i32, ptr @size, align 4, !tbaa !9
  %28 = sext i32 %27 to i64
  %29 = load i32, ptr %2, align 4, !tbaa !9
  %30 = sext i32 %29 to i64
  %31 = getelementptr inbounds [1000001 x i64], ptr @dist1, i64 0, i64 %30
  %32 = load i64, ptr %31, align 8, !tbaa !5
  %33 = mul nsw i64 %28, %32
  %34 = load i32, ptr getelementptr inbounds ([3 x i32], ptr @size, i64 0, i64 2), align 4, !tbaa !9
  %35 = sext i32 %34 to i64
  %36 = load i32, ptr %2, align 4, !tbaa !9
  %37 = sext i32 %36 to i64
  %38 = getelementptr inbounds [1000001 x i64], ptr @val1, i64 0, i64 %37
  %39 = load i64, ptr %38, align 8, !tbaa !5
  %40 = mul nsw i64 %35, %39
  %41 = add nsw i64 %33, %40
  store i64 %41, ptr %1, align 8, !tbaa !5
  br label %42

42:                                               ; preds = %26, %8
  br label %43

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

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

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

@size = external dso_local local_unnamed_addr global [3 x i32], align 4
@dist1 = external dso_local local_unnamed_addr global [1000001 x i64], align 16
@val1 = external dso_local local_unnamed_addr global [1000001 x i64], align 16

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

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

; Function Attrs: mustprogress uwtable
define dso_local noundef range(i64 0, -9223372036854775808) i64 @_Z5checkv() local_unnamed_addr #0 {
  tail call void @_Z4pfs2i(i32 noundef 0)
  tail call void @_Z4dfs2i(i32 noundef 0)
  %1 = load i32, ptr getelementptr inbounds (i8, ptr @size, i64 4), align 4, !tbaa !5
  %2 = icmp sgt i32 %1, 0
  br i1 %2, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %0
  %3 = load i32, ptr @size, align 4, !tbaa !5
  %4 = sext i32 %3 to i64
  %5 = load i32, ptr getelementptr inbounds (i8, ptr @size, i64 8), align 4, !tbaa !5
  %6 = sext i32 %5 to i64
  %wide.trip.count = zext nneg i32 %1 to i64
  %xtraiter = and i64 %wide.trip.count, 1
  %7 = icmp eq i32 %1, 1
  br i1 %7, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph.new

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

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

._crit_edge.loopexit.epilog-lcssa:                ; preds = %._crit_edge.loopexit.unr-lcssa
  %8 = getelementptr inbounds [1000001 x i64], ptr @dist1, i64 0, i64 %indvars.iv.unr
  %9 = load i64, ptr %8, align 8, !tbaa !9
  %10 = mul nsw i64 %9, %4
  %11 = getelementptr inbounds [1000001 x i64], ptr @val1, i64 0, i64 %indvars.iv.unr
  %12 = load i64, ptr %11, align 8, !tbaa !9
  %13 = mul nsw i64 %12, %6
  %14 = add nsw i64 %13, %10
  %spec.select.epil = tail call i64 @llvm.smax.i64(i64 %.0810.unr, i64 %14)
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit.epilog-lcssa, %._crit_edge.loopexit.unr-lcssa, %0
  %.08.lcssa = phi i64 [ 0, %0 ], [ %spec.select.lcssa.ph, %._crit_edge.loopexit.unr-lcssa ], [ %spec.select.epil, %._crit_edge.loopexit.epilog-lcssa ]
  ret i64 %.08.lcssa

15:                                               ; preds = %15, %.lr.ph.new
  %indvars.iv = phi i64 [ 0, %.lr.ph.new ], [ %indvars.iv.next.1, %15 ]
  %.0810 = phi i64 [ 0, %.lr.ph.new ], [ %spec.select.1, %15 ]
  %niter = phi i64 [ 0, %.lr.ph.new ], [ %niter.next.1, %15 ]
  %16 = getelementptr inbounds [1000001 x i64], ptr @dist1, i64 0, i64 %indvars.iv
  %17 = load i64, ptr %16, align 16, !tbaa !9
  %18 = mul nsw i64 %17, %4
  %19 = getelementptr inbounds [1000001 x i64], ptr @val1, i64 0, i64 %indvars.iv
  %20 = load i64, ptr %19, align 16, !tbaa !9
  %21 = mul nsw i64 %20, %6
  %22 = add nsw i64 %21, %18
  %spec.select = tail call i64 @llvm.smax.i64(i64 %.0810, i64 %22)
  %indvars.iv.next = or disjoint i64 %indvars.iv, 1
  %23 = getelementptr inbounds [1000001 x i64], ptr @dist1, i64 0, i64 %indvars.iv.next
  %24 = load i64, ptr %23, align 8, !tbaa !9
  %25 = mul nsw i64 %24, %4
  %26 = getelementptr inbounds [1000001 x i64], ptr @val1, i64 0, i64 %indvars.iv.next
  %27 = load i64, ptr %26, align 8, !tbaa !9
  %28 = mul nsw i64 %27, %6
  %29 = add nsw i64 %28, %25
  %spec.select.1 = tail call i64 @llvm.smax.i64(i64 %spec.select, i64 %29)
  %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 %15, !llvm.loop !11
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.smax.i64(i64, i64) #1

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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }

!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 = !{!10, !10, i64 0}
!10 = !{!"long long", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}


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