; LLVM IR from erthink/libmdbx :: src (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.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.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.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: hot nounwind uwtable
define dso_local zeroext i1 @txn_gc_detent(ptr noundef %0) #1 {
  %2 = alloca i1, align 1
  %3 = alloca ptr, align 8
  %4 = alloca i64, align 8
  %5 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #4
  %6 = load ptr, ptr %3, align 8, !tbaa !5
  %7 = getelementptr inbounds %struct.MDBX_txn, ptr %6, i32 0, i32 9
  %8 = load ptr, ptr %7, align 8, !tbaa !9
  %9 = load ptr, ptr %3, align 8, !tbaa !5
  %10 = getelementptr inbounds %struct.MDBX_txn, ptr %9, i32 0, i32 17
  %11 = getelementptr inbounds %struct.anon.4, ptr %10, i32 0, i32 0
  %12 = getelementptr inbounds %struct.troika, ptr %11, i32 0, i32 5
  %13 = load ptr, ptr %3, align 8, !tbaa !5
  %14 = getelementptr inbounds %struct.MDBX_txn, ptr %13, i32 0, i32 17
  %15 = getelementptr inbounds %struct.anon.4, ptr %14, i32 0, i32 0
  %16 = getelementptr inbounds %struct.troika, ptr %15, i32 0, i32 2
  %17 = load i8, ptr %16, align 2, !tbaa !16
  %18 = zext i8 %17 to i64
  %19 = getelementptr inbounds [3 x i64], ptr %12, i64 0, i64 %18
  %20 = load i64, ptr %19, align 8, !tbaa !16
  %21 = call i64 @mvcc_shapshot_oldest(ptr noundef %8, i64 noundef %20)
  store i64 %21, ptr %4, align 8, !tbaa !17
  %22 = load i64, ptr %4, align 8, !tbaa !17
  %23 = load ptr, ptr %3, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.MDBX_txn, ptr %23, i32 0, i32 9
  %25 = load ptr, ptr %24, align 8, !tbaa !9
  %26 = getelementptr inbounds %struct.MDBX_env, ptr %25, i32 0, i32 40
  %27 = getelementptr inbounds %struct.anon.15, ptr %26, i32 0, i32 0
  %28 = load i64, ptr %27, align 8, !tbaa !18
  %29 = icmp eq i64 %22, %28
  %30 = xor i1 %29, true
  %31 = xor i1 %30, true
  %32 = zext i1 %31 to i32
  %33 = sext i32 %32 to i64
  %34 = call i64 @llvm.expect.i64(i64 %33, i64 1)
  %35 = icmp ne i64 %34, 0
  br i1 %35, label %36, label %37

36:                                               ; preds = %1
  store i1 false, ptr %2, align 1
  store i32 1, ptr %5, align 4
  br label %44

37:                                               ; preds = %1
  %38 = load i64, ptr %4, align 8, !tbaa !17
  %39 = load ptr, ptr %3, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.MDBX_txn, ptr %39, i32 0, i32 9
  %41 = load ptr, ptr %40, align 8, !tbaa !9
  %42 = getelementptr inbounds %struct.MDBX_env, ptr %41, i32 0, i32 40
  %43 = getelementptr inbounds %struct.anon.15, ptr %42, i32 0, i32 0
  store i64 %38, ptr %43, align 8, !tbaa !18
  store i1 true, ptr %2, align 1
  store i32 1, ptr %5, align 4
  br label %44

44:                                               ; preds = %37, %36
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #4
  %45 = load i1, ptr %2, align 1
  ret i1 %45
}

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

declare i64 @mvcc_shapshot_oldest(ptr noundef, i64 noundef) #3

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

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(none) }
attributes #1 = { hot 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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { "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 80}
!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 = !{!7, !7, i64 0}
!17 = !{!12, !12, i64 0}
!18 = !{!19, !12, i64 392}
!19 = !{!"MDBX_env", !7, i64 0, !11, i64 4, !11, i64 8, !20, i64 16, !11, i64 56, !11, i64 60, !20, 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, !21, 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, !12, i64 256, !22, i64 264, !25, i64 320, !23, i64 360, !6, i64 368, !6, i64 376, !6, i64 384, !26, i64 392, !7, i64 400, !11, i64 440, !11, i64 444, !6, i64 448, !27, i64 456, !7, i64 496, !7, i64 536}
!20 = !{!"osal_mmap", !7, i64 0, !11, i64 8, !12, i64 16, !12, i64 24, !12, i64 32}
!21 = !{!"", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24}
!22 = !{!"", !11, i64 0, !11, i64 4, !11, i64 8, !11, i64 12, !12, i64 16, !7, i64 24, !7, i64 25, !7, i64 26, !7, i64 27, !11, i64 28, !11, i64 32, !23, i64 36, !23, i64 37, !23, i64 38, !24, i64 40, !7, i64 48}
!23 = !{!"_Bool", !7, i64 0}
!24 = !{!"", !14, i64 0, !14, i64 2, !14, i64 4, !14, i64 6}
!25 = !{!"", !12, i64 0, !12, i64 8, !12, i64 16, !12, i64 24, !12, i64 32}
!26 = !{!"", !12, i64 0}
!27 = !{!"osal_ioring", !11, i64 0, !11, i64 4, !11, i64 8, !6, i64 16, !6, i64 24, !6, i64 32}


; 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: hot nounwind uwtable
define dso_local noundef zeroext i1 @txn_gc_detent(ptr nocapture noundef readonly %0) local_unnamed_addr #0 {
  %2 = getelementptr inbounds i8, ptr %0, i64 80
  %3 = load ptr, ptr %2, align 8, !tbaa !5
  %4 = getelementptr inbounds i8, ptr %0, i64 176
  %5 = getelementptr inbounds i8, ptr %0, i64 170
  %6 = load i8, ptr %5, align 2, !tbaa !15
  %7 = zext i8 %6 to i64
  %8 = getelementptr inbounds [3 x i64], ptr %4, i64 0, i64 %7
  %9 = load i64, ptr %8, align 8, !tbaa !15
  %10 = tail call i64 @mvcc_shapshot_oldest(ptr noundef %3, i64 noundef %9) #2
  %11 = load ptr, ptr %2, align 8, !tbaa !5
  %12 = getelementptr inbounds i8, ptr %11, i64 392
  %13 = load i64, ptr %12, align 8, !tbaa !16
  %14 = icmp ne i64 %10, %13
  br i1 %14, label %15, label %16, !prof !26

15:                                               ; preds = %1
  store i64 %10, ptr %12, align 8, !tbaa !16
  br label %16

16:                                               ; preds = %1, %15
  ret i1 %14
}

declare i64 @mvcc_shapshot_oldest(ptr noundef, i64 noundef) local_unnamed_addr #1

attributes #0 = { hot 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 = { "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 }

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


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