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

; 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_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.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_uint64_t = type { i64 }
%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.8 }
%struct.geo = type { i16, i16, i32, i32, %union.anon.6, %union.anon.7 }
%union.anon.6 = type { i32 }
%union.anon.7 = type { i32 }
%struct.MDBX_canary = type { i64, i64, i64, i64 }
%union.anon.8 = type { %struct.anon.10 }
%struct.anon.10 = type { %struct.troika, ptr, %struct.anon.11, i8, i32, i32, i64, ptr, ptr, ptr, i64, %union.anon.12 }
%struct.troika = type { i8, i8, i8, i8, i32, [3 x i64] }
%struct.anon.11 = type { %struct.MDBX_rkl, i64, %struct.MDBX_rkl }
%struct.MDBX_rkl = type { i64, i64, i32, i32, ptr, [12 x i64] }
%union.anon.12 = type { %struct.anon.13 }
%struct.anon.13 = type { i64, ptr }

; Function Attrs: nounwind uwtable
define dso_local ptr @env_owned_wrtxn(ptr noundef %0) #0 {
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca i8, align 1
  %5 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  %6 = load ptr, ptr %3, align 8, !tbaa !5
  %7 = getelementptr inbounds %struct.MDBX_env, ptr %6, i32 0, i32 24
  %8 = load ptr, ptr %7, align 8, !tbaa !9
  %9 = icmp ne ptr %8, null
  %10 = xor i1 %9, true
  %11 = xor i1 %10, true
  %12 = zext i1 %11 to i32
  %13 = sext i32 %12 to i64
  %14 = call i64 @llvm.expect.i64(i64 %13, i64 1)
  %15 = icmp ne i64 %14, 0
  br i1 %15, label %16, label %64

16:                                               ; preds = %1
  call void @llvm.lifetime.start.p0(i64 1, ptr %4) #4
  %17 = load ptr, ptr %3, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.MDBX_env, ptr %17, i32 0, i32 1
  %19 = load i32, ptr %18, align 4, !tbaa !22
  %20 = and i32 %19, 2097152
  %21 = icmp ne i32 %20, 0
  br i1 %21, label %22, label %30

22:                                               ; preds = %16
  %23 = load ptr, ptr %3, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.MDBX_env, ptr %23, i32 0, i32 24
  %25 = load ptr, ptr %24, align 8, !tbaa !9
  %26 = getelementptr inbounds %struct.MDBX_txn, ptr %25, i32 0, i32 3
  %27 = load i64, ptr %26, align 8, !tbaa !23
  %28 = icmp ne i64 %27, 0
  %29 = zext i1 %28 to i32
  br label %39

30:                                               ; preds = %16
  %31 = load ptr, ptr %3, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.MDBX_env, ptr %31, i32 0, i32 24
  %33 = load ptr, ptr %32, align 8, !tbaa !9
  %34 = getelementptr inbounds %struct.MDBX_txn, ptr %33, i32 0, i32 3
  %35 = load i64, ptr %34, align 8, !tbaa !23
  %36 = call i64 @osal_thread_self()
  %37 = icmp eq i64 %35, %36
  %38 = zext i1 %37 to i32
  br label %39

39:                                               ; preds = %30, %22
  %40 = phi i32 [ %29, %22 ], [ %38, %30 ]
  %41 = icmp ne i32 %40, 0
  %42 = zext i1 %41 to i8
  store i8 %42, ptr %4, align 1, !tbaa !27
  %43 = load i8, ptr %4, align 1, !tbaa !27, !range !28, !noundef !29
  %44 = trunc i8 %43 to i1
  br i1 %44, label %45, label %60

45:                                               ; preds = %39
  %46 = load ptr, ptr %3, align 8, !tbaa !5
  %47 = getelementptr inbounds %struct.MDBX_env, ptr %46, i32 0, i32 39
  %48 = load ptr, ptr %47, align 8, !tbaa !30
  %49 = icmp ne ptr %48, null
  br i1 %49, label %50, label %54

50:                                               ; preds = %45
  %51 = load ptr, ptr %3, align 8, !tbaa !5
  %52 = getelementptr inbounds %struct.MDBX_env, ptr %51, i32 0, i32 39
  %53 = load ptr, ptr %52, align 8, !tbaa !30
  br label %58

54:                                               ; preds = %45
  %55 = load ptr, ptr %3, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct.MDBX_env, ptr %55, i32 0, i32 24
  %57 = load ptr, ptr %56, align 8, !tbaa !9
  br label %58

58:                                               ; preds = %54, %50
  %59 = phi ptr [ %53, %50 ], [ %57, %54 ]
  store ptr %59, ptr %2, align 8
  store i32 1, ptr %5, align 4
  br label %61

60:                                               ; preds = %39
  store i32 0, ptr %5, align 4
  br label %61

61:                                               ; preds = %60, %58
  call void @llvm.lifetime.end.p0(i64 1, ptr %4) #4
  %62 = load i32, ptr %5, align 4
  switch i32 %62, label %67 [
    i32 0, label %63
    i32 1, label %65
  ]

63:                                               ; preds = %61
  br label %64

64:                                               ; preds = %63, %1
  store ptr null, ptr %2, align 8
  br label %65

65:                                               ; preds = %64, %61
  %66 = load ptr, ptr %2, align 8
  ret ptr %66

67:                                               ; preds = %61
  unreachable
}

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

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

