; LLVM IR from lzyrapx/Competitive-Programming :: 51Nod/1174（RMQ）.ll (Z8RMQ_initi)

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

@dp = external dso_local global [10010 x [30 x i32]], align 16
@num = external dso_local global [10010 x i32], align 16

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i32 @_Z3maxii(i32 noundef, i32 noundef) #0

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_Z8RMQ_initi(i32 noundef %0) #0 {
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  store i32 %0, ptr %2, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  call void @llvm.memset.p0.i64(ptr align 16 @dp, i8 0, i64 1201200, i1 false)
  store i32 1, ptr %3, align 4, !tbaa !5
  br label %5

5:                                                ; preds = %18, %1
  %6 = load i32, ptr %3, align 4, !tbaa !5
  %7 = load i32, ptr %2, align 4, !tbaa !5
  %8 = icmp slt i32 %6, %7
  br i1 %8, label %9, label %21

9:                                                ; preds = %5
  %10 = load i32, ptr %3, align 4, !tbaa !5
  %11 = sext i32 %10 to i64
  %12 = getelementptr inbounds [10010 x i32], ptr @num, i64 0, i64 %11
  %13 = load i32, ptr %12, align 4, !tbaa !5
  %14 = load i32, ptr %3, align 4, !tbaa !5
  %15 = sext i32 %14 to i64
  %16 = getelementptr inbounds [10010 x [30 x i32]], ptr @dp, i64 0, i64 %15
  %17 = getelementptr inbounds [30 x i32], ptr %16, i64 0, i64 0
  store i32 %13, ptr %17, align 8, !tbaa !5
  br label %18

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

21:                                               ; preds = %5
  store i32 1, ptr %4, align 4, !tbaa !5
  br label %22

22:                                               ; preds = %68, %21
  %23 = load i32, ptr %4, align 4, !tbaa !5
  %24 = shl i32 1, %23
  %25 = load i32, ptr %2, align 4, !tbaa !5
  %26 = icmp sle i32 %24, %25
  br i1 %26, label %27, label %71

27:                                               ; preds = %22
  store i32 0, ptr %3, align 4, !tbaa !5
  br label %28

28:                                               ; preds = %64, %27
  %29 = load i32, ptr %3, align 4, !tbaa !5
  %30 = load i32, ptr %4, align 4, !tbaa !5
  %31 = sub nsw i32 %30, 1
  %32 = shl i32 1, %31
  %33 = add nsw i32 %29, %32
  %34 = load i32, ptr %2, align 4, !tbaa !5
  %35 = icmp slt i32 %33, %34
  br i1 %35, label %36, label %67

36:                                               ; preds = %28
  %37 = load i32, ptr %3, align 4, !tbaa !5
  %38 = sext i32 %37 to i64
  %39 = getelementptr inbounds [10010 x [30 x i32]], ptr @dp, i64 0, i64 %38
  %40 = load i32, ptr %4, align 4, !tbaa !5
  %41 = sub nsw i32 %40, 1
  %42 = sext i32 %41 to i64
  %43 = getelementptr inbounds [30 x i32], ptr %39, i64 0, i64 %42
  %44 = load i32, ptr %43, align 4, !tbaa !5
  %45 = load i32, ptr %3, align 4, !tbaa !5
  %46 = load i32, ptr %4, align 4, !tbaa !5
  %47 = sub nsw i32 %46, 1
  %48 = shl i32 1, %47
  %49 = add nsw i32 %45, %48
  %50 = sext i32 %49 to i64
  %51 = getelementptr inbounds [10010 x [30 x i32]], ptr @dp, i64 0, i64 %50
  %52 = load i32, ptr %4, align 4, !tbaa !5
  %53 = sub nsw i32 %52, 1
  %54 = sext i32 %53 to i64
  %55 = getelementptr inbounds [30 x i32], ptr %51, i64 0, i64 %54
  %56 = load i32, ptr %55, align 4, !tbaa !5
  %57 = call noundef i32 @_Z3maxii(i32 noundef %44, i32 noundef %56)
  %58 = load i32, ptr %3, align 4, !tbaa !5
  %59 = sext i32 %58 to i64
  %60 = getelementptr inbounds [10010 x [30 x i32]], ptr @dp, i64 0, i64 %59
  %61 = load i32, ptr %4, align 4, !tbaa !5
  %62 = sext i32 %61 to i64
  %63 = getelementptr inbounds [30 x i32], ptr %60, i64 0, i64 %62
  store i32 %57, ptr %63, align 4, !tbaa !5
  br label %64

