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

; 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 @_Z3Kthii(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 %71

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 [100005 x [2 x i32]], ptr @ch, i64 0, i64 %12
  %14 = getelementptr inbounds [2 x i32], ptr %13, i64 0, i64 0
  %15 = load i32, ptr %14, align 8, !tbaa !5
  %16 = sext i32 %15 to i64
  %17 = getelementptr inbounds [100005 x i32], ptr @s, i64 0, i64 %16
  %18 = load i32, ptr %17, align 4, !tbaa !5
  %19 = icmp sle i32 %10, %18
  br i1 %19, label %20, label %28

20:                                               ; preds = %9
  %21 = load i32, ptr %4, align 4, !tbaa !5
  %22 = sext i32 %21 to i64
  %23 = getelementptr inbounds [100005 x [2 x i32]], ptr @ch, i64 0, i64 %22
  %24 = getelementptr inbounds [2 x i32], ptr %23, i64 0, i64 0
  %25 = load i32, ptr %24, align 8, !tbaa !5
  %26 = load i32, ptr %5, align 4, !tbaa !5
  %27 = call noundef i32 @_Z3Kthii(i32 noundef %25, i32 noundef %26)
  store i32 %27, ptr %3, align 4
  br label %71

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 [100005 x [2 x i32]], ptr @ch, i64 0, i64 %31
  %33 = getelementptr inbounds [2 x i32], ptr %32, i64 0, i64 0
  %34 = load i32, ptr %33, align 8, !tbaa !5
  %35 = sext i32 %34 to i64
  %36 = getelementptr inbounds [100005 x i32], ptr @s, i64 0, i64 %35
  %37 = load i32, ptr %36, align 4, !tbaa !5
  %38 = load i32, ptr %4, align 4, !tbaa !5
  %39 = sext i32 %38 to i64
  %40 = getelementptr inbounds [100005 x i32], ptr @cnt, i64 0, i64 %39
  %41 = load i32, ptr %40, align 4, !tbaa !5
  %42 = add nsw i32 %37, %41
  %43 = icmp sgt i32 %29, %42
  br i1 %43, label %44, label %66

44:                                               ; preds = %28
  %45 = load i32, ptr %4, align 4, !tbaa !5
  %46 = sext i32 %45 to i64
  %47 = getelementptr inbounds [100005 x [2 x i32]], ptr @ch, i64 0, i64 %46
  %48 = getelementptr inbounds [2 x i32], ptr %47, i64 0, i64 1
  %49 = load i32, ptr %48, align 4, !tbaa !5
  %50 = load i32, ptr %5, align 4, !tbaa !5
  %51 = load i32, ptr %4, align 4, !tbaa !5
  %52 = sext i32 %51 to i64
  %53 = getelementptr inbounds [100005 x [2 x i32]], ptr @ch, i64 0, i64 %52
  %54 = getelementptr inbounds [2 x i32], ptr %53, i64 0, i64 0
  %55 = load i32, ptr %54, align 8, !tbaa !5
  %56 = sext i32 %55 to i64
  %57 = getelementptr inbounds [100005 x i32], ptr @s, i64 0, i64 %56
  %58 = load i32, ptr %57, align 4, !tbaa !5
  %59 = sub nsw i32 %50, %58
  %60 = load i32, ptr %4, align 4, !tbaa !5
  %61 = sext i32 %60 to i64
  %62 = getelementptr inbounds [100005 x i32], ptr @cnt, i64 0, i64 %61
  %63 = load i32, ptr %62, align 4, !tbaa !5
  %64 = sub nsw i32 %59, %63
  %65 = call noundef i32 @_Z3Kthii(i32 noundef %49, i32 noundef %64)
  store i32 %65, ptr %3, align 4
  br label %71

66:                                               ; preds = %28
  %67 = load i32, ptr %4, align 4, !tbaa !5
  %68 = sext i32 %67 to i64
  %69 = getelementptr inbounds [100005 x i32], ptr @key, i64 0, i64 %68
  %70 = load i32, ptr %69, align 4, !tbaa !5
  store i32 %70, ptr %3, align 4
  br label %71

71:                                               ; preds = %66, %44, %20, %8
  %72 = load i32, ptr %3, align 4
  ret i32 %72
}

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

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

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

3:                                                ; preds = %tailrecurse
  %4 = sext i32 %.tr to i64
  %5 = getelementptr inbounds [100005 x [2 x i32]], ptr @ch, i64 0, i64 %4
  %6 = load i32, ptr %5, align 8, !tbaa !5
  %7 = sext i32 %6 to i64
  %8 = getelementptr inbounds [100005 x i32], ptr @s, i64 0, i64 %7
  %9 = load i32, ptr %8, align 4, !tbaa !5
  %.not17 = icmp slt i32 %9, %.tr18.ph
  br i1 %.not17, label %10, label %tailrecurse

10:                                               ; preds = %3
  %11 = getelementptr inbounds [100005 x i32], ptr @cnt, i64 0, i64 %4
  %12 = load i32, ptr %11, align 4, !tbaa !5
  %13 = add nsw i32 %12, %9
  %14 = icmp slt i32 %13, %.tr18.ph
  br i1 %14, label %15, label %18

15:                                               ; preds = %10
  %gep = getelementptr [100005 x [2 x i32]], ptr getelementptr inbounds (i8, ptr @ch, i64 4), i64 0, i64 %4
  %16 = load i32, ptr %gep, align 4, !tbaa !5
  %17 = sub i32 %.tr18.ph, %13
  br label %tailrecurse.outer

18:                                               ; preds = %10
  %19 = getelementptr inbounds [100005 x i32], ptr @key, i64 0, i64 %4
  %20 = load i32, ptr %19, align 4, !tbaa !5
  br label %.loopexit

.loopexit:                                        ; preds = %tailrecurse, %18
  %.0 = phi i32 [ %20, %18 ], [ 0, %tailrecurse ]
  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, !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,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass