; LLVM IR from ncduy0303/Competitive-Programming :: Graphs/Lowest Common Ancestor/LCA (RMQ).ll (Z18build_sparse_tablev)

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

%"struct.std::array" = type { [2 x i32] }

@n = external dso_local global i32, align 4
@dep = external dso_local global [400002 x i32], align 16
@euler = external dso_local global [400002 x i32], align 16
@dp = external dso_local global [400002 x [30 x %"struct.std::array"]], 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
define dso_local void @_Z18build_sparse_tablev() #1 {
  %1 = alloca i32, align 4
  %2 = alloca %"struct.std::array", align 4
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr %1) #4
  store i32 1, ptr %1, align 4, !tbaa !5
  br label %6

6:                                                ; preds = %27, %0
  %7 = load i32, ptr %1, align 4, !tbaa !5
  %8 = load i32, ptr @n, align 4, !tbaa !5
  %9 = mul nsw i32 2, %8
  %10 = icmp sle i32 %7, %9
  br i1 %10, label %12, label %11

11:                                               ; preds = %6
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #4
  br label %30

12:                                               ; preds = %6
  call void @llvm.lifetime.start.p0(i64 8, ptr %2) #4
  %13 = getelementptr inbounds %"struct.std::array", ptr %2, i32 0, i32 0
  %14 = load i32, ptr %1, align 4, !tbaa !5
  %15 = sext i32 %14 to i64
  %16 = getelementptr inbounds [400002 x i32], ptr @dep, i64 0, i64 %15
  %17 = load i32, ptr %16, align 4, !tbaa !5
  store i32 %17, ptr %13, align 4, !tbaa !5
  %18 = getelementptr inbounds i32, ptr %13, i64 1
  %19 = load i32, ptr %1, align 4, !tbaa !5
  %20 = sext i32 %19 to i64
  %21 = getelementptr inbounds [400002 x i32], ptr @euler, i64 0, i64 %20
  %22 = load i32, ptr %21, align 4, !tbaa !5
  store i32 %22, ptr %18, align 4, !tbaa !5
  %23 = load i32, ptr %1, align 4, !tbaa !5
  %24 = sext i32 %23 to i64
  %25 = getelementptr inbounds [400002 x [30 x %"struct.std::array"]], ptr @dp, i64 0, i64 %24
  %26 = getelementptr inbounds [30 x %"struct.std::array"], ptr %25, i64 0, i64 0
  call void @llvm.memcpy.p0.p0.i64(ptr align 16 %26, ptr align 4 %2, i64 8, i1 false), !tbaa.struct !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %2) #4
  br label %27

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

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

31:                                               ; preds = %76, %30
  %32 = load i32, ptr %3, align 4, !tbaa !5
  %33 = icmp slt i32 %32, 30
  br i1 %33, label %35, label %34

34:                                               ; preds = %31
  store i32 5, ptr %4, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #4
  br label %79

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

36:                                               ; preds = %72, %35
  %37 = load i32, ptr %5, align 4, !tbaa !5
  %38 = load i32, ptr %3, align 4, !tbaa !5
  %39 = shl i32 1, %38
  %40 = add nsw i32 %37, %39
  %41 = load i32, ptr @n, align 4, !tbaa !5
  %42 = add nsw i32 %41, 1
  %43 = mul nsw i32 2, %42
  %44 = icmp sle i32 %40, %43
  br i1 %44, label %46, label %45

45:                                               ; preds = %36
  store i32 8, ptr %4, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #4
  br label %75

46:                                               ; preds = %36
  %47 = load i32, ptr %5, align 4, !tbaa !5
  %48 = sext i32 %47 to i64
  %49 = getelementptr inbounds [400002 x [30 x %"struct.std::array"]], ptr @dp, i64 0, i64 %48
  %50 = load i32, ptr %3, align 4, !tbaa !5
  %51 = sub nsw i32 %50, 1
  %52 = sext i32 %51 to i64
  %53 = getelementptr inbounds [30 x %"struct.std::array"], ptr %49, i64 0, i64 %52
  %54 = load i32, ptr %5, align 4, !tbaa !5
  %55 = load i32, ptr %3, align 4, !tbaa !5
  %56 = sub nsw i32 %55, 1
  %57 = shl i32 1, %56
  %58 = add nsw i32 %54, %57
  %59 = sext i32 %58 to i64
  %60 = getelementptr inbounds [400002 x [30 x %"struct.std::array"]], ptr @dp, i64 0, i64 %59
  %61 = load i32, ptr %3, align 4, !tbaa !5
  %62 = sub nsw i32 %61, 1
  %63 = sext i32 %62 to i64
  %64 = getelementptr inbounds [30 x %"struct.std::array"], ptr %60, i64 0, i64 %63
  %65 = call noundef nonnull align 4 dereferenceable(8) ptr @_ZSt3minISt5arrayIiLm2EEERKT_S4_S4_(ptr noundef nonnull align 4 dereferenceable(8) %53, ptr noundef nonnull align 4 dereferenceable(8) %64)
  %66 = load i32, ptr %5, align 4, !tbaa !5
  %67 = sext i32 %66 to i64
  %68 = getelementptr inbounds [400002 x [30 x %"struct.std::array"]], ptr @dp, i64 0, i64 %67
  %69 = load i32, ptr %3, align 4, !tbaa !5
  %70 = sext i32 %69 to i64
  %71 = getelementptr inbounds [30 x %"struct.std::array"], ptr %68, i64 0, i64 %70
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %71, ptr align 4 %65, i64 8, i1 false), !tbaa.struct !9
  br label %72

72:                                               ; preds = %46
  %73 = load i32, ptr %5, align 4, !tbaa !5
  %74 = add nsw i32 %73, 1
  store i32 %74, ptr %5, align 4, !tbaa !5
  br label %36, !llvm.loop !13

75:                                               ; preds = %45
  br label %76

76:                                               ; preds = %75
  %77 = load i32, ptr %3, align 4, !tbaa !5
  %78 = add nsw i32 %77, 1
  store i32 %78, ptr %3, align 4, !tbaa !5
  br label %31, !llvm.loop !14

79:                                               ; preds = %34
  ret void
}

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #2

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local noundef nonnull align 4 dereferenceable(8) ptr @_ZSt3minISt5arrayIiLm2EEERKT_S4_S4_(ptr noundef nonnull align 4 dereferenceable(8), ptr noundef nonnull align 4 dereferenceable(8)) #3

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 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { 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 #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 = !{i64 0, i64 8, !10}
!10 = !{!7, !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12}
!14 = distinct !{!14, !12}


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

%"struct.std::array" = type { [2 x i32] }

@n = external dso_local local_unnamed_addr global i32, align 4
@dep = external dso_local local_unnamed_addr global [400002 x i32], align 16
@euler = external dso_local local_unnamed_addr global [400002 x i32], align 16
@dp = external dso_local global [400002 x [30 x %"struct.std::array"]], align 16

; Function Attrs: mustprogress uwtable
define dso_local void @_Z18build_sparse_tablev() local_unnamed_addr #0 {
  %1 = load i32, ptr @n, align 4, !tbaa !5
  %.not21 = icmp slt i32 %1, 1
  br i1 %.not21, label %.preheader.preheader, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %0
  %2 = shl nuw i32 %1, 1
  %smax = tail call i32 @llvm.smax.i32(i32 %2, i32 1)
  %3 = zext nneg i32 %smax to i64
  %xtraiter = and i64 %3, 1
  %4 = icmp eq i32 %smax, 1
  br i1 %4, label %.preheader.preheader.loopexit.unr-lcssa, label %.lr.ph.preheader.new

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

.lr.ph:                                           ; preds = %.lr.ph, %.lr.ph.preheader.new
  %indvars.iv = phi i64 [ 1, %.lr.ph.preheader.new ], [ %indvars.iv.next.1, %.lr.ph ]
  %niter = phi i64 [ 0, %.lr.ph.preheader.new ], [ %niter.next.1, %.lr.ph ]
  %5 = getelementptr inbounds [400002 x i32], ptr @dep, i64 0, i64 %indvars.iv
  %6 = load i32, ptr %5, align 4, !tbaa !5
  %7 = getelementptr inbounds [400002 x i32], ptr @euler, i64 0, i64 %indvars.iv
  %8 = load i32, ptr %7, align 4, !tbaa !5
  %9 = getelementptr inbounds [400002 x [30 x %"struct.std::array"]], ptr @dp, i64 0, i64 %indvars.iv
  store i32 %6, ptr %9, align 16
  %.sroa.4.0..sroa_idx = getelementptr inbounds i8, ptr %9, i64 4
  store i32 %8, ptr %.sroa.4.0..sroa_idx, align 4, !tbaa !9
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %10 = getelementptr inbounds [400002 x i32], ptr @dep, i64 0, i64 %indvars.iv.next
  %11 = load i32, ptr %10, align 4, !tbaa !5
  %12 = getelementptr inbounds [400002 x i32], ptr @euler, i64 0, i64 %indvars.iv.next
  %13 = load i32, ptr %12, align 4, !tbaa !5
  %14 = getelementptr inbounds [400002 x [30 x %"struct.std::array"]], ptr @dp, i64 0, i64 %indvars.iv.next
  store i32 %11, ptr %14, align 16
  %.sroa.4.0..sroa_idx.1 = getelementptr inbounds i8, ptr %14, i64 4
  store i32 %13, ptr %.sroa.4.0..sroa_idx.1, align 4, !tbaa !9
  %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 %.preheader.preheader.loopexit.unr-lcssa, label %.lr.ph, !llvm.loop !10

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

.lr.ph.epil:                                      ; preds = %.preheader.preheader.loopexit.unr-lcssa
  %15 = getelementptr inbounds [400002 x i32], ptr @dep, i64 0, i64 %indvars.iv.unr
  %16 = load i32, ptr %15, align 4, !tbaa !5
  %17 = getelementptr inbounds [400002 x i32], ptr @euler, i64 0, i64 %indvars.iv.unr
  %18 = load i32, ptr %17, align 4, !tbaa !5
  %19 = getelementptr inbounds [400002 x [30 x %"struct.std::array"]], ptr @dp, i64 0, i64 %indvars.iv.unr
  store i32 %16, ptr %19, align 16
  %.sroa.4.0..sroa_idx.epil = getelementptr inbounds i8, ptr %19, i64 4
  store i32 %18, ptr %.sroa.4.0..sroa_idx.epil, align 4, !tbaa !9
  br label %.preheader.preheader

.preheader.preheader:                             ; preds = %.lr.ph.epil, %.preheader.preheader.loopexit.unr-lcssa, %0
  br label %.preheader

.preheader:                                       ; preds = %.preheader.preheader, %._crit_edge
  %20 = phi i32 [ %30, %._crit_edge ], [ %1, %.preheader.preheader ]
  %indvars.iv31 = phi i64 [ %indvars.iv.next32, %._crit_edge ], [ 1, %.preheader.preheader ]
  %21 = trunc nuw nsw i64 %indvars.iv31 to i32
  %22 = shl nuw nsw i32 1, %21
  %23 = shl i32 %20, 1
  %24 = add i32 %23, 2
  %.not1923.not = icmp slt i32 %22, %24
  br i1 %.not1923.not, label %.lr.ph25, label %._crit_edge

.lr.ph25:                                         ; preds = %.preheader
  %25 = add nsw i64 %indvars.iv31, -1
  %26 = trunc nuw nsw i64 %25 to i32
  %27 = shl nuw nsw i32 1, %26
  %28 = zext nneg i32 %27 to i64
  br label %31

29:                                               ; preds = %._crit_edge
  ret void

._crit_edge:                                      ; preds = %31, %.preheader
  %30 = phi i32 [ %20, %.preheader ], [ %38, %31 ]
  %indvars.iv.next32 = add nuw nsw i64 %indvars.iv31, 1
  %exitcond34.not = icmp eq i64 %indvars.iv.next32, 30
  br i1 %exitcond34.not, label %29, label %.preheader, !llvm.loop !12

31:                                               ; preds = %.lr.ph25, %31
  %indvars.iv28 = phi i64 [ 1, %.lr.ph25 ], [ %indvars.iv.next29, %31 ]
  %32 = getelementptr inbounds [400002 x [30 x %"struct.std::array"]], ptr @dp, i64 0, i64 %indvars.iv28, i64 %25
  %33 = add nuw nsw i64 %indvars.iv28, %28
  %34 = getelementptr inbounds [400002 x [30 x %"struct.std::array"]], ptr @dp, i64 0, i64 %33, i64 %25
  %35 = tail call noundef nonnull align 4 dereferenceable(8) ptr @_ZSt3minISt5arrayIiLm2EEERKT_S4_S4_(ptr noundef nonnull align 4 dereferenceable(8) %32, ptr noundef nonnull align 4 dereferenceable(8) %34)
  %36 = getelementptr inbounds [400002 x [30 x %"struct.std::array"]], ptr @dp, i64 0, i64 %indvars.iv28, i64 %indvars.iv31
  %37 = load i64, ptr %35, align 4, !tbaa !9
  store i64 %37, ptr %36, align 8, !tbaa !9
  %indvars.iv.next29 = add nuw nsw i64 %indvars.iv28, 1
  %38 = load i32, ptr @n, align 4, !tbaa !5
  %39 = shl i32 %38, 1
  %40 = add i32 %39, 2
  %41 = trunc i64 %indvars.iv.next29 to i32
  %42 = add i32 %22, %41
  %.not19 = icmp sgt i32 %42, %40
  br i1 %.not19, label %._crit_edge, label %31, !llvm.loop !13
}

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local noundef nonnull align 4 dereferenceable(8) ptr @_ZSt3minISt5arrayIiLm2EEERKT_S4_S4_(ptr noundef nonnull align 4 dereferenceable(8), ptr noundef nonnull align 4 dereferenceable(8)) local_unnamed_addr #1

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

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


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