; LLVM IR from lzyrapx/Competitive-Programming :: HDUOJ/2665(主席树).ll (Z5buildiiRi)

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

@lson = external dso_local global [2000200 x i32], align 16
@rson = external dso_local global [2000200 x i32], align 16
@sum = external dso_local global [2000200 x i32], align 16
@tot = external dso_local global i32, align 4

; Function Attrs: mustprogress uwtable
define dso_local void @_Z5buildiiRi(i32 noundef %0, i32 noundef %1, ptr noundef nonnull align 4 dereferenceable(4) %2) #0 {
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  store i32 %0, ptr %4, align 4, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !5
  store ptr %2, ptr %6, align 8, !tbaa !9
  %8 = load i32, ptr @tot, align 4, !tbaa !5
  %9 = add nsw i32 %8, 1
  store i32 %9, ptr @tot, align 4, !tbaa !5
  %10 = load ptr, ptr %6, align 8, !tbaa !9
  store i32 %9, ptr %10, align 4, !tbaa !5
  %11 = load ptr, ptr %6, align 8, !tbaa !9
  %12 = load i32, ptr %11, align 4, !tbaa !5
  %13 = sext i32 %12 to i64
  %14 = getelementptr inbounds [2000200 x i32], ptr @sum, i64 0, i64 %13
  store i32 0, ptr %14, align 4, !tbaa !5
  %15 = load i32, ptr %4, align 4, !tbaa !5
  %16 = load i32, ptr %5, align 4, !tbaa !5
  %17 = icmp eq i32 %15, %16
  br i1 %17, label %18, label %19

18:                                               ; preds = %3
  br label %37

19:                                               ; preds = %3
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  %20 = load i32, ptr %4, align 4, !tbaa !5
  %21 = load i32, ptr %5, align 4, !tbaa !5
  %22 = add nsw i32 %20, %21
  %23 = ashr i32 %22, 1
  store i32 %23, ptr %7, align 4, !tbaa !5
  %24 = load i32, ptr %4, align 4, !tbaa !5
  %25 = load i32, ptr %7, align 4, !tbaa !5
  %26 = load ptr, ptr %6, align 8, !tbaa !9
  %27 = load i32, ptr %26, align 4, !tbaa !5
  %28 = sext i32 %27 to i64
  %29 = getelementptr inbounds [2000200 x i32], ptr @lson, i64 0, i64 %28
  call void @_Z5buildiiRi(i32 noundef %24, i32 noundef %25, ptr noundef nonnull align 4 dereferenceable(4) %29)
  %30 = load i32, ptr %7, align 4, !tbaa !5
  %31 = add nsw i32 %30, 1
  %32 = load i32, ptr %5, align 4, !tbaa !5
  %33 = load ptr, ptr %6, align 8, !tbaa !9
  %34 = load i32, ptr %33, align 4, !tbaa !5
  %35 = sext i32 %34 to i64
  %36 = getelementptr inbounds [2000200 x i32], ptr @rson, i64 0, i64 %35
  call void @_Z5buildiiRi(i32 noundef %31, i32 noundef %32, ptr noundef nonnull align 4 dereferenceable(4) %36)
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  br label %37

37:                                               ; preds = %19, %18
  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 nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #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 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 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}


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

@lson = external dso_local global [2000200 x i32], align 16
@rson = external dso_local local_unnamed_addr global [2000200 x i32], align 16
@sum = external dso_local local_unnamed_addr global [2000200 x i32], align 16
@tot = external dso_local local_unnamed_addr global i32, align 4

; Function Attrs: mustprogress nofree nosync nounwind memory(readwrite, inaccessiblemem: none) uwtable
define dso_local void @_Z5buildiiRi(i32 noundef %0, i32 noundef %1, ptr nocapture noundef nonnull align 4 dereferenceable(4) %2) local_unnamed_addr #0 {
  %4 = load i32, ptr @tot, align 4, !tbaa !5
  %5 = add nsw i32 %4, 1
  store i32 %5, ptr @tot, align 4, !tbaa !5
  store i32 %5, ptr %2, align 4, !tbaa !5
  %6 = sext i32 %5 to i64
  %7 = getelementptr inbounds [2000200 x i32], ptr @sum, i64 0, i64 %6
  store i32 0, ptr %7, align 4, !tbaa !5
  %8 = icmp eq i32 %0, %1
  br i1 %8, label %tailrecurse._crit_edge, label %tailrecurse.preheader

tailrecurse.preheader:                            ; preds = %3
  %.pre = load i32, ptr %2, align 4, !tbaa !5
  br label %tailrecurse

tailrecurse:                                      ; preds = %tailrecurse.preheader, %tailrecurse
  %9 = phi i32 [ %19, %tailrecurse ], [ %.pre, %tailrecurse.preheader ]
  %.tr1315 = phi ptr [ %17, %tailrecurse ], [ %2, %tailrecurse.preheader ]
  %.tr14 = phi i32 [ %14, %tailrecurse ], [ %0, %tailrecurse.preheader ]
  %10 = add nsw i32 %.tr14, %1
  %11 = ashr i32 %10, 1
  %12 = sext i32 %9 to i64
  %13 = getelementptr inbounds [2000200 x i32], ptr @lson, i64 0, i64 %12
  tail call void @_Z5buildiiRi(i32 noundef %.tr14, i32 noundef %11, ptr noundef nonnull align 4 dereferenceable(4) %13)
  %14 = add nsw i32 %11, 1
  %15 = load i32, ptr %.tr1315, align 4, !tbaa !5
  %16 = sext i32 %15 to i64
  %17 = getelementptr inbounds [2000200 x i32], ptr @rson, i64 0, i64 %16
  %18 = load i32, ptr @tot, align 4, !tbaa !5
  %19 = add nsw i32 %18, 1
  store i32 %19, ptr @tot, align 4, !tbaa !5
  store i32 %19, ptr %17, align 4, !tbaa !5
  %20 = sext i32 %19 to i64
  %21 = getelementptr inbounds [2000200 x i32], ptr @sum, i64 0, i64 %20
  store i32 0, ptr %21, align 4, !tbaa !5
  %22 = icmp eq i32 %14, %1
  br i1 %22, label %tailrecurse._crit_edge, label %tailrecurse

tailrecurse._crit_edge:                           ; preds = %tailrecurse, %3
  ret void
}

attributes #0 = { mustprogress nofree nosync nounwind memory(readwrite, 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"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass