; LLVM IR from erthink/libmdbx :: src (api-txn)

; 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_txn = type { i32, i32, i64, i64, ptr, ptr, %struct.geo, i64, i64, ptr, ptr, ptr, ptr, ptr, ptr, %struct.MDBX_canary, ptr, %union.anon.1 }
%struct.geo = type { i16, i16, i32, i32, %union.anon, %union.anon.0 }
%union.anon = type { i32 }
%union.anon.0 = type { i32 }
%struct.MDBX_canary = type { i64, i64, i64, i64 }
%union.anon.1 = type { %struct.anon.2 }
%struct.anon.2 = type { %struct.troika, ptr, %struct.anon.3, i8, i32, i32, i64, ptr, ptr, ptr, i64, %union.anon.4 }
%struct.troika = type { i8, i8, i8, i8, i32, [3 x i64] }
%struct.anon.3 = type { %struct.MDBX_rkl, i64, %struct.MDBX_rkl }
%struct.MDBX_rkl = type { i64, i64, i32, i32, ptr, [12 x i64] }
%union.anon.4 = type { %struct.anon.5 }
%struct.anon.5 = type { i64, 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.15, ptr, ptr, ptr, ptr, ptr, i32, i32, i32, ptr, ptr, i64, %struct.anon.16, %struct.anon.20, i8, ptr, ptr, ptr, %struct.anon.21, %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.14, i32, i64, i64, i64 }
%union.anon.14 = type { ptr }
%union.mdbx_atomic_uint32_t = type { i32 }
%struct.anon.15 = type { ptr, ptr, ptr, ptr }
%struct.anon.16 = type { i32, i32, i32, i32, i64, i8, i8, i8, i8, i32, i32, i8, i8, i8, %struct.anon.17, %union.anon.18 }
%struct.anon.17 = type { i16, i16, i16, i16 }
%union.anon.18 = type { i32 }
%struct.anon.20 = type { i64, i64, i64, i64, i64 }
%struct.anon.21 = 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 }

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

; Function Attrs: nounwind willreturn memory(read) uwtable
define dso_local ptr @mdbx_txn_env(ptr noundef %0) #1 {
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  %4 = load ptr, ptr %3, align 8, !tbaa !5
  %5 = icmp ne ptr %4, null
  br i1 %5, label %6, label %18

6:                                                ; preds = %1
  %7 = load ptr, ptr %3, align 8, !tbaa !5
  %8 = getelementptr inbounds %struct.MDBX_txn, ptr %7, i32 0, i32 0
  %9 = load i32, ptr %8, align 8, !tbaa !9
  %10 = icmp ne i32 %9, 332741169
  br i1 %10, label %18, label %11

11:                                               ; preds = %6
  %12 = load ptr, ptr %3, align 8, !tbaa !5
  %13 = getelementptr inbounds %struct.MDBX_txn, ptr %12, i32 0, i32 9
  %14 = load ptr, ptr %13, align 8, !tbaa !16
  %15 = getelementptr inbounds %struct.MDBX_env, ptr %14, i32 0, i32 0
  %16 = load volatile i32, ptr %15, align 8, !tbaa !17
  %17 = icmp ne i32 %16, 445224513
  br label %18

18:                                               ; preds = %11, %6, %1
  %19 = phi i1 [ true, %6 ], [ true, %1 ], [ %17, %11 ]
  %20 = xor i1 %19, true
  %21 = xor i1 %20, true
  %22 = zext i1 %21 to i32
  %23 = sext i32 %22 to i64
  %24 = call i64 @llvm.expect.i64(i64 %23, i64 0)
  %25 = icmp ne i64 %24, 0
  br i1 %25, label %26, label %27

26:                                               ; preds = %18
  store ptr null, ptr %2, align 8
  br label %31

27:                                               ; preds = %18
  %28 = load ptr, ptr %3, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.MDBX_txn, ptr %28, i32 0, i32 9
  %30 = load ptr, ptr %29, align 8, !tbaa !16
  store ptr %30, ptr %2, align 8
  br label %31

31:                                               ; preds = %27, %26
  %32 = load ptr, ptr %2, align 8
  ret ptr %32
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(none) }
attributes #1 = { 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" }

!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, !11, i64 0}
!10 = !{!"MDBX_txn", !11, i64 0, !11, i64 4, !12, i64 8, !12, i64 16, !6, i64 24, !6, i64 32, !13, i64 40, !12, i64 64, !12, i64 72, !6, i64 80, !6, i64 88, !6, i64 96, !6, i64 104, !6, i64 112, !6, i64 120, !15, i64 128, !6, i64 160, !7, i64 168}
!11 = !{!"int", !7, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = !{!"geo", !14, i64 0, !14, i64 2, !11, i64 4, !11, i64 8, !7, i64 12, !7, i64 16}
!14 = !{!"short", !7, i64 0}
!15 = !{!"MDBX_canary", !12, i64 0, !12, i64 8, !12, i64 16, !12, i64 24}
!16 = !{!10, !6, i64 80}
!17 = !{!7, !7, 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"

; Function Attrs: mustprogress nofree norecurse nounwind willreturn memory(read) uwtable
define dso_local ptr @mdbx_txn_env(ptr noundef readonly %0) local_unnamed_addr #0 {
  %.not = icmp eq ptr %0, null
  br i1 %.not, label %.critedge, label %2, !prof !5

2:                                                ; preds = %1
  %3 = load i32, ptr %0, align 8, !tbaa !6
  %.not6 = icmp eq i32 %3, 332741169
  br i1 %.not6, label %4, label %.critedge, !prof !16

4:                                                ; preds = %2
  %5 = getelementptr inbounds i8, ptr %0, i64 80
  %6 = load ptr, ptr %5, align 8, !tbaa !17
  %7 = load volatile i32, ptr %6, align 8, !tbaa !18
  %.not7 = icmp eq i32 %7, 445224513
  %spec.select = select i1 %.not7, ptr %6, ptr null, !prof !16
  br label %.critedge

.critedge:                                        ; preds = %4, %2, %1
  %.0 = phi ptr [ null, %1 ], [ null, %2 ], [ %spec.select, %4 ]
  ret ptr %.0
}

attributes #0 = { mustprogress nofree norecurse 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" }

!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 = !{!"branch_weights", !"expected", i32 1, i32 2000}
!6 = !{!7, !8, i64 0}
!7 = !{!"MDBX_txn", !8, i64 0, !8, i64 4, !11, i64 8, !11, i64 16, !12, i64 24, !12, i64 32, !13, 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, !15, i64 128, !12, i64 160, !9, i64 168}
!8 = !{!"int", !9, i64 0}
!9 = !{!"omnipotent char", !10, i64 0}
!10 = !{!"Simple C/C++ TBAA"}
!11 = !{!"long", !9, i64 0}
!12 = !{!"any pointer", !9, i64 0}
!13 = !{!"geo", !14, i64 0, !14, i64 2, !8, i64 4, !8, i64 8, !9, i64 12, !9, i64 16}
!14 = !{!"short", !9, i64 0}
!15 = !{!"MDBX_canary", !11, i64 0, !11, i64 8, !11, i64 16, !11, i64 24}
!16 = !{!"branch_weights", !"expected", i32 2000, i32 1}
!17 = !{!7, !12, i64 80}
!18 = !{!9, !9, i64 0}


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