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

; 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.MDBX_txn = type { i32, i32, i64, i64, ptr, ptr, %struct.geo, i64, i64, ptr, ptr, ptr, ptr, ptr, ptr, %struct.MDBX_canary, ptr, %union.anon.1 }
%struct.geo = type { i16, i16, i32, i32, %union.anon, %union.anon.0 }
%union.anon = type { i32 }
%union.anon.0 = type { i32 }
%struct.MDBX_canary = type { i64, i64, i64, i64 }
%union.anon.1 = type { %struct.anon.2 }
%struct.anon.2 = type { %struct.troika, ptr, %struct.anon.3, i8, i32, i32, i64, ptr, ptr, ptr, i64, %union.anon.4 }
%struct.troika = type { i8, i8, i8, i8, i32, [3 x i64] }
%struct.anon.3 = type { %struct.MDBX_rkl, i64, %struct.MDBX_rkl }
%struct.MDBX_rkl = type { i64, i64, i32, i32, ptr, [12 x i64] }
%union.anon.4 = type { %struct.anon.5 }
%struct.anon.5 = type { i64, ptr }
%struct.dpl = type { i64, i64, i64, i64, [18 x %struct.dp] }
%struct.dp = type { ptr, i32, i32 }

@globals = external global %struct.libmdbx_globals, align 8
@__func__.dpl_lru_reduce = external hidden unnamed_addr constant [15 x i8], align 1
@.str.30 = external hidden unnamed_addr constant [21 x i8], align 1
@.str.31 = external hidden unnamed_addr constant [54 x i8], align 1

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

; Function Attrs: noreturn
declare void @mdbx_assert_fail(ptr noundef, ptr noundef, ptr noundef, i32 noundef) #1

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

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

declare void @debug_log(i32 noundef, ptr noundef, i32 noundef, ptr noundef, ...) #3

; Function Attrs: noinline nounwind uwtable
define dso_local void @dpl_lru_reduce(ptr noundef %0) #4 {
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca i64, align 8
  %5 = alloca ptr, align 8
  store ptr %0, ptr %2, align 8, !tbaa !5
  br label %6

6:                                                ; preds = %1
  %7 = load i8, ptr getelementptr inbounds (%struct.libmdbx_globals, ptr @globals, i32 0, i32 5), align 2, !tbaa !9
  %8 = zext i8 %7 to i32
  %9 = icmp sle i32 4, %8
  %10 = xor i1 %9, true
  %11 = xor i1 %10, true
  %12 = zext i1 %11 to i32
  %13 = sext i32 %12 to i64
  %14 = call i64 @llvm.expect.i64(i64 %13, i64 0)
  %15 = icmp ne i64 %14, 0
  br i1 %15, label %16, label %26

16:                                               ; preds = %6
  %17 = load ptr, ptr %2, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.MDBX_txn, ptr %17, i32 0, i32 17
  %19 = getelementptr inbounds %struct.anon.2, ptr %18, i32 0, i32 5
  %20 = load i32, ptr %19, align 8, !tbaa !14
  %21 = load ptr, ptr %2, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.MDBX_txn, ptr %21, i32 0, i32 17
  %23 = getelementptr inbounds %struct.anon.2, ptr %22, i32 0, i32 5
  %24 = load i32, ptr %23, align 8, !tbaa !14
  %25 = lshr i32 %24, 1
  call void (i32, ptr, i32, ptr, ...) @debug_log(i32 noundef 4, ptr noundef @__func__.dpl_lru_reduce, i32 noundef 400, ptr noundef @.str.30, i32 noundef %20, i32 noundef %25)
  br label %26

26:                                               ; preds = %16, %6
  br label %27

27:                                               ; preds = %26
  br label %28

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

39:                                               ; preds = %28
  br label %40

40:                                               ; preds = %39
  %41 = load ptr, ptr %2, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.MDBX_txn, ptr %41, i32 0, i32 1
  %43 = load i32, ptr %42, align 4, !tbaa !16
  %44 = and i32 %43, 655360
  %45 = icmp eq i32 %44, 0
  %46 = xor i1 %45, true
  %47 = xor i1 %46, true
  %48 = xor i1 %47, true
  %49 = zext i1 %48 to i32
  %50 = sext i32 %49 to i64
  %51 = call i64 @llvm.expect.i64(i64 %50, i64 0)
  %52 = icmp ne i64 %51, 0
  br i1 %52, label %53, label %57

53:                                               ; preds = %40
  %54 = load ptr, ptr %2, align 8, !tbaa !5
  %55 = getelementptr inbounds %struct.MDBX_txn, ptr %54, i32 0, i32 9
  %56 = load ptr, ptr %55, align 8, !tbaa !21
  call void @mdbx_assert_fail(ptr noundef %56, ptr noundef @.str.31, ptr noundef @__func__.dpl_lru_reduce, i32 noundef 401) #5
  unreachable

57:                                               ; preds = %40
  br label %58

58:                                               ; preds = %57
  br label %59

59:                                               ; preds = %58, %28
  br label %60

60:                                               ; preds = %59
  br label %61

61:                                               ; preds = %98, %60
  %62 = load ptr, ptr %2, align 8, !tbaa !5
  %63 = getelementptr inbounds %struct.MDBX_txn, ptr %62, i32 0, i32 17
  %64 = getelementptr inbounds %struct.anon.2, ptr %63, i32 0, i32 5
  %65 = load i32, ptr %64, align 8, !tbaa !14
  %66 = lshr i32 %65, 1
  store i32 %66, ptr %64, align 8, !tbaa !14
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #6
  %67 = load ptr, ptr %2, align 8, !tbaa !5
  %68 = getelementptr inbounds %struct.MDBX_txn, ptr %67, i32 0, i32 17
  %69 = getelementptr inbounds %struct.anon.2, ptr %68, i32 0, i32 7
  %70 = load ptr, ptr %69, align 8, !tbaa !14
  store ptr %70, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #6
  store i64 1, ptr %4, align 8, !tbaa !22
  br label %71

71:                                               ; preds = %91, %61
  %72 = load i64, ptr %4, align 8, !tbaa !22
  %73 = load ptr, ptr %3, align 8, !tbaa !5
  %74 = getelementptr inbounds %struct.dpl, ptr %73, i32 0, i32 1
  %75 = load i64, ptr %74, align 8, !tbaa !23
  %76 = icmp ule i64 %72, %75
  br i1 %76, label %78, label %77

77:                                               ; preds = %71
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #6
  br label %94

78:                                               ; preds = %71
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #6
  %79 = load ptr, ptr %3, align 8, !tbaa !5
  %80 = getelementptr inbounds %struct.dpl, ptr %79, i32 0, i32 4
  %81 = load i64, ptr %4, align 8, !tbaa !22
  %82 = getelementptr inbounds [18 x %struct.dp], ptr %80, i64 0, i64 %81
  %83 = getelementptr inbounds %struct.dp, ptr %82, i32 0, i32 0
  %84 = load ptr, ptr %83, align 8, !tbaa !25
  %85 = ptrtoint ptr %84 to i64
  %86 = add nsw i64 %85, -8
  %87 = inttoptr i64 %86 to ptr
  store ptr %87, ptr %5, align 8, !tbaa !5
  %88 = load ptr, ptr %5, align 8, !tbaa !5
  %89 = load i64, ptr %88, align 8, !tbaa !22
  %90 = lshr i64 %89, 1
  store i64 %90, ptr %88, align 8, !tbaa !22
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #6
  br label %91

91:                                               ; preds = %78
  %92 = load i64, ptr %4, align 8, !tbaa !22
  %93 = add i64 %92, 1
  store i64 %93, ptr %4, align 8, !tbaa !22
  br label %71, !llvm.loop !27

94:                                               ; preds = %77
  %95 = load ptr, ptr %2, align 8, !tbaa !5
  %96 = getelementptr inbounds %struct.MDBX_txn, ptr %95, i32 0, i32 4
  %97 = load ptr, ptr %96, align 8, !tbaa !29
  store ptr %97, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #6
  br label %98

98:                                               ; preds = %94
  %99 = load ptr, ptr %2, align 8, !tbaa !5
  %100 = icmp ne ptr %99, null
  br i1 %100, label %61, label %101, !llvm.loop !30

