// File: /workspace/github_c_makefile_star2_O3/FH0[P]tun_direct/lwip-2.1.2/src/core/ipv6/nd6.c
// Function: nd6_new_router
static s8_t
nd6_new_router(const ip6_addr_t *router_addr, struct netif *netif)
{
  s8_t router_index;
  s8_t free_router_index;
  s8_t neighbor_index;

  IP6_ADDR_ZONECHECK_NETIF(router_addr, netif);

  /* Do we have a neighbor entry for this router? */
  neighbor_index = nd6_find_neighbor_cache_entry(router_addr);
  if (neighbor_index < 0) {
    /* Create a neighbor entry for this router. */
    neighbor_index = nd6_new_neighbor_cache_entry();
    if (neighbor_index < 0) {
      /* Could not create neighbor entry for this router. */
      return -1;
    }
    ip6_addr_set(&(neighbor_cache[neighbor_index].next_hop_address), router_addr);
    neighbor_cache[neighbor_index].netif = netif;
    neighbor_cache[neighbor_index].q = NULL;
    neighbor_cache[neighbor_index].state = ND6_INCOMPLETE;
    neighbor_cache[neighbor_index].counter.probes_sent = 1;
    nd6_send_neighbor_cache_probe(&neighbor_cache[neighbor_index], ND6_SEND_FLAG_MULTICAST_DEST);
  }

  /* Mark neighbor as router. */
  neighbor_cache[neighbor_index].isrouter = 1;

  /* Look for empty entry. */
  free_router_index = LWIP_ND6_NUM_ROUTERS;
  for (router_index = LWIP_ND6_NUM_ROUTERS - 1; router_index >= 0; router_index--) {
    /* check if router already exists (this is a special case for 2 netifs on the same subnet
       - e.g. wifi and cable) */
    if(default_router_list[router_index].neighbor_entry == &(neighbor_cache[neighbor_index])){ 
      return router_index; 
    } 
    if (default_router_list[router_index].neighbor_entry == NULL) {
      /* remember lowest free index to create a new entry */
      free_router_index = router_index;
    }
  }
  if (free_router_index < LWIP_ND6_NUM_ROUTERS) {
    default_router_list[free_router_index].neighbor_entry = &(neighbor_cache[neighbor_index]);
    return free_router_index;
  }

  /* Could not create a router entry. */

  /* Mark neighbor entry as not-router. Entry might be useful as neighbor still. */
  neighbor_cache[neighbor_index].isrouter = 0;

  /* router not found. */
  return -1;
}

// Assembly:
pushq %r15
pushq %r14
pushq %rbx
movq %rdi, %rbx
leaq 0xa39e(%rip), %r15      # 0x1c770
leaq 0x24(%r15), %rdi
callq 0x11e3c
testb %al, %al
js 0x123e5
movzbl %al, %r14d
jmp 0x1243e
callq 0x120ee
testb %al, %al
js 0x1249a
movzbl %al, %r14d
imulq $0x38, %r14, %rax
leaq 0xa60f(%rip), %rcx      # 0x1ca10
leaq (%rcx,%rax), %rsi
movups 0x24(%r15), %xmm0
movups %xmm0, (%rax,%rcx)
movb 0x34(%r15), %dl
movb %dl, 0x10(%rax,%rcx)
movq %rbx, 0x18(%rax,%rcx)
movq $0x0, 0x28(%rax,%rcx)
movb $0x1, 0x30(%rax,%rcx)
movl $0x1, 0x34(%rax,%rcx)
movq %rbx, %rdi
movl $0x1, %edx
callq 0x12a9b
imulq $0x38, %r14, %rax
leaq 0xa5c7(%rip), %rdx      # 0x1ca10
leaq (%rdx,%rax), %rcx
movb $0x1, 0x31(%rax,%rdx)
movb $0x2, %al
movb $0x3, %dl
leaq 0xa7e3(%rip), %rsi      # 0x1cc40
movzbl %al, %edi
movl %edi, %r8d
shll $0x4, %r8d
movq (%r8,%rsi), %r8
cmpq %rcx, %r8
je 0x1249c
testq %r8, %r8
movzbl %dl, %edx
cmovel %edi, %edx
addb $-0x1, %dil
movl %edi, %eax
jb 0x1245d
cmpb $0x2, %dl
jg 0x12496
movsbq %dl, %rax
shlq $0x4, %rax
movq %rcx, (%rax,%rsi)
movl %edx, %eax
jmp 0x1249c
movb $0x0, 0x31(%rcx)
movb $-0x1, %al
popq %rbx
popq %r14
popq %r15
retq