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

; 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_txn = type { i32, i32, i64, i64, ptr, ptr, %struct.geo, i64, i64, ptr, ptr, ptr, ptr, ptr, ptr, %struct.MDBX_canary, ptr, %union.anon.2 }
%struct.geo = type { i16, i16, i32, i32, %union.anon, %union.anon.1 }
%union.anon = type { i32 }
%union.anon.1 = type { i32 }
%struct.MDBX_canary = type { i64, i64, i64, i64 }
%union.anon.2 = type { %struct.anon.4 }
%struct.anon.4 = type { %struct.troika, ptr, %struct.anon.5, i8, i32, i32, i64, ptr, ptr, ptr, i64, %union.anon.6 }
%struct.troika = type { i8, i8, i8, i8, i32, [3 x i64] }
%struct.anon.5 = type { %struct.MDBX_rkl, i64, %struct.MDBX_rkl }
%struct.MDBX_rkl = type { i64, i64, i32, i32, ptr, [12 x i64] }
%union.anon.6 = type { %struct.anon.7 }
%struct.anon.7 = type { i64, ptr }
%struct.tree = type { i16, i16, i32, i32, i32, i32, i32, i64, i64, i64 }

@globals = external global %struct.libmdbx_globals, align 8
@.str.30 = external hidden unnamed_addr constant [25 x i8], align 1
@__func__.dbi_dig = external hidden unnamed_addr constant [8 x i8], align 1

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

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

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

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

; Function Attrs: inlinehint nounwind uwtable
declare hidden zeroext i8 @dbi_state(ptr noundef, i64 noundef) #3

; Function Attrs: cold nounwind optsize uwtable
define dso_local ptr @dbi_dig(ptr noundef %0, i64 noundef %1, ptr noundef %2) #4 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i64, align 8
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca i8, align 1
  %10 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store i64 %1, ptr %6, align 8, !tbaa !9
  store ptr %2, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #5
  %11 = load ptr, ptr %5, align 8, !tbaa !5
  store ptr %11, ptr %8, align 8, !tbaa !5
  br label %12

12:                                               ; preds = %97, %3
  br label %13

13:                                               ; preds = %12
  %14 = load i8, ptr getelementptr inbounds (%struct.libmdbx_globals, ptr @globals, i32 0, i32 4), align 1, !tbaa !11
  %15 = zext i8 %14 to i32
  %16 = and i32 %15, 1
  %17 = icmp ne i32 %16, 0
  %18 = xor i1 %17, true
  %19 = xor i1 %18, true
  %20 = zext i1 %19 to i32
  %21 = sext i32 %20 to i64
  %22 = call i64 @llvm.expect.i64(i64 %21, i64 1)
  %23 = icmp ne i64 %22, 0
  br i1 %23, label %24, label %47

24:                                               ; preds = %13
  br label %25

25:                                               ; preds = %24
  %26 = load ptr, ptr %5, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.MDBX_txn, ptr %26, i32 0, i32 2
  %28 = load i64, ptr %27, align 8, !tbaa !15
  %29 = load ptr, ptr %8, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.MDBX_txn, ptr %29, i32 0, i32 2
  %31 = load i64, ptr %30, align 8, !tbaa !15
  %32 = icmp eq i64 %28, %31
  %33 = xor i1 %32, true
  %34 = xor i1 %33, true
  %35 = xor i1 %34, true
  %36 = zext i1 %35 to i32
  %37 = sext i32 %36 to i64
  %38 = call i64 @llvm.expect.i64(i64 %37, i64 0)
  %39 = icmp ne i64 %38, 0
  br i1 %39, label %40, label %44

40:                                               ; preds = %25
  %41 = load ptr, ptr %5, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.MDBX_txn, ptr %41, i32 0, i32 9
  %43 = load ptr, ptr %42, align 8, !tbaa !20
  call void @mdbx_assert_fail(ptr noundef %43, ptr noundef @.str.30, ptr noundef @__func__.dbi_dig, i32 noundef 672) #6
  unreachable

44:                                               ; preds = %25
  br label %45

45:                                               ; preds = %44
  br label %46

46:                                               ; preds = %45
  br label %47

47:                                               ; preds = %46, %13
  br label %48

48:                                               ; preds = %47
  br label %49

49:                                               ; preds = %48
  call void @llvm.lifetime.start.p0(i64 1, ptr %9) #5
  %50 = load ptr, ptr %8, align 8, !tbaa !5
  %51 = load i64, ptr %6, align 8, !tbaa !9
  %52 = call zeroext i8 @dbi_state(ptr noundef %50, i64 noundef %51)
  store i8 %52, ptr %9, align 1, !tbaa !21
  %53 = load i8, ptr %9, align 1, !tbaa !21
  %54 = zext i8 %53 to i32
  %55 = and i32 %54, 128
  %56 = icmp ne i32 %55, 0
  br i1 %56, label %57, label %90

