; LLVM IR from upupming/algorithm :: acwing/算法竞赛进阶指南/0x46-二叉查找树与平衡树初步/253. 普通平衡树.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 [100010 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
  store i32 %0, ptr %2, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #2
  store i32 1, ptr %3, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #2
  %5 = load i32, ptr @root, align 4, !tbaa !5
  store i32 %5, ptr %4, align 4, !tbaa !5
  br label %6

6:                                                ; preds = %88, %1
  %7 = load i32, ptr %4, align 4, !tbaa !5
  %8 = icmp ne i32 %7, 0
  br i1 %8, label %9, label %90

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

17:                                               ; preds = %9
  %18 = load i32, ptr %4, align 4, !tbaa !5
  %19 = sext i32 %18 to i64
  %20 = getelementptr inbounds [100010 x %struct.Treap], ptr @a, i64 0, i64 %19
  %21 = getelementptr inbounds %struct.Treap, ptr %20, i32 0, i32 0
  %22 = load i32, ptr %21, align 8, !tbaa !11
  %23 = icmp sgt i32 %22, 0
  br i1 %23, label %24, label %45

24:                                               ; preds = %17
  %25 = load i32, ptr %4, align 4, !tbaa !5
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds [100010 x %struct.Treap], ptr @a, i64 0, i64 %26
  %28 = getelementptr inbounds %struct.Treap, ptr %27, i32 0, i32 0
  %29 = load i32, ptr %28, align 8, !tbaa !11
  store i32 %29, ptr %4, align 4, !tbaa !5
  br label %30

30:                                               ; preds = %37, %24
  %31 = load i32, ptr %4, align 4, !tbaa !5
  %32 = sext i32 %31 to i64
  %33 = getelementptr inbounds [100010 x %struct.Treap], ptr @a, i64 0, i64 %32
  %34 = getelementptr inbounds %struct.Treap, ptr %33, i32 0, i32 1
  %35 = load i32, ptr %34, align 4, !tbaa !12
  %36 = icmp sgt i32 %35, 0
  br i1 %36, label %37, label %43

37:                                               ; preds = %30
  %38 = load i32, ptr %4, align 4, !tbaa !5
  %39 = sext i32 %38 to i64
  %40 = getelementptr inbounds [100010 x %struct.Treap], ptr @a, i64 0, i64 %39
  %41 = getelementptr inbounds %struct.Treap, ptr %40, i32 0, i32 1
  %42 = load i32, ptr %41, align 4, !tbaa !12
  store i32 %42, ptr %4, align 4, !tbaa !5
  br label %30, !llvm.loop !13

43:                                               ; preds = %30
  %44 = load i32, ptr %4, align 4, !tbaa !5
  store i32 %44, ptr %3, align 4, !tbaa !5
  br label %45

45:                                               ; preds = %43, %17
  br label %90

46:                                               ; preds = %9
  %47 = load i32, ptr %4, align 4, !tbaa !5
  %48 = sext i32 %47 to i64
  %49 = getelementptr inbounds [100010 x %struct.Treap], ptr @a, i64 0, i64 %48
  %50 = getelementptr inbounds %struct.Treap, ptr %49, i32 0, i32 2
  %51 = load i32, ptr %50, align 8, !tbaa !9
  %52 = load i32, ptr %2, align 4, !tbaa !5
  %53 = icmp slt i32 %51, %52
  br i1 %53, label %54, label %68

54:                                               ; preds = %46
  %55 = load i32, ptr %4, align 4, !tbaa !5
  %56 = sext i32 %55 to i64
  %57 = getelementptr inbounds [100010 x %struct.Treap], ptr @a, i64 0, i64 %56
  %58 = getelementptr inbounds %struct.Treap, ptr %57, i32 0, i32 2
  %59 = load i32, ptr %58, align 8, !tbaa !9
  %60 = load i32, ptr %3, align 4, !tbaa !5
  %61 = sext i32 %60 to i64
  %62 = getelementptr inbounds [100010 x %struct.Treap], ptr @a, i64 0, i64 %61
  %63 = getelementptr inbounds %struct.Treap, ptr %62, i32 0, i32 2
  %64 = load i32, ptr %63, align 8, !tbaa !9
  %65 = icmp sgt i32 %59, %64
  br i1 %65, label %66, label %68

66:                                               ; preds = %54
  %67 = load i32, ptr %4, align 4, !tbaa !5
  store i32 %67, ptr %3, align 4, !tbaa !5
  br label %68

68:                                               ; preds = %66, %54, %46
  %69 = load i32, ptr %2, align 4, !tbaa !5
  %70 = load i32, ptr %4, align 4, !tbaa !5
  %71 = sext i32 %70 to i64
  %72 = getelementptr inbounds [100010 x %struct.Treap], ptr @a, i64 0, i64 %71
  %73 = getelementptr inbounds %struct.Treap, ptr %72, i32 0, i32 2
  %74 = load i32, ptr %73, align 8, !tbaa !9
  %75 = icmp slt i32 %69, %74
  br i1 %75, label %76, label %82

76:                                               ; preds = %68
  %77 = load i32, ptr %4, align 4, !tbaa !5
  %78 = sext i32 %77 to i64
  %79 = getelementptr inbounds [100010 x %struct.Treap], ptr @a, i64 0, i64 %78
  %80 = getelementptr inbounds %struct.Treap, ptr %79, i32 0, i32 0
  %81 = load i32, ptr %80, align 8, !tbaa !11
  br label %88

82:                                               ; preds = %68
  %83 = load i32, ptr %4, align 4, !tbaa !5
  %84 = sext i32 %83 to i64
  %85 = getelementptr inbounds [100010 x %struct.Treap], ptr @a, i64 0, i64 %84
  %86 = getelementptr inbounds %struct.Treap, ptr %85, i32 0, i32 1
  %87 = load i32, ptr %86, align 4, !tbaa !12
  br label %88

88:                                               ; preds = %82, %76
  %89 = phi i32 [ %81, %76 ], [ %87, %82 ]
  store i32 %89, ptr %4, align 4, !tbaa !5
  br label %6, !llvm.loop !15

90:                                               ; preds = %45, %6
  %91 = load i32, ptr %3, align 4, !tbaa !5
  %92 = sext i32 %91 to i64
  %93 = getelementptr inbounds [100010 x %struct.Treap], ptr @a, i64 0, i64 %92
  %94 = getelementptr inbounds %struct.Treap, ptr %93, i32 0, i32 2
  %95 = load i32, ptr %94, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #2
  ret i32 %95
}

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"}
!15 = distinct !{!15, !14}


