// File: /workspace/github_c_makefile_star2_O2/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 $0x28, %rsp
movl %r9d, 0x8(%rsp)
movq %r8, 0x10(%rsp)
movq %rcx, %r12
movl %edx, %r15d
movq %rsi, 0x20(%rsp)
movq %rdi, %rbx
movl 0x68(%rsp), %eax
movl %eax, 0xc(%rsp)
movb 0x60(%rsp), %bpl
pushq $0x5
popq %rdi
callq 0x63a8
leaq 0xad91(%rip), %rsi      # 0xf063
xorl %r13d, %r13d
pushq $0x4
popq %rdi
xorl %eax, %eax
callq 0xcb1c
movq %rbx, 0x18(%rsp)
addq $0x48, %rbx
movq %rbx, %r14
movq (%r14), %r14
cmpq %rbx, %r14
je 0x4333
cmpl %r15d, 0x8(%r14)
jne 0x4310
movq 0x30(%r14), %rax
cmpq %r12, %rax
je 0x4307
testb %bpl, %bpl
je 0x4315
cmpq %r12, %rax
cmoveq %r14, %r13
jmp 0x42eb
testb %bpl, %bpl
jne 0x42eb
movq 0x10(%r14), %rdi
leaq 0x18(%r14), %rsi
xorl %edx, %edx
callq 0x6494
movq 0x10(%r14), %rdi
callq 0x64b1
movb %al, 0x19(%r14)
jmp 0x42eb
testq %r13, %r13
je 0x4350
leaq 0xad78(%rip), %rsi      # 0xf0b7
pushq $0x4
popq %rdi
xorl %eax, %eax
callq 0xcb1c
movq 0x18(%rsp), %rbx
jmp 0x4371
movl %r15d, %edi
callq 0x4137
movq 0x18(%rsp), %rbx
movq %rbx, %rdi
movq %rax, %rsi
movl %r15d, %edx
movq %r12, %rcx
callq 0x4034
movq %rax, %r13
movl 0xc(%rsp), %r14d
movl %r14d, 0x1c(%r13)
movq 0x10(%r13), %rdi
leaq 0x18(%r13), %rsi
movq 0x20(%rsp), %r15
movzbl 0x10(%r15), %edx
callq 0x6494
movq 0x10(%r13), %rdi
callq 0x64b1
movb %al, 0x19(%r13)
testb %bpl, %bpl
jne 0x43ae
movb 0x2(%r15), %cl
movb %cl, 0x44(%rbx)
movb %cl, 0x1a(%r13)
movq 0x8(%rbx), %rsi
cmpq %r13, %rsi
je 0x43ee
testq %rsi, %rsi
je 0x43c1
cmpb 0x19(%rsi), %al
jbe 0x43ce
movswl 0x8(%rsp), %ecx
movq %rbx, %rdi
movq %r13, %rsi
jmp 0x43f6
jne 0x43ee
movq 0x28(%r13), %rax
movq 0x20(%rax), %rax
movzbl 0x1c(%r12), %ecx
movb (%rax,%rcx), %al
movq 0x28(%rsi), %rdx
movq 0x20(%rdx), %rdx
cmpb (%rdx,%rcx), %al
ja 0x43c1
movswl 0x8(%rsp), %ecx
movq %rbx, %rdi
movq 0x10(%rsp), %rdx
callq 0x2f26
movb 0x34(%rbx), %al
movb 0x3(%r15), %cl
cmpb %cl, %al
je 0x441f
movzwl 0x6(%r15), %edx
movzbl %cl, %esi
movq %rbx, %rdi
callq 0x3135
movb 0x3(%r15), %al
movb %al, 0x34(%rbx)
movq %rbx, %rdi
callq 0x726f
leaq 0x12e89(%rip), %rax     # 0x172ba
movzbl (%rax), %ecx
cmpl $0x3, %ecx
jb 0x4506
cmpb $0x0, 0x34(%rbx)
setne %dl
leaq 0x12e75(%rip), %rax     # 0x172c0
movq (%rax), %rax
testq %rax, %rax
setne %sil
andb %dl, %sil
cmpb $0x1, %sil
jne 0x4506
movq 0x8(%rax), %rax
cmpq %rbx, %rax
je 0x4506
leaq 0x12e5a(%rip), %rdx     # 0x172d0
movl (%rdx), %edx
testl %edx, %edx
jne 0x44af
movq 0x8(%rbx), %rsi
movb 0x19(%rsi), %sil
movq 0x8(%rax), %rdi
cmpb 0x19(%rdi), %sil
jbe 0x44af
cmpl $0x3, %ecx
je 0x44b3
cmpl (%rbx), %edx
je 0x44b3
movq 0x8(%rbx), %rdx
movzbl 0x19(%rdx), %edx
movq 0x8(%rax), %rsi
movzbl 0x19(%rsi), %esi
subl %esi, %edx
cmpl %ecx, %edx
jge 0x44b3
jmp 0x4506
cmpl (%rbx), %edx
jne 0x4506
leaq 0x12e66(%rip), %rdx     # 0x17320
movq %rdx, %rcx
movq (%rcx), %rcx
cmpq %rdx, %rcx
je 0x4506
cmpq %rbx, 0x8(%rcx)
jne 0x44bd
testq %rcx, %rcx
je 0x4506
subl 0x14(%rcx), %r14d
addl $0xffff8ad0, %r14d      # imm = 0xFFFF8AD0
testl %r14d, %r14d
jle 0x4506
movq 0x8(%rax), %rax
movzbl 0x19(%rax), %edx
movq 0x8(%rbx), %rax
movzbl 0x19(%rax), %ecx
leaq 0xabf4(%rip), %rsi      # 0xf0eb
pushq $0x3
popq %rdi
xorl %eax, %eax
callq 0xcb1c
callq 0xb095
pushq $0x5
popq %rdi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x63c4