57:                                               ; preds = %49
  %58 = load i8, ptr %9, align 1, !tbaa !21
  %59 = zext i8 %58 to i32
  %60 = and i32 %59, 82
  switch i32 %60, label %70 [
    i32 16, label %61
    i32 64, label %61
    i32 0, label %67
    i32 18, label %69
    i32 66, label %69
  ]

61:                                               ; preds = %57, %57
  %62 = load ptr, ptr %8, align 8, !tbaa !5
  %63 = getelementptr inbounds %struct.MDBX_txn, ptr %62, i32 0, i32 10
  %64 = load ptr, ptr %63, align 8, !tbaa !22
  %65 = load i64, ptr %6, align 8, !tbaa !9
  %66 = getelementptr inbounds %struct.tree, ptr %64, i64 %65
  store ptr %66, ptr %4, align 8
  store i32 1, ptr %10, align 4
  br label %94

67:                                               ; preds = %57
  %68 = load ptr, ptr %7, align 8, !tbaa !5
  store ptr %68, ptr %4, align 8
  store i32 1, ptr %10, align 4
  br label %94

69:                                               ; preds = %57, %57
  br label %89

70:                                               ; preds = %57
  br label %71

71:                                               ; preds = %70
  %72 = load i8, ptr getelementptr inbounds (%struct.libmdbx_globals, ptr @globals, i32 0, i32 4), align 1, !tbaa !11
  %73 = zext i8 %72 to i32
  %74 = and i32 %73, 1
  %75 = icmp ne i32 %74, 0
  %76 = xor i1 %75, true
  %77 = xor i1 %76, true
  %78 = zext i1 %77 to i32
  %79 = sext i32 %78 to i64
  %80 = call i64 @llvm.expect.i64(i64 %79, i64 1)
  %81 = icmp ne i64 %80, 0
  br i1 %81, label %82, label %86

82:                                               ; preds = %71
  br label %83

83:                                               ; preds = %82
  br label %84

84:                                               ; preds = %83
  br label %85

85:                                               ; preds = %84
  br label %86

86:                                               ; preds = %85, %71
  br label %87

87:                                               ; preds = %86
  br label %88

88:                                               ; preds = %87
  br label %89

89:                                               ; preds = %88, %69
  br label %90

90:                                               ; preds = %89, %49
  %91 = load ptr, ptr %8, align 8, !tbaa !5
  %92 = getelementptr inbounds %struct.MDBX_txn, ptr %91, i32 0, i32 4
  %93 = load ptr, ptr %92, align 8, !tbaa !23
  store ptr %93, ptr %8, align 8, !tbaa !5
  store i32 0, ptr %10, align 4
  br label %94

94:                                               ; preds = %90, %67, %61
  call void @llvm.lifetime.end.p0(i64 1, ptr %9) #5
  %95 = load i32, ptr %10, align 4
  switch i32 %95, label %102 [
    i32 0, label %96
  ]

96:                                               ; preds = %94
  br label %97

97:                                               ; preds = %96
  %98 = load ptr, ptr %8, align 8, !tbaa !5
  %99 = icmp ne ptr %98, null
  br i1 %99, label %12, label %100, !llvm.loop !24

100:                                              ; preds = %97
  %101 = load ptr, ptr %7, align 8, !tbaa !5
  store ptr %101, ptr %4, align 8
  store i32 1, ptr %10, align 4
  br label %102

102:                                              ; preds = %100, %94
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #5
  %103 = load ptr, ptr %4, align 8
  ret ptr %103
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(none) }
attributes #1 = { 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 #2 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { 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 #4 = { cold nounwind optsize 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 }
attributes #6 = { 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, !7, i64 25}
!12 = !{!"libmdbx_globals", !7, i64 0, !13, i64 16, !13, i64 20, !7, i64 24, !7, i64 25, !7, i64 26, !14, i64 27, !13, i64 28, !7, i64 32, !7, i64 40, !10, i64 80, !6, i64 88}
!13 = !{!"int", !7, i64 0}
!14 = !{!"_Bool", !7, i64 0}
!15 = !{!16, !10, i64 8}
!16 = !{!"MDBX_txn", !13, i64 0, !13, i64 4, !10, i64 8, !10, i64 16, !6, i64 24, !6, i64 32, !17, i64 40, !10, i64 64, !10, i64 72, !6, i64 80, !6, i64 88, !6, i64 96, !6, i64 104, !6, i64 112, !6, i64 120, !19, i64 128, !6, i64 160, !7, i64 168}
!17 = !{!"geo", !18, i64 0, !18, i64 2, !13, i64 4, !13, i64 8, !7, i64 12, !7, i64 16}
!18 = !{!"short", !7, i64 0}
!19 = !{!"MDBX_canary", !10, i64 0, !10, i64 8, !10, i64 16, !10, i64 24}
!20 = !{!16, !6, i64 80}
!21 = !{!7, !7, i64 0}
!22 = !{!16, !6, i64 88}
!23 = !{!16, !6, i64 24}
!24 = distinct !{!24, !25}
!25 = !{!"llvm.loop.mustprogress"}


