; LLVM IR from Tunghohin/Competitive_Coding :: Luogu/p5854.ll (Z5buildi)

; 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.Node = type { i32, i32 }

@a = external dso_local global [10000010 x i32], align 16
@tr = external dso_local global [10000010 x %struct.Node], align 16
@stk = external dso_local global [10000010 x i32], align 16

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

7:                                                ; preds = %63, %1
  %8 = load i32, ptr %5, align 4, !tbaa !5
  %9 = load i32, ptr %2, align 4, !tbaa !5
  %10 = icmp sle i32 %8, %9
  br i1 %10, label %12, label %11

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

12:                                               ; preds = %7
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  store i32 0, ptr %6, align 4, !tbaa !5
  br label %13

13:                                               ; preds = %31, %12
  %14 = load i32, ptr %4, align 4, !tbaa !5
  %15 = icmp ne i32 %14, 0
  br i1 %15, label %16, label %29

16:                                               ; preds = %13
  %17 = load i32, ptr %4, align 4, !tbaa !5
  %18 = sext i32 %17 to i64
  %19 = getelementptr inbounds [10000010 x i32], ptr @stk, i64 0, i64 %18
  %20 = load i32, ptr %19, align 4, !tbaa !5
  %21 = sext i32 %20 to i64
  %22 = getelementptr inbounds [10000010 x i32], ptr @a, i64 0, i64 %21
  %23 = load i32, ptr %22, align 4, !tbaa !5
  %24 = load i32, ptr %5, align 4, !tbaa !5
  %25 = sext i32 %24 to i64
  %26 = getelementptr inbounds [10000010 x i32], ptr @a, i64 0, i64 %25
  %27 = load i32, ptr %26, align 4, !tbaa !5
  %28 = icmp sgt i32 %23, %27
  br label %29

29:                                               ; preds = %16, %13
  %30 = phi i1 [ false, %13 ], [ %28, %16 ]
  br i1 %30, label %31, label %38

31:                                               ; preds = %29
  %32 = load i32, ptr %4, align 4, !tbaa !5
  %33 = sext i32 %32 to i64
  %34 = getelementptr inbounds [10000010 x i32], ptr @stk, i64 0, i64 %33
  %35 = load i32, ptr %34, align 4, !tbaa !5
  store i32 %35, ptr %6, align 4, !tbaa !5
  %36 = load i32, ptr %4, align 4, !tbaa !5
  %37 = add nsw i32 %36, -1
  store i32 %37, ptr %4, align 4, !tbaa !5
  br label %13, !llvm.loop !9

38:                                               ; preds = %29
  %39 = load i32, ptr %4, align 4, !tbaa !5
  %40 = icmp ne i32 %39, 0
  br i1 %40, label %41, label %50

41:                                               ; preds = %38
  %42 = load i32, ptr %5, align 4, !tbaa !5
  %43 = load i32, ptr %4, align 4, !tbaa !5
  %44 = sext i32 %43 to i64
  %45 = getelementptr inbounds [10000010 x i32], ptr @stk, i64 0, i64 %44
  %46 = load i32, ptr %45, align 4, !tbaa !5
  %47 = sext i32 %46 to i64
  %48 = getelementptr inbounds [10000010 x %struct.Node], ptr @tr, i64 0, i64 %47
  %49 = getelementptr inbounds %struct.Node, ptr %48, i32 0, i32 1
  store i32 %42, ptr %49, align 4, !tbaa !11
  br label %52

50:                                               ; preds = %38
  %51 = load i32, ptr %5, align 4, !tbaa !5
  store i32 %51, ptr %3, align 4, !tbaa !5
  br label %52

52:                                               ; preds = %50, %41
  %53 = load i32, ptr %6, align 4, !tbaa !5
  %54 = load i32, ptr %5, align 4, !tbaa !5
  %55 = sext i32 %54 to i64
  %56 = getelementptr inbounds [10000010 x %struct.Node], ptr @tr, i64 0, i64 %55
  %57 = getelementptr inbounds %struct.Node, ptr %56, i32 0, i32 0
  store i32 %53, ptr %57, align 8, !tbaa !13
  %58 = load i32, ptr %5, align 4, !tbaa !5
  %59 = load i32, ptr %4, align 4, !tbaa !5
  %60 = add nsw i32 %59, 1
  store i32 %60, ptr %4, align 4, !tbaa !5
  %61 = sext i32 %60 to i64
  %62 = getelementptr inbounds [10000010 x i32], ptr @stk, i64 0, i64 %61
  store i32 %58, ptr %62, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  br label %63

63:                                               ; preds = %52
  %64 = load i32, ptr %5, align 4, !tbaa !5
  %65 = add nsw i32 %64, 1
  store i32 %65, ptr %5, align 4, !tbaa !5
  br label %7, !llvm.loop !14

66:                                               ; preds = %11
  %67 = load i32, ptr %3, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #2
  ret i32 %67
}

; 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = !{!12, !6, i64 4}
!12 = !{!"_ZTS4Node", !6, i64 0, !6, i64 4}
!13 = !{!12, !6, i64 0}
!14 = distinct !{!14, !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"

%struct.Node = type { i32, i32 }

@a = external dso_local local_unnamed_addr global [10000010 x i32], align 16
@tr = external dso_local local_unnamed_addr global [10000010 x %struct.Node], align 16
@stk = external dso_local local_unnamed_addr global [10000010 x i32], align 16

; Function Attrs: mustprogress nofree norecurse nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) uwtable
define dso_local noundef i32 @_Z5buildi(i32 noundef %0) local_unnamed_addr #0 {
  %.not31 = icmp slt i32 %0, 1
  br i1 %.not31, label %._crit_edge, label %.preheader.preheader

.preheader.preheader:                             ; preds = %1
  %2 = add nuw i32 %0, 1
  %wide.trip.count = zext i32 %2 to i64
  br label %.preheader

.preheader:                                       ; preds = %.preheader.preheader, %.critedge22
  %indvars.iv43 = phi i64 [ 1, %.preheader.preheader ], [ %indvars.iv.next44, %.critedge22 ]
  %.01833 = phi i32 [ 0, %.preheader.preheader ], [ %24, %.critedge22 ]
  %.01932 = phi i32 [ 0, %.preheader.preheader ], [ %.120, %.critedge22 ]
  %.not2127 = icmp eq i32 %.01833, 0
  %3 = trunc nuw nsw i64 %indvars.iv43 to i32
  br i1 %.not2127, label %.critedge22, label %.lr.ph

.lr.ph:                                           ; preds = %.preheader
  %4 = getelementptr inbounds [10000010 x i32], ptr @a, i64 0, i64 %indvars.iv43
  %5 = load i32, ptr %4, align 4, !tbaa !5
  %6 = sext i32 %.01833 to i64
  %7 = getelementptr inbounds [10000010 x i32], ptr @stk, i64 0, i64 %6
  %8 = load i32, ptr %7, align 4, !tbaa !5
  %9 = sext i32 %8 to i64
  %10 = getelementptr inbounds [10000010 x i32], ptr @a, i64 0, i64 %9
  %11 = load i32, ptr %10, align 4, !tbaa !5
  %12 = icmp sgt i32 %11, %5
  br i1 %12, label %.lr.ph57, label %.critedge

._crit_edge:                                      ; preds = %.critedge22, %1
  %.019.lcssa = phi i32 [ 0, %1 ], [ %.120, %.critedge22 ]
  ret i32 %.019.lcssa

13:                                               ; preds = %.lr.ph57
  %14 = getelementptr inbounds [10000010 x i32], ptr @stk, i64 0, i64 %indvars.iv.next
  %15 = load i32, ptr %14, align 4, !tbaa !5
  %16 = sext i32 %15 to i64
  %17 = getelementptr inbounds [10000010 x i32], ptr @a, i64 0, i64 %16
  %18 = load i32, ptr %17, align 4, !tbaa !5
  %19 = icmp sgt i32 %18, %5
  br i1 %19, label %.lr.ph57, label %.critedge, !llvm.loop !9

.lr.ph57:                                         ; preds = %.lr.ph, %13
  %20 = phi i32 [ %15, %13 ], [ %8, %.lr.ph ]
  %indvars.iv56 = phi i64 [ %indvars.iv.next, %13 ], [ %6, %.lr.ph ]
  %indvars.iv.next = add nsw i64 %indvars.iv56, -1
  %.not21 = icmp eq i64 %indvars.iv.next, 0
  br i1 %.not21, label %.critedge22, label %13, !llvm.loop !9

.critedge:                                        ; preds = %13, %.lr.ph
  %indvars.iv.lcssa = phi i64 [ %6, %.lr.ph ], [ %indvars.iv.next, %13 ]
  %.029.lcssa = phi i32 [ 0, %.lr.ph ], [ %20, %13 ]
  %.lcssa = phi i64 [ %9, %.lr.ph ], [ %16, %13 ]
  %21 = trunc nsw i64 %indvars.iv.lcssa to i32
  %22 = getelementptr inbounds [10000010 x %struct.Node], ptr @tr, i64 0, i64 %.lcssa, i32 1
  store i32 %3, ptr %22, align 4, !tbaa !11
  br label %.critedge22

.critedge22:                                      ; preds = %.lr.ph57, %.preheader, %.critedge
  %.126 = phi i32 [ %21, %.critedge ], [ 0, %.preheader ], [ 0, %.lr.ph57 ]
  %.024 = phi i32 [ %.029.lcssa, %.critedge ], [ 0, %.preheader ], [ %20, %.lr.ph57 ]
  %.120 = phi i32 [ %.01932, %.critedge ], [ %3, %.preheader ], [ %3, %.lr.ph57 ]
  %23 = getelementptr inbounds [10000010 x %struct.Node], ptr @tr, i64 0, i64 %indvars.iv43
  store i32 %.024, ptr %23, align 8, !tbaa !13
  %24 = add nsw i32 %.126, 1
  %25 = sext i32 %24 to i64
  %26 = getelementptr inbounds [10000010 x i32], ptr @stk, i64 0, i64 %25
  store i32 %3, ptr %26, align 4, !tbaa !5
  %indvars.iv.next44 = add nuw nsw i64 %indvars.iv43, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next44, %wide.trip.count
  br i1 %exitcond.not, label %._crit_edge, label %.preheader, !llvm.loop !14
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind memory(readwrite, 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"}
!11 = !{!12, !6, i64 4}
!12 = !{!"_ZTS4Node", !6, i64 0, !6, i64 4}
!13 = !{!12, !6, i64 0}
!14 = distinct !{!14, !10}


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