; 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.dbi_snap_result = type { i32, i32 }
%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.9, ptr, ptr, ptr, ptr, ptr, i32, i32, i32, ptr, ptr, i64, %struct.anon.10, %struct.anon.14, i8, ptr, ptr, ptr, %struct.anon.15, %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.8, i32, i64, i64, i64 }
%union.anon.8 = type { ptr }
%union.mdbx_atomic_uint32_t = type { i32 }
%struct.anon.9 = type { ptr, ptr, ptr, ptr }
%struct.anon.10 = type { i32, i32, i32, i32, i64, i8, i8, i8, i8, i32, i32, i8, i8, i8, %struct.anon.11, %union.anon.12 }
%struct.anon.11 = type { i16, i16, i16, i16 }
%union.anon.12 = type { i32 }
%struct.anon.14 = type { i64, i64, i64, i64, i64 }
%struct.anon.15 = 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.1 = external hidden unnamed_addr constant [17 x i8], align 1
@__func__.dbi_snap = external hidden unnamed_addr constant [9 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: nounwind uwtable
define dso_local i64 @dbi_snap(ptr noundef %0, i64 noundef %1) #2 {
  %3 = alloca %struct.dbi_snap_result, align 4
  %4 = alloca ptr, align 8
  %5 = alloca i64, align 8
  %6 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i64 %1, ptr %5, align 8, !tbaa !9
  br label %7

7:                                                ; preds = %2
  %8 = load i8, ptr getelementptr inbounds (%struct.libmdbx_globals, ptr @globals, i32 0, i32 4), align 1, !tbaa !11
  %9 = zext i8 %8 to i32
  %10 = and i32 %9, 1
  %11 = icmp ne i32 %10, 0
  %12 = xor i1 %11, true
  %13 = xor i1 %12, true
  %14 = zext i1 %13 to i32
  %15 = sext i32 %14 to i64
  %16 = call i64 @llvm.expect.i64(i64 %15, i64 1)
  %17 = icmp ne i64 %16, 0
  br i1 %17, label %18, label %37

18:                                               ; preds = %7
  br label %19

19:                                               ; preds = %18
  %20 = load i64, ptr %5, align 8, !tbaa !9
  %21 = load ptr, ptr %4, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.MDBX_env, ptr %21, i32 0, i32 42
  %23 = load i32, ptr %22, align 8, !tbaa !15
  %24 = zext i32 %23 to i64
  %25 = icmp ult i64 %20, %24
  %26 = xor i1 %25, true
  %27 = xor i1 %26, true
  %28 = xor i1 %27, true
  %29 = zext i1 %28 to i32
  %30 = sext i32 %29 to i64
  %31 = call i64 @llvm.expect.i64(i64 %30, i64 0)
  %32 = icmp ne i64 %31, 0
  br i1 %32, label %33, label %35

33:                                               ; preds = %19
  %34 = load ptr, ptr %4, align 8, !tbaa !5
  call void @mdbx_assert_fail(ptr noundef %34, ptr noundef @.str.1, ptr noundef @__func__.dbi_snap, i32 noundef 25) #5
  unreachable

35:                                               ; preds = %19
  br label %36

36:                                               ; preds = %35
  br label %37

37:                                               ; preds = %36, %7
  br label %38

38:                                               ; preds = %37
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #6
  %39 = load ptr, ptr %4, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.MDBX_env, ptr %39, i32 0, i32 27
  %41 = load ptr, ptr %40, align 8, !tbaa !25
  %42 = load i64, ptr %5, align 8, !tbaa !9
  %43 = getelementptr inbounds %union.mdbx_atomic_uint32_t, ptr %41, i64 %42
  %44 = call i32 @atomic_load32(ptr noundef %43, i32 noundef 1)
  store i32 %44, ptr %6, align 4, !tbaa !26
  br label %45

45:                                               ; preds = %62, %38
  %46 = load i32, ptr %6, align 4, !tbaa !26
  %47 = getelementptr inbounds %struct.dbi_snap_result, ptr %3, i32 0, i32 0
  store i32 %46, ptr %47, align 4, !tbaa !27
  %48 = load ptr, ptr %4, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct.MDBX_env, ptr %48, i32 0, i32 26
  %50 = load ptr, ptr %49, align 8, !tbaa !29
  %51 = load i64, ptr %5, align 8, !tbaa !9
  %52 = getelementptr inbounds i8, ptr %50, i64 %51
  %53 = load i8, ptr %52, align 1, !tbaa !30
  %54 = zext i8 %53 to i32
  %55 = getelementptr inbounds %struct.dbi_snap_result, ptr %3, i32 0, i32 1
  store i32 %54, ptr %55, align 4, !tbaa !31
  %56 = load ptr, ptr %4, align 8, !tbaa !5
  %57 = getelementptr inbounds %struct.MDBX_env, ptr %56, i32 0, i32 27
  %58 = load ptr, ptr %57, align 8, !tbaa !25
  %59 = load i64, ptr %5, align 8, !tbaa !9
  %60 = getelementptr inbounds %union.mdbx_atomic_uint32_t, ptr %58, i64 %59
  %61 = call i32 @atomic_load32(ptr noundef %60, i32 noundef 1)
  store i32 %61, ptr %6, align 4, !tbaa !26
  br label %62

62:                                               ; preds = %45
  %63 = load i32, ptr %6, align 4, !tbaa !26
  %64 = getelementptr inbounds %struct.dbi_snap_result, ptr %3, i32 0, i32 0
  %65 = load i32, ptr %64, align 4, !tbaa !27
  %66 = icmp ne i32 %63, %65
  %67 = xor i1 %66, true
  %68 = xor i1 %67, true
  %69 = zext i1 %68 to i32
  %70 = sext i32 %69 to i64
  %71 = call i64 @llvm.expect.i64(i64 %70, i64 0)
  %72 = icmp ne i64 %71, 0
  br i1 %72, label %45, label %73, !llvm.loop !32

73:                                               ; preds = %62
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #6
  %74 = load i64, ptr %3, align 4
  ret i64 %74
}

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

