; 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 }

@__func__.mdbx_txn_abort = external hidden unnamed_addr constant [15 x i8], align 1

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

; Function Attrs: alwaysinline nounwind uwtable
declare hidden i32 @check_txn(ptr noundef, i32 noundef) #1

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

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @check_env(ptr noundef, i1 noundef zeroext) #3

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @log_if_error(i32 noundef, ptr noundef, i32 noundef) #3

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

; Function Attrs: nounwind uwtable
declare dso_local i32 @mdbx_txn_break(ptr noundef) #4

; Function Attrs: nounwind uwtable
define dso_local i32 @mdbx_txn_abort(ptr noundef %0) #4 {
  %2 = alloca i32, align 4
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #6
  %6 = load ptr, ptr %3, align 8, !tbaa !5
  %7 = call i32 @check_txn(ptr noundef %6, i32 noundef 0)
  store i32 %7, ptr %4, align 4, !tbaa !9
  %8 = load i32, ptr %4, align 4, !tbaa !9
  %9 = icmp ne i32 %8, 0
  %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 0)
  %15 = icmp ne i64 %14, 0
  br i1 %15, label %16, label %19

16:                                               ; preds = %1
  %17 = load i32, ptr %4, align 4, !tbaa !9
  %18 = call i32 @log_if_error(i32 noundef %17, ptr noundef @__func__.mdbx_txn_abort, i32 noundef 101)
  store i32 %18, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %61

19:                                               ; preds = %1
  %20 = load ptr, ptr %3, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.MDBX_txn, ptr %20, i32 0, i32 9
  %22 = load ptr, ptr %21, align 8, !tbaa !11
  %23 = call i32 @check_env(ptr noundef %22, i1 noundef zeroext true)
  store i32 %23, ptr %4, align 4, !tbaa !9
  %24 = load i32, ptr %4, align 4, !tbaa !9
  %25 = icmp ne i32 %24, 0
  %26 = xor i1 %25, true
  %27 = xor i1 %26, true
  %28 = zext i1 %27 to i32
  %29 = sext i32 %28 to i64
  %30 = call i64 @llvm.expect.i64(i64 %29, i64 0)
  %31 = icmp ne i64 %30, 0
  br i1 %31, label %32, label %35

32:                                               ; preds = %19
  %33 = load i32, ptr %4, align 4, !tbaa !9
  %34 = call i32 @log_if_error(i32 noundef %33, ptr noundef @__func__.mdbx_txn_abort, i32 noundef 105)
  store i32 %34, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %61

35:                                               ; preds = %19
  %36 = load ptr, ptr %3, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.MDBX_txn, ptr %36, i32 0, i32 1
  %38 = load i32, ptr %37, align 4, !tbaa !17
  %39 = and i32 %38, 2228224
  %40 = icmp eq i32 %39, 2097152
  br i1 %40, label %41, label %57

41:                                               ; preds = %35
  %42 = load ptr, ptr %3, align 8, !tbaa !5
  %43 = getelementptr inbounds %struct.MDBX_txn, ptr %42, i32 0, i32 3
  %44 = load i64, ptr %43, align 8, !tbaa !18
  %45 = call i64 @osal_thread_self()
  %46 = icmp ne i64 %44, %45
  %47 = xor i1 %46, true
  %48 = xor i1 %47, true
  %49 = zext i1 %48 to i32
  %50 = sext i32 %49 to i64
  %51 = call i64 @llvm.expect.i64(i64 %50, i64 0)
  %52 = icmp ne i64 %51, 0
  br i1 %52, label %53, label %57

53:                                               ; preds = %41
  %54 = load ptr, ptr %3, align 8, !tbaa !5
  %55 = call i32 @mdbx_txn_break(ptr noundef %54)
  %56 = call i32 @log_if_error(i32 noundef -30416, ptr noundef @__func__.mdbx_txn_abort, i32 noundef 111)
  store i32 %56, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %61

57:                                               ; preds = %41, %35
  %58 = load ptr, ptr %3, align 8, !tbaa !5
  %59 = call i32 @txn_abort(ptr noundef %58)
  %60 = call i32 @log_if_error(i32 noundef %59, ptr noundef @__func__.mdbx_txn_abort, i32 noundef 115)
  store i32 %60, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %61

61:                                               ; preds = %57, %53, %32, %16
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #6
  %62 = load i32, ptr %2, align 4
  ret i32 %62
}

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