; 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 [100010 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 {
  %.026 = load i32, ptr @root, align 4, !tbaa !5
  %.not27 = icmp eq i32 %.026, 0
  br i1 %.not27, label %.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %1, %21
  %.029 = phi i32 [ %.0, %21 ], [ %.026, %1 ]
  %.01828 = phi i32 [ %.119, %21 ], [ 1, %1 ]
  %2 = sext i32 %.029 to i64
  %3 = getelementptr inbounds [100010 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 %7, label %14

7:                                                ; preds = %.lr.ph
  %8 = load i32, ptr %3, align 8, !tbaa !11
  %9 = icmp sgt i32 %8, 0
  br i1 %9, label %.preheader, label %.loopexit

.preheader:                                       ; preds = %7, %.preheader
  %.1 = phi i32 [ %12, %.preheader ], [ %8, %7 ]
  %10 = zext nneg i32 %.1 to i64
  %11 = getelementptr inbounds [100010 x %struct.Treap], ptr @a, i64 0, i64 %10, i32 1
  %12 = load i32, ptr %11, align 4, !tbaa !12
  %13 = icmp sgt i32 %12, 0
  br i1 %13, label %.preheader, label %.loopexit, !llvm.loop !13

14:                                               ; preds = %.lr.ph
  %15 = icmp slt i32 %5, %0
  br i1 %15, label %16, label %21

16:                                               ; preds = %14
  %17 = sext i32 %.01828 to i64
  %18 = getelementptr inbounds [100010 x %struct.Treap], ptr @a, i64 0, i64 %17, i32 2
  %19 = load i32, ptr %18, align 8, !tbaa !9
  %20 = icmp sgt i32 %5, %19
  %spec.select = select i1 %20, i32 %.029, i32 %.01828
  br label %21

21:                                               ; preds = %16, %14
  %.119 = phi i32 [ %.01828, %14 ], [ %spec.select, %16 ]
  %22 = icmp sgt i32 %5, %0
  %.in.idx = select i1 %22, 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 %.loopexit, label %.lr.ph, !llvm.loop !15

.loopexit:                                        ; preds = %21, %.preheader, %1, %7
  %.2 = phi i32 [ %.01828, %7 ], [ 1, %1 ], [ %.1, %.preheader ], [ %.119, %21 ]
  %23 = sext i32 %.2 to i64
  %24 = getelementptr inbounds [100010 x %struct.Treap], ptr @a, i64 0, i64 %23, i32 2
  %25 = load i32, ptr %24, align 8, !tbaa !9
  ret i32 %25
}

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 = !{!10, !6, i64 0}
!12 = !{!10, !6, i64 4}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = distinct !{!15, !14}


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