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

11:                                               ; preds = %61, %59, %3
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #5
  %12 = load i32, ptr %5, align 4, !tbaa !5
  %13 = load ptr, ptr %6, align 8, !tbaa !9
  %14 = load i64, ptr %7, align 8, !tbaa !11
  %15 = icmp ule i64 %14, 1056964608
  %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 %24

22:                                               ; preds = %11
  %23 = load i64, ptr %7, align 8, !tbaa !11
  br label %25

24:                                               ; preds = %11
  br label %25

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

37:                                               ; preds = %25
  store i32 0, ptr %4, align 4
  store i32 1, ptr %9, align 4
  br label %59

38:                                               ; preds = %25
  %39 = load i64, ptr %8, align 8, !tbaa !11
  %40 = icmp slt i64 %39, 0
  br i1 %40, label %41, label %50

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

46:                                               ; preds = %41
  %47 = load i32, ptr %10, align 4, !tbaa !5
  store i32 %47, ptr %4, align 4
  store i32 1, ptr %9, align 4
  br label %49

48:                                               ; preds = %41
  store i32 2, ptr %9, align 4
  br label %49

49:                                               ; preds = %48, %46
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #5
  br label %59

50:                                               ; preds = %38
  %51 = load i64, ptr %8, align 8, !tbaa !11
  %52 = load i64, ptr %7, align 8, !tbaa !11
  %53 = sub i64 %52, %51
  store i64 %53, ptr %7, align 8, !tbaa !11
  %54 = load ptr, ptr %6, align 8, !tbaa !9
  %55 = ptrtoint ptr %54 to i64
  %56 = load i64, ptr %8, align 8, !tbaa !11
  %57 = add nsw i64 %55, %56
  %58 = inttoptr i64 %57 to ptr
  store ptr %58, ptr %6, align 8, !tbaa !9
  store i32 0, ptr %9, align 4
  br label %59

59:                                               ; preds = %50, %49, %37
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #5
  %60 = load i32, ptr %9, align 4
  switch i32 %60, label %64 [
    i32 0, label %61
    i32 1, label %62
    i32 2, label %11
  ]

61:                                               ; preds = %59
  br label %11

62:                                               ; preds = %59
  %63 = load i32, ptr %4, align 4
  ret i32 %63

64:                                               ; preds = %59
  unreachable
}

declare i64 @write(i32 noundef, ptr 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: nofree nounwind uwtable
define dso_local i32 @osal_write(i32 noundef %0, ptr noundef %1, i64 noundef %2) local_unnamed_addr #1 {
  %4 = tail call i64 @llvm.umin.i64(i64 %2, i64 1056964608)
  %5 = tail call i64 @write(i32 noundef %0, ptr noundef %1, i64 noundef %4) #4
  %6 = icmp eq i64 %5, %2
  br i1 %6, label %.thread, label %.lr.ph, !prof !5

.lr.ph:                                           ; preds = %3, %select.unfold
  %7 = phi i64 [ %18, %select.unfold ], [ %5, %3 ]
  %.01636 = phi ptr [ %.117, %select.unfold ], [ %1, %3 ]
  %.01835 = phi i64 [ %.119, %select.unfold ], [ %2, %3 ]
  %8 = icmp slt i64 %7, 0
  br i1 %8, label %9, label %12

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

12:                                               ; preds = %.lr.ph
  %13 = sub i64 %.01835, %7
  %14 = ptrtoint ptr %.01636 to i64
  %15 = add nsw i64 %7, %14
  %16 = inttoptr i64 %15 to ptr
  br label %select.unfold

select.unfold:                                    ; preds = %9, %12
  %.119 = phi i64 [ %13, %12 ], [ %.01835, %9 ]
  %.117 = phi ptr [ %16, %12 ], [ %.01636, %9 ]
  %17 = tail call i64 @llvm.umin.i64(i64 %.119, i64 1056964608)
  %18 = tail call i64 @write(i32 noundef %0, ptr noundef %.117, i64 noundef %17) #4
  %19 = icmp eq i64 %.119, %18
  br i1 %19, label %.thread, label %.lr.ph, !prof !10

.thread:                                          ; preds = %select.unfold, %9, %3
  %.228 = phi i32 [ 0, %3 ], [ 0, %select.unfold ], [ %11, %9 ]
  ret i32 %.228
}

; Function Attrs: nofree
declare noundef i64 @write(i32 noundef, ptr nocapture noundef readonly, 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 = { nofree 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 = { nofree "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,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass