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

; 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.walk_ctx = type { ptr, i32, i32, ptr, ptr, ptr }
%struct.walk_tbl = type { %struct.iovec, ptr, ptr }
%struct.iovec = type { ptr, i64 }
%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.tree = type { i16, i16, i32, i32, i32, i32, i32, i64, i64, i64 }

; Function Attrs: cold nounwind optsize uwtable
define dso_local i32 @walk_pages(ptr noundef %0, ptr noundef %1, ptr noundef %2, i32 noundef %3) #0 {
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  %12 = alloca %struct.walk_ctx, align 8
  %13 = alloca %struct.walk_tbl, align 8
  store ptr %0, ptr %6, align 8, !tbaa !5
  store ptr %1, ptr %7, align 8, !tbaa !5
  store ptr %2, ptr %8, align 8, !tbaa !5
  store i32 %3, ptr %9, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #4
  %14 = load ptr, ptr %6, align 8, !tbaa !5
  %15 = call i32 @check_txn(ptr noundef %14, i32 noundef 51)
  store i32 %15, ptr %10, align 4, !tbaa !9
  %16 = load i32, ptr %10, align 4, !tbaa !9
  %17 = icmp ne i32 %16, 0
  %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 0)
  %23 = icmp ne i64 %22, 0
  br i1 %23, label %24, label %26

24:                                               ; preds = %4
  %25 = load i32, ptr %10, align 4, !tbaa !9
  store i32 %25, ptr %5, align 4
  store i32 1, ptr %11, align 4
  br label %63

26:                                               ; preds = %4
  call void @llvm.lifetime.start.p0(i64 40, ptr %12) #4
  %27 = getelementptr inbounds %struct.walk_ctx, ptr %12, i32 0, i32 0
  %28 = load ptr, ptr %8, align 8, !tbaa !5
  store ptr %28, ptr %27, align 8, !tbaa !11
  %29 = getelementptr inbounds %struct.walk_ctx, ptr %12, i32 0, i32 1
  %30 = load i32, ptr %9, align 4, !tbaa !9
  store i32 %30, ptr %29, align 8, !tbaa !13
  %31 = getelementptr inbounds %struct.walk_ctx, ptr %12, i32 0, i32 2
  store i32 0, ptr %31, align 4, !tbaa !14
  %32 = getelementptr inbounds %struct.walk_ctx, ptr %12, i32 0, i32 3
  %33 = load ptr, ptr %7, align 8, !tbaa !5
  store ptr %33, ptr %32, align 8, !tbaa !15
  %34 = getelementptr inbounds %struct.walk_ctx, ptr %12, i32 0, i32 4
  %35 = load ptr, ptr %6, align 8, !tbaa !5
  store ptr %35, ptr %34, align 8, !tbaa !16
  %36 = getelementptr inbounds %struct.walk_ctx, ptr %12, i32 0, i32 5
  store ptr null, ptr %36, align 8, !tbaa !17
  call void @llvm.lifetime.start.p0(i64 32, ptr %13) #4
  %37 = getelementptr inbounds %struct.walk_tbl, ptr %13, i32 0, i32 0
  %38 = getelementptr inbounds %struct.iovec, ptr %37, i32 0, i32 0
  store ptr inttoptr (i64 -1 to ptr), ptr %38, align 8, !tbaa !18
  %39 = getelementptr inbounds %struct.iovec, ptr %37, i32 0, i32 1
  store i64 0, ptr %39, align 8, !tbaa !21
  %40 = getelementptr inbounds %struct.walk_tbl, ptr %13, i32 0, i32 1
  %41 = load ptr, ptr %6, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.MDBX_txn, ptr %41, i32 0, i32 10
  %43 = load ptr, ptr %42, align 8, !tbaa !22
  %44 = getelementptr inbounds %struct.tree, ptr %43, i64 0
  store ptr %44, ptr %40, align 8, !tbaa !27
  %45 = getelementptr inbounds %struct.walk_tbl, ptr %13, i32 0, i32 2
  store ptr null, ptr %45, align 8, !tbaa !29
  %46 = call i32 @walk_tbl(ptr noundef %12, ptr noundef %13) #5
  store i32 %46, ptr %10, align 4, !tbaa !9
  %47 = load i32, ptr %10, align 4, !tbaa !9
  %48 = icmp ne i32 %47, -1
  br i1 %48, label %49, label %52

49:                                               ; preds = %26
  %50 = load i32, ptr %10, align 4, !tbaa !9
  %51 = icmp ne i32 %50, 0
  br i1 %51, label %61, label %52

52:                                               ; preds = %49, %26
  %53 = getelementptr inbounds %struct.walk_tbl, ptr %13, i32 0, i32 0
  %54 = getelementptr inbounds %struct.iovec, ptr %53, i32 0, i32 0
  store ptr null, ptr %54, align 8, !tbaa !30
  %55 = load ptr, ptr %6, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct.MDBX_txn, ptr %55, i32 0, i32 10
  %57 = load ptr, ptr %56, align 8, !tbaa !22
  %58 = getelementptr inbounds %struct.tree, ptr %57, i64 1
  %59 = getelementptr inbounds %struct.walk_tbl, ptr %13, i32 0, i32 1
  store ptr %58, ptr %59, align 8, !tbaa !27
  %60 = call i32 @walk_tbl(ptr noundef %12, ptr noundef %13) #5
  store i32 %60, ptr %10, align 4, !tbaa !9
  br label %61

61:                                               ; preds = %52, %49
  %62 = load i32, ptr %10, align 4, !tbaa !9
  store i32 %62, ptr %5, align 4
  store i32 1, ptr %11, align 4
  call void @llvm.lifetime.end.p0(i64 32, ptr %13) #4
  call void @llvm.lifetime.end.p0(i64 40, ptr %12) #4
  br label %63

63:                                               ; preds = %61, %24
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #4
  %64 = load i32, ptr %5, align 4
  ret i32 %64
}

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

; Function Attrs: alwaysinline nounwind uwtable
declare hidden i32 @check_txn(ptr noundef, i32 noundef) #2

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

; Function Attrs: cold nounwind optsize uwtable
declare hidden i32 @walk_tbl(ptr noundef, ptr noundef) #0

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

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

!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 0}
!12 = !{!"walk_ctx", !6, i64 0, !10, i64 8, !10, i64 12, !6, i64 16, !6, i64 24, !6, i64 32}
!13 = !{!12, !10, i64 8}
!14 = !{!12, !10, i64 12}
!15 = !{!12, !6, i64 16}
!16 = !{!12, !6, i64 24}
!17 = !{!12, !6, i64 32}
!18 = !{!19, !6, i64 0}
!19 = !{!"iovec", !6, i64 0, !20, i64 8}
!20 = !{!"long", !7, i64 0}
!21 = !{!19, !20, i64 8}
!22 = !{!23, !6, i64 88}
!23 = !{!"MDBX_txn", !10, i64 0, !10, i64 4, !20, i64 8, !20, i64 16, !6, i64 24, !6, i64 32, !24, i64 40, !20, i64 64, !20, i64 72, !6, i64 80, !6, i64 88, !6, i64 96, !6, i64 104, !6, i64 112, !6, i64 120, !26, i64 128, !6, i64 160, !7, i64 168}
!24 = !{!"geo", !25, i64 0, !25, i64 2, !10, i64 4, !10, i64 8, !7, i64 12, !7, i64 16}
!25 = !{!"short", !7, i64 0}
!26 = !{!"MDBX_canary", !20, i64 0, !20, i64 8, !20, i64 16, !20, i64 24}
!27 = !{!28, !6, i64 16}
!28 = !{!"walk_tbl", !19, i64 0, !6, i64 16, !6, i64 24}
!29 = !{!28, !6, i64 24}
!30 = !{!28, !6, 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"

%struct.walk_ctx = type { ptr, i32, i32, ptr, ptr, ptr }
%struct.walk_tbl = type { %struct.iovec, ptr, ptr }
%struct.iovec = type { ptr, i64 }

; Function Attrs: cold nounwind optsize uwtable
define dso_local i32 @walk_pages(ptr noundef %0, ptr noundef %1, ptr noundef %2, i32 noundef %3) local_unnamed_addr #0 {
  %5 = alloca %struct.walk_ctx, align 8
  %6 = alloca %struct.walk_tbl, align 8
  %7 = tail call i32 @check_txn(ptr noundef %0, i32 noundef 51)
  %.not = icmp eq i32 %7, 0
  br i1 %.not, label %8, label %26, !prof !5

8:                                                ; preds = %4
  call void @llvm.lifetime.start.p0(i64 40, ptr nonnull %5) #3
  store ptr %2, ptr %5, align 8, !tbaa !6
  %9 = getelementptr inbounds i8, ptr %5, i64 8
  store i32 %3, ptr %9, align 8, !tbaa !12
  %10 = getelementptr inbounds i8, ptr %5, i64 12
  store i32 0, ptr %10, align 4, !tbaa !13
  %11 = getelementptr inbounds i8, ptr %5, i64 16
  store ptr %1, ptr %11, align 8, !tbaa !14
  %12 = getelementptr inbounds i8, ptr %5, i64 24
  store ptr %0, ptr %12, align 8, !tbaa !15
  %13 = getelementptr inbounds i8, ptr %5, i64 32
  store ptr null, ptr %13, align 8, !tbaa !16
  call void @llvm.lifetime.start.p0(i64 32, ptr nonnull %6) #3
  store ptr inttoptr (i64 -1 to ptr), ptr %6, align 8, !tbaa !17
  %14 = getelementptr inbounds i8, ptr %6, i64 8
  store i64 0, ptr %14, align 8, !tbaa !20
  %15 = getelementptr inbounds i8, ptr %6, i64 16
  %16 = getelementptr inbounds i8, ptr %0, i64 88
  %17 = load ptr, ptr %16, align 8, !tbaa !21
  store ptr %17, ptr %15, align 8, !tbaa !26
  %18 = getelementptr inbounds i8, ptr %6, i64 24
  store ptr null, ptr %18, align 8, !tbaa !28
  %19 = call i32 @walk_tbl(ptr noundef nonnull %5, ptr noundef nonnull %6) #4
  %20 = add i32 %19, -1
  %or.cond = icmp ult i32 %20, -2
  br i1 %or.cond, label %25, label %21

21:                                               ; preds = %8
  store ptr null, ptr %6, align 8, !tbaa !29
  %22 = load ptr, ptr %16, align 8, !tbaa !21
  %23 = getelementptr inbounds i8, ptr %22, i64 48
  store ptr %23, ptr %15, align 8, !tbaa !26
  %24 = call i32 @walk_tbl(ptr noundef nonnull %5, ptr noundef nonnull %6) #4
  br label %25

25:                                               ; preds = %8, %21
  %.0 = phi i32 [ %19, %8 ], [ %24, %21 ]
  call void @llvm.lifetime.end.p0(i64 32, ptr nonnull %6) #3
  call void @llvm.lifetime.end.p0(i64 40, ptr nonnull %5) #3
  br label %26

26:                                               ; preds = %4, %25
  %.013 = phi i32 [ %.0, %25 ], [ %7, %4 ]
  ret i32 %.013
}

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

