// File: /workspace/github_c_makefile_star2_O0/jflatow[P]jfdb/src/trie.h
// Function: JFT_scan_child_slot
static inline JFT_Offset *JFT_scan_child_slot(const JFT *trie,
                                              JFT_Symbol symbol,
                                              int delta) {
  JFT_TypeInfo length = JFT_type_info(trie);
  const uint8_t *symbols = JFT_type_data(trie);
  JFT_Offset *offsets = (JFT_Offset *)(symbols + length);
  JFT_Offset lo = 0, hi = length, mid = 0;
  JFT_Symbol at = 0;

  // if looking for nil, return min, max, or nil (children can be missing, so search)
  if (symbol == JFT_SYMBOL_NIL) {
    if (delta > 0) {
      for (int i = lo; i < hi; i++)
        if (offsets[i])
          return offsets + i;
    } else if (delta < 0) {
      for (int i = hi - 1; i >= lo; i--)
        if (offsets[i])
          return offsets + i;
    }
    return 0;
  }

  // first binary search for the symbol, then adjust according to delta
  while (lo < hi) {
    mid = (lo + hi) / 2;
    at = *(symbols + mid);
    if (at < symbol)
      lo = mid + 1;
    else if (at > symbol)
      hi = mid;
    else
      break;
  }

  // we either found the symbol, or the bounds collapsed
  if (delta == 0) {
    // we want the symbol, only return if bounds are slack
    if (lo < hi)
      return offsets + mid;
  } else if (delta > 0) {
    // we want the symbol after, return either this or the next (if exists)
    if (at > symbol)
      return offsets + mid;
    else if (mid < length - 1)
      return offsets + mid + 1;
  } else if (delta < 0) {
    // we want the symbol before, return either this or the prev (if exists)
    if (at < symbol)
      return offsets + mid;
    else if (mid > 0)
      return offsets + mid - 1;
  }
  return NULL;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movq -0x10(%rbp), %rdi
callq 0x7950
movl %eax, -0x20(%rbp)
movq -0x10(%rbp), %rdi
callq 0x7970
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
movl -0x20(%rbp), %ecx
addq %rcx, %rax
movq %rax, -0x30(%rbp)
movq $0x0, -0x38(%rbp)
movl -0x20(%rbp), %eax
movq %rax, -0x40(%rbp)
movq $0x0, -0x48(%rbp)
movq $0x0, -0x50(%rbp)
cmpq $-0x2, -0x18(%rbp)
jne 0x7811
cmpl $0x0, -0x1c(%rbp)
jle 0x77b1
movq -0x38(%rbp), %rax
movl %eax, -0x54(%rbp)
movslq -0x54(%rbp), %rax
cmpq -0x40(%rbp), %rax
jae 0x77af
movq -0x30(%rbp), %rax
movslq -0x54(%rbp), %rcx
cmpq $0x0, (%rax,%rcx,8)
je 0x77a2
movq -0x30(%rbp), %rax
movslq -0x54(%rbp), %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, -0x8(%rbp)
jmp 0x7946
jmp 0x77a4
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x7771
jmp 0x7804
cmpl $0x0, -0x1c(%rbp)
jge 0x7802
movq -0x40(%rbp), %rax
subq $0x1, %rax
movl %eax, -0x58(%rbp)
movslq -0x58(%rbp), %rax
cmpq -0x38(%rbp), %rax
jb 0x7800
movq -0x30(%rbp), %rax
movslq -0x58(%rbp), %rcx
cmpq $0x0, (%rax,%rcx,8)
je 0x77f3
movq -0x30(%rbp), %rax
movslq -0x58(%rbp), %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, -0x8(%rbp)
jmp 0x7946
jmp 0x77f5
movl -0x58(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x58(%rbp)
jmp 0x77c2
jmp 0x7802
jmp 0x7804
movq $0x0, -0x8(%rbp)
jmp 0x7946
jmp 0x7813
movq -0x38(%rbp), %rax
cmpq -0x40(%rbp), %rax
jae 0x786e
movq -0x38(%rbp), %rax
addq -0x40(%rbp), %rax
shrq %rax
movq %rax, -0x48(%rbp)
movq -0x28(%rbp), %rax
movq -0x48(%rbp), %rcx
movzbl (%rax,%rcx), %eax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
cmpq -0x18(%rbp), %rax
jae 0x7854
movq -0x48(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x786c
movq -0x50(%rbp), %rax
cmpq -0x18(%rbp), %rax
jbe 0x7868
movq -0x48(%rbp), %rax
movq %rax, -0x40(%rbp)
jmp 0x786a
jmp 0x786e
jmp 0x786c
jmp 0x7813
cmpl $0x0, -0x1c(%rbp)
jne 0x789b
movq -0x38(%rbp), %rax
cmpq -0x40(%rbp), %rax
jae 0x7896
movq -0x30(%rbp), %rax
movq -0x48(%rbp), %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, -0x8(%rbp)
jmp 0x7946
jmp 0x793e
cmpl $0x0, -0x1c(%rbp)
jle 0x78f1
movq -0x50(%rbp), %rax
cmpq -0x18(%rbp), %rax
jbe 0x78c3
movq -0x30(%rbp), %rax
movq -0x48(%rbp), %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, -0x8(%rbp)
jmp 0x7946
movq -0x48(%rbp), %rax
movl -0x20(%rbp), %ecx
subl $0x1, %ecx
movl %ecx, %ecx
cmpq %rcx, %rax
jae 0x78ed
movq -0x30(%rbp), %rax
movq -0x48(%rbp), %rcx
shlq $0x3, %rcx
addq %rcx, %rax
addq $0x8, %rax
movq %rax, -0x8(%rbp)
jmp 0x7946
jmp 0x78ef
jmp 0x793c
cmpl $0x0, -0x1c(%rbp)
jge 0x793a
movq -0x50(%rbp), %rax
cmpq -0x18(%rbp), %rax
jae 0x7916
movq -0x30(%rbp), %rax
movq -0x48(%rbp), %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, -0x8(%rbp)
jmp 0x7946
cmpq $0x0, -0x48(%rbp)
jbe 0x7936
movq -0x30(%rbp), %rax
movq -0x48(%rbp), %rcx
shlq $0x3, %rcx
addq %rcx, %rax
addq $-0x8, %rax
movq %rax, -0x8(%rbp)
jmp 0x7946
jmp 0x7938
jmp 0x793a
jmp 0x793c
jmp 0x793e
movq $0x0, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x60, %rsp
popq %rbp
retq