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

; 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

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_Z12GetRankByValii(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 %9

8:                                                ; preds = %2
  store i32 0, ptr %3, align 4
  br label %68

9:                                                ; preds = %2
  %10 = load i32, ptr %5, 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 %28

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 = sext i32 %22 to i64
  %24 = getelementptr inbounds [100010 x %struct.Treap], ptr @a, i64 0, i64 %23
  %25 = getelementptr inbounds %struct.Treap, ptr %24, i32 0, i32 5
  %26 = load i32, ptr %25, align 4, !tbaa !12
  %27 = add nsw i32 %26, 1
  store i32 %27, ptr %3, align 4
  br label %68

28:                                               ; preds = %9
  %29 = load i32, ptr %5, align 4, !tbaa !5
  %30 = load i32, ptr %4, align 4, !tbaa !5
  %31 = sext i32 %30 to i64
  %32 = getelementptr inbounds [100010 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 = icmp slt i32 %29, %34
  br i1 %35, label %36, label %44

36:                                               ; preds = %28
  %37 = load i32, ptr %4, align 4, !tbaa !5
  %38 = sext i32 %37 to i64
  %39 = getelementptr inbounds [100010 x %struct.Treap], ptr @a, i64 0, i64 %38
  %40 = getelementptr inbounds %struct.Treap, ptr %39, i32 0, i32 0
  %41 = load i32, ptr %40, align 8, !tbaa !11
  %42 = load i32, ptr %5, align 4, !tbaa !5
  %43 = call noundef i32 @_Z12GetRankByValii(i32 noundef %41, i32 noundef %42)
  store i32 %43, ptr %3, align 4
  br label %68

44:                                               ; preds = %28
  %45 = load i32, ptr %4, align 4, !tbaa !5
  %46 = sext i32 %45 to i64
  %47 = getelementptr inbounds [100010 x %struct.Treap], ptr @a, i64 0, i64 %46
  %48 = getelementptr inbounds %struct.Treap, ptr %47, i32 0, i32 1
  %49 = load i32, ptr %48, align 4, !tbaa !13
  %50 = load i32, ptr %5, align 4, !tbaa !5
  %51 = call noundef i32 @_Z12GetRankByValii(i32 noundef %49, i32 noundef %50)
  %52 = load i32, ptr %4, align 4, !tbaa !5
  %53 = sext i32 %52 to i64
  %54 = getelementptr inbounds [100010 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
  %57 = sext i32 %56 to i64
  %58 = getelementptr inbounds [100010 x %struct.Treap], ptr @a, i64 0, i64 %57
  %59 = getelementptr inbounds %struct.Treap, ptr %58, i32 0, i32 5
  %60 = load i32, ptr %59, align 4, !tbaa !12
  %61 = add nsw i32 %51, %60
  %62 = load i32, ptr %4, align 4, !tbaa !5
  %63 = sext i32 %62 to i64
  %64 = getelementptr inbounds [100010 x %struct.Treap], ptr @a, i64 0, i64 %63
  %65 = getelementptr inbounds %struct.Treap, ptr %64, i32 0, i32 4
  %66 = load i32, ptr %65, align 8, !tbaa !14
  %67 = add nsw i32 %61, %66
  store i32 %67, ptr %3, align 4
  br label %68

68:                                               ; preds = %44, %36, %17, %8
  %69 = load i32, ptr %3, align 4
  ret i32 %69
}

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 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 20}
!13 = !{!10, !6, i64 4}
!14 = !{!10, !6, i64 16}


; 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

; Function Attrs: mustprogress nofree nosync nounwind willreturn memory(read, argmem: none, inaccessiblemem: none) uwtable
define dso_local noundef i32 @_Z12GetRankByValii(i32 noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = icmp eq i32 %0, 0
  br i1 %3, label %common.ret29, label %.lr.ph

.lr.ph:                                           ; preds = %2, %tailrecurse
  %.tr18 = phi i32 [ %17, %tailrecurse ], [ %0, %2 ]
  %4 = sext i32 %.tr18 to i64
  %5 = getelementptr inbounds [100010 x %struct.Treap], ptr @a, i64 0, i64 %4
  %6 = getelementptr inbounds i8, ptr %5, i64 8
  %7 = load i32, ptr %6, align 8, !tbaa !5
  %8 = icmp eq i32 %7, %1
  br i1 %8, label %9, label %15

9:                                                ; preds = %.lr.ph
  %10 = load i32, ptr %5, align 8, !tbaa !10
  %11 = sext i32 %10 to i64
  %12 = getelementptr inbounds [100010 x %struct.Treap], ptr @a, i64 0, i64 %11, i32 5
  %13 = load i32, ptr %12, align 4, !tbaa !11
  %14 = add nsw i32 %13, 1
  br label %common.ret29

15:                                               ; preds = %.lr.ph
  %16 = icmp sgt i32 %7, %1
  br i1 %16, label %tailrecurse, label %19

tailrecurse:                                      ; preds = %15
  %17 = load i32, ptr %5, align 8, !tbaa !10
  %18 = icmp eq i32 %17, 0
  br i1 %18, label %common.ret29, label %.lr.ph

common.ret29:                                     ; preds = %9, %2, %tailrecurse, %19
  %common.ret29.op = phi i32 [ %30, %19 ], [ %14, %9 ], [ 0, %2 ], [ 0, %tailrecurse ]
  ret i32 %common.ret29.op

19:                                               ; preds = %15
  %20 = getelementptr inbounds i8, ptr %5, i64 4
  %21 = load i32, ptr %20, align 4, !tbaa !12
  %22 = tail call noundef i32 @_Z12GetRankByValii(i32 noundef %21, i32 noundef %1)
  %23 = load i32, ptr %5, align 8, !tbaa !10
  %24 = sext i32 %23 to i64
  %25 = getelementptr inbounds [100010 x %struct.Treap], ptr @a, i64 0, i64 %24, i32 5
  %26 = load i32, ptr %25, align 4, !tbaa !11
  %27 = add nsw i32 %26, %22
  %28 = getelementptr inbounds i8, ptr %5, i64 16
  %29 = load i32, ptr %28, align 8, !tbaa !13
  %30 = add nsw i32 %27, %29
  br label %common.ret29
}

attributes #0 = { mustprogress nofree 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 8}
!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 0}
!11 = !{!6, !7, i64 20}
!12 = !{!6, !7, i64 4}
!13 = !{!6, !7, i64 16}


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