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

@__func__.mdbx_cursor_put = external hidden unnamed_addr constant [16 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: nounwind uwtable
define dso_local i32 @mdbx_cursor_put(ptr noundef %0, ptr noundef %1, ptr noundef %2, i32 noundef %3) #3 {
  %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
  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
  %12 = load ptr, ptr %7, align 8, !tbaa !5
  %13 = icmp eq ptr %12, null
  br i1 %13, label %17, label %14

14:                                               ; preds = %4
  %15 = load ptr, ptr %8, align 8, !tbaa !5
  %16 = icmp eq ptr %15, null
  br label %17

17:                                               ; preds = %14, %4
  %18 = phi i1 [ true, %4 ], [ %16, %14 ]
  %19 = xor i1 %18, true
  %20 = xor i1 %19, true
  %21 = zext i1 %20 to i32
  %22 = sext i32 %21 to i64
  %23 = call i64 @llvm.expect.i64(i64 %22, i64 0)
  %24 = icmp ne i64 %23, 0
  br i1 %24, label %25, label %27

25:                                               ; preds = %17
  %26 = call i32 @log_if_error(i32 noundef 22, ptr noundef @__func__.mdbx_cursor_put, i32 noundef 716)
  store i32 %26, ptr %5, align 4
  br label %101

27:                                               ; preds = %17
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #5
  %28 = load ptr, ptr %6, align 8, !tbaa !5
  %29 = call i32 @cursor_check_rw(ptr noundef %28)
  store i32 %29, ptr %10, align 4, !tbaa !9
  %30 = load i32, ptr %10, align 4, !tbaa !9
  %31 = icmp ne i32 %30, 0
  %32 = xor i1 %31, true
  %33 = xor i1 %32, true
  %34 = zext i1 %33 to i32
  %35 = sext i32 %34 to i64
  %36 = call i64 @llvm.expect.i64(i64 %35, i64 0)
  %37 = icmp ne i64 %36, 0
  br i1 %37, label %38, label %41

38:                                               ; preds = %27
  %39 = load i32, ptr %10, align 4, !tbaa !9
  %40 = call i32 @log_if_error(i32 noundef %39, ptr noundef @__func__.mdbx_cursor_put, i32 noundef 720)
  store i32 %40, ptr %5, align 4
  store i32 1, ptr %11, align 4
  br label %100

41:                                               ; preds = %27
  %42 = load i32, ptr %9, align 4, !tbaa !9
  %43 = and i32 %42, 524288
  %44 = icmp ne i32 %43, 0
  %45 = xor i1 %44, true
  %46 = xor i1 %45, true
  %47 = zext i1 %46 to i32
  %48 = sext i32 %47 to i64
  %49 = call i64 @llvm.expect.i64(i64 %48, i64 0)
  %50 = icmp ne i64 %49, 0
  br i1 %50, label %51, label %69

51:                                               ; preds = %41
  %52 = load ptr, ptr %6, align 8, !tbaa !5
  %53 = load ptr, ptr %7, align 8, !tbaa !5
  %54 = load ptr, ptr %8, align 8, !tbaa !5
  %55 = load i32, ptr %9, align 4, !tbaa !9
  %56 = call i32 @cursor_check_multiple(ptr noundef %52, ptr noundef %53, ptr noundef %54, i32 noundef %55)
  store i32 %56, ptr %10, align 4, !tbaa !9
  %57 = load i32, ptr %10, align 4, !tbaa !9
  %58 = icmp ne i32 %57, 0
  %59 = xor i1 %58, true
  %60 = xor i1 %59, true
  %61 = zext i1 %60 to i32
  %62 = sext i32 %61 to i64
  %63 = call i64 @llvm.expect.i64(i64 %62, i64 0)
  %64 = icmp ne i64 %63, 0
  br i1 %64, label %65, label %68

65:                                               ; preds = %51
  %66 = load i32, ptr %10, align 4, !tbaa !9
  %67 = call i32 @log_if_error(i32 noundef %66, ptr noundef @__func__.mdbx_cursor_put, i32 noundef 725)
  store i32 %67, ptr %5, align 4
  store i32 1, ptr %11, align 4
  br label %100

68:                                               ; preds = %51
  br label %69

69:                                               ; preds = %68, %41
  %70 = load i32, ptr %9, align 4, !tbaa !9
  %71 = and i32 %70, 65536
  %72 = icmp ne i32 %71, 0
  br i1 %72, label %73, label %93

73:                                               ; preds = %69
  %74 = load ptr, ptr %6, align 8, !tbaa !5
  %75 = getelementptr inbounds %struct.MDBX_cursor, ptr %74, i32 0, i32 6
  %76 = load ptr, ptr %75, align 8, !tbaa !11
  %77 = getelementptr inbounds %struct.tree, ptr %76, i32 0, i32 0
  %78 = load i16, ptr %77, align 4, !tbaa !13
  %79 = zext i16 %78 to i32
  %80 = and i32 %79, 116
  %81 = icmp ne i32 %80, 0
  %82 = xor i1 %81, true
  %83 = xor i1 %82, true
  %84 = zext i1 %83 to i32
  %85 = sext i32 %84 to i64
  %86 = call i64 @llvm.expect.i64(i64 %85, i64 0)
  %87 = icmp ne i64 %86, 0
  br i1 %87, label %88, label %90

88:                                               ; preds = %73
  %89 = call i32 @log_if_error(i32 noundef -30784, ptr noundef @__func__.mdbx_cursor_put, i32 noundef 730)
  store i32 %89, ptr %5, align 4
  store i32 1, ptr %11, align 4
  br label %100

90:                                               ; preds = %73
  %91 = load ptr, ptr %8, align 8, !tbaa !5
  %92 = getelementptr inbounds %struct.iovec, ptr %91, i32 0, i32 0
  store ptr null, ptr %92, align 8, !tbaa !17
  br label %93

93:                                               ; preds = %90, %69
  %94 = load ptr, ptr %6, align 8, !tbaa !5
  %95 = load ptr, ptr %7, align 8, !tbaa !5
  %96 = load ptr, ptr %8, align 8, !tbaa !5
  %97 = load i32, ptr %9, align 4, !tbaa !9
  %98 = call i32 @cursor_put_checklen(ptr noundef %94, ptr noundef %95, ptr noundef %96, i32 noundef %97)
  %99 = call i32 @log_if_error(i32 noundef %98, ptr noundef @__func__.mdbx_cursor_put, i32 noundef 734)
  store i32 %99, ptr %5, align 4
  store i32 1, ptr %11, align 4
  br label %100

100:                                              ; preds = %93, %88, %65, %38
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #5
  br label %101

101:                                              ; preds = %100, %25
  %102 = load i32, ptr %5, align 4
  ret i32 %102
}

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

declare i32 @cursor_check_multiple(ptr noundef, ptr noundef, ptr noundef, i32 noundef) #4

declare i32 @cursor_put_checklen(ptr noundef, ptr noundef, ptr noundef, i32 noundef) #4

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

!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 24}
!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, !15, i64 0}
!14 = !{!"tree", !15, i64 0, !15, i64 2, !10, i64 4, !10, i64 8, !10, i64 12, !10, i64 16, !10, i64 20, !16, i64 24, !16, i64 32, !16, i64 40}
!15 = !{!"short", !7, i64 0}
!16 = !{!"long", !7, i64 0}
!17 = !{!18, !6, i64 0}
!18 = !{!"iovec", !6, i64 0, !16, i64 8}


; 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_put = external hidden unnamed_addr constant [16 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: nounwind uwtable
define dso_local i32 @mdbx_cursor_put(ptr noundef %0, ptr noundef %1, ptr noundef %2, i32 noundef %3) local_unnamed_addr #1 {
  %5 = icmp eq ptr %1, null
  %6 = icmp eq ptr %2, null
  %7 = or i1 %5, %6
  br i1 %7, label %8, label %10, !prof !5

8:                                                ; preds = %4
  %9 = tail call i32 @log_if_error(i32 noundef 22, ptr noundef nonnull @__func__.mdbx_cursor_put, i32 noundef 716)
  br label %33

10:                                               ; preds = %4
  %11 = tail call i32 @cursor_check_rw(ptr noundef %0)
  %.not = icmp eq i32 %11, 0
  br i1 %.not, label %14, label %12, !prof !6

12:                                               ; preds = %10
  %13 = tail call i32 @log_if_error(i32 noundef %11, ptr noundef nonnull @__func__.mdbx_cursor_put, i32 noundef 720)
  br label %33

14:                                               ; preds = %10
  %15 = and i32 %3, 524288
  %.not23 = icmp eq i32 %15, 0
  br i1 %.not23, label %20, label %16, !prof !6

16:                                               ; preds = %14
  %17 = tail call i32 @cursor_check_multiple(ptr noundef %0, ptr noundef nonnull %1, ptr noundef nonnull %2, i32 noundef %3) #3
  %.not24 = icmp eq i32 %17, 0
  br i1 %.not24, label %20, label %18, !prof !6

18:                                               ; preds = %16
  %19 = tail call i32 @log_if_error(i32 noundef %17, ptr noundef nonnull @__func__.mdbx_cursor_put, i32 noundef 725)
  br label %33

20:                                               ; preds = %16, %14
  %21 = and i32 %3, 65536
  %.not25 = icmp eq i32 %21, 0
  br i1 %.not25, label %30, label %22

22:                                               ; preds = %20
  %23 = getelementptr inbounds i8, ptr %0, i64 24
  %24 = load ptr, ptr %23, align 8, !tbaa !7
  %25 = load i16, ptr %24, align 4, !tbaa !13
  %26 = and i16 %25, 116
  %.not26 = icmp eq i16 %26, 0
  br i1 %.not26, label %29, label %27, !prof !6

27:                                               ; preds = %22
  %28 = tail call i32 @log_if_error(i32 noundef -30784, ptr noundef nonnull @__func__.mdbx_cursor_put, i32 noundef 730)
  br label %33

29:                                               ; preds = %22
  store ptr null, ptr %2, align 8, !tbaa !17
  br label %30

30:                                               ; preds = %29, %20
  %31 = tail call i32 @cursor_put_checklen(ptr noundef %0, ptr noundef nonnull %1, ptr noundef nonnull %2, i32 noundef %3) #3
  %32 = tail call i32 @log_if_error(i32 noundef %31, ptr noundef nonnull @__func__.mdbx_cursor_put, i32 noundef 734)
  br label %33

33:                                               ; preds = %12, %18, %27, %30, %8
  %.1 = phi i32 [ %9, %8 ], [ %13, %12 ], [ %19, %18 ], [ %28, %27 ], [ %32, %30 ]
  ret i32 %.1
}

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

declare i32 @cursor_check_multiple(ptr noundef, ptr noundef, ptr noundef, i32 noundef) local_unnamed_addr #2

declare i32 @cursor_put_checklen(ptr noundef, ptr noundef, ptr noundef, i32 noundef) local_unnamed_addr #2

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

!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 1, i32 2000}
!6 = !{!"branch_weights", !"expected", i32 2000, i32 1}
!7 = !{!8, !12, i64 24}
!8 = !{!"MDBX_cursor", !9, i64 0, !10, i64 4, !10, i64 6, !10, i64 7, !12, i64 8, !12, i64 16, !12, i64 24, !12, i64 32, !12, i64 40, !10, i64 48, !10, i64 304, !12, i64 368, !12, i64 376}
!9 = !{!"int", !10, i64 0}
!10 = !{!"omnipotent char", !11, i64 0}
!11 = !{!"Simple C/C++ TBAA"}
!12 = !{!"any pointer", !10, i64 0}
!13 = !{!14, !15, i64 0}
!14 = !{!"tree", !15, i64 0, !15, i64 2, !9, i64 4, !9, i64 8, !9, i64 12, !9, i64 16, !9, i64 20, !16, i64 24, !16, i64 32, !16, i64 40}
!15 = !{!"short", !10, i64 0}
!16 = !{!"long", !10, i64 0}
!17 = !{!18, !12, i64 0}
!18 = !{!"iovec", !12, i64 0, !16, i64 8}


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