// File: /workspace/github_c_makefile_star2_O3/open-mesh-mirror[P]batmand/originator.c
// Function: update_orig
void update_orig(struct orig_node *orig_node, struct bat_packet *in, uint32_t neigh, struct batman_if *if_incoming, unsigned char *hna_recv_buff, int16_t hna_buff_len, uint8_t is_duplicate, uint32_t curr_time)
{
	struct list_head *list_pos;
	struct gw_node *gw_node;
	struct neigh_node *neigh_node = NULL, *tmp_neigh_node = NULL;
	prof_start(PROF_update_originator);

	debug_output(4, "update_originator(): Searching and updating originator entry of received packet,  \n");

	list_for_each(list_pos, &orig_node->neigh_list) {
		tmp_neigh_node = list_entry(list_pos, struct neigh_node, list);

		if ((tmp_neigh_node->addr == neigh ) && (tmp_neigh_node->if_incoming == if_incoming)) {
			neigh_node = tmp_neigh_node;
			continue;
		}

		if (is_duplicate)
			continue;

		ring_buffer_set(tmp_neigh_node->tq_recv, &tmp_neigh_node->tq_index, 0);
		tmp_neigh_node->tq_avg = ring_buffer_avg(tmp_neigh_node->tq_recv);
	}

	if (!neigh_node)
		neigh_node = create_neighbor(orig_node, get_orig_node(neigh), neigh, if_incoming);
	else
		debug_output(4, "Updating existing last-hop neighbour of originator\n");

	neigh_node->last_valid = curr_time;

	ring_buffer_set(neigh_node->tq_recv, &neigh_node->tq_index, in->tq);
	neigh_node->tq_avg = ring_buffer_avg(neigh_node->tq_recv);

	if (!is_duplicate) {
		orig_node->last_ttl = in->ttl;
		neigh_node->last_ttl = in->ttl;
	}

	/**
	 * if we got have a better tq value via this neighbour or
	 * same tq value but the link is more symetric change the next hop
	 * router
	 */
	if ((orig_node->router != neigh_node) && ((!orig_node->router) ||
	    (neigh_node->tq_avg > orig_node->router->tq_avg) ||
	    ((neigh_node->tq_avg == orig_node->router->tq_avg) &&
	     (neigh_node->orig_node->bcast_own_sum[if_incoming->if_num] > orig_node->router->orig_node->bcast_own_sum[if_incoming->if_num]))))
		update_routes(orig_node, neigh_node, hna_recv_buff, hna_buff_len);
	else
		update_routes(orig_node, orig_node->router, hna_recv_buff, hna_buff_len);

	if (orig_node->gwflags != in->gwflags)
		update_gw_list(orig_node, in->gwflags, in->gwport);

	orig_node->gwflags = in->gwflags;
	hna_global_check_tq(orig_node);

	/* restart gateway selection if we have more packets and fast or late switching enabled */
	if ((routing_class > 2) && (orig_node->gwflags != 0) && (curr_gateway != NULL)) {

		/* if the node is not our current gateway and
		   we have preferred gateray disabled and a better tq value or we found our preferred gateway */
		if ((curr_gateway->orig_node != orig_node) &&
				   (((pref_gateway == 0) && (orig_node->router->tq_avg > curr_gateway->orig_node->router->tq_avg)) || (pref_gateway == orig_node->orig))) {

			/* it is our preferred gateway or we have fast switching or the tq is $routing_class better than our old tq */
			if ((pref_gateway == orig_node->orig) || (routing_class == 3) || (orig_node->router->tq_avg - curr_gateway->orig_node->router->tq_avg >= routing_class)) {

				gw_node = NULL;

				list_for_each(list_pos, &gw_list) {
					gw_node = list_entry(list_pos, struct gw_node, list);

					if (gw_node->orig_node == orig_node)
						break;

					gw_node = NULL;
				}

				/* if this gateway had not a gateway failure within the last 30 seconds */
				if ((gw_node != NULL) && ((int)(curr_time - (gw_node->last_failure + 30000)) > 0)) {

					debug_output(3, "Gateway client - restart gateway selection: better gateway found (tq curr: %i, tq new: %i) \n", curr_gateway->orig_node->router->tq_avg, orig_node->router->tq_avg);

					del_default_route();

				}

			}

		}

	}

	prof_stop(PROF_update_originator);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl %r9d, 0xc(%rsp)
movq %r8, 0x20(%rsp)
movq %rcx, 0x10(%rsp)
movl %edx, %r15d
movq %rsi, 0x30(%rsp)
movq %rdi, %r13
movl 0x78(%rsp), %eax
movl %eax, 0x1c(%rsp)
movb 0x70(%rsp), %bpl
movl $0x5, %edi
callq 0x6bbc
leaq 0xbaef(%rip), %rsi      # 0x10063
movl $0x4, %edi
xorl %eax, %eax
callq 0xd88c
leaq 0x48(%r13), %r14
movq 0x48(%r13), %rbx
cmpq %r14, %rbx
je 0x4607
movq %r13, 0x28(%rsp)
xorl %r12d, %r12d
movq %r12, %r13
cmpl %r15d, 0x8(%rbx)
jne 0x45bd
testb %bpl, %bpl
setne %al
movq 0x10(%rsp), %rcx
cmpq %rcx, 0x30(%rbx)
sete %cl
movq %r13, %r12
cmoveq %rbx, %r12
orb %al, %cl
je 0x45c2
jmp 0x45e0
testb %bpl, %bpl
jne 0x45dd
movq 0x10(%rbx), %rdi
leaq 0x18(%rbx), %rsi
xorl %edx, %edx
callq 0x6cac
movq 0x10(%rbx), %rdi
callq 0x6cc9
movb %al, 0x19(%rbx)
movq %r13, %r12
movq (%rbx), %rbx
cmpq %r14, %rbx
jne 0x4595
testq %r12, %r12
movq 0x28(%rsp), %r13
je 0x4607
leaq 0xbabe(%rip), %rsi      # 0x100b7
movl $0x4, %edi
xorl %eax, %eax
callq 0xd88c
jmp 0x4625
movl %r15d, %edi
callq 0x43c8
movq %r13, %rdi
movq %rax, %rsi
movl %r15d, %edx
movq 0x10(%rsp), %rcx
callq 0x42bc
movq %rax, %r12
movl 0x1c(%rsp), %ebx
movl %ebx, 0x1c(%r12)
movq 0x10(%r12), %rdi
leaq 0x18(%r12), %rsi
movq 0x30(%rsp), %r14
movzbl 0x10(%r14), %edx
callq 0x6cac
movq 0x10(%r12), %rdi
callq 0x6cc9
movb %al, 0x19(%r12)
testb %bpl, %bpl
jne 0x4668
movb 0x2(%r14), %cl
movb %cl, 0x44(%r13)
movb %cl, 0x1a(%r12)
movq 0x8(%r13), %rsi
cmpq %r12, %rsi
je 0x46ac
testq %rsi, %rsi
je 0x467b
cmpb 0x19(%rsi), %al
jbe 0x4688
movswl 0xc(%rsp), %ecx
movq %r13, %rdi
movq %r12, %rsi
jmp 0x46b4
jne 0x46ac
movq 0x28(%r12), %rax
movq 0x20(%rax), %rax
movq 0x10(%rsp), %rcx
movzbl 0x1c(%rcx), %ecx
movb (%rax,%rcx), %al
movq 0x28(%rsi), %rdx
movq 0x20(%rdx), %rdx
cmpb (%rdx,%rcx), %al
ja 0x467b
movswl 0xc(%rsp), %ecx
movq %r13, %rdi
movq 0x20(%rsp), %rdx
callq 0x3034
movb 0x34(%r13), %al
movb 0x3(%r14), %cl
cmpb %cl, %al
je 0x46de
movzwl 0x6(%r14), %edx
movzbl %cl, %esi
movq %r13, %rdi
callq 0x325b
movb 0x3(%r14), %al
movb %al, 0x34(%r13)
movq %r13, %rdi
callq 0x7ad7
leaq 0x12bc9(%rip), %rax     # 0x172ba
movzbl (%rax), %ecx
cmpl $0x3, %ecx
jb 0x47ca
cmpb $0x0, 0x34(%r13)
setne %dl
leaq 0x12bb4(%rip), %rax     # 0x172c0
movq (%rax), %rax
testq %rax, %rax
setne %sil
andb %dl, %sil
cmpb $0x1, %sil
jne 0x47ca
movq 0x8(%rax), %rax
cmpq %r13, %rax
je 0x47ca
leaq 0x12b99(%rip), %rdx     # 0x172d0
movl (%rdx), %edx
testl %edx, %edx
jne 0x4772
movq 0x8(%r13), %rsi
movb 0x19(%rsi), %sil
movq 0x8(%rax), %rdi
cmpb 0x19(%rdi), %sil
jbe 0x4772
cmpl $0x3, %ecx
je 0x4778
cmpl (%r13), %edx
je 0x4778
movq 0x8(%r13), %rdx
movzbl 0x19(%rdx), %edx
movq 0x8(%rax), %rsi
movzbl 0x19(%rsi), %esi
subl %esi, %edx
cmpl %ecx, %edx
jge 0x4778
jmp 0x47ca
cmpl (%r13), %edx
jne 0x47ca
leaq 0x12ba1(%rip), %rdx     # 0x17320
movq %rdx, %rcx
movq (%rcx), %rcx
cmpq %rdx, %rcx
je 0x47ca
cmpq %r13, 0x8(%rcx)
jne 0x4782
testq %rcx, %rcx
je 0x47ca
subl 0x14(%rcx), %ebx
addl $0xffff8ad0, %ebx       # imm = 0xFFFF8AD0
testl %ebx, %ebx
jle 0x47ca
movq 0x8(%rax), %rax
movzbl 0x19(%rax), %edx
movq 0x8(%r13), %rax
movzbl 0x19(%rax), %ecx
leaq 0xb932(%rip), %rsi      # 0x100eb
movl $0x3, %edi
xorl %eax, %eax
callq 0xd88c
callq 0xbbf4
movl $0x5, %edi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x6bd8