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

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

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

; Function Attrs: nounwind willreturn memory(none)
declare ptr @__errno_location() #2

; 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
define dso_local i32 @osal_pwrite(i32 noundef %0, ptr noundef %1, i64 noundef %2, i64 noundef %3) #3 {
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  %8 = alloca i64, align 8
  %9 = alloca i64, align 8
  %10 = alloca i64, align 8
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  store i32 %0, ptr %6, align 4, !tbaa !5
  store ptr %1, ptr %7, align 8, !tbaa !9
  store i64 %2, ptr %8, align 8, !tbaa !11
  store i64 %3, ptr %9, align 8, !tbaa !11
  br label %13

13:                                               ; preds = %67, %65, %4
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #5
  %14 = load i32, ptr %6, align 4, !tbaa !5
  %15 = load ptr, ptr %7, align 8, !tbaa !9
  %16 = load i64, ptr %8, align 8, !tbaa !11
  %17 = icmp ule i64 %16, 1056964608
  %18 = xor i1 %17, true
  %19 = xor i1 %18, true
  %20 = zext i1 %19 to i32
  %21 = sext i32 %20 to i64
  %22 = call i64 @llvm.expect.i64(i64 %21, i64 1)
  %23 = icmp ne i64 %22, 0
  br i1 %23, label %24, label %26

24:                                               ; preds = %13
  %25 = load i64, ptr %8, align 8, !tbaa !11
  br label %27

26:                                               ; preds = %13
  br label %27

27:                                               ; preds = %26, %24
  %28 = phi i64 [ %25, %24 ], [ 1056964608, %26 ]
  %29 = load i64, ptr %9, align 8, !tbaa !11
  %30 = call i64 @pwrite64(i32 noundef %14, ptr noundef %15, i64 noundef %28, i64 noundef %29)
  store i64 %30, ptr %10, align 8, !tbaa !11
  %31 = load i64, ptr %8, align 8, !tbaa !11
  %32 = load i64, ptr %10, align 8, !tbaa !11
  %33 = icmp eq i64 %31, %32
  %34 = xor i1 %33, true
  %35 = xor i1 %34, true
  %36 = zext i1 %35 to i32
  %37 = sext i32 %36 to i64
  %38 = call i64 @llvm.expect.i64(i64 %37, i64 1)
  %39 = icmp ne i64 %38, 0
  br i1 %39, label %40, label %41

40:                                               ; preds = %27
  store i32 0, ptr %5, align 4
  store i32 1, ptr %11, align 4
  br label %65

41:                                               ; preds = %27
  %42 = load i64, ptr %10, align 8, !tbaa !11
  %43 = icmp slt i64 %42, 0
  br i1 %43, label %44, label %53

44:                                               ; preds = %41
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #5
  %45 = call ptr @__errno_location() #6
  %46 = load i32, ptr %45, align 4, !tbaa !5
  store i32 %46, ptr %12, align 4, !tbaa !5
  %47 = load i32, ptr %12, align 4, !tbaa !5
  %48 = icmp ne i32 %47, 4
  br i1 %48, label %49, label %51

49:                                               ; preds = %44
  %50 = load i32, ptr %12, align 4, !tbaa !5
  store i32 %50, ptr %5, align 4
  store i32 1, ptr %11, align 4
  br label %52

51:                                               ; preds = %44
  store i32 2, ptr %11, align 4
  br label %52

52:                                               ; preds = %51, %49
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #5
  br label %65

53:                                               ; preds = %41
  %54 = load i64, ptr %10, align 8, !tbaa !11
  %55 = load i64, ptr %8, align 8, !tbaa !11
  %56 = sub i64 %55, %54
  store i64 %56, ptr %8, align 8, !tbaa !11
  %57 = load i64, ptr %10, align 8, !tbaa !11
  %58 = load i64, ptr %9, align 8, !tbaa !11
  %59 = add i64 %58, %57
  store i64 %59, ptr %9, align 8, !tbaa !11
  %60 = load ptr, ptr %7, align 8, !tbaa !9
  %61 = ptrtoint ptr %60 to i64
  %62 = load i64, ptr %10, align 8, !tbaa !11
  %63 = add nsw i64 %61, %62
  %64 = inttoptr i64 %63 to ptr
  store ptr %64, ptr %7, align 8, !tbaa !9
  store i32 0, ptr %11, align 4
  br label %65

