; LLVM IR from lzyrapx/Competitive-Programming :: BZOJ/3224（splay模板）.ll (Z4Rankii)

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

@ch = external dso_local global [100005 x [2 x i32]], align 16
@key = external dso_local global [100005 x i32], align 16
@s = external dso_local global [100005 x i32], align 16
@cnt = external dso_local global [100005 x i32], align 16

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_Z4Rankii(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 ne i32 %6, 0
  br i1 %7, label %9, label %8

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

9:                                                ; preds = %2
  %10 = load i32, ptr %4, align 4, !tbaa !5
  %11 = sext i32 %10 to i64
  %12 = getelementptr inbounds [100005 x i32], ptr @key, i64 0, i64 %11
  %13 = load i32, ptr %12, align 4, !tbaa !5
  %14 = load i32, ptr %5, align 4, !tbaa !5
  %15 = icmp eq i32 %13, %14
  br i1 %15, label %16, label %26

16:                                               ; preds = %9
  %17 = load i32, ptr %4, align 4, !tbaa !5
  %18 = sext i32 %17 to i64
  %19 = getelementptr inbounds [100005 x [2 x i32]], ptr @ch, i64 0, i64 %18
  %20 = getelementptr inbounds [2 x i32], ptr %19, i64 0, i64 0
  %21 = load i32, ptr %20, align 8, !tbaa !5
  %22 = sext i32 %21 to i64
  %23 = getelementptr inbounds [100005 x i32], ptr @s, i64 0, i64 %22
  %24 = load i32, ptr %23, align 4, !tbaa !5
  %25 = add nsw i32 %24, 1
  store i32 %25, ptr %3, align 4
  br label %63

26:                                               ; preds = %9
  %27 = load i32, ptr %4, align 4, !tbaa !5
  %28 = sext i32 %27 to i64
  %29 = getelementptr inbounds [100005 x i32], ptr @key, i64 0, i64 %28
  %30 = load i32, ptr %29, align 4, !tbaa !5
  %31 = load i32, ptr %5, align 4, !tbaa !5
  %32 = icmp slt i32 %30, %31
  br i1 %32, label %33, label %55

33:                                               ; preds = %26
  %34 = load i32, ptr %4, align 4, !tbaa !5
  %35 = sext i32 %34 to i64
  %36 = getelementptr inbounds [100005 x [2 x i32]], ptr @ch, i64 0, i64 %35
  %37 = getelementptr inbounds [2 x i32], ptr %36, i64 0, i64 0
  %38 = load i32, ptr %37, align 8, !tbaa !5
  %39 = sext i32 %38 to i64
  %40 = getelementptr inbounds [100005 x i32], ptr @s, i64 0, i64 %39
  %41 = load i32, ptr %40, align 4, !tbaa !5
  %42 = load i32, ptr %4, align 4, !tbaa !5
  %43 = sext i32 %42 to i64
  %44 = getelementptr inbounds [100005 x i32], ptr @cnt, i64 0, i64 %43
  %45 = load i32, ptr %44, align 4, !tbaa !5
  %46 = add nsw i32 %41, %45
  %47 = load i32, ptr %4, align 4, !tbaa !5
  %48 = sext i32 %47 to i64
  %49 = getelementptr inbounds [100005 x [2 x i32]], ptr @ch, i64 0, i64 %48
  %50 = getelementptr inbounds [2 x i32], ptr %49, i64 0, i64 1
  %51 = load i32, ptr %50, align 4, !tbaa !5
  %52 = load i32, ptr %5, align 4, !tbaa !5
  %53 = call noundef i32 @_Z4Rankii(i32 noundef %51, i32 noundef %52)
  %54 = add nsw i32 %46, %53
  store i32 %54, ptr %3, align 4
  br label %63

55:                                               ; preds = %26
  %56 = load i32, ptr %4, align 4, !tbaa !5
  %57 = sext i32 %56 to i64
  %58 = getelementptr inbounds [100005 x [2 x i32]], ptr @ch, i64 0, i64 %57
  %59 = getelementptr inbounds [2 x i32], ptr %58, i64 0, i64 0
  %60 = load i32, ptr %59, align 8, !tbaa !5
  %61 = load i32, ptr %5, align 4, !tbaa !5
  %62 = call noundef i32 @_Z4Rankii(i32 noundef %60, i32 noundef %61)
  store i32 %62, ptr %3, align 4
  br label %63

63:                                               ; preds = %55, %33, %16, %8
  %64 = load i32, ptr %3, align 4
  ret i32 %64
}

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


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

