// File: /workspace/github_c_makefile_star2_O3/open-mesh-mirror[P]batmand/batman.c
// Function: update_routes
void update_routes(struct orig_node *orig_node, struct neigh_node *neigh_node, unsigned char *hna_recv_buff, int16_t hna_buff_len)
{
	char orig_str[ADDR_STR_LEN], next_str[ADDR_STR_LEN];
	struct neigh_node *old_router;

	prof_start(PROF_update_routes);
	debug_output(4, "update_routes() \n");

	/* also handles orig_node->router == NULL and neigh_node == NULL */
	if ((orig_node != NULL) && (orig_node->router != neigh_node)) {
		old_router = orig_node->router;

		if ( ( orig_node != NULL ) && ( neigh_node != NULL ) ) {
			addr_to_string( orig_node->orig, orig_str, ADDR_STR_LEN );
			addr_to_string( neigh_node->addr, next_str, ADDR_STR_LEN );
			debug_output( 4, "Route to %s via %s\n", orig_str, next_str );
		}

		/* adds duplicated code but makes it more readable */

		/* new route added */
		if ((orig_node->router == NULL) && (neigh_node != NULL)) {

			debug_output(4, "Adding new route\n");

			add_del_route(orig_node->orig, 32, neigh_node->addr, neigh_node->if_incoming->addr.sin_addr.s_addr,
					neigh_node->if_incoming->if_index, neigh_node->if_incoming->dev, BATMAN_RT_TABLE_HOSTS, ROUTE_TYPE_UNICAST, ROUTE_ADD);

			orig_node->batman_if = neigh_node->if_incoming;
			orig_node->router = neigh_node;

			/* add new announced network(s) */
			hna_global_add(orig_node, hna_recv_buff, hna_buff_len);

		/* route deleted */
		} else if ((orig_node->router != NULL) && (neigh_node == NULL)) {

			debug_output(4, "Deleting previous route\n");

			/* remove old announced network(s) */
			hna_global_del(orig_node);

			add_del_route(orig_node->orig, 32, orig_node->router->addr, 0, orig_node->batman_if->if_index,
					orig_node->batman_if->dev, BATMAN_RT_TABLE_HOSTS, ROUTE_TYPE_UNICAST, ROUTE_DEL);

		/* route changed */
		} else {

			debug_output(4, "Route changed\n");

			/* add new route */
			add_del_route(orig_node->orig, 32, neigh_node->addr, neigh_node->if_incoming->addr.sin_addr.s_addr,
					neigh_node->if_incoming->if_index, neigh_node->if_incoming->dev, BATMAN_RT_TABLE_HOSTS, ROUTE_TYPE_UNICAST, ROUTE_ADD);

			/* delete old route */
			add_del_route(orig_node->orig, 32, orig_node->router->addr, 0, orig_node->batman_if->if_index,
					orig_node->batman_if->dev, BATMAN_RT_TABLE_HOSTS, ROUTE_TYPE_UNICAST, ROUTE_DEL);


			orig_node->batman_if = neigh_node->if_incoming;
			orig_node->router = neigh_node;

			/* update announced network(s) */
			hna_global_update(orig_node, hna_recv_buff, hna_buff_len, old_router);
		}

		orig_node->router = neigh_node;

	} else if (orig_node != NULL) {
		hna_global_update(orig_node, hna_recv_buff, hna_buff_len,
				  orig_node->router);
	}

	prof_stop(PROF_update_routes);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl %ecx, %ebp
movq %rdx, %r15
movq %rsi, %r14
movq %rdi, %rbx
movl $0x1, %edi
callq 0x6bbc
leaq 0xca35(%rip), %rsi      # 0xfa93
movl $0x4, %edi
xorl %eax, %eax
callq 0xd88c
testq %rbx, %rbx
je 0x3242
movq 0x8(%rbx), %r12
cmpq %r14, %r12
je 0x313d
movq %r15, 0x8(%rsp)
testq %r14, %r14
je 0x3153
movl (%rbx), %edi
leaq 0x20(%rsp), %r13
movq %r13, %rsi
movl $0x10, %edx
callq 0xbbac
movl 0x8(%r14), %edi
leaq 0x10(%rsp), %r15
movq %r15, %rsi
movl $0x10, %edx
callq 0xbbac
leaq 0xc9e6(%rip), %rsi      # 0xfaa5
movl $0x4, %edi
movq %r13, %rdx
movq %r15, %rcx
xorl %eax, %eax
callq 0xd88c
cmpq $0x0, 0x8(%rbx)
jne 0x31a7
leaq 0xc9d6(%rip), %rsi      # 0xfab9
movl $0x4, %edi
xorl %eax, %eax
callq 0xd88c
movl (%rbx), %edi
movl 0x8(%r14), %edx
movq 0x30(%r14), %rax
movl 0x34(%rax), %ecx
movl 0x20(%rax), %r8d
movq 0x8(%rax), %r9
subq $0x8, %rsp
movl $0x20, %esi
xorl %eax, %eax
pushq %rax
pushq %rax
pushq $0x42
callq 0x7cd4
addq $0x20, %rsp
movq 0x30(%r14), %rax
movq %rax, 0x10(%rbx)
movq %r14, 0x8(%rbx)
movswl %bp, %edx
movq %rbx, %rdi
movq 0x8(%rsp), %rsi
callq 0x7383
jmp 0x323e
movswl %bp, %edx
movq %rbx, %rdi
movq %r15, %rsi
movq %r12, %rcx
callq 0x7649
jmp 0x3242
testq %r12, %r12
je 0x31a7
leaq 0xc96c(%rip), %rsi      # 0xfacb
movl $0x4, %edi
xorl %eax, %eax
callq 0xd88c
movq %rbx, %rdi
callq 0x78ff
movl (%rbx), %edi
movq 0x8(%rbx), %rax
movl 0x8(%rax), %edx
movq 0x10(%rbx), %rax
movl 0x20(%rax), %r8d
movq 0x8(%rax), %r9
subq $0x8, %rsp
movl $0x20, %esi
xorl %ecx, %ecx
pushq $0x1
pushq $0x0
pushq $0x42
callq 0x7cd4
addq $0x20, %rsp
jmp 0x323e
leaq 0xc936(%rip), %rsi      # 0xfae4
xorl %r15d, %r15d
movl $0x4, %edi
xorl %eax, %eax
callq 0xd88c
movl (%rbx), %edi
movl 0x8(%r14), %edx
movq 0x30(%r14), %rax
movl 0x34(%rax), %ecx
movl 0x20(%rax), %r8d
movq 0x8(%rax), %r9
subq $0x8, %rsp
movl $0x42, %r13d
movl $0x20, %esi
pushq %r15
pushq %r15
pushq %r13
callq 0x7cd4
addq $0x20, %rsp
movl (%rbx), %edi
movq 0x8(%rbx), %rax
movl 0x8(%rax), %edx
movq 0x10(%rbx), %rax
movl 0x20(%rax), %r8d
movq 0x8(%rax), %r9
subq $0x8, %rsp
movl $0x20, %esi
xorl %ecx, %ecx
pushq $0x1
pushq %r15
pushq %r13
callq 0x7cd4
addq $0x20, %rsp
movq 0x30(%r14), %rax
movq %rax, 0x10(%rbx)
movq %r14, 0x8(%rbx)
movswl %bp, %edx
movq %rbx, %rdi
movq 0x8(%rsp), %rsi
movq %r12, %rcx
callq 0x7649
movq %r14, 0x8(%rbx)
movl $0x1, %edi
callq 0x6bd8
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq