; LLVM IR from upupming/algorithm :: acwing/算法竞赛进阶指南/0x49-总结与练习/265. 营业额统计.ll (Z6GetPrei)

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

@a = external dso_local global [40010 x %struct.Treap], align 16
@root = external dso_local global i32, align 4

; 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 nounwind uwtable
define dso_local noundef i32 @_Z6GetPrei(i32 noundef %0) #1 {
  %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 %3, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #2
  store i32 1, ptr %4, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #2
  %7 = load i32, ptr @root, align 4, !tbaa !5
  store i32 %7, ptr %5, align 4, !tbaa !5
  br label %8

8:                                                ; preds = %63, %1
  %9 = load i32, ptr %5, align 4, !tbaa !5
  %10 = icmp ne i32 %9, 0
  br i1 %10, label %11, label %65

11:                                               ; preds = %8
  %12 = load i32, ptr %3, align 4, !tbaa !5
  %13 = load i32, ptr %5, align 4, !tbaa !5
  %14 = sext i32 %13 to i64
  %15 = getelementptr inbounds [40010 x %struct.Treap], ptr @a, i64 0, i64 %14
  %16 = getelementptr inbounds %struct.Treap, ptr %15, i32 0, i32 2
  %17 = load i32, ptr %16, align 8, !tbaa !9
  %18 = icmp eq i32 %12, %17
  br i1 %18, label %19, label %21

19:                                               ; preds = %11
  %20 = load i32, ptr %3, align 4, !tbaa !5
  store i32 %20, ptr %2, align 4
  store i32 1, ptr %6, align 4
  br label %71

21:                                               ; preds = %11
  %22 = load i32, ptr %5, align 4, !tbaa !5
  %23 = sext i32 %22 to i64
  %24 = getelementptr inbounds [40010 x %struct.Treap], ptr @a, i64 0, i64 %23
  %25 = getelementptr inbounds %struct.Treap, ptr %24, i32 0, i32 2
  %26 = load i32, ptr %25, align 8, !tbaa !9
  %27 = load i32, ptr %3, align 4, !tbaa !5
  %28 = icmp slt i32 %26, %27
  br i1 %28, label %29, label %43

29:                                               ; preds = %21
  %30 = load i32, ptr %5, align 4, !tbaa !5
  %31 = sext i32 %30 to i64
  %32 = getelementptr inbounds [40010 x %struct.Treap], ptr @a, i64 0, i64 %31
  %33 = getelementptr inbounds %struct.Treap, ptr %32, i32 0, i32 2
  %34 = load i32, ptr %33, align 8, !tbaa !9
  %35 = load i32, ptr %4, align 4, !tbaa !5
  %36 = sext i32 %35 to i64
  %37 = getelementptr inbounds [40010 x %struct.Treap], ptr @a, i64 0, i64 %36
  %38 = getelementptr inbounds %struct.Treap, ptr %37, i32 0, i32 2
  %39 = load i32, ptr %38, align 8, !tbaa !9
  %40 = icmp sgt i32 %34, %39
  br i1 %40, label %41, label %43

41:                                               ; preds = %29
  %42 = load i32, ptr %5, align 4, !tbaa !5
  store i32 %42, ptr %4, align 4, !tbaa !5
  br label %43

43:                                               ; preds = %41, %29, %21
  %44 = load i32, ptr %3, align 4, !tbaa !5
  %45 = load i32, ptr %5, align 4, !tbaa !5
  %46 = sext i32 %45 to i64
  %47 = getelementptr inbounds [40010 x %struct.Treap], ptr @a, i64 0, i64 %46
  %48 = getelementptr inbounds %struct.Treap, ptr %47, i32 0, i32 2
  %49 = load i32, ptr %48, align 8, !tbaa !9
  %50 = icmp slt i32 %44, %49
  br i1 %50, label %51, label %57

51:                                               ; preds = %43
  %52 = load i32, ptr %5, align 4, !tbaa !5
  %53 = sext i32 %52 to i64
  %54 = getelementptr inbounds [40010 x %struct.Treap], ptr @a, i64 0, i64 %53
  %55 = getelementptr inbounds %struct.Treap, ptr %54, i32 0, i32 0
  %56 = load i32, ptr %55, align 8, !tbaa !11
  br label %63