declare i32 @txn_abort(ptr noundef) #5

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #2 = { nocallback nofree nosync nounwind willreturn memory(none) }
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 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 = { "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 #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 = !{!"int", !7, i64 0}
!11 = !{!12, !6, i64 80}
!12 = !{!"MDBX_txn", !10, i64 0, !10, i64 4, !13, i64 8, !13, i64 16, !6, i64 24, !6, i64 32, !14, 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, !16, i64 128, !6, i64 160, !7, i64 168}
!13 = !{!"long", !7, i64 0}
!14 = !{!"geo", !15, i64 0, !15, i64 2, !10, i64 4, !10, i64 8, !7, i64 12, !7, i64 16}
!15 = !{!"short", !7, i64 0}
!16 = !{!"MDBX_canary", !13, i64 0, !13, i64 8, !13, i64 16, !13, i64 24}
!17 = !{!12, !10, i64 4}
!18 = !{!12, !13, i64 16}


; 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"

@__func__.mdbx_txn_abort = external hidden unnamed_addr constant [15 x i8], align 1

; Function Attrs: alwaysinline nounwind uwtable
declare hidden i32 @check_txn(ptr noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @check_env(ptr noundef, i1 noundef zeroext) local_unnamed_addr #1

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @log_if_error(i32 noundef, ptr noundef, i32 noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare dso_local i32 @mdbx_txn_break(ptr noundef) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
define dso_local i32 @mdbx_txn_abort(ptr noundef %0) local_unnamed_addr #2 {
  %2 = tail call i32 @check_txn(ptr noundef %0, i32 noundef 0)
  %.not = icmp eq i32 %2, 0
  br i1 %.not, label %5, label %3, !prof !5

3:                                                ; preds = %1
  %4 = tail call i32 @log_if_error(i32 noundef %2, ptr noundef nonnull @__func__.mdbx_txn_abort, i32 noundef 101)
  br label %26

5:                                                ; preds = %1
  %6 = getelementptr inbounds i8, ptr %0, i64 80
  %7 = load ptr, ptr %6, align 8, !tbaa !6
  %8 = tail call i32 @check_env(ptr noundef %7, i1 noundef zeroext true)
  %.not12 = icmp eq i32 %8, 0
  br i1 %.not12, label %11, label %9, !prof !5

9:                                                ; preds = %5
  %10 = tail call i32 @log_if_error(i32 noundef %8, ptr noundef nonnull @__func__.mdbx_txn_abort, i32 noundef 105)
  br label %26

11:                                               ; preds = %5
  %12 = getelementptr inbounds i8, ptr %0, i64 4
  %13 = load i32, ptr %12, align 4, !tbaa !16
  %14 = and i32 %13, 2228224
  %15 = icmp eq i32 %14, 2097152
  br i1 %15, label %16, label %23

16:                                               ; preds = %11
  %17 = getelementptr inbounds i8, ptr %0, i64 16
  %18 = load i64, ptr %17, align 8, !tbaa !17
  %19 = tail call i64 @osal_thread_self()
  %.not13 = icmp eq i64 %18, %19
  br i1 %.not13, label %23, label %20, !prof !5

20:                                               ; preds = %16
  %21 = tail call i32 @mdbx_txn_break(ptr noundef nonnull %0)
  %22 = tail call i32 @log_if_error(i32 noundef -30416, ptr noundef nonnull @__func__.mdbx_txn_abort, i32 noundef 111)
  br label %26

23:                                               ; preds = %16, %11
  %24 = tail call i32 @txn_abort(ptr noundef nonnull %0) #4
  %25 = tail call i32 @log_if_error(i32 noundef %24, ptr noundef nonnull @__func__.mdbx_txn_abort, i32 noundef 115)
  br label %26

26:                                               ; preds = %23, %20, %9, %3
  %.0 = phi i32 [ %4, %3 ], [ %10, %9 ], [ %22, %20 ], [ %25, %23 ]
  ret i32 %.0
}

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

declare i32 @txn_abort(ptr noundef) local_unnamed_addr #3

attributes #0 = { 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 #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 = { 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 = { "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 = !{!"branch_weights", !"expected", i32 2000, i32 1}
!6 = !{!7, !12, i64 80}
!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 = !{!7, !8, i64 4}
!17 = !{!7, !11, i64 16}


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