; LLVM IR from erthink/libmdbx :: src (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.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.2 }
%struct.geo = type { i16, i16, i32, i32, %union.anon.0, %union.anon.1 }
%union.anon.0 = type { i32 }
%union.anon.1 = type { i32 }
%struct.MDBX_canary = type { i64, i64, i64, i64 }
%union.anon.2 = type { %struct.anon.4 }
%struct.anon.4 = type { %struct.troika, ptr, %struct.anon.5, i8, i32, i32, i64, ptr, ptr, ptr, i64, %union.anon.6 }
%struct.troika = type { i8, i8, i8, i8, i32, [3 x i64] }
%struct.anon.5 = type { %struct.MDBX_rkl, i64, %struct.MDBX_rkl }
%struct.MDBX_rkl = type { i64, i64, i32, i32, ptr, [12 x i64] }
%union.anon.6 = type { %struct.anon.7 }
%struct.anon.7 = type { i64, ptr }
%struct.anon = type { i8, i8 }
%struct.inner_cursor = type { %struct.MDBX_cursor, %struct.tree }
%struct.tree = type { i16, i16, i32, i32, i32, i32, i32, i64, i64, i64 }

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

; Function Attrs: nounwind uwtable
define dso_local zeroext i1 @cursor_is_tracked(ptr noundef %0) #1 {
  %2 = alloca i1, align 1
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #3
  %6 = load ptr, ptr %3, align 8, !tbaa !5
  %7 = getelementptr inbounds %struct.MDBX_cursor, ptr %6, i32 0, i32 5
  %8 = load ptr, ptr %7, align 8, !tbaa !9
  %9 = getelementptr inbounds %struct.MDBX_txn, ptr %8, i32 0, i32 14
  %10 = load ptr, ptr %9, align 8, !tbaa !12
  %11 = load ptr, ptr %3, align 8, !tbaa !5
  %12 = call i64 @cursor_dbi(ptr noundef %11) #4
  %13 = getelementptr inbounds ptr, ptr %10, i64 %12
  %14 = load ptr, ptr %13, align 8, !tbaa !5
  store ptr %14, ptr %4, align 8, !tbaa !5
  br label %15

15:                                               ; preds = %40, %1
  %16 = load ptr, ptr %4, align 8, !tbaa !5
  %17 = icmp ne ptr %16, null
  br i1 %17, label %19, label %18

18:                                               ; preds = %15
  store i32 2, ptr %5, align 4
  br label %44

19:                                               ; preds = %15
  %20 = load ptr, ptr %3, align 8, !tbaa !5
  %21 = load ptr, ptr %3, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.MDBX_cursor, ptr %21, i32 0, i32 1
  %23 = getelementptr inbounds %struct.anon, ptr %22, i32 0, i32 0
  %24 = load i8, ptr %23, align 4, !tbaa !18
  %25 = sext i8 %24 to i32
  %26 = and i32 %25, 1
  %27 = icmp ne i32 %26, 0
  br i1 %27, label %28, label %33

28:                                               ; preds = %19
  %29 = load ptr, ptr %4, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.MDBX_cursor, ptr %29, i32 0, i32 8
  %31 = load ptr, ptr %30, align 8, !tbaa !19
  %32 = getelementptr inbounds %struct.inner_cursor, ptr %31, i32 0, i32 0
  br label %35

33:                                               ; preds = %19
  %34 = load ptr, ptr %4, align 8, !tbaa !5
  br label %35

35:                                               ; preds = %33, %28
  %36 = phi ptr [ %32, %28 ], [ %34, %33 ]
  %37 = icmp eq ptr %20, %36
  br i1 %37, label %38, label %39

38:                                               ; preds = %35
  store i1 true, ptr %2, align 1
  store i32 1, ptr %5, align 4
  br label %44

39:                                               ; preds = %35
  br label %40

40:                                               ; preds = %39
  %41 = load ptr, ptr %4, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.MDBX_cursor, ptr %41, i32 0, i32 11
  %43 = load ptr, ptr %42, align 8, !tbaa !20
  store ptr %43, ptr %4, align 8, !tbaa !5
  br label %15, !llvm.loop !21

44:                                               ; preds = %38, %18
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  %45 = load i32, ptr %5, align 4
  switch i32 %45, label %49 [
    i32 2, label %46
    i32 1, label %47
  ]

46:                                               ; preds = %44
  store i1 false, ptr %2, align 1
  br label %47

47:                                               ; preds = %46, %44
  %48 = load i1, ptr %2, align 1
  ret i1 %48

49:                                               ; preds = %44
  unreachable
}

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

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { 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 #3 = { nounwind }
attributes #4 = { 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, !6, i64 16}
!10 = !{!"MDBX_cursor", !11, 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}
!11 = !{!"int", !7, i64 0}
!12 = !{!13, !6, i64 120}
!13 = !{!"MDBX_txn", !11, i64 0, !11, i64 4, !14, i64 8, !14, i64 16, !6, i64 24, !6, i64 32, !15, i64 40, !14, i64 64, !14, i64 72, !6, i64 80, !6, i64 88, !6, i64 96, !6, i64 104, !6, i64 112, !6, i64 120, !17, i64 128, !6, i64 160, !7, i64 168}
!14 = !{!"long", !7, i64 0}
!15 = !{!"geo", !16, i64 0, !16, i64 2, !11, i64 4, !11, i64 8, !7, i64 12, !7, i64 16}
!16 = !{!"short", !7, i64 0}
!17 = !{!"MDBX_canary", !14, i64 0, !14, i64 8, !14, i64 16, !14, i64 24}
!18 = !{!7, !7, i64 0}
!19 = !{!10, !6, i64 40}
!20 = !{!10, !6, i64 368}
!21 = distinct !{!21, !22}
!22 = !{!"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"

; Function Attrs: nofree nounwind memory(read) uwtable
define dso_local zeroext i1 @cursor_is_tracked(ptr noundef readonly %0) local_unnamed_addr #0 {
  %2 = getelementptr inbounds i8, ptr %0, i64 16
  %3 = load ptr, ptr %2, align 8, !tbaa !5
  %4 = getelementptr inbounds i8, ptr %3, i64 120
  %5 = load ptr, ptr %4, align 8, !tbaa !11
  %6 = tail call i64 @cursor_dbi(ptr noundef %0) #2
  %7 = getelementptr inbounds ptr, ptr %5, i64 %6
  %.0912 = load ptr, ptr %7, align 8, !tbaa !17
  %.not13.not = icmp eq ptr %.0912, null
  br i1 %.not13.not, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %1
  %8 = getelementptr inbounds i8, ptr %0, i64 4
  %9 = load i8, ptr %8, align 4, !tbaa !18
  %10 = and i8 %9, 1
  %.not11 = icmp eq i8 %10, 0
  br i1 %.not11, label %.lr.ph.split.us, label %.lr.ph.split

.lr.ph.split.us:                                  ; preds = %.lr.ph
  %11 = icmp eq ptr %.0912, %0
  br i1 %11, label %._crit_edge, label %.lr.ph21

.lr.ph21:                                         ; preds = %.lr.ph.split.us, %.lr.ph21
  %.0914.us.pn = phi ptr [ %.09.us, %.lr.ph21 ], [ %.0912, %.lr.ph.split.us ]
  %12 = getelementptr inbounds i8, ptr %.0914.us.pn, i64 368
  %.09.us = load ptr, ptr %12, align 8, !tbaa !17
  %.not.us = icmp ne ptr %.09.us, null
  %13 = icmp ne ptr %.09.us, %0
  %or.cond.not = and i1 %13, %.not.us
  br i1 %or.cond.not, label %.lr.ph21, label %._crit_edge, !llvm.loop !19

.lr.ph.split:                                     ; preds = %.lr.ph
  %14 = getelementptr inbounds i8, ptr %.0912, i64 40
  %15 = load ptr, ptr %14, align 8, !tbaa !21
  %16 = icmp eq ptr %15, %0
  br i1 %16, label %._crit_edge, label %.lr.ph19

.lr.ph19:                                         ; preds = %.lr.ph.split, %18
  %.0914.pn = phi ptr [ %.09, %18 ], [ %.0912, %.lr.ph.split ]
  %17 = getelementptr inbounds i8, ptr %.0914.pn, i64 368
  %.09 = load ptr, ptr %17, align 8, !tbaa !17
  %.not.not.not = icmp ne ptr %.09, null
  br i1 %.not.not.not, label %18, label %._crit_edge, !llvm.loop !19

18:                                               ; preds = %.lr.ph19
  %19 = getelementptr inbounds i8, ptr %.09, i64 40
  %20 = load ptr, ptr %19, align 8, !tbaa !21
  %21 = icmp eq ptr %20, %0
  br i1 %21, label %._crit_edge, label %.lr.ph19, !llvm.loop !19

._crit_edge:                                      ; preds = %.lr.ph19, %18, %.lr.ph21, %.lr.ph.split.us, %.lr.ph.split, %1
  %.not.lcssa = phi i1 [ false, %1 ], [ true, %.lr.ph.split.us ], [ true, %.lr.ph.split ], [ %.not.us, %.lr.ph21 ], [ %.not.not.not, %18 ], [ %.not.not.not, %.lr.ph19 ]
  ret i1 %.not.lcssa
}

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

attributes #0 = { nofree nounwind 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 #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 = { 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, !10, i64 16}
!6 = !{!"MDBX_cursor", !7, i64 0, !8, i64 4, !8, i64 6, !8, i64 7, !10, i64 8, !10, i64 16, !10, i64 24, !10, i64 32, !10, i64 40, !8, i64 48, !8, i64 304, !10, i64 368, !10, i64 376}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!12, !10, i64 120}
!12 = !{!"MDBX_txn", !7, i64 0, !7, i64 4, !13, i64 8, !13, i64 16, !10, i64 24, !10, i64 32, !14, i64 40, !13, i64 64, !13, i64 72, !10, i64 80, !10, i64 88, !10, i64 96, !10, i64 104, !10, i64 112, !10, i64 120, !16, i64 128, !10, i64 160, !8, i64 168}
!13 = !{!"long", !8, i64 0}
!14 = !{!"geo", !15, i64 0, !15, i64 2, !7, i64 4, !7, i64 8, !8, i64 12, !8, i64 16}
!15 = !{!"short", !8, i64 0}
!16 = !{!"MDBX_canary", !13, i64 0, !13, i64 8, !13, i64 16, !13, i64 24}
!17 = !{!10, !10, i64 0}
!18 = !{!8, !8, i64 0}
!19 = distinct !{!19, !20}
!20 = !{!"llvm.loop.mustprogress"}
!21 = !{!6, !10, i64 40}


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