; LLVM IR from erthink/libmdbx :: src (api-cursor)

; 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.MDBX_cursor = type { i32, %union.anon, i8, i8, ptr, ptr, ptr, ptr, ptr, [32 x ptr], [32 x i16], ptr, ptr }
%union.anon = type { i16 }
%struct.inner_cursor = type { %struct.MDBX_cursor, %struct.tree }
%struct.tree = type { i16, i16, i32, i32, i32, i32, i32, i64, i64, i64 }
%struct.anon = type { i8, i8 }

@__func__.mdbx_cursor_on_last_dup = external hidden unnamed_addr constant [24 x i8], align 1

; 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: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #0

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

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @cursor_check_pure(ptr noundef) #2

; Function Attrs: inlinehint nounwind willreturn memory(read) uwtable
declare hidden zeroext i1 @is_filled(ptr noundef) #3

; Function Attrs: inlinehint nounwind willreturn memory(read) uwtable
declare hidden i64 @page_numkeys(ptr noundef) #3

; Function Attrs: nounwind willreturn memory(read) uwtable
define dso_local i32 @mdbx_cursor_on_last_dup(ptr noundef %0) #4 {
  %2 = alloca i32, align 4
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i64, align 8
  %7 = alloca i64, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #5
  %8 = load ptr, ptr %3, align 8, !tbaa !5
  %9 = call i32 @cursor_check_pure(ptr noundef %8)
  store i32 %9, ptr %4, align 4, !tbaa !9
  %10 = load i32, ptr %4, align 4, !tbaa !9
  %11 = icmp ne i32 %10, 0
  %12 = xor i1 %11, true
  %13 = xor i1 %12, true
  %14 = zext i1 %13 to i32
  %15 = sext i32 %14 to i64
  %16 = call i64 @llvm.expect.i64(i64 %15, i64 0)
  %17 = icmp ne i64 %16, 0
  br i1 %17, label %18, label %21

18:                                               ; preds = %1
  %19 = load i32, ptr %4, align 4, !tbaa !9
  %20 = call i32 @log_if_error(i32 noundef %19, ptr noundef @__func__.mdbx_cursor_on_last_dup, i32 noundef 451)
  store i32 %20, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %71

21:                                               ; preds = %1
  %22 = load ptr, ptr %3, align 8, !tbaa !5
  %23 = call zeroext i1 @is_filled(ptr noundef %22) #6
  br i1 %23, label %24, label %70

24:                                               ; preds = %21
  %25 = load ptr, ptr %3, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.MDBX_cursor, ptr %25, i32 0, i32 8
  %27 = load ptr, ptr %26, align 8, !tbaa !11
  %28 = icmp ne ptr %27, null
  br i1 %28, label %29, label %70

29:                                               ; preds = %24
  %30 = load ptr, ptr %3, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.MDBX_cursor, ptr %30, i32 0, i32 8
  %32 = load ptr, ptr %31, align 8, !tbaa !11
  %33 = getelementptr inbounds %struct.inner_cursor, ptr %32, i32 0, i32 0
  store ptr %33, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #5
  store i64 0, ptr %6, align 8, !tbaa !13
  br label %34

34:                                               ; preds = %64, %29
  %35 = load i64, ptr %6, align 8, !tbaa !13
  %36 = load ptr, ptr %3, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.MDBX_cursor, ptr %36, i32 0, i32 1
  %38 = getelementptr inbounds %struct.anon, ptr %37, i32 0, i32 1
  %39 = load i8, ptr %38, align 1, !tbaa !15
  %40 = sext i8 %39 to i64
  %41 = icmp sle i64 %35, %40
  br i1 %41, label %43, label %42

42:                                               ; preds = %34
  store i32 2, ptr %5, align 4
  br label %67

43:                                               ; preds = %34
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #5
  %44 = load ptr, ptr %3, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.MDBX_cursor, ptr %44, i32 0, i32 9
  %46 = load i64, ptr %6, align 8, !tbaa !13
  %47 = getelementptr inbounds [32 x ptr], ptr %45, i64 0, i64 %46
  %48 = load ptr, ptr %47, align 8, !tbaa !5
  %49 = call i64 @page_numkeys(ptr noundef %48) #6
  store i64 %49, ptr %7, align 8, !tbaa !13
  %50 = load ptr, ptr %3, align 8, !tbaa !5
  %51 = getelementptr inbounds %struct.MDBX_cursor, ptr %50, i32 0, i32 10
  %52 = load i64, ptr %6, align 8, !tbaa !13
  %53 = getelementptr inbounds [32 x i16], ptr %51, i64 0, i64 %52
  %54 = load i16, ptr %53, align 2, !tbaa !16
  %55 = zext i16 %54 to i64
  %56 = load i64, ptr %7, align 8, !tbaa !13
  %57 = sub i64 %56, 1
  %58 = icmp ult i64 %55, %57
  br i1 %58, label %59, label %60

59:                                               ; preds = %43
  store i32 0, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %61

60:                                               ; preds = %43
  store i32 0, ptr %5, align 4
  br label %61

61:                                               ; preds = %60, %59
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #5
  %62 = load i32, ptr %5, align 4
  switch i32 %62, label %67 [
    i32 0, label %63
  ]

63:                                               ; preds = %61
  br label %64

64:                                               ; preds = %63
  %65 = load i64, ptr %6, align 8, !tbaa !13
  %66 = add nsw i64 %65, 1
  store i64 %66, ptr %6, align 8, !tbaa !13
  br label %34, !llvm.loop !18