101:                                              ; preds = %98
  ret void
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(none) }
attributes #1 = { noreturn "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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { noinline 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 = { noreturn }
attributes #6 = { 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, !6, i64 0}
!6 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !7, i64 26}
!10 = !{!"libmdbx_globals", !7, i64 0, !11, i64 16, !11, i64 20, !7, i64 24, !7, i64 25, !7, i64 26, !12, i64 27, !11, i64 28, !7, i64 32, !7, i64 40, !13, i64 80, !6, i64 88}
!11 = !{!"int", !7, i64 0}
!12 = !{!"_Bool", !7, i64 0}
!13 = !{!"long", !7, i64 0}
!14 = !{!7, !7, i64 0}
!15 = !{!10, !7, i64 25}
!16 = !{!17, !11, i64 4}
!17 = !{!"MDBX_txn", !11, i64 0, !11, i64 4, !13, i64 8, !13, i64 16, !6, i64 24, !6, i64 32, !18, i64 40, !13, i64 64, !13, i64 72, !6, i64 80, !6, i64 88, !6, i64 96, !6, i64 104, !6, i64 112, !6, i64 120, !20, i64 128, !6, i64 160, !7, i64 168}
!18 = !{!"geo", !19, i64 0, !19, i64 2, !11, i64 4, !11, i64 8, !7, i64 12, !7, i64 16}
!19 = !{!"short", !7, i64 0}
!20 = !{!"MDBX_canary", !13, i64 0, !13, i64 8, !13, i64 16, !13, i64 24}
!21 = !{!17, !6, i64 80}
!22 = !{!13, !13, i64 0}
!23 = !{!24, !13, i64 8}
!24 = !{!"dpl", !13, i64 0, !13, i64 8, !13, i64 16, !13, i64 24, !7, i64 32}
!25 = !{!26, !6, i64 0}
!26 = !{!"dp", !6, i64 0, !11, i64 8, !11, i64 12}
!27 = distinct !{!27, !28}
!28 = !{!"llvm.loop.mustprogress"}
!29 = !{!17, !6, i64 24}
!30 = distinct !{!30, !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 }
%struct.dp = type { ptr, i32, i32 }

@globals = external local_unnamed_addr global %struct.libmdbx_globals, align 8
@__func__.dpl_lru_reduce = external hidden unnamed_addr constant [15 x i8], align 1
@.str.30 = external hidden unnamed_addr constant [21 x i8], align 1
@.str.31 = external hidden unnamed_addr constant [54 x i8], align 1

; Function Attrs: noreturn
declare void @mdbx_assert_fail(ptr noundef, ptr noundef, ptr noundef, i32 noundef) local_unnamed_addr #0

declare void @debug_log(i32 noundef, ptr noundef, i32 noundef, ptr noundef, ...) local_unnamed_addr #1

; Function Attrs: noinline nounwind uwtable
define dso_local void @dpl_lru_reduce(ptr nocapture noundef %0) local_unnamed_addr #2 {
  %2 = load i8, ptr getelementptr inbounds (i8, ptr @globals, i64 26), align 2, !tbaa !5
  %3 = icmp ugt i8 %2, 3
  br i1 %3, label %4, label %8, !prof !13

4:                                                ; preds = %1
  %5 = getelementptr inbounds i8, ptr %0, i64 480
  %6 = load i32, ptr %5, align 8, !tbaa !14
  %7 = lshr i32 %6, 1
  tail call void (i32, ptr, i32, ptr, ...) @debug_log(i32 noundef 4, ptr noundef nonnull @__func__.dpl_lru_reduce, i32 noundef 400, ptr noundef nonnull @.str.30, i32 noundef %6, i32 noundef %7) #3
  br label %8

8:                                                ; preds = %4, %1
  %9 = load i8, ptr getelementptr inbounds (i8, ptr @globals, i64 25), align 1, !tbaa !15
  %10 = and i8 %9, 1
  %.not = icmp eq i8 %10, 0
  br i1 %.not, label %.preheader, label %11, !prof !13

11:                                               ; preds = %8
  %12 = getelementptr inbounds i8, ptr %0, i64 4
  %13 = load i32, ptr %12, align 4, !tbaa !16
  %14 = and i32 %13, 655360
  %.not15 = icmp eq i32 %14, 0
  br i1 %.not15, label %.preheader, label %15, !prof !21

.preheader:                                       ; preds = %8, %11
  br label %18

15:                                               ; preds = %11
  %16 = getelementptr inbounds i8, ptr %0, i64 80
  %17 = load ptr, ptr %16, align 8, !tbaa !22
  tail call void @mdbx_assert_fail(ptr noundef %17, ptr noundef nonnull @.str.31, ptr noundef nonnull @__func__.dpl_lru_reduce, i32 noundef 401) #4
  unreachable

18:                                               ; preds = %.preheader, %._crit_edge
  %.0 = phi ptr [ %28, %._crit_edge ], [ %0, %.preheader ]
  %19 = getelementptr inbounds i8, ptr %.0, i64 480
  %20 = load i32, ptr %19, align 8, !tbaa !14
  %21 = lshr i32 %20, 1
  store i32 %21, ptr %19, align 8, !tbaa !14
  %22 = getelementptr inbounds i8, ptr %.0, i64 496
  %23 = load ptr, ptr %22, align 8, !tbaa !14
  %24 = getelementptr inbounds i8, ptr %23, i64 8
  %25 = load i64, ptr %24, align 8, !tbaa !23
  %.not1618 = icmp eq i64 %25, 0
  br i1 %.not1618, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %18
  %26 = getelementptr inbounds i8, ptr %23, i64 32
  br label %29

._crit_edge:                                      ; preds = %29, %18
  %27 = getelementptr inbounds i8, ptr %.0, i64 24
  %28 = load ptr, ptr %27, align 8, !tbaa !25
  %.not17 = icmp eq ptr %28, null
  br i1 %.not17, label %39, label %18, !llvm.loop !26

29:                                               ; preds = %.lr.ph, %29
  %.01319 = phi i64 [ 1, %.lr.ph ], [ %37, %29 ]
  %30 = getelementptr inbounds [18 x %struct.dp], ptr %26, i64 0, i64 %.01319
  %31 = load ptr, ptr %30, align 8, !tbaa !28
  %32 = ptrtoint ptr %31 to i64
  %33 = add nsw i64 %32, -8
  %34 = inttoptr i64 %33 to ptr
  %35 = load i64, ptr %34, align 8, !tbaa !30
  %36 = lshr i64 %35, 1
  store i64 %36, ptr %34, align 8, !tbaa !30
  %37 = add i64 %.01319, 1
  %38 = load i64, ptr %24, align 8, !tbaa !23
  %.not16 = icmp ugt i64 %37, %38
  br i1 %.not16, label %._crit_edge, label %29, !llvm.loop !31

39:                                               ; preds = %._crit_edge
  ret void
}

attributes #0 = { noreturn "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 = { "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 = { noinline 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 = { nounwind }
attributes #4 = { noreturn 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, !7, i64 26}
!6 = !{!"libmdbx_globals", !7, i64 0, !9, i64 16, !9, i64 20, !7, i64 24, !7, i64 25, !7, i64 26, !10, i64 27, !9, i64 28, !7, i64 32, !7, i64 40, !11, i64 80, !12, i64 88}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!"int", !7, i64 0}
!10 = !{!"_Bool", !7, i64 0}
!11 = !{!"long", !7, i64 0}
!12 = !{!"any pointer", !7, i64 0}
!13 = !{!"branch_weights", !"expected", i32 1, i32 2000}
!14 = !{!7, !7, i64 0}
!15 = !{!6, !7, i64 25}
!16 = !{!17, !9, i64 4}
!17 = !{!"MDBX_txn", !9, i64 0, !9, i64 4, !11, i64 8, !11, i64 16, !12, i64 24, !12, i64 32, !18, i64 40, !11, i64 64, !11, i64 72, !12, i64 80, !12, i64 88, !12, i64 96, !12, i64 104, !12, i64 112, !12, i64 120, !20, i64 128, !12, i64 160, !7, i64 168}
!18 = !{!"geo", !19, i64 0, !19, i64 2, !9, i64 4, !9, i64 8, !7, i64 12, !7, i64 16}
!19 = !{!"short", !7, i64 0}
!20 = !{!"MDBX_canary", !11, i64 0, !11, i64 8, !11, i64 16, !11, i64 24}
!21 = !{!"branch_weights", !"expected", i32 2000, i32 1}
!22 = !{!17, !12, i64 80}
!23 = !{!24, !11, i64 8}
!24 = !{!"dpl", !11, i64 0, !11, i64 8, !11, i64 16, !11, i64 24, !7, i64 32}
!25 = !{!17, !12, i64 24}
!26 = distinct !{!26, !27}
!27 = !{!"llvm.loop.mustprogress"}
!28 = !{!29, !12, i64 0}
!29 = !{!"dp", !12, i64 0, !9, i64 8, !9, i64 12}
!30 = !{!11, !11, i64 0}
!31 = distinct !{!31, !27}


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