// File: /workspace/github_c_makefile_star2_O3/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 0xa94a(%rip), %rax      # 0x1ca40
xorl %ebx, %ebx
cmpb $0x0, (%rax)
je 0x12234
incq %rbx
addq $0x38, %rax
cmpq $0xa, %rbx
jne 0x120f8
leaq 0xa92c(%rip), %rax      # 0x1ca41
xorl %ebx, %ebx
cmpb $0x3, -0x1(%rax)
jne 0x12126
cmpb $0x0, (%rax)
je 0x1222c
incq %rbx
addq $0x38, %rax
cmpq $0xa, %rbx
jne 0x12117
leaq 0xa907(%rip), %rax      # 0x1ca41
xorl %ebx, %ebx
cmpb $0x5, -0x1(%rax)
jne 0x1214b
cmpb $0x0, (%rax)
je 0x1222c
incq %rbx
addq $0x38, %rax
cmpq $0xa, %rbx
jne 0x1213c
leaq 0xa8e2(%rip), %rax      # 0x1ca41
xorl %ebx, %ebx
cmpb $0x4, -0x1(%rax)
jne 0x12170
cmpb $0x0, (%rax)
je 0x1222c
incq %rbx
addq $0x38, %rax
cmpq $0xa, %rbx
jne 0x12161
movb $-0x1, %bl
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
leaq 0xa8b5(%rip), %rcx      # 0x1ca40
xorl %edx, %edx
cmpb $0x2, (%rcx)
jne 0x121a6
cmpb $0x0, 0x1(%rcx)
jne 0x121a6
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 0x1218d
testb %bl, %bl
jns 0x1222c
movb $-0x1, %bl
xorl %eax, %eax
leaq 0xa882(%rip), %rcx      # 0x1ca44
xorl %edx, %edx
cmpq $0x0, -0xc(%rcx)
jne 0x121e4
cmpb $0x1, -0x4(%rcx)
jne 0x121e4
cmpb $0x0, -0x3(%rcx)
jne 0x121e4
movl (%rcx), %esi
cmpl %edx, %esi
movzbl %bl, %ebx
cmovael %eax, %ebx
cmoval %esi, %edx
incq %rax
addq $0x38, %rcx
cmpq $0xa, %rax
jne 0x121c4
testb %bl, %bl
jns 0x1222c
movb $-0x1, %bl
xorl %eax, %eax
leaq 0xa840(%rip), %rcx      # 0x1ca40
xorl %edx, %edx
cmpb $0x1, (%rcx)
jne 0x1221b
cmpb $0x0, 0x1(%rcx)
jne 0x1221b
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 0x12202
testb %bl, %bl
js 0x12238
movsbl %bl, %edi
callq 0x12a44
movl %ebx, %eax
popq %rbx
retq
movb $-0x1, %bl
jmp 0x12234