// File: /workspace/github_c_makefile_star2_O1/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 0xb36c(%rip), %rax      # 0x1da40
xorl %ebx, %ebx
cmpb $0x0, (%rax)
je 0x12810
incb %bl
addq $0x38, %rax
cmpb $0xa, %bl
jne 0x126d6
leaq 0xb350(%rip), %rax      # 0x1da41
xorl %ebx, %ebx
cmpb $0x3, -0x1(%rax)
jne 0x12702
cmpb $0x0, (%rax)
je 0x12808
incq %rbx
addq $0x38, %rax
cmpq $0xa, %rbx
jne 0x126f3
leaq 0xb32b(%rip), %rax      # 0x1da41
xorl %ebx, %ebx
cmpb $0x5, -0x1(%rax)
jne 0x12727
cmpb $0x0, (%rax)
je 0x12808
incq %rbx
addq $0x38, %rax
cmpq $0xa, %rbx
jne 0x12718
leaq 0xb306(%rip), %rax      # 0x1da41
xorl %ebx, %ebx
cmpb $0x4, -0x1(%rax)
jne 0x1274c
cmpb $0x0, (%rax)
je 0x12808
incq %rbx
addq $0x38, %rax
cmpq $0xa, %rbx
jne 0x1273d
movb $-0x1, %bl
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
leaq 0xb2d9(%rip), %rcx      # 0x1da40
xorl %edx, %edx
cmpb $0x2, (%rcx)
jne 0x12782
cmpb $0x0, 0x1(%rcx)
jne 0x12782
movl 0x4(%rcx), %esi
cmpl %eax, %esi
movzbl %bl, %ebx
cmovbl %edx, %ebx
cmovbl %esi, %eax
incq %rdx
addq $0x38, %rcx
cmpq $0xa, %rdx
jne 0x12769
testb %bl, %bl
jns 0x12808
movb $-0x1, %bl
xorl %eax, %eax
leaq 0xb2a6(%rip), %rcx      # 0x1da44
xorl %edx, %edx
cmpq $0x0, -0xc(%rcx)
jne 0x127c0
cmpb $0x1, -0x4(%rcx)
jne 0x127c0
cmpb $0x0, -0x3(%rcx)
jne 0x127c0
movl (%rcx), %esi
cmpl %edx, %esi
movzbl %bl, %ebx
cmovael %eax, %ebx
cmoval %esi, %edx
incq %rax
addq $0x38, %rcx
cmpq $0xa, %rax
jne 0x127a0
testb %bl, %bl
jns 0x12808
movb $-0x1, %bl
xorl %eax, %eax
leaq 0xb264(%rip), %rcx      # 0x1da40
xorl %edx, %edx
cmpb $0x1, (%rcx)
jne 0x127f7
cmpb $0x0, 0x1(%rcx)
jne 0x127f7
movl 0x4(%rcx), %esi
cmpl %edx, %esi
movzbl %bl, %ebx
cmovael %eax, %ebx
cmoval %esi, %edx
incq %rax
addq $0x38, %rcx
cmpq $0xa, %rax
jne 0x127de
testb %bl, %bl
js 0x12814
movsbl %bl, %edi
callq 0x130db
movl %ebx, %eax
popq %rbx
retq
movb $-0x1, %bl
jmp 0x12810