// File: /workspace/github_c_makefile_star2_O1/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 $0x28, %rsp
movl %ecx, %ebp
movq %rdx, %r13
movq %rsi, %r14
movq %rdi, %rbx
movl $0x1, %edi
callq 0x6880
leaq 0xca3d(%rip), %rsi      # 0xfa93
movl $0x4, %edi
xorl %eax, %eax
callq 0xd5b0
testq %rbx, %rbx
je 0x3241
movq 0x8(%rbx), %r12
cmpq %r14, %r12
je 0x3131
testq %r14, %r14
je 0x30bb
movl (%rbx), %edi
movq %rsp, %rsi
movl $0x10, %edx
callq 0xb8b8
movl 0x8(%r14), %edi
leaq 0x10(%rsp), %r15
movq %r15, %rsi
movl $0x10, %edx
callq 0xb8b8
leaq 0xc9fc(%rip), %rsi      # 0xfaa5
movl $0x4, %edi
movq %rsp, %rdx
movq %r15, %rcx
xorl %eax, %eax
callq 0xd5b0
testq %r14, %r14
sete %cl
movq 0x8(%rbx), %rax
testq %rax, %rax
setne %dl
orb %cl, %dl
jne 0x3147
leaq 0xc9e3(%rip), %rsi      # 0xfab9
xorl %r15d, %r15d
movl $0x4, %edi
xorl %eax, %eax
callq 0xd5b0
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
pushq %r15
pushq %r15
pushq $0x42
callq 0x79b0
addq $0x20, %rsp
movq 0x30(%r14), %rax
movq %rax, 0x10(%rbx)
movq %r14, 0x8(%rbx)
movswl %bp, %edx
movq %rbx, %rdi
movq %r13, %rsi
callq 0x7039
jmp 0x323d
movswl %bp, %edx
movq %rbx, %rdi
movq %r13, %rsi
movq %r12, %rcx
callq 0x7314
jmp 0x3241
testq %rax, %rax
sete %al
testq %r14, %r14
setne %cl
orb %al, %cl
jne 0x31a6
leaq 0xc96d(%rip), %rsi      # 0xfacb
movl $0x4, %edi
xorl %eax, %eax
callq 0xd5b0
movq %rbx, %rdi
callq 0x75e8
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 0x79b0
addq $0x20, %rsp
jmp 0x323d
leaq 0xc937(%rip), %rsi      # 0xfae4
xorl %r15d, %r15d
movl $0x4, %edi
xorl %eax, %eax
callq 0xd5b0
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, %eax
movl $0x20, %esi
pushq %r15
pushq %r15
pushq %rax
callq 0x79b0
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
movl $0x42, %eax
pushq %rax
callq 0x79b0
addq $0x20, %rsp
movq 0x30(%r14), %rax
movq %rax, 0x10(%rbx)
movq %r14, 0x8(%rbx)
movswl %bp, %edx
movq %rbx, %rdi
movq %r13, %rsi
movq %r12, %rcx
callq 0x7314
movq %r14, 0x8(%rbx)
movl $0x1, %edi
callq 0x689c
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq