; 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.libmdbx_globals = type { %union.bin128, i32, i32, i8, i8, i8, i8, i32, %union.logger_union, %union.pthread_mutex_t, i64, ptr }
%union.bin128 = type { %struct.anon }
%struct.anon = type { i64, i64 }
%union.logger_union = type { ptr }
%union.pthread_mutex_t = type { %struct.__pthread_mutex_s }
%struct.__pthread_mutex_s = type { i32, i32, i32, i32, i32, i16, i16, %struct.__pthread_internal_list }
%struct.__pthread_internal_list = type { ptr, ptr }
%struct.MDBX_rkl_iter = type { ptr, i32, i32 }
%struct.MDBX_rkl = type { i64, i64, i32, i32, ptr, [12 x i64] }

@globals = external global %struct.libmdbx_globals, align 8
@.str.9 = external hidden unnamed_addr constant [70 x i8], align 1
@__func__.rkl_iterator = external hidden unnamed_addr constant [13 x i8], align 1

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(none)
declare i64 @llvm.expect.i64(i64, i64) #1

; Function Attrs: noreturn
declare void @mdbx_assert_fail(ptr noundef, ptr noundef, ptr noundef, i32 noundef) #2

; Function Attrs: nounwind willreturn memory(read) uwtable
declare dso_local i64 @rkl_len(ptr noundef) #3

; Function Attrs: alwaysinline nounwind uwtable
declare hidden ptr @rkl_bsearch(ptr noundef, i64 noundef, i64 noundef) #4

; Function Attrs: nounwind uwtable
define dso_local { ptr, i64 } @rkl_iterator(ptr noundef %0, i1 noundef zeroext %1) #5 {
  %3 = alloca %struct.MDBX_rkl_iter, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i8, align 1
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  %8 = zext i1 %1 to i8
  store i8 %8, ptr %5, align 1, !tbaa !9
  %9 = getelementptr inbounds %struct.MDBX_rkl_iter, ptr %3, i32 0, i32 0
  %10 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %10, ptr %9, align 8, !tbaa !11
  %11 = getelementptr inbounds %struct.MDBX_rkl_iter, ptr %3, i32 0, i32 1
  %12 = load i8, ptr %5, align 1, !tbaa !9, !range !14, !noundef !15
  %13 = trunc i8 %12 to i1
  br i1 %13, label %14, label %17

14:                                               ; preds = %2
  %15 = load ptr, ptr %4, align 8, !tbaa !5
  %16 = call i64 @rkl_len(ptr noundef %15) #7
  br label %18

17:                                               ; preds = %2
  br label %18

18:                                               ; preds = %17, %14
  %19 = phi i64 [ %16, %14 ], [ 0, %17 ]
  %20 = trunc i64 %19 to i32
  store i32 %20, ptr %11, align 8, !tbaa !16
  %21 = getelementptr inbounds %struct.MDBX_rkl_iter, ptr %3, i32 0, i32 2
  store i32 0, ptr %21, align 4, !tbaa !17
  %22 = load ptr, ptr %4, align 8, !tbaa !5
  %23 = call zeroext i1 @solid_empty(ptr noundef %22)
  br i1 %23, label %110, label %24

24:                                               ; preds = %18
  %25 = load ptr, ptr %4, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.MDBX_rkl, ptr %25, i32 0, i32 2
  %27 = load i32, ptr %26, align 8, !tbaa !18
  %28 = icmp ne i32 %27, 0
  br i1 %28, label %29, label %110

29:                                               ; preds = %24
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #8
  %30 = load ptr, ptr %4, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.MDBX_rkl, ptr %30, i32 0, i32 4
  %32 = load ptr, ptr %31, align 8, !tbaa !21
  %33 = load ptr, ptr %4, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.MDBX_rkl, ptr %33, i32 0, i32 2
  %35 = load i32, ptr %34, align 8, !tbaa !18
  %36 = zext i32 %35 to i64
  %37 = load ptr, ptr %4, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.MDBX_rkl, ptr %37, i32 0, i32 0
  %39 = load i64, ptr %38, align 8, !tbaa !22
  %40 = call ptr @rkl_bsearch(ptr noundef %32, i64 noundef %36, i64 noundef %39)
  store ptr %40, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #8
  %41 = load ptr, ptr %4, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.MDBX_rkl, ptr %41, i32 0, i32 4
  %43 = load ptr, ptr %42, align 8, !tbaa !21
  %44 = load ptr, ptr %4, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.MDBX_rkl, ptr %44, i32 0, i32 2
  %46 = load i32, ptr %45, align 8, !tbaa !18
  %47 = zext i32 %46 to i64
  %48 = getelementptr inbounds i64, ptr %43, i64 %47
  store ptr %48, ptr %7, align 8, !tbaa !5
  br label %49

49:                                               ; preds = %29
  %50 = load i8, ptr getelementptr inbounds (%struct.libmdbx_globals, ptr @globals, i32 0, i32 4), align 1, !tbaa !23
  %51 = zext i8 %50 to i32
  %52 = and i32 %51, 1
  %53 = icmp ne i32 %52, 0
  %54 = xor i1 %53, true
  %55 = xor i1 %54, true
  %56 = zext i1 %55 to i32
  %57 = sext i32 %56 to i64
  %58 = call i64 @llvm.expect.i64(i64 %57, i64 1)
  %59 = icmp ne i64 %58, 0
  br i1 %59, label %60, label %97

60:                                               ; preds = %49
  br label %61

61:                                               ; preds = %60
  %62 = load ptr, ptr %6, align 8, !tbaa !5
  %63 = load ptr, ptr %4, align 8, !tbaa !5
  %64 = getelementptr inbounds %struct.MDBX_rkl, ptr %63, i32 0, i32 4
  %65 = load ptr, ptr %64, align 8, !tbaa !21
  %66 = icmp uge ptr %62, %65
  br i1 %66, label %67, label %84

67:                                               ; preds = %61
  %68 = load ptr, ptr %6, align 8, !tbaa !5
  %69 = load ptr, ptr %7, align 8, !tbaa !5
  %70 = icmp ule ptr %68, %69
  br i1 %70, label %71, label %84

71:                                               ; preds = %67
  %72 = load ptr, ptr %6, align 8, !tbaa !5
  %73 = load ptr, ptr %7, align 8, !tbaa !5
  %74 = icmp eq ptr %72, %73
  br i1 %74, label %82, label %75

75:                                               ; preds = %71
  %76 = load ptr, ptr %6, align 8, !tbaa !5
  %77 = load i64, ptr %76, align 8, !tbaa !25
  %78 = load ptr, ptr %4, align 8, !tbaa !5
  %79 = getelementptr inbounds %struct.MDBX_rkl, ptr %78, i32 0, i32 0
  %80 = load i64, ptr %79, align 8, !tbaa !22
  %81 = icmp ugt i64 %77, %80
  br label %82

82:                                               ; preds = %75, %71
  %83 = phi i1 [ true, %71 ], [ %81, %75 ]
  br label %84

84:                                               ; preds = %82, %67, %61
  %85 = phi i1 [ false, %67 ], [ false, %61 ], [ %83, %82 ]
  %86 = xor i1 %85, true
  %87 = xor i1 %86, true
  %88 = xor i1 %87, true
  %89 = zext i1 %88 to i32
  %90 = sext i32 %89 to i64
  %91 = call i64 @llvm.expect.i64(i64 %90, i64 0)
  %92 = icmp ne i64 %91, 0
  br i1 %92, label %93, label %94

93:                                               ; preds = %84
  call void @mdbx_assert_fail(ptr noundef null, ptr noundef @.str.9, ptr noundef @__func__.rkl_iterator, i32 noundef 420) #9
  unreachable

94:                                               ; preds = %84
  br label %95

95:                                               ; preds = %94
  br label %96

96:                                               ; preds = %95
  br label %97

97:                                               ; preds = %96, %49
  br label %98

98:                                               ; preds = %97
  br label %99

99:                                               ; preds = %98
  %100 = load ptr, ptr %6, align 8, !tbaa !5
  %101 = load ptr, ptr %4, align 8, !tbaa !5
  %102 = getelementptr inbounds %struct.MDBX_rkl, ptr %101, i32 0, i32 4
  %103 = load ptr, ptr %102, align 8, !tbaa !21
  %104 = ptrtoint ptr %100 to i64
  %105 = ptrtoint ptr %103 to i64
  %106 = sub i64 %104, %105
  %107 = sdiv exact i64 %106, 8
  %108 = trunc i64 %107 to i32
  %109 = getelementptr inbounds %struct.MDBX_rkl_iter, ptr %3, i32 0, i32 2
  store i32 %108, ptr %109, align 4, !tbaa !17
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #8
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #8
  br label %110

110:                                              ; preds = %99, %24, %18
  %111 = load { ptr, i64 }, ptr %3, align 8
  ret { ptr, i64 } %111
}

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

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nosync nounwind willreturn memory(none) }
attributes #2 = { noreturn "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 #3 = { 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" }
attributes #4 = { alwaysinline 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 #5 = { 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 #6 = { 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 #7 = { nounwind willreturn memory(read) }
attributes #8 = { nounwind }
attributes #9 = { noreturn }

!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, !10, i64 0}
!10 = !{!"_Bool", !7, i64 0}
!11 = !{!12, !6, i64 0}
!12 = !{!"MDBX_rkl_iter", !6, i64 0, !13, i64 8, !13, i64 12}
!13 = !{!"int", !7, i64 0}
!14 = !{i8 0, i8 2}
!15 = !{}
!16 = !{!12, !13, i64 8}
!17 = !{!12, !13, i64 12}
!18 = !{!19, !13, i64 16}
!19 = !{!"MDBX_rkl", !20, i64 0, !20, i64 8, !13, i64 16, !13, i64 20, !6, i64 24, !7, i64 32}
!20 = !{!"long", !7, i64 0}
!21 = !{!19, !6, i64 24}
!22 = !{!19, !20, i64 0}
!23 = !{!24, !7, i64 25}
!24 = !{!"libmdbx_globals", !7, i64 0, !13, i64 16, !13, i64 20, !7, i64 24, !7, i64 25, !7, i64 26, !10, i64 27, !13, i64 28, !7, i64 32, !7, i64 40, !20, i64 80, !6, i64 88}
!25 = !{!20, !20, i64 0}


; 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.libmdbx_globals = type { %union.bin128, i32, i32, i8, i8, i8, i8, i32, %union.logger_union, %union.pthread_mutex_t, i64, ptr }
%union.bin128 = type { %struct.anon }
%struct.anon = type { i64, i64 }
%union.logger_union = type { ptr }
%union.pthread_mutex_t = type { %struct.__pthread_mutex_s }
%struct.__pthread_mutex_s = type { i32, i32, i32, i32, i32, i16, i16, %struct.__pthread_internal_list }
%struct.__pthread_internal_list = type { ptr, ptr }

@globals = external local_unnamed_addr global %struct.libmdbx_globals, align 8
@.str.9 = external hidden unnamed_addr constant [70 x i8], align 1
@__func__.rkl_iterator = external hidden unnamed_addr constant [13 x i8], align 1

; Function Attrs: noreturn
declare void @mdbx_assert_fail(ptr noundef, ptr noundef, ptr noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress nofree nounwind willreturn memory(read) uwtable
declare dso_local i64 @rkl_len(ptr noundef) local_unnamed_addr #1

; Function Attrs: alwaysinline nounwind uwtable
declare hidden ptr @rkl_bsearch(ptr noundef, i64 noundef, i64 noundef) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
define dso_local { ptr, i64 } @rkl_iterator(ptr noundef %0, i1 noundef zeroext %1) local_unnamed_addr #3 {
  br i1 %1, label %3, label %6

3:                                                ; preds = %2
  %4 = tail call i64 @rkl_len(ptr noundef %0) #5
  %5 = and i64 %4, 4294967295
  br label %6

6:                                                ; preds = %2, %3
  %.sroa.2.8.insert.ext = phi i64 [ %5, %3 ], [ 0, %2 ]
  %7 = tail call zeroext i1 @solid_empty(ptr noundef %0)
  br i1 %7, label %34, label %8

8:                                                ; preds = %6
  %9 = getelementptr inbounds i8, ptr %0, i64 16
  %10 = load i32, ptr %9, align 8, !tbaa !5
  %.not = icmp eq i32 %10, 0
  br i1 %.not, label %34, label %11

11:                                               ; preds = %8
  %12 = getelementptr inbounds i8, ptr %0, i64 24
  %13 = load ptr, ptr %12, align 8, !tbaa !12
  %14 = zext i32 %10 to i64
  %15 = load i64, ptr %0, align 8, !tbaa !13
  %16 = tail call ptr @rkl_bsearch(ptr noundef %13, i64 noundef %14, i64 noundef %15)
  %17 = load ptr, ptr %12, align 8, !tbaa !12
  %18 = load i32, ptr %9, align 8, !tbaa !5
  %19 = zext i32 %18 to i64
  %20 = getelementptr inbounds i64, ptr %17, i64 %19
  %21 = load i8, ptr getelementptr inbounds (i8, ptr @globals, i64 25), align 1, !tbaa !14
  %22 = and i8 %21, 1
  %.not21 = icmp eq i8 %22, 0
  br i1 %.not21, label %.critedge25, label %23, !prof !17

23:                                               ; preds = %11
  %.not22 = icmp ult ptr %16, %17
  %.not23 = icmp ugt ptr %16, %20
  %or.cond = select i1 %.not22, i1 true, i1 %.not23
  br i1 %or.cond, label %.critedge, label %24, !prof !18

24:                                               ; preds = %23
  %25 = icmp eq ptr %16, %20
  br i1 %25, label %.critedge25, label %26

26:                                               ; preds = %24
  %27 = load i64, ptr %16, align 8, !tbaa !19
  %28 = load i64, ptr %0, align 8, !tbaa !13
  %.not26 = icmp ugt i64 %27, %28
  br i1 %.not26, label %.critedge25, label %.critedge, !prof !20

.critedge:                                        ; preds = %23, %26
  tail call void @mdbx_assert_fail(ptr noundef null, ptr noundef nonnull @.str.9, ptr noundef nonnull @__func__.rkl_iterator, i32 noundef 420) #6
  unreachable

.critedge25:                                      ; preds = %24, %26, %11
  %29 = ptrtoint ptr %16 to i64
  %30 = ptrtoint ptr %17 to i64
  %31 = sub i64 %29, %30
  %32 = shl i64 %31, 29
  %33 = and i64 %32, -4294967296
  br label %34

34:                                               ; preds = %.critedge25, %8, %6
  %.sroa.4.0 = phi i64 [ 0, %6 ], [ %33, %.critedge25 ], [ 0, %8 ]
  %.fca.0.insert = insertvalue { ptr, i64 } poison, ptr %0, 0
  %.sroa.2.8.insert.insert = or disjoint i64 %.sroa.4.0, %.sroa.2.8.insert.ext
  %.fca.1.insert = insertvalue { ptr, i64 } %.fca.0.insert, i64 %.sroa.2.8.insert.insert, 1
  ret { ptr, i64 } %.fca.1.insert
}

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

attributes #0 = { noreturn "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" }
attributes #2 = { alwaysinline 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 #3 = { 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 #4 = { 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 #5 = { nounwind willreturn memory(read) }
attributes #6 = { noreturn nounwind }

!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 = !{!6, !7, i64 0}
!14 = !{!15, !8, i64 25}
!15 = !{!"libmdbx_globals", !8, i64 0, !10, i64 16, !10, i64 20, !8, i64 24, !8, i64 25, !8, i64 26, !16, i64 27, !10, i64 28, !8, i64 32, !8, i64 40, !7, i64 80, !11, i64 88}
!16 = !{!"_Bool", !8, i64 0}
!17 = !{!"branch_weights", !"expected", i32 1, i32 2000}
!18 = !{!"branch_weights", i32 4001, i32 4000000}
!19 = !{!7, !7, i64 0}
!20 = !{!"branch_weights", !"expected", i32 2000, i32 1}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,LowerExpectIntrinsicPass,SROAPass,SimplifyCFGPass,TailCallElimPass