// File: /workspace/github_c_makefile_star2_O1/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, 0x1c(%rsp)
movq %r8, 0x28(%rsp)
movq %rcx, 0x10(%rsp)
movl %edx, %r15d
movq %rsi, 0x30(%rsp)
movq %rdi, %rbx
movl 0x78(%rsp), %eax
movl %eax, 0xc(%rsp)
movb 0x70(%rsp), %bpl
movl $0x5, %edi
callq 0x6880
leaq 0xbb00(%rip), %rsi      # 0x10063
xorl %r12d, %r12d
movl $0x4, %edi
xorl %eax, %eax
callq 0xd5b0
leaq 0x48(%rbx), %r14
movq %rbx, 0x20(%rsp)
movq 0x48(%rbx), %rbx
cmpq %r14, %rbx
je 0x45da
xorl %r13d, %r13d
cmpl %r15d, 0x8(%rbx)
jne 0x45ac
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 0x45b1
jmp 0x45cf
testb %bpl, %bpl
jne 0x45cc
movq 0x10(%rbx), %rdi
leaq 0x18(%rbx), %rsi
xorl %edx, %edx
callq 0x6970
movq 0x10(%rbx), %rdi
callq 0x698d
movb %al, 0x19(%rbx)
movq %r13, %r12
movq (%rbx), %rbx
movq %r12, %r13
cmpq %r14, %rbx
jne 0x4587
testq %r12, %r12
je 0x45f9
leaq 0xbad1(%rip), %rsi      # 0x100b7
movl $0x4, %edi
xorl %eax, %eax
callq 0xd5b0
movq 0x20(%rsp), %rbx
jmp 0x461c
movl %r15d, %edi
callq 0x43bb
movq 0x20(%rsp), %rbx
movq %rbx, %rdi
movq %rax, %rsi
movl %r15d, %edx
movq 0x10(%rsp), %rcx
callq 0x42ac
movq %rax, %r12
movl 0xc(%rsp), %r14d
movl %r14d, 0x1c(%r12)
movq 0x10(%r12), %rdi
leaq 0x18(%r12), %rsi
movq 0x30(%rsp), %r15
movzbl 0x10(%r15), %edx
callq 0x6970
movq 0x10(%r12), %rdi
callq 0x698d
movb %al, 0x19(%r12)
testb %bpl, %bpl
jne 0x465f
movb 0x2(%r15), %cl
movb %cl, 0x44(%rbx)
movb %cl, 0x1a(%r12)
movq 0x8(%rbx), %rcx
cmpq %r12, %rcx
je 0x4696
testq %rcx, %rcx
je 0x4699
cmpb 0x19(%rcx), %al
ja 0x4699
jne 0x4696
movq 0x28(%r12), %rax
movq 0x20(%rax), %rax
movq 0x10(%rsp), %rdx
movzbl 0x1c(%rdx), %edx
movb (%rax,%rdx), %al
movq 0x28(%rcx), %rsi
movq 0x20(%rsi), %rsi
cmpb (%rsi,%rdx), %al
ja 0x4699
movq %rcx, %r12
movswl 0x1c(%rsp), %ecx
movq %rbx, %rdi
movq %r12, %rsi
movq 0x28(%rsp), %rdx
callq 0x302c
movb 0x3(%r15), %al
cmpb %al, 0x34(%rbx)
je 0x46c7
movzwl 0x6(%r15), %edx
movzbl %al, %esi
movq %rbx, %rdi
callq 0x325a
movb 0x3(%r15), %al
movb %al, 0x34(%rbx)
movq %rbx, %rdi
callq 0x77b3
leaq 0x12bdd(%rip), %rax     # 0x172ba
movzbl (%rax), %ecx
cmpl $0x3, %ecx
jb 0x47b6
cmpb $0x0, 0x34(%rbx)
setne %dl
leaq 0x12bc9(%rip), %rax     # 0x172c0
movq (%rax), %rax
testq %rax, %rax
setne %sil
andb %dl, %sil
cmpb $0x1, %sil
jne 0x47b6
movq 0x8(%rax), %rax
cmpq %rbx, %rax
je 0x47b6
leaq 0x12bae(%rip), %rdx     # 0x172d0
movl (%rdx), %edx
testl %edx, %edx
jne 0x473a
movq 0x8(%rbx), %rsi
movb 0x19(%rsi), %sil
movq 0x8(%rax), %rdi
cmpb 0x19(%rdi), %sil
ja 0x473e
cmpl (%rbx), %edx
jne 0x47b6
cmpl $0x3, %ecx
je 0x475d
cmpl (%rbx), %edx
je 0x475d
movq 0x8(%rbx), %rdx
movzbl 0x19(%rdx), %edx
movq 0x8(%rax), %rsi
movzbl 0x19(%rsi), %esi
subl %esi, %edx
cmpl %ecx, %edx
jl 0x47b6
leaq 0x12bbc(%rip), %rdx     # 0x17320
movq %rdx, %rcx
movq (%rcx), %rcx
cmpq %rdx, %rcx
je 0x4777
cmpq %rbx, 0x8(%rcx)
jne 0x4767
jmp 0x4779
xorl %ecx, %ecx
testq %rcx, %rcx
je 0x47b6
subl 0x14(%rcx), %r14d
addl $0xffff8ad0, %r14d      # imm = 0xFFFF8AD0
testl %r14d, %r14d
jle 0x47b6
movq 0x8(%rax), %rax
movzbl 0x19(%rax), %edx
movq 0x8(%rbx), %rax
movzbl 0x19(%rax), %ecx
leaq 0xb946(%rip), %rsi      # 0x100eb
movl $0x3, %edi
xorl %eax, %eax
callq 0xd5b0
callq 0xb900
movl $0x5, %edi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x689c