65:                                               ; preds = %53, %52, %40
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #5
  %66 = load i32, ptr %11, align 4
  switch i32 %66, label %70 [
    i32 0, label %67
    i32 1, label %68
    i32 2, label %13
  ]

67:                                               ; preds = %65
  br label %13

68:                                               ; preds = %65
  %69 = load i32, ptr %5, align 4
  ret i32 %69

70:                                               ; preds = %65
  unreachable
}

declare i64 @pwrite64(i32 noundef, ptr noundef, i64 noundef, i64 noundef) #4

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nosync nounwind willreturn memory(none) }
attributes #2 = { nounwind willreturn memory(none) "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 = { nounwind willreturn memory(none) }

!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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"long", !7, i64 0}


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

; Function Attrs: mustprogress nofree nosync nounwind willreturn memory(none)
declare ptr @__errno_location() local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local i32 @osal_pwrite(i32 noundef %0, ptr noundef %1, i64 noundef %2, i64 noundef %3) local_unnamed_addr #1 {
  %5 = tail call i64 @llvm.umin.i64(i64 %2, i64 1056964608)
  %6 = tail call i64 @pwrite64(i32 noundef %0, ptr noundef %1, i64 noundef %5, i64 noundef %3) #4
  %7 = icmp eq i64 %6, %2
  br i1 %7, label %.thread, label %.lr.ph, !prof !5

.lr.ph:                                           ; preds = %4, %select.unfold
  %8 = phi i64 [ %20, %select.unfold ], [ %6, %4 ]
  %.01944 = phi ptr [ %.120, %select.unfold ], [ %1, %4 ]
  %.02143 = phi i64 [ %.122, %select.unfold ], [ %3, %4 ]
  %.02342 = phi i64 [ %.124, %select.unfold ], [ %2, %4 ]
  %9 = icmp slt i64 %8, 0
  br i1 %9, label %10, label %13

10:                                               ; preds = %.lr.ph
  %11 = tail call ptr @__errno_location() #5
  %12 = load i32, ptr %11, align 4, !tbaa !6
  %.not = icmp eq i32 %12, 4
  br i1 %.not, label %select.unfold, label %.thread

13:                                               ; preds = %.lr.ph
  %14 = sub i64 %.02342, %8
  %15 = add i64 %8, %.02143
  %16 = ptrtoint ptr %.01944 to i64
  %17 = add nsw i64 %8, %16
  %18 = inttoptr i64 %17 to ptr
  br label %select.unfold

select.unfold:                                    ; preds = %10, %13
  %.124 = phi i64 [ %14, %13 ], [ %.02342, %10 ]
  %.122 = phi i64 [ %15, %13 ], [ %.02143, %10 ]
  %.120 = phi ptr [ %18, %13 ], [ %.01944, %10 ]
  %19 = tail call i64 @llvm.umin.i64(i64 %.124, i64 1056964608)
  %20 = tail call i64 @pwrite64(i32 noundef %0, ptr noundef %.120, i64 noundef %19, i64 noundef %.122) #4
  %21 = icmp eq i64 %.124, %20
  br i1 %21, label %.thread, label %.lr.ph, !prof !10

.thread:                                          ; preds = %select.unfold, %10, %4
  %.234 = phi i32 [ 0, %4 ], [ 0, %select.unfold ], [ %12, %10 ]
  ret i32 %.234
}

declare i64 @pwrite64(i32 noundef, ptr noundef, i64 noundef, i64 noundef) local_unnamed_addr #2

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

attributes #0 = { mustprogress nofree nosync nounwind willreturn memory(none) "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 = { 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 = { "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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #4 = { nounwind }
attributes #5 = { nounwind willreturn memory(none) }

!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", i32 1999, i32 1}
!6 = !{!7, !7, i64 0}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"branch_weights", i32 1, i32 0}


; Effective O3 passes: ADCEPass,CorrelatedValuePropagationPass,EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopUnrollPass,LowerExpectIntrinsicPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass