; 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_write_result = type { i32, i32 }
%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_write = external hidden unnamed_addr constant [18 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: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #3

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

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

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

17:                                               ; preds = %11
  store i32 2, ptr %7, align 4
  br label %104

18:                                               ; preds = %11
  br label %19

19:                                               ; preds = %18
  %20 = load i8, ptr getelementptr inbounds (%struct.libmdbx_globals, ptr @globals, i32 0, i32 4), align 1, !tbaa !14
  %21 = zext i8 %20 to i32
  %22 = and i32 %21, 1
  %23 = icmp ne i32 %22, 0
  %24 = xor i1 %23, true
  %25 = xor i1 %24, true
  %26 = zext i1 %25 to i32
  %27 = sext i32 %26 to i64
  %28 = call i64 @llvm.expect.i64(i64 %27, i64 1)
  %29 = icmp ne i64 %28, 0
  br i1 %29, label %30, label %47

30:                                               ; preds = %19
  br label %31

31:                                               ; preds = %30
  %32 = load ptr, ptr %6, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.ior_item, ptr %32, i32 0, i32 1
  %34 = load i64, ptr %33, align 8, !tbaa !18
  %35 = icmp ugt i64 %34, 0
  %36 = xor i1 %35, true
  %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 %44

43:                                               ; preds = %31
  call void @mdbx_assert_fail(ptr noundef null, ptr noundef @.str.12, ptr noundef @__func__.osal_ioring_write, i32 noundef 976) #7
  unreachable

44:                                               ; preds = %31
  br label %45

45:                                               ; preds = %44
  br label %46

46:                                               ; preds = %45
  br label %47

47:                                               ; preds = %46, %19
  br label %48

48:                                               ; preds = %47
  br label %49

49:                                               ; preds = %48
  %50 = load ptr, ptr %6, align 8, !tbaa !5
  %51 = getelementptr inbounds %struct.ior_item, ptr %50, i32 0, i32 1
  %52 = load i64, ptr %51, align 8, !tbaa !18
  %53 = icmp eq i64 %52, 1
  br i1 %53, label %54, label %71

54:                                               ; preds = %49
  %55 = load i32, ptr %5, align 4, !tbaa !9
  %56 = load ptr, ptr %6, align 8, !tbaa !5
  %57 = getelementptr inbounds %struct.ior_item, ptr %56, i32 0, i32 2
  %58 = getelementptr inbounds [1 x %struct.iovec], ptr %57, i64 0, i64 0
  %59 = getelementptr inbounds %struct.iovec, ptr %58, i32 0, i32 0
  %60 = load ptr, ptr %59, align 8, !tbaa !20
  %61 = load ptr, ptr %6, align 8, !tbaa !5
  %62 = getelementptr inbounds %struct.ior_item, ptr %61, i32 0, i32 2
  %63 = getelementptr inbounds [1 x %struct.iovec], ptr %62, i64 0, i64 0
  %64 = getelementptr inbounds %struct.iovec, ptr %63, i32 0, i32 1
  %65 = load i64, ptr %64, align 8, !tbaa !20
  %66 = load ptr, ptr %6, align 8, !tbaa !5
  %67 = getelementptr inbounds %struct.ior_item, ptr %66, i32 0, i32 0
  %68 = load i64, ptr %67, align 8, !tbaa !21
  %69 = call i32 @osal_pwrite(i32 noundef %55, ptr noundef %60, i64 noundef %65, i64 noundef %68)
  %70 = getelementptr inbounds %struct.osal_ioring_write_result, ptr %3, i32 0, i32 0
  store i32 %69, ptr %70, align 4, !tbaa !22
  br label %84

71:                                               ; preds = %49
  %72 = load i32, ptr %5, align 4, !tbaa !9
  %73 = load ptr, ptr %6, align 8, !tbaa !5
  %74 = getelementptr inbounds %struct.ior_item, ptr %73, i32 0, i32 2
  %75 = getelementptr inbounds [1 x %struct.iovec], ptr %74, i64 0, i64 0
  %76 = load ptr, ptr %6, align 8, !tbaa !5
  %77 = getelementptr inbounds %struct.ior_item, ptr %76, i32 0, i32 1
  %78 = load i64, ptr %77, align 8, !tbaa !18
  %79 = load ptr, ptr %6, align 8, !tbaa !5
  %80 = getelementptr inbounds %struct.ior_item, ptr %79, i32 0, i32 0
  %81 = load i64, ptr %80, align 8, !tbaa !21
  %82 = call i32 @osal_pwritev(i32 noundef %72, ptr noundef %75, i64 noundef %78, i64 noundef %81)
  %83 = getelementptr inbounds %struct.osal_ioring_write_result, ptr %3, i32 0, i32 0
  store i32 %82, ptr %83, align 4, !tbaa !22
  br label %84

84:                                               ; preds = %71, %54
  %85 = load ptr, ptr %6, align 8, !tbaa !5
  %86 = load ptr, ptr %6, align 8, !tbaa !5
  %87 = getelementptr inbounds %struct.ior_item, ptr %86, i32 0, i32 1
  %88 = load i64, ptr %87, align 8, !tbaa !18
  %89 = call ptr @ior_next(ptr noundef %85, i64 noundef %88)
  store ptr %89, ptr %6, align 8, !tbaa !5
  %90 = getelementptr inbounds %struct.osal_ioring_write_result, ptr %3, i32 0, i32 1
  %91 = load i32, ptr %90, align 4, !tbaa !24
  %92 = add i32 %91, 1
  store i32 %92, ptr %90, align 4, !tbaa !24
  %93 = getelementptr inbounds %struct.osal_ioring_write_result, ptr %3, i32 0, i32 0
  %94 = load i32, ptr %93, align 4, !tbaa !22
  %95 = icmp ne i32 %94, 0
  %96 = xor i1 %95, true
  %97 = xor i1 %96, true
  %98 = zext i1 %97 to i32
  %99 = sext i32 %98 to i64
  %100 = call i64 @llvm.expect.i64(i64 %99, i64 0)
  %101 = icmp ne i64 %100, 0
  br i1 %101, label %102, label %103

102:                                              ; preds = %84
  store i32 2, ptr %7, align 4
  br label %104

103:                                              ; preds = %84
  br label %11, !llvm.loop !25

104:                                              ; preds = %102, %17
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #6
  br label %105

105:                                              ; preds = %104
  %106 = load i64, ptr %3, align 4
  ret i64 %106
}

; Function Attrs: nounwind uwtable
declare dso_local i32 @osal_pwrite(i32 noundef, ptr noundef, i64 noundef, i64 noundef) #5

; Function Attrs: nounwind uwtable
declare dso_local i32 @osal_pwritev(i32 noundef, ptr noundef, i64 noundef, i64 noundef) #5

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


; 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
@.str.12 = external hidden unnamed_addr constant [17 x i8], align 1
@__func__.osal_ioring_write = external hidden unnamed_addr constant [18 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 i64 @osal_ioring_write(ptr nocapture noundef readonly %0, i32 noundef %1) local_unnamed_addr #2 {
  %3 = getelementptr inbounds i8, ptr %0, i64 24
  %4 = load ptr, ptr %3, align 8, !tbaa !5
  %5 = getelementptr inbounds i8, ptr %0, i64 16
  br label %6

6:                                                ; preds = %24, %2
  %.sroa.5.0 = phi i32 [ 0, %2 ], [ %27, %24 ]
  %.0 = phi ptr [ %4, %2 ], [ %26, %24 ]
  %7 = load ptr, ptr %5, align 8, !tbaa !11
  %.not = icmp ugt ptr %.0, %7
  br i1 %.not, label %.loopexit, label %8

8:                                                ; preds = %6
  %9 = load i8, ptr getelementptr inbounds (i8, ptr @globals, i64 25), align 1, !tbaa !12
  %10 = and i8 %9, 1
  %.not18 = icmp ne i8 %10, 0
  %.phi.trans.insert = getelementptr inbounds i8, ptr %.0, i64 8
  %.pre = load i64, ptr %.phi.trans.insert, align 8, !tbaa !16
  %.not19 = icmp eq i64 %.pre, 0
  %or.cond = select i1 %.not18, i1 %.not19, i1 false
  br i1 %or.cond, label %11, label %._crit_edge, !prof !18

11:                                               ; preds = %8
  tail call void @mdbx_assert_fail(ptr noundef null, ptr noundef nonnull @.str.12, ptr noundef nonnull @__func__.osal_ioring_write, i32 noundef 976) #3
  unreachable

._crit_edge:                                      ; preds = %8
  %12 = getelementptr inbounds i8, ptr %.0, i64 8
  %13 = icmp eq i64 %.pre, 1
  %14 = getelementptr inbounds i8, ptr %.0, i64 16
  br i1 %13, label %15, label %21

15:                                               ; preds = %._crit_edge
  %16 = load ptr, ptr %14, align 8, !tbaa !19
  %17 = getelementptr inbounds i8, ptr %.0, i64 24
  %18 = load i64, ptr %17, align 8, !tbaa !19
  %19 = load i64, ptr %.0, align 8, !tbaa !20
  %20 = tail call i32 @osal_pwrite(i32 noundef %1, ptr noundef %16, i64 noundef %18, i64 noundef %19)
  br label %24

21:                                               ; preds = %._crit_edge
  %22 = load i64, ptr %.0, align 8, !tbaa !20
  %23 = tail call i32 @osal_pwritev(i32 noundef %1, ptr noundef nonnull %14, i64 noundef %.pre, i64 noundef %22)
  br label %24

24:                                               ; preds = %21, %15
  %.sroa.0.1 = phi i32 [ %20, %15 ], [ %23, %21 ]
  %25 = load i64, ptr %12, align 8, !tbaa !16
  %26 = tail call ptr @ior_next(ptr noundef nonnull %.0, i64 noundef %25)
  %27 = add i32 %.sroa.5.0, 1
  %.not20 = icmp eq i32 %.sroa.0.1, 0
  br i1 %.not20, label %6, label %28, !prof !21, !llvm.loop !22

28:                                               ; preds = %24
  %29 = zext i32 %.sroa.0.1 to i64
  br label %.loopexit

.loopexit:                                        ; preds = %6, %28
  %.sroa.5.1 = phi i32 [ %27, %28 ], [ %.sroa.5.0, %6 ]
  %.sroa.0.2 = phi i64 [ %29, %28 ], [ 0, %6 ]
  %.sroa.5.0.insert.ext = zext i32 %.sroa.5.1 to i64
  %.sroa.5.0.insert.shift = shl nuw i64 %.sroa.5.0.insert.ext, 32
  %.sroa.0.0.insert.insert = or disjoint i64 %.sroa.5.0.insert.shift, %.sroa.0.2
  ret i64 %.sroa.0.0.insert.insert
}

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

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

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 }

!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 = !{!17, !15, i64 8}
!17 = !{!"ior_item", !15, i64 0, !15, i64 8, !8, i64 16}
!18 = !{!"branch_weights", i32 2000, i32 4002001}
!19 = !{!8, !8, i64 0}
!20 = !{!17, !15, i64 0}
!21 = !{!"branch_weights", !"expected", i32 2000, i32 1}
!22 = distinct !{!22, !23}
!23 = !{!"llvm.loop.mustprogress"}


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