; Function Attrs: inlinehint nounwind uwtable
declare hidden i64 @osal_thread_self() #3

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #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 = { nocallback nofree nosync nounwind willreturn memory(none) }
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 = { 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, !6, i64 192}
!10 = !{!"MDBX_env", !7, i64 0, !11, i64 4, !11, i64 8, !12, i64 16, !11, i64 56, !11, i64 60, !12, i64 64, !6, i64 104, !14, i64 112, !14, i64 114, !14, i64 116, !14, i64 118, !14, i64 120, !14, i64 122, !7, i64 124, !7, i64 128, !7, i64 129, !14, i64 130, !11, i64 132, !11, i64 136, !11, i64 140, !11, i64 144, !15, i64 152, !6, i64 184, !6, i64 192, !6, i64 200, !6, i64 208, !6, i64 216, !11, i64 224, !11, i64 228, !11, i64 232, !6, i64 240, !6, i64 248, !13, i64 256, !16, i64 264, !19, i64 320, !17, i64 360, !6, i64 368, !6, i64 376, !6, i64 384, !20, i64 392, !7, i64 400, !11, i64 440, !11, i64 444, !6, i64 448, !21, i64 456, !7, i64 496, !7, i64 536}
!11 = !{!"int", !7, i64 0}
!12 = !{!"osal_mmap", !7, i64 0, !11, i64 8, !13, i64 16, !13, i64 24, !13, i64 32}
!13 = !{!"long", !7, i64 0}
!14 = !{!"short", !7, i64 0}
!15 = !{!"", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24}
!16 = !{!"", !11, i64 0, !11, i64 4, !11, i64 8, !11, i64 12, !13, i64 16, !7, i64 24, !7, i64 25, !7, i64 26, !7, i64 27, !11, i64 28, !11, i64 32, !17, i64 36, !17, i64 37, !17, i64 38, !18, i64 40, !7, i64 48}
!17 = !{!"_Bool", !7, i64 0}
!18 = !{!"", !14, i64 0, !14, i64 2, !14, i64 4, !14, i64 6}
!19 = !{!"", !13, i64 0, !13, i64 8, !13, i64 16, !13, i64 24, !13, i64 32}
!20 = !{!"", !13, i64 0}
!21 = !{!"osal_ioring", !11, i64 0, !11, i64 4, !11, i64 8, !6, i64 16, !6, i64 24, !6, i64 32}
!22 = !{!10, !11, i64 4}
!23 = !{!24, !13, i64 16}
!24 = !{!"MDBX_txn", !11, i64 0, !11, i64 4, !13, i64 8, !13, i64 16, !6, i64 24, !6, i64 32, !25, i64 40, !13, i64 64, !13, i64 72, !6, i64 80, !6, i64 88, !6, i64 96, !6, i64 104, !6, i64 112, !6, i64 120, !26, i64 128, !6, i64 160, !7, i64 168}
!25 = !{!"geo", !14, i64 0, !14, i64 2, !11, i64 4, !11, i64 8, !7, i64 12, !7, i64 16}
!26 = !{!"MDBX_canary", !13, i64 0, !13, i64 8, !13, i64 16, !13, i64 24}
!27 = !{!17, !17, i64 0}
!28 = !{i8 0, i8 2}
!29 = !{}
!30 = !{!10, !6, i64 384}


; 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: nounwind uwtable
define dso_local ptr @env_owned_wrtxn(ptr nocapture noundef readonly %0) local_unnamed_addr #0 {
  %2 = getelementptr inbounds i8, ptr %0, i64 192
  %3 = load ptr, ptr %2, align 8, !tbaa !5
  %.not = icmp eq ptr %3, null
  br i1 %.not, label %.thread, label %4, !prof !21

4:                                                ; preds = %1
  %5 = getelementptr inbounds i8, ptr %0, i64 4
  %6 = load i32, ptr %5, align 4, !tbaa !22
  %7 = and i32 %6, 2097152
  %.not12 = icmp eq i32 %7, 0
  %8 = getelementptr inbounds i8, ptr %3, i64 16
  %9 = load i64, ptr %8, align 8, !tbaa !23
  br i1 %.not12, label %11, label %10

10:                                               ; preds = %4
  %.not19 = icmp eq i64 %9, 0
  br i1 %.not19, label %.thread, label %14

11:                                               ; preds = %4
  %12 = tail call i64 @osal_thread_self()
  %13 = icmp eq i64 %9, %12
  br i1 %13, label %14, label %.thread

14:                                               ; preds = %10, %11
  %15 = getelementptr inbounds i8, ptr %0, i64 384
  %16 = load ptr, ptr %15, align 8, !tbaa !27
  %.not13 = icmp eq ptr %16, null
  br i1 %.not13, label %17, label %.thread

17:                                               ; preds = %14
  %18 = load ptr, ptr %2, align 8, !tbaa !5
  br label %.thread

.thread:                                          ; preds = %10, %11, %1, %14, %17
  %.1 = phi ptr [ %18, %17 ], [ %16, %14 ], [ null, %1 ], [ null, %11 ], [ null, %10 ]
  ret ptr %.1
}

; Function Attrs: inlinehint nounwind uwtable
declare hidden i64 @osal_thread_self() local_unnamed_addr #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 = { 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" }

!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, !12, i64 192}
!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 = !{!"branch_weights", !"expected", i32 1, i32 2000}
!22 = !{!6, !9, i64 4}
!23 = !{!24, !11, i64 16}
!24 = !{!"MDBX_txn", !9, i64 0, !9, i64 4, !11, i64 8, !11, i64 16, !12, i64 24, !12, i64 32, !25, 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, !26, i64 128, !12, i64 160, !7, i64 168}
!25 = !{!"geo", !13, i64 0, !13, i64 2, !9, i64 4, !9, i64 8, !7, i64 12, !7, i64 16}
!26 = !{!"MDBX_canary", !11, i64 0, !11, i64 8, !11, i64 16, !11, i64 24}
!27 = !{!6, !12, i64 384}


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