; Function Attrs: alwaysinline nounwind uwtable
declare hidden i32 @check_txn(ptr noundef, i32 noundef) local_unnamed_addr #2

; Function Attrs: cold nounwind optsize uwtable
declare hidden i32 @walk_tbl(ptr noundef, ptr noundef) local_unnamed_addr #0

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

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

!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, !8, i64 0}
!7 = !{!"walk_ctx", !8, i64 0, !11, i64 8, !11, i64 12, !8, i64 16, !8, i64 24, !8, i64 32}
!8 = !{!"any pointer", !9, i64 0}
!9 = !{!"omnipotent char", !10, i64 0}
!10 = !{!"Simple C/C++ TBAA"}
!11 = !{!"int", !9, i64 0}
!12 = !{!7, !11, i64 8}
!13 = !{!7, !11, i64 12}
!14 = !{!7, !8, i64 16}
!15 = !{!7, !8, i64 24}
!16 = !{!7, !8, i64 32}
!17 = !{!18, !8, i64 0}
!18 = !{!"iovec", !8, i64 0, !19, i64 8}
!19 = !{!"long", !9, i64 0}
!20 = !{!18, !19, i64 8}
!21 = !{!22, !8, i64 88}
!22 = !{!"MDBX_txn", !11, i64 0, !11, i64 4, !19, i64 8, !19, i64 16, !8, i64 24, !8, i64 32, !23, i64 40, !19, i64 64, !19, i64 72, !8, i64 80, !8, i64 88, !8, i64 96, !8, i64 104, !8, i64 112, !8, i64 120, !25, i64 128, !8, i64 160, !9, i64 168}
!23 = !{!"geo", !24, i64 0, !24, i64 2, !11, i64 4, !11, i64 8, !9, i64 12, !9, i64 16}
!24 = !{!"short", !9, i64 0}
!25 = !{!"MDBX_canary", !19, i64 0, !19, i64 8, !19, i64 16, !19, i64 24}
!26 = !{!27, !8, i64 16}
!27 = !{!"walk_tbl", !18, i64 0, !8, i64 16, !8, i64 24}
!28 = !{!27, !8, i64 24}
!29 = !{!27, !8, i64 0}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,LowerExpectIntrinsicPass,SROAPass,SimplifyCFGPass,TailCallElimPass