64:                                               ; preds = %36
  %65 = load i32, ptr %3, align 4, !tbaa !5
  %66 = add nsw i32 %65, 1
  store i32 %66, ptr %3, align 4, !tbaa !5
  br label %28, !llvm.loop !11

67:                                               ; preds = %28
  br label %68

68:                                               ; preds = %67
  %69 = load i32, ptr %4, align 4, !tbaa !5
  %70 = add nsw i32 %69, 1
  store i32 %70, ptr %4, align 4, !tbaa !5
  br label %22, !llvm.loop !12

71:                                               ; preds = %22
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #3
  ret void
}

; 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 nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #2

; 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 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #3 = { 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 = distinct !{!12, !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"

@dp = external dso_local local_unnamed_addr global [10010 x [30 x i32]], align 16
@num = external dso_local local_unnamed_addr global [10010 x i32], align 16

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i32 @_Z3maxii(i32 noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_Z8RMQ_initi(i32 noundef %0) local_unnamed_addr #0 {
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(1201200) @dp, i8 0, i64 1201200, i1 false)
  %2 = icmp sgt i32 %0, 1
  br i1 %2, label %.lr.ph.preheader, label %._crit_edge28

.lr.ph.preheader:                                 ; preds = %1
  %wide.trip.count = zext nneg i32 %0 to i64
  %3 = add nsw i64 %wide.trip.count, -1
  %4 = add nsw i64 %wide.trip.count, -2
  %xtraiter = and i64 %3, 3
  %5 = icmp ult i64 %4, 3
  br i1 %5, label %.preheader.preheader.unr-lcssa, label %.lr.ph.preheader.new

.lr.ph.preheader.new:                             ; preds = %.lr.ph.preheader
  %unroll_iter = and i64 %3, -4
  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.3, %.lr.ph ]
  %niter = phi i64 [ 0, %.lr.ph.preheader.new ], [ %niter.next.3, %.lr.ph ]
  %6 = getelementptr inbounds [10010 x i32], ptr @num, i64 0, i64 %indvars.iv
  %7 = load i32, ptr %6, align 4, !tbaa !5
  %8 = getelementptr inbounds [10010 x [30 x i32]], ptr @dp, i64 0, i64 %indvars.iv
  store i32 %7, ptr %8, align 8, !tbaa !5
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %9 = getelementptr inbounds [10010 x i32], ptr @num, i64 0, i64 %indvars.iv.next
  %10 = load i32, ptr %9, align 4, !tbaa !5
  %11 = getelementptr inbounds [10010 x [30 x i32]], ptr @dp, i64 0, i64 %indvars.iv.next
  store i32 %10, ptr %11, align 8, !tbaa !5
  %indvars.iv.next.1 = add nuw nsw i64 %indvars.iv, 2
  %12 = getelementptr inbounds [10010 x i32], ptr @num, i64 0, i64 %indvars.iv.next.1
  %13 = load i32, ptr %12, align 4, !tbaa !5
  %14 = getelementptr inbounds [10010 x [30 x i32]], ptr @dp, i64 0, i64 %indvars.iv.next.1
  store i32 %13, ptr %14, align 8, !tbaa !5
  %indvars.iv.next.2 = add nuw nsw i64 %indvars.iv, 3
  %15 = getelementptr inbounds [10010 x i32], ptr @num, i64 0, i64 %indvars.iv.next.2
  %16 = load i32, ptr %15, align 4, !tbaa !5
  %17 = getelementptr inbounds [10010 x [30 x i32]], ptr @dp, i64 0, i64 %indvars.iv.next.2
  store i32 %16, ptr %17, align 8, !tbaa !5
  %indvars.iv.next.3 = add nuw nsw i64 %indvars.iv, 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 %.preheader.preheader.unr-lcssa, label %.lr.ph, !llvm.loop !9

