; LLVM IR from erthink/libmdbx :: src (tree-ops)

; 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.6 }
%struct.anon.6 = 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_env = type { %union.mdbx_atomic_uint32_t, i32, i32, %struct.osal_mmap, i32, i32, %struct.osal_mmap, ptr, i16, i16, i16, i16, i16, i16, %union.mdbx_atomic_uint32_t, i8, i8, i16, i32, i32, i32, i32, %struct.anon, ptr, ptr, ptr, ptr, ptr, i32, i32, i32, ptr, ptr, i64, %struct.anon.0, %struct.anon.4, i8, ptr, ptr, ptr, %struct.anon.5, %union.pthread_mutex_t, i32, i32, ptr, %struct.osal_ioring, %union.pthread_mutex_t, [71 x %union.mdbx_atomic_uint64_t] }
%struct.osal_mmap = type { %union.anon, i32, i64, i64, i64 }
%union.anon = type { ptr }
%union.mdbx_atomic_uint32_t = type { i32 }
%struct.anon = type { ptr, ptr, ptr, ptr }
%struct.anon.0 = type { i32, i32, i32, i32, i64, i8, i8, i8, i8, i32, i32, i8, i8, i8, %struct.anon.1, %union.anon.2 }
%struct.anon.1 = type { i16, i16, i16, i16 }
%union.anon.2 = type { i32 }
%struct.anon.4 = type { i64, i64, i64, i64, i64 }
%struct.anon.5 = type { i64 }
%struct.osal_ioring = type { i32, i32, i32, ptr, ptr, ptr }
%union.mdbx_atomic_uint64_t = type { i64 }

@globals = external global %struct.libmdbx_globals, align 8
@.str = external hidden unnamed_addr constant [99 x i8], align 1
@__func__.recalculate_merge_thresholds = external hidden unnamed_addr constant [29 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local void @recalculate_merge_thresholds(ptr noundef %0) #0 {
  %2 = alloca ptr, align 8
  %3 = alloca i64, align 8
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #5
  %4 = load ptr, ptr %2, align 8, !tbaa !5
  %5 = call i64 @page_space(ptr noundef %4) #6
  store i64 %5, ptr %3, align 8, !tbaa !9
  %6 = load i64, ptr %3, align 8, !tbaa !9
  %7 = load i64, ptr %3, align 8, !tbaa !9
  %8 = load ptr, ptr %2, align 8, !tbaa !5
  %9 = getelementptr inbounds %struct.MDBX_env, ptr %8, i32 0, i32 34
  %10 = getelementptr inbounds %struct.anon.0, ptr %9, i32 0, i32 9
  %11 = load i32, ptr %10, align 4, !tbaa !11
  %12 = zext i32 %11 to i64
  %13 = mul i64 %7, %12
  %14 = lshr i64 %13, 16
  %15 = sub i64 %6, %14
  %16 = trunc i64 %15 to i16
  %17 = load ptr, ptr %2, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.MDBX_env, ptr %17, i32 0, i32 17
  store i16 %16, ptr %18, align 2, !tbaa !23
  br label %19

19:                                               ; preds = %1
  %20 = load i8, ptr getelementptr inbounds (%struct.libmdbx_globals, ptr @globals, i32 0, i32 4), align 1, !tbaa !24
  %21 = zext i8 %20 to i32
  %22 = and i32 %21, 1
  %23 = icmp ne i32 %22, 0
  %24 = xor i1 %23, true
  %25 = xor i1 %24, true
  %26 = zext i1 %25 to i32
  %27 = sext i32 %26 to i64
  %28 = call i64 @llvm.expect.i64(i64 %27, i64 1)
  %29 = icmp ne i64 %28, 0
  br i1 %29, label %30, label %62

30:                                               ; preds = %19
  br label %31

31:                                               ; preds = %30
  %32 = load ptr, ptr %2, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.MDBX_env, ptr %32, i32 0, i32 17
  %34 = load i16, ptr %33, align 2, !tbaa !23
  %35 = zext i16 %34 to i64
  %36 = load i64, ptr %3, align 8, !tbaa !9
  %37 = udiv i64 %36, 2
  %38 = icmp uge i64 %35, %37
  br i1 %38, label %39, label %48

39:                                               ; preds = %31
  %40 = load ptr, ptr %2, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.MDBX_env, ptr %40, i32 0, i32 17
  %42 = load i16, ptr %41, align 2, !tbaa !23
  %43 = zext i16 %42 to i64
  %44 = load i64, ptr %3, align 8, !tbaa !9
  %45 = udiv i64 %44, 64
  %46 = mul i64 %45, 63
  %47 = icmp ule i64 %43, %46
  br label %48

48:                                               ; preds = %39, %31
  %49 = phi i1 [ false, %31 ], [ %47, %39 ]
  %50 = xor i1 %49, true
  %51 = xor i1 %50, true
  %52 = xor i1 %51, true
  %53 = zext i1 %52 to i32
  %54 = sext i32 %53 to i64
  %55 = call i64 @llvm.expect.i64(i64 %54, i64 0)
  %56 = icmp ne i64 %55, 0
  br i1 %56, label %57, label %59

57:                                               ; preds = %48
  %58 = load ptr, ptr %2, align 8, !tbaa !5
  call void @mdbx_assert_fail(ptr noundef %58, ptr noundef @.str, ptr noundef @__func__.recalculate_merge_thresholds, i32 noundef 44) #7
  unreachable

59:                                               ; preds = %48
  br label %60

60:                                               ; preds = %59
  br label %61

61:                                               ; preds = %60
  br label %62

62:                                               ; preds = %61, %19
  br label %63

63:                                               ; preds = %62
  br label %64

64:                                               ; preds = %63
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #5
  ret void
}

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

; Function Attrs: inlinehint nounwind willreturn memory(read) uwtable
declare hidden i64 @page_space(ptr noundef) #2

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

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

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

attributes #0 = { 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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { inlinehint 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 #3 = { nocallback nofree nosync nounwind willreturn memory(none) }
attributes #4 = { 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 #5 = { nounwind }
attributes #6 = { nounwind willreturn memory(read) }
attributes #7 = { 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 = !{!"long", !7, i64 0}
!11 = !{!12, !13, i64 292}
!12 = !{!"MDBX_env", !7, i64 0, !13, i64 4, !13, i64 8, !14, i64 16, !13, i64 56, !13, i64 60, !14, i64 64, !6, i64 104, !15, i64 112, !15, i64 114, !15, i64 116, !15, i64 118, !15, i64 120, !15, i64 122, !7, i64 124, !7, i64 128, !7, i64 129, !15, i64 130, !13, i64 132, !13, i64 136, !13, i64 140, !13, i64 144, !16, i64 152, !6, i64 184, !6, i64 192, !6, i64 200, !6, i64 208, !6, i64 216, !13, i64 224, !13, i64 228, !13, i64 232, !6, i64 240, !6, i64 248, !10, i64 256, !17, i64 264, !20, i64 320, !18, i64 360, !6, i64 368, !6, i64 376, !6, i64 384, !21, i64 392, !7, i64 400, !13, i64 440, !13, i64 444, !6, i64 448, !22, i64 456, !7, i64 496, !7, i64 536}
!13 = !{!"int", !7, i64 0}
!14 = !{!"osal_mmap", !7, i64 0, !13, i64 8, !10, i64 16, !10, i64 24, !10, i64 32}
!15 = !{!"short", !7, i64 0}
!16 = !{!"", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24}
!17 = !{!"", !13, i64 0, !13, i64 4, !13, i64 8, !13, i64 12, !10, i64 16, !7, i64 24, !7, i64 25, !7, i64 26, !7, i64 27, !13, i64 28, !13, i64 32, !18, i64 36, !18, i64 37, !18, i64 38, !19, i64 40, !7, i64 48}
!18 = !{!"_Bool", !7, i64 0}
!19 = !{!"", !15, i64 0, !15, i64 2, !15, i64 4, !15, i64 6}
!20 = !{!"", !10, i64 0, !10, i64 8, !10, i64 16, !10, i64 24, !10, i64 32}
!21 = !{!"", !10, i64 0}
!22 = !{!"osal_ioring", !13, i64 0, !13, i64 4, !13, i64 8, !6, i64 16, !6, i64 24, !6, i64 32}
!23 = !{!12, !15, i64 130}
!24 = !{!25, !7, i64 25}
!25 = !{!"libmdbx_globals", !7, i64 0, !13, i64 16, !13, i64 20, !7, i64 24, !7, i64 25, !7, i64 26, !18, i64 27, !13, i64 28, !7, i64 32, !7, i64 40, !10, i64 80, !6, i64 88}


