; 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.anon = type { i8, i8 }

@__func__.mdbx_cursor_on_first = external hidden unnamed_addr constant [21 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: nounwind willreturn memory(read) uwtable
define dso_local i32 @mdbx_cursor_on_first(ptr noundef %0) #3 {
  %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
  store ptr %0, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #4
  %7 = load ptr, ptr %3, align 8, !tbaa !5
  %8 = call i32 @cursor_check_pure(ptr noundef %7)
  store i32 %8, ptr %4, align 4, !tbaa !9
  %9 = load i32, ptr %4, align 4, !tbaa !9
  %10 = icmp ne i32 %9, 0
  %11 = xor i1 %10, true
  %12 = xor i1 %11, true
  %13 = zext i1 %12 to i32
  %14 = sext i32 %13 to i64
  %15 = call i64 @llvm.expect.i64(i64 %14, i64 0)
  %16 = icmp ne i64 %15, 0
  br i1 %16, label %17, label %20

17:                                               ; preds = %1
  %18 = load i32, ptr %4, align 4, !tbaa !9
  %19 = call i32 @log_if_error(i32 noundef %18, ptr noundef @__func__.mdbx_cursor_on_first, i32 noundef 408)
  store i32 %19, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %45

20:                                               ; preds = %1
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #4
  store i64 0, ptr %6, align 8, !tbaa !11
  br label %21

21:                                               ; preds = %39, %20
  %22 = load i64, ptr %6, align 8, !tbaa !11
  %23 = load ptr, ptr %3, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.MDBX_cursor, ptr %23, i32 0, i32 1
  %25 = getelementptr inbounds %struct.anon, ptr %24, i32 0, i32 1
  %26 = load i8, ptr %25, align 1, !tbaa !13
  %27 = sext i8 %26 to i64
  %28 = icmp sle i64 %22, %27
  br i1 %28, label %30, label %29

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

30:                                               ; preds = %21
  %31 = load ptr, ptr %3, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.MDBX_cursor, ptr %31, i32 0, i32 10
  %33 = load i64, ptr %6, align 8, !tbaa !11
  %34 = getelementptr inbounds [32 x i16], ptr %32, i64 0, i64 %33
  %35 = load i16, ptr %34, align 2, !tbaa !14
  %36 = icmp ne i16 %35, 0
  br i1 %36, label %37, label %38

37:                                               ; preds = %30
  store i32 0, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %42

38:                                               ; preds = %30
  br label %39

39:                                               ; preds = %38
  %40 = load i64, ptr %6, align 8, !tbaa !11
  %41 = add nsw i64 %40, 1
  store i64 %41, ptr %6, align 8, !tbaa !11
  br label %21, !llvm.loop !16

42:                                               ; preds = %37, %29
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #4
  %43 = load i32, ptr %5, align 4
  switch i32 %43, label %45 [
    i32 2, label %44
  ]

44:                                               ; preds = %42
  store i32 -1, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %45

45:                                               ; preds = %44, %42, %17
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #4
  %46 = load i32, ptr %2, align 4
  ret i32 %46
}

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 = { 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 }

!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, !12, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = !{!7, !7, i64 0}
!14 = !{!15, !15, i64 0}
!15 = !{!"short", !7, i64 0}
!16 = distinct !{!16, !17}
!17 = !{!"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_first = external hidden unnamed_addr constant [21 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: mustprogress nofree nounwind willreturn memory(read) uwtable
define dso_local i32 @mdbx_cursor_on_first(ptr noundef %0) local_unnamed_addr #1 {
  %2 = tail call i32 @cursor_check_pure(ptr noundef %0)
  %.not = icmp eq i32 %2, 0
  br i1 %.not, label %.preheader, label %8, !prof !5

.preheader:                                       ; preds = %1
  %3 = getelementptr inbounds i8, ptr %0, i64 304
  %4 = getelementptr inbounds i8, ptr %0, i64 5
  %5 = load i8, ptr %4, align 1, !tbaa !6
  %6 = sext i8 %5 to i64
  %.not1214 = icmp slt i8 %5, 0
  br i1 %.not1214, label %._crit_edge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %.preheader
  %7 = load i16, ptr %3, align 2, !tbaa !9
  %.not1319 = icmp eq i16 %7, 0
  br i1 %.not1319, label %.lr.ph21, label %._crit_edge

8:                                                ; preds = %1
  %9 = tail call i32 @log_if_error(i32 noundef %2, ptr noundef nonnull @__func__.mdbx_cursor_on_first, i32 noundef 408)
  br label %._crit_edge

.lr.ph21:                                         ; preds = %.lr.ph.preheader, %.lr.ph
  %.01520 = phi i64 [ %10, %.lr.ph ], [ 0, %.lr.ph.preheader ]
  %exitcond = icmp eq i64 %.01520, %6
  br i1 %exitcond, label %._crit_edge.loopexit.loopexit, label %.lr.ph, !llvm.loop !11

.lr.ph:                                           ; preds = %.lr.ph21
  %10 = add nuw nsw i64 %.01520, 1
  %11 = getelementptr inbounds [32 x i16], ptr %3, i64 0, i64 %10
  %12 = load i16, ptr %11, align 2, !tbaa !9
  %.not13 = icmp eq i16 %12, 0
  br i1 %.not13, label %.lr.ph21, label %._crit_edge.loopexit.loopexit, !llvm.loop !11

._crit_edge.loopexit.loopexit:                    ; preds = %.lr.ph, %.lr.ph21
  %.not12.le = icmp sge i64 %.01520, %6
  %13 = sext i1 %.not12.le to i32
  br label %._crit_edge

._crit_edge:                                      ; preds = %.lr.ph.preheader, %._crit_edge.loopexit.loopexit, %.preheader, %8
  %.1 = phi i32 [ %9, %8 ], [ -1, %.preheader ], [ 0, %.lr.ph.preheader ], [ %13, %._crit_edge.loopexit.loopexit ]
  ret i32 %.1
}

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 = { 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" }

!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 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"short", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}


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