.preheader.preheader.unr-lcssa:                   ; preds = %.lr.ph, %.lr.ph.preheader
  %indvars.iv.unr = phi i64 [ 1, %.lr.ph.preheader ], [ %indvars.iv.next.3, %.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.unr-lcssa, %.lr.ph.epil
  %indvars.iv.epil = phi i64 [ %indvars.iv.next.epil, %.lr.ph.epil ], [ %indvars.iv.unr, %.preheader.preheader.unr-lcssa ]
  %epil.iter = phi i64 [ %epil.iter.next, %.lr.ph.epil ], [ 0, %.preheader.preheader.unr-lcssa ]
  %18 = getelementptr inbounds [10010 x i32], ptr @num, i64 0, i64 %indvars.iv.epil
  %19 = load i32, ptr %18, align 4, !tbaa !5
  %20 = getelementptr inbounds [10010 x [30 x i32]], ptr @dp, i64 0, i64 %indvars.iv.epil
  store i32 %19, ptr %20, align 8, !tbaa !5
  %indvars.iv.next.epil = add nuw nsw i64 %indvars.iv.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 %.preheader.preheader, label %.lr.ph.epil, !llvm.loop !11

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

.preheader:                                       ; preds = %.preheader.preheader, %._crit_edge
  %indvars.iv35 = phi i64 [ %indvars.iv.next36, %._crit_edge ], [ 1, %.preheader.preheader ]
  %21 = add nsw i64 %indvars.iv35, -1
  %22 = trunc nuw nsw i64 %21 to i32
  %23 = shl nuw i32 1, %22
  %24 = icmp slt i32 %23, %0
  br i1 %24, label %.lr.ph25, label %._crit_edge

.lr.ph25:                                         ; preds = %.preheader
  %25 = sext i32 %23 to i64
  %26 = sub i32 %0, %23
  %wide.trip.count33 = zext i32 %26 to i64
  br label %27

27:                                               ; preds = %.lr.ph25, %27
  %indvars.iv30 = phi i64 [ 0, %.lr.ph25 ], [ %indvars.iv.next31, %27 ]
  %28 = add nuw nsw i64 %indvars.iv30, %25
  %29 = getelementptr inbounds [10010 x [30 x i32]], ptr @dp, i64 0, i64 %indvars.iv30, i64 %21
  %30 = load i32, ptr %29, align 4, !tbaa !5
  %31 = getelementptr inbounds [10010 x [30 x i32]], ptr @dp, i64 0, i64 %28, i64 %21
  %32 = load i32, ptr %31, align 4, !tbaa !5
  %33 = tail call noundef i32 @_Z3maxii(i32 noundef %30, i32 noundef %32)
  %34 = getelementptr inbounds [10010 x [30 x i32]], ptr @dp, i64 0, i64 %indvars.iv30, i64 %indvars.iv35
  store i32 %33, ptr %34, align 4, !tbaa !5
  %indvars.iv.next31 = add nuw nsw i64 %indvars.iv30, 1
  %exitcond34.not = icmp eq i64 %indvars.iv.next31, %wide.trip.count33
  br i1 %exitcond34.not, label %._crit_edge, label %27, !llvm.loop !13

._crit_edge:                                      ; preds = %27, %.preheader
  %indvars.iv.next36 = add nuw nsw i64 %indvars.iv35, 1
  %35 = trunc nuw nsw i64 %indvars.iv35 to i32
  %36 = shl nuw i32 2, %35
  %.not = icmp sgt i32 %36, %0
  br i1 %.not, label %._crit_edge28, label %.preheader, !llvm.loop !14

._crit_edge28:                                    ; preds = %._crit_edge, %1
  ret void
}

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #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 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }

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


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