; Function Attrs: alwaysinline nounwind uwtable
declare hidden i32 @atomic_load32(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) #3

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 = { 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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { noreturn }
attributes #6 = { 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, !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, !13, i64 440}
!16 = !{!"MDBX_env", !7, i64 0, !13, i64 4, !13, i64 8, !17, i64 16, !13, i64 56, !13, i64 60, !17, i64 64, !6, i64 104, !18, i64 112, !18, i64 114, !18, i64 116, !18, i64 118, !18, i64 120, !18, i64 122, !7, i64 124, !7, i64 128, !7, i64 129, !18, i64 130, !13, i64 132, !13, i64 136, !13, i64 140, !13, i64 144, !19, 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, !20, i64 264, !22, i64 320, !14, i64 360, !6, i64 368, !6, i64 376, !6, i64 384, !23, i64 392, !7, i64 400, !13, i64 440, !13, i64 444, !6, i64 448, !24, i64 456, !7, i64 496, !7, i64 536}
!17 = !{!"osal_mmap", !7, i64 0, !13, i64 8, !10, i64 16, !10, i64 24, !10, i64 32}
!18 = !{!"short", !7, i64 0}
!19 = !{!"", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24}
!20 = !{!"", !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, !14, i64 36, !14, i64 37, !14, i64 38, !21, i64 40, !7, i64 48}
!21 = !{!"", !18, i64 0, !18, i64 2, !18, i64 4, !18, i64 6}
!22 = !{!"", !10, i64 0, !10, i64 8, !10, i64 16, !10, i64 24, !10, i64 32}
!23 = !{!"", !10, i64 0}
!24 = !{!"osal_ioring", !13, i64 0, !13, i64 4, !13, i64 8, !6, i64 16, !6, i64 24, !6, i64 32}
!25 = !{!16, !6, i64 216}
!26 = !{!13, !13, i64 0}
!27 = !{!28, !13, i64 0}
!28 = !{!"dbi_snap_result", !13, i64 0, !13, i64 4}
!29 = !{!16, !6, i64 208}
!30 = !{!7, !7, i64 0}
!31 = !{!28, !13, i64 4}
!32 = distinct !{!32, !33}
!33 = !{!"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 }
%union.mdbx_atomic_uint32_t = type { i32 }

@globals = external local_unnamed_addr global %struct.libmdbx_globals, align 8
@.str.1 = external hidden unnamed_addr constant [17 x i8], align 1
@__func__.dbi_snap = external hidden unnamed_addr constant [9 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: nounwind uwtable
define dso_local range(i64 0, 1099511627776) i64 @dbi_snap(ptr noundef %0, i64 noundef %1) local_unnamed_addr #1 {
  %3 = load i8, ptr getelementptr inbounds (i8, ptr @globals, i64 25), align 1, !tbaa !5
  %4 = and i8 %3, 1
  %.not = icmp eq i8 %4, 0
  br i1 %.not, label %10, label %5, !prof !13

5:                                                ; preds = %2
  %6 = getelementptr inbounds i8, ptr %0, i64 440
  %7 = load i32, ptr %6, align 8, !tbaa !14
  %8 = zext i32 %7 to i64
  %.not11 = icmp ugt i64 %8, %1
  br i1 %.not11, label %10, label %9, !prof !24

9:                                                ; preds = %5
  tail call void @mdbx_assert_fail(ptr noundef nonnull %0, ptr noundef nonnull @.str.1, ptr noundef nonnull @__func__.dbi_snap, i32 noundef 25) #3
  unreachable

10:                                               ; preds = %2, %5
  %11 = getelementptr inbounds i8, ptr %0, i64 216
  %12 = load ptr, ptr %11, align 8, !tbaa !25
  %13 = getelementptr inbounds %union.mdbx_atomic_uint32_t, ptr %12, i64 %1
  %14 = tail call i32 @atomic_load32(ptr noundef %13, i32 noundef 1)
  %15 = getelementptr inbounds i8, ptr %0, i64 208
  br label %16

16:                                               ; preds = %16, %10
  %.0 = phi i32 [ %14, %10 ], [ %22, %16 ]
  %17 = load ptr, ptr %15, align 8, !tbaa !26
  %18 = getelementptr inbounds i8, ptr %17, i64 %1
  %19 = load i8, ptr %18, align 1, !tbaa !27
  %20 = load ptr, ptr %11, align 8, !tbaa !25
  %21 = getelementptr inbounds %union.mdbx_atomic_uint32_t, ptr %20, i64 %1
  %22 = tail call i32 @atomic_load32(ptr noundef %21, i32 noundef 1)
  %.not12 = icmp eq i32 %22, %.0
  br i1 %.not12, label %23, label %16, !prof !24, !llvm.loop !28

23:                                               ; preds = %16
  %.sroa.3.0.insert.ext = zext i8 %19 to i64
  %.sroa.3.0.insert.shift = shl nuw nsw i64 %.sroa.3.0.insert.ext, 32
  %.sroa.0.0.insert.ext = zext i32 %.0 to i64
  %.sroa.0.0.insert.insert = or disjoint i64 %.sroa.3.0.insert.shift, %.sroa.0.0.insert.ext
  ret i64 %.sroa.0.0.insert.insert
}

; Function Attrs: alwaysinline nounwind uwtable
declare hidden i32 @atomic_load32(ptr noundef, i32 noundef) local_unnamed_addr #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 = { 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 = { 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 = { 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, !9, i64 440}
!15 = !{!"MDBX_env", !7, i64 0, !9, i64 4, !9, i64 8, !16, i64 16, !9, i64 56, !9, i64 60, !16, i64 64, !12, i64 104, !17, i64 112, !17, i64 114, !17, i64 116, !17, i64 118, !17, i64 120, !17, i64 122, !7, i64 124, !7, i64 128, !7, i64 129, !17, i64 130, !9, i64 132, !9, i64 136, !9, i64 140, !9, i64 144, !18, 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, !19, i64 264, !21, i64 320, !10, i64 360, !12, i64 368, !12, i64 376, !12, i64 384, !22, i64 392, !7, i64 400, !9, i64 440, !9, i64 444, !12, i64 448, !23, i64 456, !7, i64 496, !7, i64 536}
!16 = !{!"osal_mmap", !7, i64 0, !9, i64 8, !11, i64 16, !11, i64 24, !11, i64 32}
!17 = !{!"short", !7, i64 0}
!18 = !{!"", !12, i64 0, !12, i64 8, !12, i64 16, !12, i64 24}
!19 = !{!"", !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, !10, i64 36, !10, i64 37, !10, i64 38, !20, i64 40, !7, i64 48}
!20 = !{!"", !17, i64 0, !17, i64 2, !17, i64 4, !17, i64 6}
!21 = !{!"", !11, i64 0, !11, i64 8, !11, i64 16, !11, i64 24, !11, i64 32}
!22 = !{!"", !11, i64 0}
!23 = !{!"osal_ioring", !9, i64 0, !9, i64 4, !9, i64 8, !12, i64 16, !12, i64 24, !12, i64 32}
!24 = !{!"branch_weights", !"expected", i32 2000, i32 1}
!25 = !{!15, !12, i64 216}
!26 = !{!15, !12, i64 208}
!27 = !{!7, !7, i64 0}
!28 = distinct !{!28, !29}
!29 = !{!"llvm.loop.mustprogress"}


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