; 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.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.12 }
%struct.anon.12 = 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.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 }

@globals = external global %struct.libmdbx_globals, align 8
@__func__.mdbx_txn_unpark = external hidden unnamed_addr constant [16 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [31 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: noreturn
declare void @mdbx_assert_fail(ptr noundef, ptr noundef, ptr noundef, i32 noundef) #4

; Function Attrs: nounwind uwtable
define dso_local i32 @mdbx_txn_unpark(ptr noundef %0, i1 noundef zeroext %1) #5 {
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca i8, align 1
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  %8 = zext i1 %1 to i8
  store i8 %8, ptr %5, align 1, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #7
  %9 = load ptr, ptr %4, align 8, !tbaa !5
  %10 = call i32 @check_txn(ptr noundef %9, i32 noundef 17)
  store i32 %10, ptr %6, align 4, !tbaa !11
  %11 = load i32, ptr %6, align 4, !tbaa !11
  %12 = icmp ne i32 %11, 0
  %13 = xor i1 %12, true
  %14 = xor i1 %13, true
  %15 = zext i1 %14 to i32
  %16 = sext i32 %15 to i64
  %17 = call i64 @llvm.expect.i64(i64 %16, i64 0)
  %18 = icmp ne i64 %17, 0
  br i1 %18, label %19, label %22

19:                                               ; preds = %2
  %20 = load i32, ptr %6, align 4, !tbaa !11
  %21 = call i32 @log_if_error(i32 noundef %20, ptr noundef @__func__.mdbx_txn_unpark, i32 noundef 143)
  store i32 %21, ptr %3, align 4
  store i32 1, ptr %7, align 4
  br label %115

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

35:                                               ; preds = %22
  %36 = load i32, ptr %6, align 4, !tbaa !11
  %37 = call i32 @log_if_error(i32 noundef %36, ptr noundef @__func__.mdbx_txn_unpark, i32 noundef 147)
  store i32 %37, ptr %3, align 4
  store i32 1, ptr %7, align 4
  br label %115

38:                                               ; preds = %22
  %39 = load ptr, ptr %4, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.MDBX_txn, ptr %39, i32 0, i32 1
  %41 = load i32, ptr %40, align 4, !tbaa !19
  %42 = and i32 %41, 131104
  %43 = icmp eq i32 %42, 131104
  %44 = xor i1 %43, true
  %45 = xor i1 %44, true
  %46 = xor i1 %45, true
  %47 = zext i1 %46 to i32
  %48 = sext i32 %47 to i64
  %49 = call i64 @llvm.expect.i64(i64 %48, i64 0)
  %50 = icmp ne i64 %49, 0
  br i1 %50, label %51, label %52

51:                                               ; preds = %38
  store i32 0, ptr %3, align 4
  store i32 1, ptr %7, align 4
  br label %115

52:                                               ; preds = %38
  %53 = load ptr, ptr %4, align 8, !tbaa !5
  %54 = call i32 @txn_ro_unpark(ptr noundef %53)
  store i32 %54, ptr %6, align 4, !tbaa !11
  %55 = load i32, ptr %6, align 4, !tbaa !11
  %56 = icmp ne i32 %55, -30411
  %57 = xor i1 %56, true
  %58 = xor i1 %57, true
  %59 = zext i1 %58 to i32
  %60 = sext i32 %59 to i64
  %61 = call i64 @llvm.expect.i64(i64 %60, i64 1)
  %62 = icmp ne i64 %61, 0
  br i1 %62, label %66, label %63

63:                                               ; preds = %52
  %64 = load i8, ptr %5, align 1, !tbaa !9, !range !20, !noundef !21
  %65 = trunc i8 %64 to i1
  br i1 %65, label %69, label %66

66:                                               ; preds = %63, %52
  %67 = load i32, ptr %6, align 4, !tbaa !11
  %68 = call i32 @log_if_error(i32 noundef %67, ptr noundef @__func__.mdbx_txn_unpark, i32 noundef 154)
  store i32 %68, ptr %3, align 4
  store i32 1, ptr %7, align 4
  br label %115

69:                                               ; preds = %63
  br label %70

70:                                               ; preds = %69
  %71 = load i8, ptr getelementptr inbounds (%struct.libmdbx_globals, ptr @globals, i32 0, i32 4), align 1, !tbaa !22
  %72 = zext i8 %71 to i32
  %73 = and i32 %72, 1
  %74 = icmp ne i32 %73, 0
  %75 = xor i1 %74, true
  %76 = xor i1 %75, true
  %77 = zext i1 %76 to i32
  %78 = sext i32 %77 to i64
  %79 = call i64 @llvm.expect.i64(i64 %78, i64 1)
  %80 = icmp ne i64 %79, 0
  br i1 %80, label %81, label %102

81:                                               ; preds = %70
  br label %82

82:                                               ; preds = %81
  %83 = load ptr, ptr %4, align 8, !tbaa !5
  %84 = getelementptr inbounds %struct.MDBX_txn, ptr %83, i32 0, i32 1
  %85 = load i32, ptr %84, align 4, !tbaa !19
  %86 = and i32 %85, 1
  %87 = icmp ne i32 %86, 0
  %88 = xor i1 %87, true
  %89 = xor i1 %88, true
  %90 = xor i1 %89, true
  %91 = zext i1 %90 to i32
  %92 = sext i32 %91 to i64
  %93 = call i64 @llvm.expect.i64(i64 %92, i64 0)
  %94 = icmp ne i64 %93, 0
  br i1 %94, label %95, label %99

95:                                               ; preds = %82
  %96 = load ptr, ptr %4, align 8, !tbaa !5
  %97 = getelementptr inbounds %struct.MDBX_txn, ptr %96, i32 0, i32 9
  %98 = load ptr, ptr %97, align 8, !tbaa !13
  call void @mdbx_assert_fail(ptr noundef %98, ptr noundef @.str.3, ptr noundef @__func__.mdbx_txn_unpark, i32 noundef 156) #8
  unreachable

99:                                               ; preds = %82
  br label %100

100:                                              ; preds = %99
  br label %101

101:                                              ; preds = %100
  br label %102

102:                                              ; preds = %101, %70
  br label %103

103:                                              ; preds = %102
  br label %104

104:                                              ; preds = %103
  %105 = load ptr, ptr %4, align 8, !tbaa !5
  %106 = call i32 @txn_renew(ptr noundef %105, i32 noundef 131072)
  store i32 %106, ptr %6, align 4, !tbaa !11
  %107 = load i32, ptr %6, align 4, !tbaa !11
  %108 = icmp eq i32 %107, 0
  br i1 %108, label %109, label %110

109:                                              ; preds = %104
  br label %113

110:                                              ; preds = %104
  %111 = load i32, ptr %6, align 4, !tbaa !11
  %112 = call i32 @log_if_error(i32 noundef %111, ptr noundef @__func__.mdbx_txn_unpark, i32 noundef 158)
  br label %113

113:                                              ; preds = %110, %109
  %114 = phi i32 [ -1, %109 ], [ %112, %110 ]
  store i32 %114, ptr %3, align 4
  store i32 1, ptr %7, align 4
  br label %115

115:                                              ; preds = %113, %66, %51, %35, %19
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #7
  %116 = load i32, ptr %3, align 4
  ret i32 %116
}

declare i32 @txn_ro_unpark(ptr noundef) #6

declare i32 @txn_renew(ptr noundef, i32 noundef) #6

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 = { 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 #5 = { 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 #6 = { "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 #7 = { nounwind }
attributes #8 = { noreturn }

!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 = !{!"_Bool", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!14, !6, i64 80}
!14 = !{!"MDBX_txn", !12, i64 0, !12, i64 4, !15, i64 8, !15, i64 16, !6, i64 24, !6, i64 32, !16, i64 40, !15, i64 64, !15, i64 72, !6, i64 80, !6, i64 88, !6, i64 96, !6, i64 104, !6, i64 112, !6, i64 120, !18, i64 128, !6, i64 160, !7, i64 168}
!15 = !{!"long", !7, i64 0}
!16 = !{!"geo", !17, i64 0, !17, i64 2, !12, i64 4, !12, i64 8, !7, i64 12, !7, i64 16}
!17 = !{!"short", !7, i64 0}
!18 = !{!"MDBX_canary", !15, i64 0, !15, i64 8, !15, i64 16, !15, i64 24}
!19 = !{!14, !12, i64 4}
!20 = !{i8 0, i8 2}
!21 = !{}
!22 = !{!23, !7, i64 25}
!23 = !{!"libmdbx_globals", !7, i64 0, !12, i64 16, !12, i64 20, !7, i64 24, !7, i64 25, !7, i64 26, !10, i64 27, !12, i64 28, !7, i64 32, !7, i64 40, !15, i64 80, !6, i64 88}


; 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.12 }
%struct.anon.12 = 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 }

@globals = external local_unnamed_addr global %struct.libmdbx_globals, align 8
@__func__.mdbx_txn_unpark = external hidden unnamed_addr constant [16 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [31 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: noreturn
declare void @mdbx_assert_fail(ptr noundef, ptr noundef, ptr noundef, i32 noundef) local_unnamed_addr #2

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

4:                                                ; preds = %2
  %5 = tail call i32 @log_if_error(i32 noundef %3, ptr noundef nonnull @__func__.mdbx_txn_unpark, i32 noundef 143)
  br label %34

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

10:                                               ; preds = %6
  %11 = tail call i32 @log_if_error(i32 noundef %9, ptr noundef nonnull @__func__.mdbx_txn_unpark, i32 noundef 147)
  br label %34

12:                                               ; preds = %6
  %13 = getelementptr inbounds i8, ptr %0, i64 4
  %14 = load i32, ptr %13, align 4, !tbaa !16
  %15 = and i32 %14, 131104
  %.not20 = icmp eq i32 %15, 131104
  br i1 %.not20, label %16, label %34, !prof !5

16:                                               ; preds = %12
  %17 = tail call i32 @txn_ro_unpark(ptr noundef nonnull %0) #5
  %.not21 = icmp eq i32 %17, -30411
  br i1 %.not21, label %18, label %19, !prof !17

18:                                               ; preds = %16
  br i1 %1, label %21, label %19

19:                                               ; preds = %18, %16
  %20 = tail call i32 @log_if_error(i32 noundef %17, ptr noundef nonnull @__func__.mdbx_txn_unpark, i32 noundef 154)
  br label %34

21:                                               ; preds = %18
  %22 = load i8, ptr getelementptr inbounds (i8, ptr @globals, i64 25), align 1, !tbaa !18
  %23 = and i8 %22, 1
  %.not22 = icmp eq i8 %23, 0
  br i1 %.not22, label %29, label %24, !prof !17

24:                                               ; preds = %21
  %25 = load i32, ptr %13, align 4, !tbaa !16
  %26 = and i32 %25, 1
  %.not23 = icmp eq i32 %26, 0
  br i1 %.not23, label %27, label %29, !prof !17

27:                                               ; preds = %24
  %28 = load ptr, ptr %7, align 8, !tbaa !6
  tail call void @mdbx_assert_fail(ptr noundef %28, ptr noundef nonnull @.str.3, ptr noundef nonnull @__func__.mdbx_txn_unpark, i32 noundef 156) #6
  unreachable

29:                                               ; preds = %24, %21
  %30 = tail call i32 @txn_renew(ptr noundef nonnull %0, i32 noundef 131072) #5
  %31 = icmp eq i32 %30, 0
  br i1 %31, label %34, label %32

32:                                               ; preds = %29
  %33 = tail call i32 @log_if_error(i32 noundef %30, ptr noundef nonnull @__func__.mdbx_txn_unpark, i32 noundef 158)
  br label %34

34:                                               ; preds = %32, %29, %12, %19, %10, %4
  %.0 = phi i32 [ %5, %4 ], [ %11, %10 ], [ %20, %19 ], [ 0, %12 ], [ %33, %32 ], [ -1, %29 ]
  ret i32 %.0
}

declare i32 @txn_ro_unpark(ptr noundef) local_unnamed_addr #4

declare i32 @txn_renew(ptr noundef, i32 noundef) local_unnamed_addr #4

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 = { 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 #3 = { 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 = { "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 = { nounwind }
attributes #6 = { 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 = !{!"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 = !{!"branch_weights", !"expected", i32 1, i32 2000}
!18 = !{!19, !9, i64 25}
!19 = !{!"libmdbx_globals", !9, i64 0, !8, i64 16, !8, i64 20, !9, i64 24, !9, i64 25, !9, i64 26, !20, i64 27, !8, i64 28, !9, i64 32, !9, i64 40, !11, i64 80, !12, i64 88}
!20 = !{!"_Bool", !9, i64 0}


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