57:                                               ; preds = %43
  %58 = load i32, ptr %5, align 4, !tbaa !5
  %59 = sext i32 %58 to i64
  %60 = getelementptr inbounds [40010 x %struct.Treap], ptr @a, i64 0, i64 %59
  %61 = getelementptr inbounds %struct.Treap, ptr %60, i32 0, i32 1
  %62 = load i32, ptr %61, align 4, !tbaa !12
  br label %63

63:                                               ; preds = %57, %51
  %64 = phi i32 [ %56, %51 ], [ %62, %57 ]
  store i32 %64, ptr %5, align 4, !tbaa !5
  br label %8, !llvm.loop !13

65:                                               ; preds = %8
  %66 = load i32, ptr %4, align 4, !tbaa !5
  %67 = sext i32 %66 to i64
  %68 = getelementptr inbounds [40010 x %struct.Treap], ptr @a, i64 0, i64 %67
  %69 = getelementptr inbounds %struct.Treap, ptr %68, i32 0, i32 2
  %70 = load i32, ptr %69, align 8, !tbaa !9
  store i32 %70, ptr %2, align 4
  store i32 1, ptr %6, align 4
  br label %71

71:                                               ; preds = %65, %19
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  %72 = load i32, ptr %2, align 4
  ret i32 %72
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #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, !6, i64 8}
!10 = !{!"_ZTS5Treap", !6, i64 0, !6, i64 4, !6, i64 8, !6, i64 12, !6, i64 16, !6, i64 20}
!11 = !{!10, !6, i64 0}
!12 = !{!10, !6, i64 4}
!13 = distinct !{!13, !14}
!14 = !{!"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"

%struct.Treap = type { i32, i32, i32, i32, i32, i32 }

@a = external dso_local local_unnamed_addr global [40010 x %struct.Treap], align 16
@root = external dso_local local_unnamed_addr global i32, align 4

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(read, argmem: none, inaccessiblemem: none) uwtable
define dso_local noundef i32 @_Z6GetPrei(i32 noundef %0) local_unnamed_addr #0 {
  %.021 = load i32, ptr @root, align 4, !tbaa !5
  %.not22 = icmp eq i32 %.021, 0
  br i1 %.not22, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %1, %14
  %.024 = phi i32 [ %.0, %14 ], [ %.021, %1 ]
  %.01523 = phi i32 [ %.1, %14 ], [ 1, %1 ]
  %2 = sext i32 %.024 to i64
  %3 = getelementptr inbounds [40010 x %struct.Treap], ptr @a, i64 0, i64 %2
  %4 = getelementptr inbounds i8, ptr %3, i64 8
  %5 = load i32, ptr %4, align 8, !tbaa !9
  %6 = icmp eq i32 %5, %0
  br i1 %6, label %.loopexit, label %7

7:                                                ; preds = %.lr.ph
  %8 = icmp slt i32 %5, %0
  br i1 %8, label %9, label %14

9:                                                ; preds = %7
  %10 = sext i32 %.01523 to i64
  %11 = getelementptr inbounds [40010 x %struct.Treap], ptr @a, i64 0, i64 %10, i32 2
  %12 = load i32, ptr %11, align 8, !tbaa !9
  %13 = icmp sgt i32 %5, %12
  %spec.select = select i1 %13, i32 %.024, i32 %.01523
  br label %14

14:                                               ; preds = %9, %7
  %.1 = phi i32 [ %.01523, %7 ], [ %spec.select, %9 ]
  %15 = icmp sgt i32 %5, %0
  %.in.idx = select i1 %15, i64 0, i64 4
  %.in = getelementptr inbounds i8, ptr %3, i64 %.in.idx
  %.0 = load i32, ptr %.in, align 4, !tbaa !5
  %.not = icmp eq i32 %.0, 0
  br i1 %.not, label %._crit_edge.loopexit, label %.lr.ph, !llvm.loop !11

._crit_edge.loopexit:                             ; preds = %14
  %16 = sext i32 %.1 to i64
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit, %1
  %.015.lcssa = phi i64 [ 1, %1 ], [ %16, %._crit_edge.loopexit ]
  %17 = getelementptr inbounds [40010 x %struct.Treap], ptr @a, i64 0, i64 %.015.lcssa, i32 2
  %18 = load i32, ptr %17, align 8, !tbaa !9
  br label %.loopexit

.loopexit:                                        ; preds = %.lr.ph, %._crit_edge
  %.016 = phi i32 [ %18, %._crit_edge ], [ %0, %.lr.ph ]
  ret i32 %.016
}

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


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