@ch = external dso_local local_unnamed_addr global [100005 x [2 x i32]], align 16
@key = external dso_local local_unnamed_addr global [100005 x i32], align 16
@s = external dso_local local_unnamed_addr global [100005 x i32], align 16
@cnt = external dso_local local_unnamed_addr global [100005 x i32], align 16

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

.lr.ph:                                           ; preds = %2, %tailrecurse.outer
  %.tr.ph25 = phi i32 [ %24, %tailrecurse.outer ], [ %0, %2 ]
  %accumulator.tr.ph24 = phi i32 [ %26, %tailrecurse.outer ], [ 0, %2 ]
  br label %3

3:                                                ; preds = %.lr.ph, %tailrecurse
  %.tr21 = phi i32 [ %.tr.ph25, %.lr.ph ], [ %18, %tailrecurse ]
  %4 = sext i32 %.tr21 to i64
  %5 = getelementptr inbounds [100005 x i32], ptr @key, i64 0, i64 %4
  %6 = load i32, ptr %5, align 4, !tbaa !5
  %7 = icmp eq i32 %6, %1
  br i1 %7, label %8, label %15

8:                                                ; preds = %3
  %9 = getelementptr inbounds [100005 x [2 x i32]], ptr @ch, i64 0, i64 %4
  %10 = load i32, ptr %9, align 8, !tbaa !5
  %11 = sext i32 %10 to i64
  %12 = getelementptr inbounds [100005 x i32], ptr @s, i64 0, i64 %11
  %13 = load i32, ptr %12, align 4, !tbaa !5
  %14 = add nsw i32 %13, 1
  br label %.loopexit

15:                                               ; preds = %3
  %16 = icmp slt i32 %6, %1
  %17 = getelementptr inbounds [100005 x [2 x i32]], ptr @ch, i64 0, i64 %4
  %18 = load i32, ptr %17, align 8, !tbaa !5
  br i1 %16, label %tailrecurse.outer, label %tailrecurse

tailrecurse.outer:                                ; preds = %15
  %19 = sext i32 %18 to i64
  %20 = getelementptr inbounds [100005 x i32], ptr @s, i64 0, i64 %19
  %21 = load i32, ptr %20, align 4, !tbaa !5
  %22 = getelementptr inbounds [100005 x i32], ptr @cnt, i64 0, i64 %4
  %23 = load i32, ptr %22, align 4, !tbaa !5
  %gep = getelementptr [100005 x [2 x i32]], ptr getelementptr (i8, ptr @ch, i64 4), i64 0, i64 %4
  %24 = load i32, ptr %gep, align 4, !tbaa !5
  %25 = add i32 %21, %accumulator.tr.ph24
  %26 = add i32 %25, %23
  %.not20 = icmp eq i32 %24, 0
  br i1 %.not20, label %.loopexit, label %.lr.ph

tailrecurse:                                      ; preds = %15
  %.not = icmp eq i32 %18, 0
  br i1 %.not, label %.loopexit, label %3

.loopexit:                                        ; preds = %tailrecurse.outer, %tailrecurse, %2, %8
  %accumulator.tr.ph19 = phi i32 [ %accumulator.tr.ph24, %8 ], [ 0, %2 ], [ %accumulator.tr.ph24, %tailrecurse ], [ %26, %tailrecurse.outer ]
  %.0 = phi i32 [ %14, %8 ], [ 0, %2 ], [ 0, %tailrecurse ], [ 0, %tailrecurse.outer ]
  %accumulator.ret.tr = add nsw i32 %.0, %accumulator.tr.ph19
  ret i32 %accumulator.ret.tr
}

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


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