; 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 }
%struct.tree = type { i16, i16, i32, i32, i32, i32, i32, i64, i64, i64 }

@globals = external local_unnamed_addr global %struct.libmdbx_globals, align 8
@.str.30 = external hidden unnamed_addr constant [25 x i8], align 1
@__func__.dbi_dig = external hidden unnamed_addr constant [8 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: inlinehint nounwind uwtable
declare hidden zeroext i8 @dbi_state(ptr noundef, i64 noundef) local_unnamed_addr #1

; Function Attrs: cold nounwind optsize uwtable
define dso_local ptr @dbi_dig(ptr noundef %0, i64 noundef %1, ptr noundef readnone %2) local_unnamed_addr #2 {
  %4 = getelementptr inbounds i8, ptr %0, i64 8
  br label %5

5:                                                ; preds = %23, %3
  %.016 = phi ptr [ %0, %3 ], [ %25, %23 ]
  %6 = load i8, ptr getelementptr inbounds (i8, ptr @globals, i64 25), align 1, !tbaa !5
  %7 = and i8 %6, 1
  %.not = icmp eq i8 %7, 0
  br i1 %.not, label %15, label %8, !prof !13

8:                                                ; preds = %5
  %9 = load i64, ptr %4, align 8, !tbaa !14
  %10 = getelementptr inbounds i8, ptr %.016, i64 8
  %11 = load i64, ptr %10, align 8, !tbaa !14
  %.not18 = icmp eq i64 %9, %11
  br i1 %.not18, label %15, label %12, !prof !19

12:                                               ; preds = %8
  %13 = getelementptr inbounds i8, ptr %0, i64 80
  %14 = load ptr, ptr %13, align 8, !tbaa !20
  tail call void @mdbx_assert_fail(ptr noundef %14, ptr noundef nonnull @.str.30, ptr noundef nonnull @__func__.dbi_dig, i32 noundef 672) #3
  unreachable

15:                                               ; preds = %8, %5
  %16 = tail call zeroext i8 @dbi_state(ptr noundef %.016, i64 noundef %1)
  %.not19 = icmp sgt i8 %16, -1
  br i1 %.not19, label %23, label %17

17:                                               ; preds = %15
  %18 = and i8 %16, 82
  switch i8 %18, label %23 [
    i8 16, label %19
    i8 64, label %19
    i8 0, label %.thread
  ]

19:                                               ; preds = %17, %17
  %20 = getelementptr inbounds i8, ptr %.016, i64 88
  %21 = load ptr, ptr %20, align 8, !tbaa !21
  %22 = getelementptr inbounds %struct.tree, ptr %21, i64 %1
  br label %.thread

23:                                               ; preds = %17, %15
  %24 = getelementptr inbounds i8, ptr %.016, i64 24
  %25 = load ptr, ptr %24, align 8, !tbaa !22
  %.not20 = icmp eq ptr %25, null
  br i1 %.not20, label %.thread, label %5, !llvm.loop !23

.thread:                                          ; preds = %17, %23, %19
  %.2 = phi ptr [ %22, %19 ], [ %2, %23 ], [ %2, %17 ]
  ret ptr %.2
}

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 = { 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 #2 = { cold nounwind optsize 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 = { 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, !7, i64 25}
!6 = !{!"libmdbx_globals", !7, i64 0, !9, i64 16, !9, i64 20, !7, i64 24, !7, i64 25, !7, i64 26, !10, i64 27, !9, i64 28, !7, i64 32, !7, i64 40, !11, i64 80, !12, i64 88}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!"int", !7, i64 0}
!10 = !{!"_Bool", !7, i64 0}
!11 = !{!"long", !7, i64 0}
!12 = !{!"any pointer", !7, i64 0}
!13 = !{!"branch_weights", !"expected", i32 1, i32 2000}
!14 = !{!15, !11, i64 8}
!15 = !{!"MDBX_txn", !9, i64 0, !9, i64 4, !11, i64 8, !11, i64 16, !12, i64 24, !12, i64 32, !16, i64 40, !11, i64 64, !11, i64 72, !12, i64 80, !12, i64 88, !12, i64 96, !12, i64 104, !12, i64 112, !12, i64 120, !18, i64 128, !12, i64 160, !7, i64 168}
!16 = !{!"geo", !17, i64 0, !17, i64 2, !9, i64 4, !9, i64 8, !7, i64 12, !7, i64 16}
!17 = !{!"short", !7, i64 0}
!18 = !{!"MDBX_canary", !11, i64 0, !11, i64 8, !11, i64 16, !11, i64 24}
!19 = !{!"branch_weights", !"expected", i32 2000, i32 1}
!20 = !{!15, !12, i64 80}
!21 = !{!15, !12, i64 88}
!22 = !{!15, !12, i64 24}
!23 = distinct !{!23, !24}
!24 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopSimplifyPass,LoopUnrollPass,LowerExpectIntrinsicPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass