; LLVM IR from VastoLorde95/Competitive-Programming :: SPOJ/Random/EKO/eko.ll (Z5checkx)

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

@trees = external dso_local global [1000000 x i32], align 16
@n = external dso_local global i64, align 8

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i64 @_Z5checkx(i64 noundef %0) #0 {
  %2 = alloca i64, align 8
  %3 = alloca i64, align 8
  %4 = alloca i64, align 8
  store i64 %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #2
  store i64 0, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #2
  store i64 0, ptr %4, align 8, !tbaa !5
  br label %5

5:                                                ; preds = %27, %1
  %6 = load i64, ptr %4, align 8, !tbaa !5
  %7 = load i64, ptr @n, align 8, !tbaa !5
  %8 = icmp slt i64 %6, %7
  br i1 %8, label %10, label %9

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

10:                                               ; preds = %5
  %11 = load i64, ptr %4, align 8, !tbaa !5
  %12 = getelementptr inbounds [1000000 x i32], ptr @trees, i64 0, i64 %11
  %13 = load i32, ptr %12, align 4, !tbaa !9
  %14 = sext i32 %13 to i64
  %15 = load i64, ptr %2, align 8, !tbaa !5
  %16 = icmp sgt i64 %14, %15
  br i1 %16, label %17, label %26

17:                                               ; preds = %10
  %18 = load i64, ptr %4, align 8, !tbaa !5
  %19 = getelementptr inbounds [1000000 x i32], ptr @trees, i64 0, i64 %18
  %20 = load i32, ptr %19, align 4, !tbaa !9
  %21 = sext i32 %20 to i64
  %22 = load i64, ptr %2, align 8, !tbaa !5
  %23 = sub nsw i64 %21, %22
  %24 = load i64, ptr %3, align 8, !tbaa !5
  %25 = add nsw i64 %24, %23
  store i64 %25, ptr %3, align 8, !tbaa !5
  br label %26

26:                                               ; preds = %17, %10
  br label %27

27:                                               ; preds = %26
  %28 = load i64, ptr %4, align 8, !tbaa !5
  %29 = add nsw i64 %28, 1
  store i64 %29, ptr %4, align 8, !tbaa !5
  br label %5, !llvm.loop !11

30:                                               ; preds = %9
  %31 = load i64, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #2
  ret i64 %31
}

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

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

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

@trees = external dso_local local_unnamed_addr global [1000000 x i32], align 16
@n = external dso_local local_unnamed_addr global i64, align 8

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

.lr.ph.preheader:                                 ; preds = %1
  %xtraiter = and i64 %2, 3
  %4 = icmp ult i64 %2, 4
  br i1 %4, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph.preheader.new

.lr.ph.preheader.new:                             ; preds = %.lr.ph.preheader
  %unroll_iter = and i64 %2, 9223372036854775804
  br label %.lr.ph

._crit_edge.loopexit.unr-lcssa:                   ; preds = %.lr.ph, %.lr.ph.preheader
  %.1.lcssa.ph = phi i64 [ poison, %.lr.ph.preheader ], [ %.1.3, %.lr.ph ]
  %.012.unr = phi i64 [ 0, %.lr.ph.preheader ], [ %39, %.lr.ph ]
  %.0811.unr = phi i64 [ 0, %.lr.ph.preheader ], [ %.1.3, %.lr.ph ]
  %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, %.lr.ph.epil
  %.012.epil = phi i64 [ %11, %.lr.ph.epil ], [ %.012.unr, %._crit_edge.loopexit.unr-lcssa ]
  %.0811.epil = phi i64 [ %.1.epil, %.lr.ph.epil ], [ %.0811.unr, %._crit_edge.loopexit.unr-lcssa ]
  %epil.iter = phi i64 [ %epil.iter.next, %.lr.ph.epil ], [ 0, %._crit_edge.loopexit.unr-lcssa ]
  %5 = getelementptr inbounds [1000000 x i32], ptr @trees, i64 0, i64 %.012.epil
  %6 = load i32, ptr %5, align 4, !tbaa !9
  %7 = sext i32 %6 to i64
  %8 = icmp sgt i64 %7, %0
  %9 = sub nsw i64 %7, %0
  %10 = select i1 %8, i64 %9, i64 0
  %.1.epil = add nsw i64 %10, %.0811.epil
  %11 = add nuw nsw i64 %.012.epil, 1
  %epil.iter.next = add i64 %epil.iter, 1
  %epil.iter.cmp.not = icmp eq i64 %epil.iter.next, %xtraiter
  br i1 %epil.iter.cmp.not, label %._crit_edge, label %.lr.ph.epil, !llvm.loop !11

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

.lr.ph:                                           ; preds = %.lr.ph, %.lr.ph.preheader.new
  %.012 = phi i64 [ 0, %.lr.ph.preheader.new ], [ %39, %.lr.ph ]
  %.0811 = phi i64 [ 0, %.lr.ph.preheader.new ], [ %.1.3, %.lr.ph ]
  %niter = phi i64 [ 0, %.lr.ph.preheader.new ], [ %niter.next.3, %.lr.ph ]
  %12 = getelementptr inbounds [1000000 x i32], ptr @trees, i64 0, i64 %.012
  %13 = load i32, ptr %12, align 16, !tbaa !9
  %14 = sext i32 %13 to i64
  %15 = icmp sgt i64 %14, %0
  %16 = sub nsw i64 %14, %0
  %17 = select i1 %15, i64 %16, i64 0
  %.1 = add nsw i64 %17, %.0811
  %18 = or disjoint i64 %.012, 1
  %19 = getelementptr inbounds [1000000 x i32], ptr @trees, i64 0, i64 %18
  %20 = load i32, ptr %19, align 4, !tbaa !9
  %21 = sext i32 %20 to i64
  %22 = icmp sgt i64 %21, %0
  %23 = sub nsw i64 %21, %0
  %24 = select i1 %22, i64 %23, i64 0
  %.1.1 = add nsw i64 %24, %.1
  %25 = or disjoint i64 %.012, 2
  %26 = getelementptr inbounds [1000000 x i32], ptr @trees, i64 0, i64 %25
  %27 = load i32, ptr %26, align 8, !tbaa !9
  %28 = sext i32 %27 to i64
  %29 = icmp sgt i64 %28, %0
  %30 = sub nsw i64 %28, %0
  %31 = select i1 %29, i64 %30, i64 0
  %.1.2 = add nsw i64 %31, %.1.1
  %32 = or disjoint i64 %.012, 3
  %33 = getelementptr inbounds [1000000 x i32], ptr @trees, i64 0, i64 %32
  %34 = load i32, ptr %33, align 4, !tbaa !9
  %35 = sext i32 %34 to i64
  %36 = icmp sgt i64 %35, %0
  %37 = sub nsw i64 %35, %0
  %38 = select i1 %36, i64 %37, i64 0
  %.1.3 = add nsw i64 %38, %.1.2
  %39 = add nuw nsw i64 %.012, 4
  %niter.next.3 = add i64 %niter, 4
  %niter.ncmp.3 = icmp eq i64 %niter.next.3, %unroll_iter
  br i1 %niter.ncmp.3, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph, !llvm.loop !13
}

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 = !{!"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.unroll.disable"}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}


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