67:                                               ; preds = %61, %42
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #5
  %68 = load i32, ptr %5, align 4
  switch i32 %68, label %71 [
    i32 2, label %69
  ]

69:                                               ; preds = %67
  br label %70

70:                                               ; preds = %69, %24, %21
  store i32 -1, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %71

71:                                               ; preds = %70, %67, %18
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #5
  %72 = load i32, ptr %2, align 4
  ret i32 %72
}

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

!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 40}
!12 = !{!"MDBX_cursor", !10, i64 0, !7, i64 4, !7, i64 6, !7, i64 7, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32, !6, i64 40, !7, i64 48, !7, i64 304, !6, i64 368, !6, i64 376}
!13 = !{!14, !14, i64 0}
!14 = !{!"long", !7, i64 0}
!15 = !{!7, !7, i64 0}
!16 = !{!17, !17, i64 0}
!17 = !{!"short", !7, i64 0}
!18 = distinct !{!18, !19}
!19 = !{!"llvm.loop.mustprogress"}


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

@__func__.mdbx_cursor_on_last_dup = external hidden unnamed_addr constant [24 x i8], align 1

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @log_if_error(i32 noundef, ptr noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @cursor_check_pure(ptr noundef) local_unnamed_addr #0

; Function Attrs: inlinehint mustprogress nofree nounwind willreturn memory(read) uwtable
declare hidden zeroext i1 @is_filled(ptr noundef) local_unnamed_addr #1

; Function Attrs: inlinehint mustprogress nofree nounwind willreturn memory(read) uwtable
declare hidden i64 @page_numkeys(ptr noundef) local_unnamed_addr #1

; Function Attrs: mustprogress nofree nounwind willreturn memory(read) uwtable
define dso_local i32 @mdbx_cursor_on_last_dup(ptr noundef %0) local_unnamed_addr #2 {
  %2 = tail call i32 @cursor_check_pure(ptr noundef %0)
  %.not = icmp eq i32 %2, 0
  br i1 %.not, label %5, label %3, !prof !5

3:                                                ; preds = %1
  %4 = tail call i32 @log_if_error(i32 noundef %2, ptr noundef nonnull @__func__.mdbx_cursor_on_last_dup, i32 noundef 451)
  br label %.critedge

5:                                                ; preds = %1
  %6 = tail call zeroext i1 @is_filled(ptr noundef %0) #3
  br i1 %6, label %7, label %.critedge

7:                                                ; preds = %5
  %8 = getelementptr inbounds i8, ptr %0, i64 40
  %9 = load ptr, ptr %8, align 8, !tbaa !6
  %.not22 = icmp eq ptr %9, null
  br i1 %.not22, label %.critedge, label %.preheader

.preheader:                                       ; preds = %7
  %10 = getelementptr inbounds i8, ptr %9, i64 48
  %11 = getelementptr inbounds i8, ptr %9, i64 5
  %12 = load i8, ptr %11, align 1, !tbaa !12
  %.not2325 = icmp slt i8 %12, 0
  br i1 %.not2325, label %.critedge, label %.lr.ph

.lr.ph:                                           ; preds = %.preheader
  %13 = zext nneg i8 %12 to i64
  %14 = getelementptr inbounds i8, ptr %9, i64 304
  br label %17

15:                                               ; preds = %17
  %16 = add nuw nsw i64 %.01726, 1
  %exitcond.not = icmp eq i64 %.01726, %13
  br i1 %exitcond.not, label %.critedge, label %17, !llvm.loop !13

17:                                               ; preds = %.lr.ph, %15
  %.01726 = phi i64 [ 0, %.lr.ph ], [ %16, %15 ]
  %18 = getelementptr inbounds [32 x ptr], ptr %10, i64 0, i64 %.01726
  %19 = load ptr, ptr %18, align 8, !tbaa !15
  %20 = tail call i64 @page_numkeys(ptr noundef %19) #3
  %21 = getelementptr inbounds [32 x i16], ptr %14, i64 0, i64 %.01726
  %22 = load i16, ptr %21, align 2, !tbaa !16
  %23 = zext i16 %22 to i64
  %24 = add i64 %20, -1
  %.not24 = icmp ugt i64 %24, %23
  br i1 %.not24, label %.critedge, label %15

.critedge:                                        ; preds = %17, %15, %.preheader, %5, %7, %3
  %.3 = phi i32 [ %4, %3 ], [ -1, %7 ], [ -1, %5 ], [ -1, %.preheader ], [ 0, %17 ], [ -1, %15 ]
  ret i32 %.3
}

attributes #0 = { inlinehint 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 #1 = { inlinehint mustprogress nofree nounwind willreturn memory(read) 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 = { mustprogress nofree nounwind willreturn memory(read) 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 willreturn memory(read) }

!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, !11, i64 40}
!7 = !{!"MDBX_cursor", !8, i64 0, !9, i64 4, !9, i64 6, !9, i64 7, !11, i64 8, !11, i64 16, !11, i64 24, !11, i64 32, !11, i64 40, !9, i64 48, !9, i64 304, !11, i64 368, !11, i64 376}
!8 = !{!"int", !9, i64 0}
!9 = !{!"omnipotent char", !10, i64 0}
!10 = !{!"Simple C/C++ TBAA"}
!11 = !{!"any pointer", !9, i64 0}
!12 = !{!9, !9, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = !{!11, !11, i64 0}
!16 = !{!17, !17, i64 0}
!17 = !{!"short", !9, i64 0}


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