; LLVM IR from erthink/libmdbx :: src (rkl)

; 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.MDBX_rkl = type { i64, i64, i32, i32, ptr, [12 x i64] }

; Function Attrs: inlinehint nounwind uwtable
declare hidden zeroext i1 @solid_empty(ptr noundef) #0

; Function Attrs: nounwind willreturn memory(read) uwtable
define dso_local i64 @rkl_highest(ptr noundef %0) #1 {
  %2 = alloca i64, align 8
  %3 = alloca ptr, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  %4 = load ptr, ptr %3, align 8, !tbaa !5
  %5 = getelementptr inbounds %struct.MDBX_rkl, ptr %4, i32 0, i32 2
  %6 = load i32, ptr %5, align 8, !tbaa !9
  %7 = icmp ne i32 %6, 0
  br i1 %7, label %8, label %44

8:                                                ; preds = %1
  %9 = load ptr, ptr %3, align 8, !tbaa !5
  %10 = call zeroext i1 @solid_empty(ptr noundef %9)
  br i1 %10, label %26, label %11

11:                                               ; preds = %8
  %12 = load ptr, ptr %3, align 8, !tbaa !5
  %13 = getelementptr inbounds %struct.MDBX_rkl, ptr %12, i32 0, i32 4
  %14 = load ptr, ptr %13, align 8, !tbaa !13
  %15 = load ptr, ptr %3, align 8, !tbaa !5
  %16 = getelementptr inbounds %struct.MDBX_rkl, ptr %15, i32 0, i32 2
  %17 = load i32, ptr %16, align 8, !tbaa !9
  %18 = sub i32 %17, 1
  %19 = zext i32 %18 to i64
  %20 = getelementptr inbounds i64, ptr %14, i64 %19
  %21 = load i64, ptr %20, align 8, !tbaa !14
  %22 = load ptr, ptr %3, align 8, !tbaa !5
  %23 = getelementptr inbounds %struct.MDBX_rkl, ptr %22, i32 0, i32 1
  %24 = load i64, ptr %23, align 8, !tbaa !15
  %25 = icmp uge i64 %21, %24
  br i1 %25, label %26, label %37

26:                                               ; preds = %11, %8
  %27 = load ptr, ptr %3, align 8, !tbaa !5
  %28 = getelementptr inbounds %struct.MDBX_rkl, ptr %27, i32 0, i32 4
  %29 = load ptr, ptr %28, align 8, !tbaa !13
  %30 = load ptr, ptr %3, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.MDBX_rkl, ptr %30, i32 0, i32 2
  %32 = load i32, ptr %31, align 8, !tbaa !9
  %33 = sub i32 %32, 1
  %34 = zext i32 %33 to i64
  %35 = getelementptr inbounds i64, ptr %29, i64 %34
  %36 = load i64, ptr %35, align 8, !tbaa !14
  br label %42

37:                                               ; preds = %11
  %38 = load ptr, ptr %3, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.MDBX_rkl, ptr %38, i32 0, i32 1
  %40 = load i64, ptr %39, align 8, !tbaa !15
  %41 = sub i64 %40, 1
  br label %42

42:                                               ; preds = %37, %26
  %43 = phi i64 [ %36, %26 ], [ %41, %37 ]
  store i64 %43, ptr %2, align 8
  br label %55

44:                                               ; preds = %1
  %45 = load ptr, ptr %3, align 8, !tbaa !5
  %46 = call zeroext i1 @solid_empty(ptr noundef %45)
  br i1 %46, label %52, label %47

47:                                               ; preds = %44
  %48 = load ptr, ptr %3, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct.MDBX_rkl, ptr %48, i32 0, i32 1
  %50 = load i64, ptr %49, align 8, !tbaa !15
  %51 = sub i64 %50, 1
  br label %53

52:                                               ; preds = %44
  br label %53

53:                                               ; preds = %52, %47
  %54 = phi i64 [ %51, %47 ], [ 0, %52 ]
  store i64 %54, ptr %2, align 8
  br label %55

55:                                               ; preds = %53, %42
  %56 = load i64, ptr %2, align 8
  ret i64 %56
}

attributes #0 = { inlinehint 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 #1 = { nounwind willreturn memory(read) 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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !12, i64 16}
!10 = !{!"MDBX_rkl", !11, i64 0, !11, i64 8, !12, i64 16, !12, i64 20, !6, i64 24, !7, i64 32}
!11 = !{!"long", !7, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!10, !6, i64 24}
!14 = !{!11, !11, i64 0}
!15 = !{!10, !11, i64 8}


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

; Function Attrs: inlinehint nounwind uwtable
declare hidden zeroext i1 @solid_empty(ptr noundef) local_unnamed_addr #0

; Function Attrs: mustprogress nofree nounwind willreturn memory(read) uwtable
define dso_local i64 @rkl_highest(ptr noundef %0) local_unnamed_addr #1 {
  %2 = getelementptr inbounds i8, ptr %0, i64 16
  %3 = load i32, ptr %2, align 8, !tbaa !5
  %.not = icmp eq i32 %3, 0
  %4 = tail call zeroext i1 @solid_empty(ptr noundef nonnull %0)
  br i1 %.not, label %15, label %5

5:                                                ; preds = %1
  %.phi.trans.insert = getelementptr inbounds i8, ptr %0, i64 24
  %.pre = load ptr, ptr %.phi.trans.insert, align 8, !tbaa !12
  %.pre12 = load i32, ptr %2, align 8, !tbaa !5
  %.pre13 = add i32 %.pre12, -1
  %.pre14 = zext i32 %.pre13 to i64
  br i1 %4, label %._crit_edge, label %6

6:                                                ; preds = %5
  %7 = getelementptr inbounds i64, ptr %.pre, i64 %.pre14
  %8 = load i64, ptr %7, align 8, !tbaa !13
  %9 = getelementptr inbounds i8, ptr %0, i64 8
  %10 = load i64, ptr %9, align 8, !tbaa !14
  %.not11 = icmp ult i64 %8, %10
  br i1 %.not11, label %13, label %._crit_edge

._crit_edge:                                      ; preds = %5, %6
  %11 = getelementptr inbounds i64, ptr %.pre, i64 %.pre14
  %12 = load i64, ptr %11, align 8, !tbaa !13
  br label %20

13:                                               ; preds = %6
  %14 = add i64 %10, -1
  br label %20

15:                                               ; preds = %1
  br i1 %4, label %20, label %16

16:                                               ; preds = %15
  %17 = getelementptr inbounds i8, ptr %0, i64 8
  %18 = load i64, ptr %17, align 8, !tbaa !14
  %19 = add i64 %18, -1
  br label %20

20:                                               ; preds = %16, %15, %._crit_edge, %13
  %.0 = phi i64 [ %12, %._crit_edge ], [ %14, %13 ], [ %19, %16 ], [ 0, %15 ]
  ret i64 %.0
}

attributes #0 = { inlinehint 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 #1 = { mustprogress nofree nounwind willreturn memory(read) 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, !10, i64 16}
!6 = !{!"MDBX_rkl", !7, i64 0, !7, i64 8, !10, i64 16, !10, i64 20, !11, i64 24, !8, i64 32}
!7 = !{!"long", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!"any pointer", !8, i64 0}
!12 = !{!6, !11, i64 24}
!13 = !{!7, !7, i64 0}
!14 = !{!6, !7, i64 8}


; Effective O3 passes: BDCEPass,CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,InstCombinePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass