; LLVM IR from upupming/algorithm :: acwing/算法竞赛进阶指南/0x46-二叉查找树与平衡树初步/253. 普通平衡树.ll (Z12GetValByRankii)

; 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
@INF = external dso_local global i32, align 4

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_Z12GetValByRankii(i32 noundef %0, i32 noundef %1) #0 {
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  store i32 %0, ptr %4, align 4, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !5
  %6 = load i32, ptr %4, align 4, !tbaa !5
  %7 = icmp eq i32 %6, 0
  br i1 %7, label %8, label %10

8:                                                ; preds = %2
  %9 = load i32, ptr @INF, align 4, !tbaa !5
  store i32 %9, ptr %3, align 4
  br label %78

10:                                               ; preds = %2
  %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 0
  %15 = load i32, ptr %14, align 8, !tbaa !9
  %16 = sext i32 %15 to i64
  %17 = getelementptr inbounds [100010 x %struct.Treap], ptr @a, i64 0, i64 %16
  %18 = getelementptr inbounds %struct.Treap, ptr %17, i32 0, i32 5
  %19 = load i32, ptr %18, align 4, !tbaa !11
  %20 = load i32, ptr %5, align 4, !tbaa !5
  %21 = icmp sge i32 %19, %20
  br i1 %21, label %22, label %30

22:                                               ; preds = %10
  %23 = load i32, ptr %4, align 4, !tbaa !5
  %24 = sext i32 %23 to i64
  %25 = getelementptr inbounds [100010 x %struct.Treap], ptr @a, i64 0, i64 %24
  %26 = getelementptr inbounds %struct.Treap, ptr %25, i32 0, i32 0
  %27 = load i32, ptr %26, align 8, !tbaa !9
  %28 = load i32, ptr %5, align 4, !tbaa !5
  %29 = call noundef i32 @_Z12GetValByRankii(i32 noundef %27, i32 noundef %28)
  store i32 %29, ptr %3, align 4
  br label %78

30:                                               ; preds = %10
  %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 0
  %35 = load i32, ptr %34, align 8, !tbaa !9
  %36 = sext i32 %35 to i64
  %37 = getelementptr inbounds [100010 x %struct.Treap], ptr @a, i64 0, i64 %36
  %38 = getelementptr inbounds %struct.Treap, ptr %37, i32 0, i32 5
  %39 = load i32, ptr %38, align 4, !tbaa !11
  %40 = load i32, ptr %4, align 4, !tbaa !5
  %41 = sext i32 %40 to i64
  %42 = getelementptr inbounds [100010 x %struct.Treap], ptr @a, i64 0, i64 %41
  %43 = getelementptr inbounds %struct.Treap, ptr %42, i32 0, i32 4
  %44 = load i32, ptr %43, align 8, !tbaa !12
  %45 = add nsw i32 %39, %44
  %46 = load i32, ptr %5, align 4, !tbaa !5
  %47 = icmp sge i32 %45, %46
  br i1 %47, label %48, label %54

48:                                               ; preds = %30
  %49 = load i32, ptr %4, align 4, !tbaa !5
  %50 = sext i32 %49 to i64
  %51 = getelementptr inbounds [100010 x %struct.Treap], ptr @a, i64 0, i64 %50
  %52 = getelementptr inbounds %struct.Treap, ptr %51, i32 0, i32 2
  %53 = load i32, ptr %52, align 8, !tbaa !13
  store i32 %53, ptr %3, align 4
  br label %78

54:                                               ; preds = %30
  %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 1
  %59 = load i32, ptr %58, align 4, !tbaa !14
  %60 = load i32, ptr %5, align 4, !tbaa !5
  %61 = load i32, ptr %4, align 4, !tbaa !5
  %62 = sext i32 %61 to i64
  %63 = getelementptr inbounds [100010 x %struct.Treap], ptr @a, i64 0, i64 %62
  %64 = getelementptr inbounds %struct.Treap, ptr %63, i32 0, i32 0
  %65 = load i32, ptr %64, align 8, !tbaa !9
  %66 = sext i32 %65 to i64
  %67 = getelementptr inbounds [100010 x %struct.Treap], ptr @a, i64 0, i64 %66
  %68 = getelementptr inbounds %struct.Treap, ptr %67, i32 0, i32 5
  %69 = load i32, ptr %68, align 4, !tbaa !11
  %70 = sub nsw i32 %60, %69
  %71 = load i32, ptr %4, align 4, !tbaa !5
  %72 = sext i32 %71 to i64
  %73 = getelementptr inbounds [100010 x %struct.Treap], ptr @a, i64 0, i64 %72
  %74 = getelementptr inbounds %struct.Treap, ptr %73, i32 0, i32 4
  %75 = load i32, ptr %74, align 8, !tbaa !12
  %76 = sub nsw i32 %70, %75
  %77 = call noundef i32 @_Z12GetValByRankii(i32 noundef %59, i32 noundef %76)
  store i32 %77, ptr %3, align 4
  br label %78

78:                                               ; preds = %54, %48, %22, %8
  %79 = load i32, ptr %3, align 4
  ret i32 %79
}

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

!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 0}
!10 = !{!"_ZTS5Treap", !6, i64 0, !6, i64 4, !6, i64 8, !6, i64 12, !6, i64 16, !6, i64 20}
!11 = !{!10, !6, i64 20}
!12 = !{!10, !6, i64 16}
!13 = !{!10, !6, i64 8}
!14 = !{!10, !6, i64 4}


; 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
@INF = 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 @_Z12GetValByRankii(i32 noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  br label %tailrecurse.outer

tailrecurse.outer:                                ; preds = %17, %2
  %.tr.ph = phi i32 [ %19, %17 ], [ %0, %2 ]
  %.tr18.ph = phi i32 [ %20, %17 ], [ %1, %2 ]
  br label %tailrecurse

tailrecurse:                                      ; preds = %tailrecurse.outer, %4
  %.tr = phi i32 [ %7, %4 ], [ %.tr.ph, %tailrecurse.outer ]
  %3 = icmp eq i32 %.tr, 0
  br i1 %3, label %.loopexit, label %4

4:                                                ; preds = %tailrecurse
  %5 = sext i32 %.tr to i64
  %6 = getelementptr inbounds [100010 x %struct.Treap], ptr @a, i64 0, i64 %5
  %7 = load i32, ptr %6, align 8, !tbaa !5
  %8 = sext i32 %7 to i64
  %9 = getelementptr inbounds [100010 x %struct.Treap], ptr @a, i64 0, i64 %8, i32 5
  %10 = load i32, ptr %9, align 4, !tbaa !10
  %.not = icmp slt i32 %10, %.tr18.ph
  br i1 %.not, label %11, label %tailrecurse

11:                                               ; preds = %4
  %12 = getelementptr inbounds i8, ptr %6, i64 16
  %13 = load i32, ptr %12, align 8, !tbaa !11
  %14 = add nsw i32 %13, %10
  %.not17 = icmp slt i32 %14, %.tr18.ph
  br i1 %.not17, label %17, label %15

15:                                               ; preds = %11
  %16 = getelementptr inbounds i8, ptr %6, i64 8
  br label %.loopexit

17:                                               ; preds = %11
  %18 = getelementptr inbounds i8, ptr %6, i64 4
  %19 = load i32, ptr %18, align 4, !tbaa !12
  %20 = sub i32 %.tr18.ph, %14
  br label %tailrecurse.outer

.loopexit:                                        ; preds = %tailrecurse, %15
  %.0.in = phi ptr [ %16, %15 ], [ @INF, %tailrecurse ]
  %.0 = load i32, ptr %.0.in, align 4, !tbaa !13
  ret i32 %.0
}

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


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