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

; 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.6 }
%struct.anon.6 = 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_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.mdbx_atomic_uint64_t = type { i64 }

@globals = external global %struct.libmdbx_globals, align 8
@__func__.coherency_timeout = external hidden unnamed_addr constant [18 x i8], align 1
@.str = external hidden unnamed_addr constant [41 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [62 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [41 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [60 x i8], align 1

; Function Attrs: cold nounwind optsize uwtable
define dso_local i32 @coherency_timeout(ptr noundef %0, i64 noundef %1, ptr noundef %2) #0 {
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca i64, align 8
  %7 = alloca ptr, align 8
  store ptr %0, ptr %5, align 8, !tbaa !5
  store i64 %1, ptr %6, align 8, !tbaa !9
  store ptr %2, ptr %7, align 8, !tbaa !5
  %8 = load ptr, ptr %5, align 8, !tbaa !5
  %9 = icmp ne ptr %8, null
  br i1 %9, label %10, label %14

10:                                               ; preds = %3
  %11 = load ptr, ptr %5, align 8, !tbaa !5
  %12 = load i64, ptr %11, align 8, !tbaa !9
  %13 = icmp eq i64 %12, 0
  br label %14

14:                                               ; preds = %10, %3
  %15 = phi i1 [ false, %3 ], [ %13, %10 ]
  %16 = xor i1 %15, true
  %17 = xor i1 %16, true
  %18 = zext i1 %17 to i32
  %19 = sext i32 %18 to i64
  %20 = call i64 @llvm.expect.i64(i64 %19, i64 1)
  %21 = icmp ne i64 %20, 0
  br i1 %21, label %22, label %25

22:                                               ; preds = %14
  %23 = call i64 @osal_monotime()
  %24 = load ptr, ptr %5, align 8, !tbaa !5
  store i64 %23, ptr %24, align 8, !tbaa !9
  br label %91

25:                                               ; preds = %14
  %26 = load ptr, ptr %5, align 8, !tbaa !5
  %27 = icmp ne ptr %26, null
  br i1 %27, label %28, label %35

28:                                               ; preds = %25
  %29 = call i64 @osal_monotime()
  %30 = load ptr, ptr %5, align 8, !tbaa !5
  %31 = load i64, ptr %30, align 8, !tbaa !9
  %32 = sub i64 %29, %31
  %33 = call i64 @osal_16dot16_to_monotime(i32 noundef 6553)
  %34 = icmp ugt i64 %32, %33
  br label %35

35:                                               ; preds = %28, %25
  %36 = phi i1 [ true, %25 ], [ %34, %28 ]
  %37 = xor i1 %36, true
  %38 = xor i1 %37, true
  %39 = zext i1 %38 to i32
  %40 = sext i32 %39 to i64
  %41 = call i64 @llvm.expect.i64(i64 %40, i64 0)
  %42 = icmp ne i64 %41, 0
  br i1 %42, label %43, label %90

43:                                               ; preds = %35
  %44 = load i64, ptr %6, align 8, !tbaa !9
  %45 = icmp sge i64 %44, 0
  br i1 %45, label %46, label %68

46:                                               ; preds = %43
  %47 = load i64, ptr %6, align 8, !tbaa !9
  %48 = load ptr, ptr %7, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct.MDBX_env, ptr %48, i32 0, i32 16
  %50 = load i8, ptr %49, align 1, !tbaa !11
  %51 = sext i8 %50 to i64
  %52 = icmp ne i64 %47, %51
  br i1 %52, label %53, label %68

53:                                               ; preds = %46
  br label %54

54:                                               ; preds = %53
  %55 = load i8, ptr getelementptr inbounds (%struct.libmdbx_globals, ptr @globals, i32 0, i32 5), align 2, !tbaa !23
  %56 = zext i8 %55 to i32
  %57 = icmp sle i32 1, %56
  %58 = xor i1 %57, true
  %59 = xor i1 %58, true
  %60 = zext i1 %59 to i32
  %61 = sext i32 %60 to i64
  %62 = call i64 @llvm.expect.i64(i64 %61, i64 0)
  %63 = icmp ne i64 %62, 0
  br i1 %63, label %64, label %66

64:                                               ; preds = %54
  %65 = load i64, ptr %6, align 8, !tbaa !9
  call void (i32, ptr, i32, ptr, ...) @debug_log(i32 noundef 1, ptr noundef @__func__.coherency_timeout, i32 noundef 99, ptr noundef @.str, i64 noundef %65, ptr noundef @.str.1)
  br label %66

66:                                               ; preds = %64, %54
  br label %67

67:                                               ; preds = %66
  br label %89

68:                                               ; preds = %46, %43
  %69 = load ptr, ptr %7, align 8, !tbaa !5
  %70 = getelementptr inbounds %struct.MDBX_env, ptr %69, i32 0, i32 16
  %71 = load i8, ptr %70, align 1, !tbaa !11
  %72 = sext i8 %71 to i32
  %73 = icmp slt i32 %72, 0
  br i1 %73, label %74, label %88

74:                                               ; preds = %68
  br label %75

75:                                               ; preds = %74
  %76 = load i8, ptr getelementptr inbounds (%struct.libmdbx_globals, ptr @globals, i32 0, i32 5), align 2, !tbaa !23
  %77 = zext i8 %76 to i32
  %78 = icmp sle i32 1, %77
  %79 = xor i1 %78, true
  %80 = xor i1 %79, true
  %81 = zext i1 %80 to i32
  %82 = sext i32 %81 to i64
  %83 = call i64 @llvm.expect.i64(i64 %82, i64 0)
  %84 = icmp ne i64 %83, 0
  br i1 %84, label %85, label %86

85:                                               ; preds = %75
  call void (i32, ptr, i32, ptr, ...) @debug_log(i32 noundef 1, ptr noundef @__func__.coherency_timeout, i32 noundef 101, ptr noundef @.str.2, ptr noundef @.str.3)
  br label %86

86:                                               ; preds = %85, %75
  br label %87

87:                                               ; preds = %86
  br label %88

88:                                               ; preds = %87, %68
  br label %89

89:                                               ; preds = %88, %67
  store i32 -30779, ptr %4, align 4
  br label %93

90:                                               ; preds = %35
  br label %91

91:                                               ; preds = %90, %22
  fence release
  %92 = call i32 @sched_yield() #4
  store i32 -1, ptr %4, align 4
  br label %93

93:                                               ; preds = %91, %89
  %94 = load i32, ptr %4, align 4
  ret i32 %94
}

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

declare i64 @osal_monotime() #2

declare i64 @osal_16dot16_to_monotime(i32 noundef) #2

declare void @debug_log(i32 noundef, ptr noundef, i32 noundef, ptr noundef, ...) #2

; Function Attrs: nounwind
declare i32 @sched_yield() #3

attributes #0 = { cold nounwind optsize 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 = { "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 "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, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = !{!12, !7, i64 129}
!12 = !{!"MDBX_env", !7, i64 0, !13, i64 4, !13, i64 8, !14, i64 16, !13, i64 56, !13, i64 60, !14, i64 64, !6, i64 104, !15, i64 112, !15, i64 114, !15, i64 116, !15, i64 118, !15, i64 120, !15, i64 122, !7, i64 124, !7, i64 128, !7, i64 129, !15, i64 130, !13, i64 132, !13, i64 136, !13, i64 140, !13, i64 144, !16, i64 152, !6, i64 184, !6, i64 192, !6, i64 200, !6, i64 208, !6, i64 216, !13, i64 224, !13, i64 228, !13, i64 232, !6, i64 240, !6, i64 248, !10, i64 256, !17, i64 264, !20, i64 320, !18, i64 360, !6, i64 368, !6, i64 376, !6, i64 384, !21, i64 392, !7, i64 400, !13, i64 440, !13, i64 444, !6, i64 448, !22, i64 456, !7, i64 496, !7, i64 536}
!13 = !{!"int", !7, i64 0}
!14 = !{!"osal_mmap", !7, i64 0, !13, i64 8, !10, i64 16, !10, i64 24, !10, i64 32}
!15 = !{!"short", !7, i64 0}
!16 = !{!"", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24}
!17 = !{!"", !13, i64 0, !13, i64 4, !13, i64 8, !13, i64 12, !10, i64 16, !7, i64 24, !7, i64 25, !7, i64 26, !7, i64 27, !13, i64 28, !13, i64 32, !18, i64 36, !18, i64 37, !18, i64 38, !19, i64 40, !7, i64 48}
!18 = !{!"_Bool", !7, i64 0}
!19 = !{!"", !15, i64 0, !15, i64 2, !15, i64 4, !15, i64 6}
!20 = !{!"", !10, i64 0, !10, i64 8, !10, i64 16, !10, i64 24, !10, i64 32}
!21 = !{!"", !10, i64 0}
!22 = !{!"osal_ioring", !13, i64 0, !13, i64 4, !13, i64 8, !6, i64 16, !6, i64 24, !6, i64 32}
!23 = !{!24, !7, i64 26}
!24 = !{!"libmdbx_globals", !7, i64 0, !13, i64 16, !13, i64 20, !7, i64 24, !7, i64 25, !7, i64 26, !18, i64 27, !13, i64 28, !7, i64 32, !7, i64 40, !10, 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.6 }
%struct.anon.6 = 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__.coherency_timeout = external hidden unnamed_addr constant [18 x i8], align 1
@.str = external hidden unnamed_addr constant [41 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [62 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [41 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [60 x i8], align 1

; Function Attrs: cold nounwind optsize uwtable
define dso_local range(i32 -30779, 0) i32 @coherency_timeout(ptr noundef %0, i64 noundef %1, ptr nocapture noundef readonly %2) local_unnamed_addr #0 {
  %.not = icmp eq ptr %0, null
  br i1 %.not, label %.critedge16, label %4, !prof !5

4:                                                ; preds = %3
  %5 = load i64, ptr %0, align 8, !tbaa !6
  %6 = icmp eq i64 %5, 0
  %7 = tail call i64 @osal_monotime() #3
  br i1 %6, label %8, label %.critedge15, !prof !10

8:                                                ; preds = %4
  store i64 %7, ptr %0, align 8, !tbaa !6
  br label %23

.critedge15:                                      ; preds = %4
  %9 = load i64, ptr %0, align 8, !tbaa !6
  %10 = sub i64 %7, %9
  %11 = tail call i64 @osal_16dot16_to_monotime(i32 noundef 6553) #3
  %12 = icmp ugt i64 %10, %11
  br i1 %12, label %.critedge16, label %23, !prof !5

.critedge16:                                      ; preds = %3, %.critedge15
  %13 = icmp slt i64 %1, 0
  %14 = getelementptr inbounds i8, ptr %2, i64 129
  %15 = load i8, ptr %14, align 1, !tbaa !11
  %16 = sext i8 %15 to i64
  %.not12 = icmp eq i64 %16, %1
  %or.cond17 = select i1 %13, i1 true, i1 %.not12
  br i1 %or.cond17, label %.critedge16._crit_edge, label %17

17:                                               ; preds = %.critedge16
  %18 = load i8, ptr getelementptr inbounds (i8, ptr @globals, i64 26), align 2, !tbaa !24
  %.not14 = icmp eq i8 %18, 0
  br i1 %.not14, label %25, label %19, !prof !10

19:                                               ; preds = %17
  tail call void (i32, ptr, i32, ptr, ...) @debug_log(i32 noundef 1, ptr noundef nonnull @__func__.coherency_timeout, i32 noundef 99, ptr noundef nonnull @.str, i64 noundef %1, ptr noundef nonnull @.str.1) #3
  br label %25

.critedge16._crit_edge:                           ; preds = %.critedge16
  %20 = icmp sgt i8 %15, -1
  %21 = load i8, ptr getelementptr inbounds (i8, ptr @globals, i64 26), align 2
  %.not13 = icmp eq i8 %21, 0
  %or.cond = select i1 %20, i1 true, i1 %.not13
  br i1 %or.cond, label %25, label %22, !prof !26

22:                                               ; preds = %.critedge16._crit_edge
  tail call void (i32, ptr, i32, ptr, ...) @debug_log(i32 noundef 1, ptr noundef nonnull @__func__.coherency_timeout, i32 noundef 101, ptr noundef nonnull @.str.2, ptr noundef nonnull @.str.3) #3
  br label %25

23:                                               ; preds = %.critedge15, %8
  fence release
  %24 = tail call i32 @sched_yield() #3
  br label %25

25:                                               ; preds = %17, %19, %22, %.critedge16._crit_edge, %23
  %.0 = phi i32 [ -1, %23 ], [ -30779, %.critedge16._crit_edge ], [ -30779, %22 ], [ -30779, %19 ], [ -30779, %17 ]
  ret i32 %.0
}

declare i64 @osal_monotime() local_unnamed_addr #1

declare i64 @osal_16dot16_to_monotime(i32 noundef) local_unnamed_addr #1

declare void @debug_log(i32 noundef, ptr noundef, i32 noundef, ptr noundef, ...) local_unnamed_addr #1

; Function Attrs: nounwind
declare i32 @sched_yield() local_unnamed_addr #2

attributes #0 = { cold nounwind optsize 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 "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 }

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


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