// File: /workspace/github_c_makefile_star2_O2/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
pushq $0x1
popq %rdi
callq 0x63a8
leaq 0xbb45(%rip), %rsi      # 0xea93
pushq $0x4
popq %rdi
xorl %eax, %eax
callq 0xcb1c
testq %rbx, %rbx
je 0x311e
movq 0x8(%rbx), %r12
cmpq %r14, %r12
je 0x3024
movq %r15, 0x8(%rsp)
testq %r14, %r14
je 0x303a
movl (%rbx), %edi
leaq 0x10(%rsp), %rsi
pushq $0x10
popq %r15
movl %r15d, %edx
callq 0xb04f
movl 0x8(%r14), %edi
leaq 0x20(%rsp), %r13
movq %r13, %rsi
movl %r15d, %edx
callq 0xb04f
leaq 0xbafb(%rip), %rsi      # 0xeaa5
pushq $0x4
popq %rdi
leaq 0x10(%rsp), %rdx
movq %r13, %rcx
xorl %eax, %eax
callq 0xcb1c
cmpq $0x0, 0x8(%rbx)
jne 0x308a
leaq 0xbaeb(%rip), %rsi      # 0xeab9
pushq $0x4
popq %rdi
xorl %eax, %eax
callq 0xcb1c
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
pushq $0x20
popq %rsi
pushq $0x0
pushq $0x0
pushq $0x42
callq 0x745c
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 0x6b32
jmp 0x311a
movswl %bp, %edx
movq %rbx, %rdi
movq %r15, %rsi
movq %r12, %rcx
callq 0x6dee
jmp 0x311e
testq %r12, %r12
je 0x308a
leaq 0xba85(%rip), %rsi      # 0xeacb
pushq $0x4
popq %rdi
xorl %eax, %eax
callq 0xcb1c
movq %rbx, %rdi
callq 0x70b9
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
pushq $0x20
popq %rsi
xorl %ecx, %ecx
pushq $0x1
pushq $0x0
pushq $0x42
callq 0x745c
addq $0x20, %rsp
jmp 0x311a
leaq 0xba53(%rip), %rsi      # 0xeae4
pushq $0x4
popq %rdi
xorl %eax, %eax
callq 0xcb1c
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
pushq $0x42
popq %r13
pushq $0x20
popq %r15
movl %r15d, %esi
pushq $0x0
pushq $0x0
pushq %r13
callq 0x745c
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 %r15d, %esi
xorl %ecx, %ecx
pushq $0x1
pushq $0x0
pushq %r13
callq 0x745c
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 0x6dee
movq %r14, 0x8(%rbx)
pushq $0x1
popq %rdi
callq 0x63c4
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq