// File: /workspace/github_c_makefile_star2_O2/FH0[P]tun_direct/lwip-2.1.2/src/core/ipv6/nd6.c
// Function: nd6_new_neighbor_cache_entry
static s8_t
nd6_new_neighbor_cache_entry(void)
{
  s8_t i;
  s8_t j;
  u32_t time;


  /* First, try to find an empty entry. */
  for (i = 0; i < LWIP_ND6_NUM_NEIGHBORS; i++) {
    if (neighbor_cache[i].state == ND6_NO_ENTRY) {
      return i;
    }
  }

  /* We need to recycle an entry. in general, do not recycle if it is a router. */

  /* Next, try to find a Stale entry. */
  for (i = 0; i < LWIP_ND6_NUM_NEIGHBORS; i++) {
    if ((neighbor_cache[i].state == ND6_STALE) &&
        (!neighbor_cache[i].isrouter)) {
      nd6_free_neighbor_cache_entry(i);
      return i;
    }
  }

  /* Next, try to find a Probe entry. */
  for (i = 0; i < LWIP_ND6_NUM_NEIGHBORS; i++) {
    if ((neighbor_cache[i].state == ND6_PROBE) &&
        (!neighbor_cache[i].isrouter)) {
      nd6_free_neighbor_cache_entry(i);
      return i;
    }
  }

  /* Next, try to find a Delayed entry. */
  for (i = 0; i < LWIP_ND6_NUM_NEIGHBORS; i++) {
    if ((neighbor_cache[i].state == ND6_DELAY) &&
        (!neighbor_cache[i].isrouter)) {
      nd6_free_neighbor_cache_entry(i);
      return i;
    }
  }

  /* Next, try to find the oldest reachable entry. */
  time = 0xfffffffful;
  j = -1;
  for (i = 0; i < LWIP_ND6_NUM_NEIGHBORS; i++) {
    if ((neighbor_cache[i].state == ND6_REACHABLE) &&
        (!neighbor_cache[i].isrouter)) {
      if (neighbor_cache[i].counter.reachable_time < time) {
        j = i;
        time = neighbor_cache[i].counter.reachable_time;
      }
    }
  }
  if (j >= 0) {
    nd6_free_neighbor_cache_entry(j);
    return j;
  }

  /* Next, find oldest incomplete entry without queued packets. */
  time = 0;
  j = -1;
  for (i = 0; i < LWIP_ND6_NUM_NEIGHBORS; i++) {
    if (
        (neighbor_cache[i].q == NULL) &&
        (neighbor_cache[i].state == ND6_INCOMPLETE) &&
        (!neighbor_cache[i].isrouter)) {
      if (neighbor_cache[i].counter.probes_sent >= time) {
        j = i;
        time = neighbor_cache[i].counter.probes_sent;
      }
    }
  }
  if (j >= 0) {
    nd6_free_neighbor_cache_entry(j);
    return j;
  }

  /* Next, find oldest incomplete entry with queued packets. */
  time = 0;
  j = -1;
  for (i = 0; i < LWIP_ND6_NUM_NEIGHBORS; i++) {
    if ((neighbor_cache[i].state == ND6_INCOMPLETE) &&
        (!neighbor_cache[i].isrouter)) {
      if (neighbor_cache[i].counter.probes_sent >= time) {
        j = i;
        time = neighbor_cache[i].counter.probes_sent;
      }
    }
  }
  if (j >= 0) {
    nd6_free_neighbor_cache_entry(j);
    return j;
  }

  /* No more entries to try. */
  return -1;
}

// Assembly:
pushq %rbx
leaq 0xbc56(%rip), %rax      # 0x1da50
xorl %ebx, %ebx
cmpq $0xa, %rbx
je 0x11e14
cmpb $0x0, (%rax)
je 0x11f44
incq %rbx
addq $0x38, %rax
jmp 0x11dfc
leaq 0xbc36(%rip), %rax      # 0x1da51
xorl %ebx, %ebx
cmpq $0xa, %rbx
je 0x11e3b
cmpb $0x3, -0x1(%rax)
jne 0x11e32
cmpb $0x0, (%rax)
je 0x11f3c
incq %rbx
addq $0x38, %rax
jmp 0x11e1d
leaq 0xbc0f(%rip), %rax      # 0x1da51
xorl %ebx, %ebx
cmpq $0xa, %rbx
je 0x11e62
cmpb $0x5, -0x1(%rax)
jne 0x11e59
cmpb $0x0, (%rax)
je 0x11f3c
incq %rbx
addq $0x38, %rax
jmp 0x11e44
leaq 0xbbe8(%rip), %rax      # 0x1da51
xorl %ebx, %ebx
cmpq $0xa, %rbx
je 0x11e89
cmpb $0x4, -0x1(%rax)
jne 0x11e80
cmpb $0x0, (%rax)
je 0x11f3c
incq %rbx
addq $0x38, %rax
jmp 0x11e6b
pushq $-0x1
popq %rax
movb $-0x1, %bl
leaq 0xbbbb(%rip), %rcx      # 0x1da50
xorl %edx, %edx
cmpq $0xa, %rdx
je 0x11ebf
cmpb $0x2, (%rcx)
jne 0x11eb6
cmpb $0x0, 0x1(%rcx)
jne 0x11eb6
movl 0x4(%rcx), %esi
cmpl %eax, %esi
movzbl %bl, %ebx
cmovbl %edx, %ebx
cmovbl %esi, %eax
incq %rdx
addq $0x38, %rcx
jmp 0x11e97
testb %bl, %bl
jns 0x11f3c
xorl %eax, %eax
movb $-0x1, %bl
leaq 0xbb86(%rip), %rcx      # 0x1da54
xorl %edx, %edx
cmpq $0xa, %rax
je 0x11eff
cmpq $0x0, -0xc(%rcx)
jne 0x11ef6
cmpb $0x1, -0x4(%rcx)
jne 0x11ef6
cmpb $0x0, -0x3(%rcx)
jne 0x11ef6
movl (%rcx), %esi
cmpl %edx, %esi
movzbl %bl, %ebx
cmovael %eax, %ebx
cmoval %esi, %edx
incq %rax
addq $0x38, %rcx
jmp 0x11ed0
testb %bl, %bl
jns 0x11f3c
xorl %eax, %eax
movb $-0x1, %bl
leaq 0xbb42(%rip), %rcx      # 0x1da50
xorl %edx, %edx
cmpq $0xa, %rax
je 0x11f38
cmpb $0x1, (%rcx)
jne 0x11f2f
cmpb $0x0, 0x1(%rcx)
jne 0x11f2f
movl 0x4(%rcx), %esi
cmpl %edx, %esi
movzbl %bl, %ebx
cmovael %eax, %ebx
cmoval %esi, %edx
incq %rax
addq $0x38, %rcx
jmp 0x11f10
testb %bl, %bl
js 0x11f48
movsbl %bl, %edi
callq 0x12409
movl %ebx, %eax
popq %rbx
retq
movb $-0x1, %bl
jmp 0x11f44