; 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.6 }
%struct.anon.6 = 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 = external hidden unnamed_addr constant [99 x i8], align 1
@__func__.recalculate_merge_thresholds = external hidden unnamed_addr constant [29 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local void @recalculate_merge_thresholds(ptr noundef %0) local_unnamed_addr #0 {
  %2 = tail call i64 @page_space(ptr noundef %0) #3
  %3 = getelementptr inbounds i8, ptr %0, i64 292
  %4 = load i32, ptr %3, align 4, !tbaa !5
  %5 = zext i32 %4 to i64
  %6 = mul i64 %2, %5
  %7 = lshr i64 %6, 16
  %8 = sub i64 %2, %7
  %9 = trunc i64 %8 to i16
  %10 = getelementptr inbounds i8, ptr %0, i64 130
  store i16 %9, ptr %10, align 2, !tbaa !21
  %11 = load i8, ptr getelementptr inbounds (i8, ptr @globals, i64 25), align 1, !tbaa !22
  %12 = and i8 %11, 1
  %.not = icmp eq i8 %12, 0
  br i1 %.not, label %21, label %13, !prof !24

13:                                               ; preds = %1
  %14 = and i64 %8, 65535
  %15 = lshr i64 %2, 1
  %.not9 = icmp ult i64 %14, %15
  %16 = lshr i64 %2, 6
  %17 = mul nuw i64 %16, 63
  %18 = icmp ugt i64 %14, %17
  %19 = select i1 %.not9, i1 true, i1 %18, !prof !24
  br i1 %19, label %20, label %21, !prof !24

20:                                               ; preds = %13
  tail call void @mdbx_assert_fail(ptr noundef nonnull %0, ptr noundef nonnull @.str, ptr noundef nonnull @__func__.recalculate_merge_thresholds, i32 noundef 44) #4
  unreachable

21:                                               ; preds = %13, %1
  ret void
}

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

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

attributes #0 = { 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 = { inlinehint 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 = { 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) }
attributes #4 = { 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, !9, i64 292}
!6 = !{!"MDBX_env", !7, i64 0, !9, i64 4, !9, i64 8, !10, i64 16, !9, i64 56, !9, i64 60, !10, i64 64, !12, i64 104, !13, i64 112, !13, i64 114, !13, i64 116, !13, i64 118, !13, i64 120, !13, i64 122, !7, i64 124, !7, i64 128, !7, i64 129, !13, i64 130, !9, i64 132, !9, i64 136, !9, i64 140, !9, i64 144, !14, i64 152, !12, i64 184, !12, i64 192, !12, i64 200, !12, i64 208, !12, i64 216, !9, i64 224, !9, i64 228, !9, i64 232, !12, i64 240, !12, i64 248, !11, i64 256, !15, i64 264, !18, i64 320, !16, i64 360, !12, i64 368, !12, i64 376, !12, i64 384, !19, i64 392, !7, i64 400, !9, i64 440, !9, i64 444, !12, i64 448, !20, i64 456, !7, i64 496, !7, i64 536}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!"int", !7, i64 0}
!10 = !{!"osal_mmap", !7, i64 0, !9, i64 8, !11, i64 16, !11, i64 24, !11, i64 32}
!11 = !{!"long", !7, i64 0}
!12 = !{!"any pointer", !7, i64 0}
!13 = !{!"short", !7, i64 0}
!14 = !{!"", !12, i64 0, !12, i64 8, !12, i64 16, !12, i64 24}
!15 = !{!"", !9, i64 0, !9, i64 4, !9, i64 8, !9, i64 12, !11, i64 16, !7, i64 24, !7, i64 25, !7, i64 26, !7, i64 27, !9, i64 28, !9, i64 32, !16, i64 36, !16, i64 37, !16, i64 38, !17, i64 40, !7, i64 48}
!16 = !{!"_Bool", !7, i64 0}
!17 = !{!"", !13, i64 0, !13, i64 2, !13, i64 4, !13, i64 6}
!18 = !{!"", !11, i64 0, !11, i64 8, !11, i64 16, !11, i64 24, !11, i64 32}
!19 = !{!"", !11, i64 0}
!20 = !{!"osal_ioring", !9, i64 0, !9, i64 4, !9, i64 8, !12, i64 16, !12, i64 24, !12, i64 32}
!21 = !{!6, !13, i64 130}
!22 = !{!23, !7, i64 25}
!23 = !{!"libmdbx_globals", !7, i64 0, !9, i64 16, !9, i64 20, !7, i64 24, !7, i64 25, !7, i64 26, !16, i64 27, !9, i64 28, !7, i64 32, !7, i64 40, !11, i64 80, !12, i64 88}
!24 = !{!"branch_weights", !"expected", i32 1, i32 2000}


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