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

@globals = external global %struct.libmdbx_globals, align 8
@.str.25 = external hidden unnamed_addr constant [43 x i8], align 1
@__func__.osal_fsync = external hidden unnamed_addr constant [11 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: nounwind willreturn memory(none)
declare ptr @__errno_location() #3

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

; Function Attrs: nounwind uwtable
define dso_local i32 @osal_fsync(i32 noundef %0, i32 noundef %1) #4 {
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  store i32 %0, ptr %4, align 4, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !5
  br label %8

8:                                                ; preds = %74, %2
  %9 = load i32, ptr %5, align 4, !tbaa !5
  %10 = and i32 %9, 6
  switch i32 %10, label %52 [
    i32 0, label %11
    i32 1, label %11
    i32 2, label %12
    i32 4, label %24
  ]

11:                                               ; preds = %8, %8
  store i32 0, ptr %3, align 4
  br label %75

12:                                               ; preds = %8
  %13 = load i32, ptr %4, align 4, !tbaa !5
  %14 = call i32 @fdatasync(i32 noundef %13)
  %15 = icmp eq i32 %14, 0
  %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 %23

22:                                               ; preds = %12
  store i32 0, ptr %3, align 4
  br label %75

23:                                               ; preds = %12
  br label %64

24:                                               ; preds = %8
  br label %25

25:                                               ; preds = %24
  %26 = load i8, ptr getelementptr inbounds (%struct.libmdbx_globals, ptr @globals, i32 0, i32 4), align 1, !tbaa !9
  %27 = zext i8 %26 to i32
  %28 = and i32 %27, 1
  %29 = icmp ne i32 %28, 0
  %30 = xor i1 %29, true
  %31 = xor i1 %30, true
  %32 = zext i1 %31 to i32
  %33 = sext i32 %32 to i64
  %34 = call i64 @llvm.expect.i64(i64 %33, i64 1)
  %35 = icmp ne i64 %34, 0
  br i1 %35, label %36, label %50

36:                                               ; preds = %25
  br label %37

37:                                               ; preds = %36
  %38 = load i32, ptr getelementptr inbounds (%struct.libmdbx_globals, ptr @globals, i32 0, i32 7), align 4, !tbaa !14
  %39 = icmp uge i32 %38, 50724864
  %40 = xor i1 %39, true
  %41 = xor i1 %40, true
  %42 = xor i1 %41, true
  %43 = zext i1 %42 to i32
  %44 = sext i32 %43 to i64
  %45 = call i64 @llvm.expect.i64(i64 %44, i64 0)
  %46 = icmp ne i64 %45, 0
  br i1 %46, label %47, label %48

47:                                               ; preds = %37
  call void @mdbx_assert_fail(ptr noundef null, ptr noundef @.str.25, ptr noundef @__func__.osal_fsync, i32 noundef 1530) #6
  unreachable

48:                                               ; preds = %37
  br label %49

49:                                               ; preds = %48
  br label %50

50:                                               ; preds = %49, %25
  br label %51

51:                                               ; preds = %50
  store i32 0, ptr %3, align 4
  br label %75

52:                                               ; preds = %8
  %53 = load i32, ptr %4, align 4, !tbaa !5
  %54 = call i32 @fsync(i32 noundef %53)
  %55 = icmp eq i32 %54, 0
  %56 = xor i1 %55, true
  %57 = xor i1 %56, true
  %58 = zext i1 %57 to i32
  %59 = sext i32 %58 to i64
  %60 = call i64 @llvm.expect.i64(i64 %59, i64 1)
  %61 = icmp ne i64 %60, 0
  br i1 %61, label %62, label %63

62:                                               ; preds = %52
  store i32 0, ptr %3, align 4
  br label %75

63:                                               ; preds = %52
  br label %64

64:                                               ; preds = %63, %23
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #7
  %65 = call ptr @__errno_location() #8
  %66 = load i32, ptr %65, align 4, !tbaa !5
  store i32 %66, ptr %6, align 4, !tbaa !5
  %67 = load i32, ptr %6, align 4, !tbaa !5
  %68 = icmp ne i32 %67, 4
  br i1 %68, label %69, label %71

69:                                               ; preds = %64
  %70 = load i32, ptr %6, align 4, !tbaa !5
  store i32 %70, ptr %3, align 4
  store i32 1, ptr %7, align 4
  br label %72

71:                                               ; preds = %64
  store i32 0, ptr %7, align 4
  br label %72

72:                                               ; preds = %71, %69
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #7
  %73 = load i32, ptr %7, align 4
  switch i32 %73, label %77 [
    i32 0, label %74
    i32 1, label %75
  ]

74:                                               ; preds = %72
  br label %8

75:                                               ; preds = %72, %62, %51, %22, %11
  %76 = load i32, ptr %3, align 4
  ret i32 %76

77:                                               ; preds = %72
  unreachable
}

declare i32 @fdatasync(i32 noundef) #5

declare i32 @fsync(i32 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 = { 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 #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 = { "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 = { cold noreturn }
attributes #7 = { nounwind }
attributes #8 = { 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, !7, i64 25}
!10 = !{!"libmdbx_globals", !7, i64 0, !6, i64 16, !6, i64 20, !7, i64 24, !7, i64 25, !7, i64 26, !11, i64 27, !6, i64 28, !7, i64 32, !7, i64 40, !12, i64 80, !13, i64 88}
!11 = !{!"_Bool", !7, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = !{!"any pointer", !7, i64 0}
!14 = !{!10, !6, i64 28}


; 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.25 = external hidden unnamed_addr constant [43 x i8], align 1
@__func__.osal_fsync = external hidden unnamed_addr constant [11 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: mustprogress nofree nosync nounwind willreturn memory(none)
declare ptr @__errno_location() local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local i32 @osal_fsync(i32 noundef %0, i32 noundef %1) local_unnamed_addr #2 {
  %3 = and i32 %1, 6
  switch i32 %3, label %.unreachabledefault [
    i32 0, label %.loopexit.split
    i32 4, label %14
    i32 2, label %.split.split.us
    i32 6, label %.split.split.us14
  ]

.split.split.us:                                  ; preds = %2, %6
  %4 = tail call i32 @fdatasync(i32 noundef %0) #4
  %5 = icmp eq i32 %4, 0
  br i1 %5, label %.loopexit.split, label %6, !prof !5

6:                                                ; preds = %.split.split.us
  %7 = tail call ptr @__errno_location() #5
  %8 = load i32, ptr %7, align 4, !tbaa !6
  %.not.not.us = icmp eq i32 %8, 4
  br i1 %.not.not.us, label %.split.split.us, label %.loopexit.split

.split.split.us14:                                ; preds = %2, %11
  %9 = tail call i32 @fsync(i32 noundef %0) #4
  %10 = icmp eq i32 %9, 0
  br i1 %10, label %.loopexit.split, label %11, !prof !5

11:                                               ; preds = %.split.split.us14
  %12 = tail call ptr @__errno_location() #5
  %13 = load i32, ptr %12, align 4, !tbaa !6
  %.not.not.us15 = icmp eq i32 %13, 4
  br i1 %.not.not.us15, label %.split.split.us14, label %.loopexit.split

14:                                               ; preds = %2
  %15 = load i8, ptr getelementptr inbounds (i8, ptr @globals, i64 25), align 1, !tbaa !10
  %16 = and i8 %15, 1
  %.not9 = icmp ne i8 %16, 0
  %17 = load i32, ptr getelementptr inbounds (i8, ptr @globals, i64 28), align 4
  %18 = icmp ult i32 %17, 50724864
  %or.cond = select i1 %.not9, i1 %18, i1 false
  br i1 %or.cond, label %19, label %.loopexit.split, !prof !15

19:                                               ; preds = %14
  tail call void @mdbx_assert_fail(ptr noundef null, ptr noundef nonnull @.str.25, ptr noundef nonnull @__func__.osal_fsync, i32 noundef 1530) #6
  unreachable

.unreachabledefault:                              ; preds = %2
  unreachable

.loopexit.split:                                  ; preds = %11, %.split.split.us14, %.split.split.us, %6, %2, %14
  %.2 = phi i32 [ 0, %14 ], [ %3, %2 ], [ 0, %.split.split.us ], [ %8, %6 ], [ 0, %.split.split.us14 ], [ %13, %11 ]
  ret i32 %.2
}

declare i32 @fdatasync(i32 noundef) local_unnamed_addr #3

declare i32 @fsync(i32 noundef) local_unnamed_addr #3

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 = { 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 #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 = { "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 }
attributes #5 = { nounwind willreturn memory(none) }
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 = !{!"branch_weights", !"expected", i32 2000, i32 1}
!6 = !{!7, !7, i64 0}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!11, !8, i64 25}
!11 = !{!"libmdbx_globals", !8, i64 0, !7, i64 16, !7, i64 20, !8, i64 24, !8, i64 25, !8, i64 26, !12, i64 27, !7, i64 28, !8, i64 32, !8, i64 40, !13, i64 80, !14, i64 88}
!12 = !{!"_Bool", !8, i64 0}
!13 = !{!"long", !8, i64 0}
!14 = !{!"any pointer", !8, i64 0}
!15 = !{!"branch_weights", i32 2000, i32 4002001}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopSimplifyCFGPass,LoopSimplifyPass,LoopUnrollPass,LowerExpectIntrinsicPass,SROAPass,SimplifyCFGPass,TailCallElimPass