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

; 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.osal_ioring = type { i32, i32, i32, ptr, ptr, ptr }
%struct.ior_item = type { i64, i64, %union.anon.9 }
%union.anon.9 = type { %struct.iovec }
%struct.iovec = type { ptr, i64 }

@globals = external global %struct.libmdbx_globals, align 8
@.str.12 = external hidden unnamed_addr constant [17 x i8], align 1
@__func__.osal_ioring_walk = external hidden unnamed_addr constant [17 x i8], align 1

; Function Attrs: cold noreturn nounwind optsize uwtable
declare dso_local void @mdbx_assert_fail(ptr noundef, ptr noundef, ptr noundef, i32 noundef) #0

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

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

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

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

; Function Attrs: nounwind uwtable
define dso_local void @osal_ioring_walk(ptr noundef %0, ptr noundef %1, ptr noundef %2) #4 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i64, align 8
  %9 = alloca i64, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  store ptr %2, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #5
  %10 = load ptr, ptr %4, align 8, !tbaa !5
  %11 = getelementptr inbounds %struct.osal_ioring, ptr %10, i32 0, i32 4
  %12 = load ptr, ptr %11, align 8, !tbaa !9
  store ptr %12, ptr %7, align 8, !tbaa !5
  br label %13

13:                                               ; preds = %86, %3
  %14 = load ptr, ptr %7, align 8, !tbaa !5
  %15 = load ptr, ptr %4, align 8, !tbaa !5
  %16 = getelementptr inbounds %struct.osal_ioring, ptr %15, i32 0, i32 3
  %17 = load ptr, ptr %16, align 8, !tbaa !12
  %18 = icmp ule ptr %14, %17
  br i1 %18, label %20, label %19

19:                                               ; preds = %13
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #5
  br label %90

20:                                               ; preds = %13
  br label %21

21:                                               ; preds = %20
  %22 = load i8, ptr getelementptr inbounds (%struct.libmdbx_globals, ptr @globals, i32 0, i32 4), align 1, !tbaa !13
  %23 = zext i8 %22 to i32
  %24 = and i32 %23, 1
  %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 1)
  %31 = icmp ne i64 %30, 0
  br i1 %31, label %32, label %49

32:                                               ; preds = %21
  br label %33

33:                                               ; preds = %32
  %34 = load ptr, ptr %7, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.ior_item, ptr %34, i32 0, i32 1
  %36 = load i64, ptr %35, align 8, !tbaa !17
  %37 = icmp ugt i64 %36, 0
  %38 = xor i1 %37, true
  %39 = xor i1 %38, true
  %40 = xor i1 %39, true
  %41 = zext i1 %40 to i32
  %42 = sext i32 %41 to i64
  %43 = call i64 @llvm.expect.i64(i64 %42, i64 0)
  %44 = icmp ne i64 %43, 0
  br i1 %44, label %45, label %46

45:                                               ; preds = %33
  call void @mdbx_assert_fail(ptr noundef null, ptr noundef @.str.12, ptr noundef @__func__.osal_ioring_walk, i32 noundef 766) #6
  unreachable

46:                                               ; preds = %33
  br label %47

47:                                               ; preds = %46
  br label %48

48:                                               ; preds = %47
  br label %49

49:                                               ; preds = %48, %21
  br label %50

50:                                               ; preds = %49
  br label %51

51:                                               ; preds = %50
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #5
  %52 = load ptr, ptr %7, align 8, !tbaa !5
  %53 = getelementptr inbounds %struct.ior_item, ptr %52, i32 0, i32 0
  %54 = load i64, ptr %53, align 8, !tbaa !19
  store i64 %54, ptr %8, align 8, !tbaa !20
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #5
  store i64 0, ptr %9, align 8, !tbaa !20
  br label %55

55:                                               ; preds = %79, %51
  %56 = load ptr, ptr %6, align 8, !tbaa !5
  %57 = load ptr, ptr %5, align 8, !tbaa !5
  %58 = load i64, ptr %8, align 8, !tbaa !20
  %59 = load ptr, ptr %7, align 8, !tbaa !5
  %60 = getelementptr inbounds %struct.ior_item, ptr %59, i32 0, i32 2
  %61 = load i64, ptr %9, align 8, !tbaa !20
  %62 = getelementptr inbounds [1 x %struct.iovec], ptr %60, i64 0, i64 %61
  %63 = getelementptr inbounds %struct.iovec, ptr %62, i32 0, i32 0
  %64 = load ptr, ptr %63, align 8, !tbaa !21
  %65 = load ptr, ptr %7, align 8, !tbaa !5
  %66 = getelementptr inbounds %struct.ior_item, ptr %65, i32 0, i32 2
  %67 = load i64, ptr %9, align 8, !tbaa !20
  %68 = getelementptr inbounds [1 x %struct.iovec], ptr %66, i64 0, i64 %67
  %69 = getelementptr inbounds %struct.iovec, ptr %68, i32 0, i32 1
  %70 = load i64, ptr %69, align 8, !tbaa !21
  call void %56(ptr noundef %57, i64 noundef %58, ptr noundef %64, i64 noundef %70)
  %71 = load ptr, ptr %7, align 8, !tbaa !5
  %72 = getelementptr inbounds %struct.ior_item, ptr %71, i32 0, i32 2
  %73 = load i64, ptr %9, align 8, !tbaa !20
  %74 = getelementptr inbounds [1 x %struct.iovec], ptr %72, i64 0, i64 %73
  %75 = getelementptr inbounds %struct.iovec, ptr %74, i32 0, i32 1
  %76 = load i64, ptr %75, align 8, !tbaa !21
  %77 = load i64, ptr %8, align 8, !tbaa !20
  %78 = add i64 %77, %76
  store i64 %78, ptr %8, align 8, !tbaa !20
  br label %79

79:                                               ; preds = %55
  %80 = load i64, ptr %9, align 8, !tbaa !20
  %81 = add i64 %80, 1
  store i64 %81, ptr %9, align 8, !tbaa !20
  %82 = load ptr, ptr %7, align 8, !tbaa !5
  %83 = getelementptr inbounds %struct.ior_item, ptr %82, i32 0, i32 1
  %84 = load i64, ptr %83, align 8, !tbaa !17
  %85 = icmp ne i64 %81, %84
  br i1 %85, label %55, label %86, !llvm.loop !22

86:                                               ; preds = %79
  %87 = load ptr, ptr %7, align 8, !tbaa !5
  %88 = load i64, ptr %9, align 8, !tbaa !20
  %89 = call ptr @ior_next(ptr noundef %87, i64 noundef %88)
  store ptr %89, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #5
  br label %13, !llvm.loop !24

90:                                               ; preds = %19
  ret void
}

attributes #0 = { cold noreturn 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(argmem: readwrite) }
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 = { nounwind }
attributes #6 = { cold 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, !6, i64 24}
!10 = !{!"osal_ioring", !11, i64 0, !11, i64 4, !11, i64 8, !6, i64 16, !6, i64 24, !6, i64 32}
!11 = !{!"int", !7, i64 0}
!12 = !{!10, !6, i64 16}
!13 = !{!14, !7, i64 25}
!14 = !{!"libmdbx_globals", !7, i64 0, !11, i64 16, !11, i64 20, !7, i64 24, !7, i64 25, !7, i64 26, !15, i64 27, !11, i64 28, !7, i64 32, !7, i64 40, !16, i64 80, !6, i64 88}
!15 = !{!"_Bool", !7, i64 0}
!16 = !{!"long", !7, i64 0}
!17 = !{!18, !16, i64 8}
!18 = !{!"ior_item", !16, i64 0, !16, i64 8, !7, i64 16}
!19 = !{!18, !16, i64 0}
!20 = !{!16, !16, i64 0}
!21 = !{!7, !7, i64 0}
!22 = distinct !{!22, !23}
!23 = !{!"llvm.loop.mustprogress"}
!24 = distinct !{!24, !23}


; 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 }
%struct.iovec = type { ptr, i64 }

@globals = external local_unnamed_addr global %struct.libmdbx_globals, align 8
@.str.12 = external hidden unnamed_addr constant [17 x i8], align 1
@__func__.osal_ioring_walk = external hidden unnamed_addr constant [17 x i8], align 1

; Function Attrs: cold noreturn nounwind optsize uwtable
declare dso_local void @mdbx_assert_fail(ptr noundef, ptr noundef, ptr noundef, i32 noundef) local_unnamed_addr #0

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

; Function Attrs: nounwind uwtable
define dso_local void @osal_ioring_walk(ptr nocapture noundef readonly %0, ptr noundef %1, ptr nocapture noundef readonly %2) local_unnamed_addr #2 {
  %4 = getelementptr inbounds i8, ptr %0, i64 24
  %5 = load ptr, ptr %4, align 8, !tbaa !5
  %6 = getelementptr inbounds i8, ptr %0, i64 16
  %7 = load ptr, ptr %6, align 8, !tbaa !11
  %.not24 = icmp ugt ptr %5, %7
  br i1 %.not24, label %._crit_edge, label %.lr.ph

._crit_edge:                                      ; preds = %27, %3
  ret void

.lr.ph:                                           ; preds = %3, %27
  %.01925 = phi ptr [ %28, %27 ], [ %5, %3 ]
  %8 = load i8, ptr getelementptr inbounds (i8, ptr @globals, i64 25), align 1, !tbaa !12
  %9 = and i8 %8, 1
  %.not21 = icmp eq i8 %9, 0
  br i1 %.not21, label %14, label %10, !prof !16

10:                                               ; preds = %.lr.ph
  %11 = getelementptr inbounds i8, ptr %.01925, i64 8
  %12 = load i64, ptr %11, align 8, !tbaa !17
  %.not22 = icmp eq i64 %12, 0
  br i1 %.not22, label %13, label %14, !prof !16

13:                                               ; preds = %10
  tail call void @mdbx_assert_fail(ptr noundef null, ptr noundef nonnull @.str.12, ptr noundef nonnull @__func__.osal_ioring_walk, i32 noundef 766) #3
  unreachable

14:                                               ; preds = %10, %.lr.ph
  %15 = load i64, ptr %.01925, align 8, !tbaa !19
  %16 = getelementptr inbounds i8, ptr %.01925, i64 16
  %17 = getelementptr inbounds i8, ptr %.01925, i64 8
  br label %18

18:                                               ; preds = %18, %14
  %.018 = phi i64 [ %15, %14 ], [ %24, %18 ]
  %.0 = phi i64 [ 0, %14 ], [ %25, %18 ]
  %19 = getelementptr inbounds [1 x %struct.iovec], ptr %16, i64 0, i64 %.0
  %20 = load ptr, ptr %19, align 8, !tbaa !20
  %21 = getelementptr inbounds i8, ptr %19, i64 8
  %22 = load i64, ptr %21, align 8, !tbaa !20
  tail call void %2(ptr noundef %1, i64 noundef %.018, ptr noundef %20, i64 noundef %22) #4
  %23 = load i64, ptr %21, align 8, !tbaa !20
  %24 = add i64 %23, %.018
  %25 = add i64 %.0, 1
  %26 = load i64, ptr %17, align 8, !tbaa !17
  %.not23 = icmp eq i64 %25, %26
  br i1 %.not23, label %27, label %18, !llvm.loop !21

27:                                               ; preds = %18
  %28 = tail call ptr @ior_next(ptr noundef nonnull %.01925, i64 noundef %25)
  %29 = load ptr, ptr %6, align 8, !tbaa !11
  %.not = icmp ugt ptr %28, %29
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !23
}

attributes #0 = { cold noreturn 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 = { 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 = { cold noreturn }
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, !10, i64 24}
!6 = !{!"osal_ioring", !7, i64 0, !7, i64 4, !7, i64 8, !10, i64 16, !10, i64 24, !10, i64 32}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!6, !10, i64 16}
!12 = !{!13, !8, i64 25}
!13 = !{!"libmdbx_globals", !8, i64 0, !7, i64 16, !7, i64 20, !8, i64 24, !8, i64 25, !8, i64 26, !14, i64 27, !7, i64 28, !8, i64 32, !8, i64 40, !15, i64 80, !10, i64 88}
!14 = !{!"_Bool", !8, i64 0}
!15 = !{!"long", !8, i64 0}
!16 = !{!"branch_weights", !"expected", i32 1, i32 2000}
!17 = !{!18, !15, i64 8}
!18 = !{!"ior_item", !15, i64 0, !15, i64 8, !8, i64 16}
!19 = !{!18, !15, i64 0}
!20 = !{!8, !8, i64 0}
!21 = distinct !{!21, !22}
!22 = !{!"llvm.loop.mustprogress"}
!23 = distinct !{!23, !22}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,LowerExpectIntrinsicPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass