// File: /workspace/github_c_makefile_star2_O1/open-mesh-mirror[P]batmand/originator.c
// Function: purge_orig
void purge_orig(uint32_t curr_time)
{
	struct hash_it_t *hashit = NULL;
	struct list_head *neigh_pos, *neigh_temp, *prev_list_head;
	struct list_head *gw_pos, *gw_pos_tmp;
	struct orig_node *orig_node;
	struct neigh_node *neigh_node, *best_neigh_node;
	struct gw_node *gw_node;
	uint8_t gw_purged = 0, neigh_purged, max_tq;
	char orig_str[ADDR_STR_LEN], neigh_str[ADDR_STR_LEN];
	prof_start( PROF_purge_originator );


	/* for all origins... */
	while ( NULL != ( hashit = hash_iterate( orig_hash, hashit ) ) ) {

		orig_node = hashit->bucket->data;

		if ((int)(curr_time - (orig_node->last_valid + (2 * purge_timeout))) > 0) {

			addr_to_string( orig_node->orig, orig_str, ADDR_STR_LEN );
			debug_output( 4, "Originator timeout: originator %s, last_valid %u \n", orig_str, orig_node->last_valid );

			hash_remove_bucket( orig_hash, hashit );

			/* for all neighbours towards this originator ... */
			list_for_each_safe( neigh_pos, neigh_temp, &orig_node->neigh_list ) {

				neigh_node = list_entry(neigh_pos, struct neigh_node, list);

				list_del((struct list_head *)&orig_node->neigh_list, neigh_pos, &orig_node->neigh_list);
				debugFree(neigh_node->tq_recv, 1407);
				debugFree(neigh_node->real_bits, 1409);
				debugFree(neigh_node, 1401);

			}

			list_for_each( gw_pos, &gw_list ) {

				gw_node = list_entry( gw_pos, struct gw_node, list );

				if ( gw_node->deleted )
					continue;

				if ( gw_node->orig_node == orig_node ) {

					addr_to_string( gw_node->orig_node->orig, orig_str, ADDR_STR_LEN );
					debug_output( 3, "Removing gateway %s from gateway list \n", orig_str );

					gw_node->deleted = get_time_msec();
					gw_purged = 1;

					break;

				}

			}

			update_routes( orig_node, NULL, NULL, 0 );

			debugFree( orig_node->bcast_own, 1403 );
			debugFree( orig_node->bcast_own_sum, 1404 );
			debugFree( orig_node, 1405 );

		} else {

			best_neigh_node = NULL;
			max_tq = neigh_purged = 0;
			prev_list_head = (struct list_head *)&orig_node->neigh_list;

			/* for all neighbours towards this originator ... */
			list_for_each_safe( neigh_pos, neigh_temp, &orig_node->neigh_list ) {

				neigh_node = list_entry( neigh_pos, struct neigh_node, list );

				if ((int)(curr_time - (neigh_node->last_valid + purge_timeout)) > 0) {

					addr_to_string( orig_node->orig, orig_str, ADDR_STR_LEN );
					addr_to_string( neigh_node->addr, neigh_str, ADDR_STR_LEN );
					debug_output( 4, "Neighbour timeout: originator %s, neighbour: %s, last_valid %u \n", orig_str, neigh_str, neigh_node->last_valid );

					if (orig_node->router == neigh_node) {

						/* we have to delete the route towards this node before it gets purged */
						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);

						/* if the neighbour is the route towards our gateway */
						if ((curr_gateway != NULL) && (curr_gateway->orig_node == orig_node))
							del_default_route();

						orig_node->router = NULL;

					}

					neigh_purged = 1;
					list_del(prev_list_head, neigh_pos, &orig_node->neigh_list);
					debugFree(neigh_node->tq_recv, 1408);
					debugFree(neigh_node->real_bits, 1410);
					debugFree(neigh_node, 1406);

				} else {

					if ((best_neigh_node == NULL) || (neigh_node->tq_avg > max_tq)) {
						best_neigh_node = neigh_node;
						max_tq = neigh_node->tq_avg;
					}

					prev_list_head = &neigh_node->list;

				}

			}

			if ((neigh_purged) && ((best_neigh_node == NULL) || (orig_node->router == NULL) || (max_tq > orig_node->router->tq_avg)))
				update_routes( orig_node, best_neigh_node, orig_node->hna_buff, orig_node->hna_buff_len );

		}

	}


	prev_list_head = (struct list_head *)&gw_list;

	list_for_each_safe( gw_pos, gw_pos_tmp, &gw_list ) {

		gw_node = list_entry(gw_pos, struct gw_node, list);

		if ((gw_node->deleted) && ((int)(curr_time - (gw_node->deleted + (2 * purge_timeout))) > 0)) {

			list_del( prev_list_head, gw_pos, &gw_list );
			debugFree( gw_pos, 1406 );

		} else {

			prev_list_head = &gw_node->list;

		}

	}

	prof_stop( PROF_purge_originator );

	if ( gw_purged )
		choose_gw();

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movl %edi, 0x4(%rsp)
movl $0x6, %edi
callq 0x6880
leaq 0x12b3f(%rip), %r13     # 0x17330
movq (%r13), %rdi
xorl %esi, %esi
callq 0x6416
testq %rax, %rax
je 0x4b69
movq %rax, %rbp
movq $0x0, 0x20(%rsp)
movq 0x8(%rbp), %rax
movq (%rax), %r12
leaq 0x12a71(%rip), %rax     # 0x17290
movl (%rax), %eax
addl %eax, %eax
addl 0x30(%r12), %eax
movl 0x4(%rsp), %ecx
subl %eax, %ecx
testl %ecx, %ecx
jle 0x48ef
movl (%r12), %edi
leaq 0x30(%rsp), %rbx
movq %rbx, %rsi
movl $0x10, %edx
callq 0xb8b8
movl 0x30(%r12), %ecx
movl $0x4, %edi
leaq 0xb8eb(%rip), %rsi      # 0x10148
movq %rbx, %rdx
xorl %eax, %eax
callq 0xd5b0
movq (%r13), %rdi
movq %rbp, %rsi
callq 0x66e0
leaq 0x48(%r12), %r14
movq 0x48(%r12), %r15
cmpq %r14, %r15
je 0x48c4
movq (%r15), %rbx
movq %r14, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x5bcf
movq 0x10(%r15), %rdi
movl $0x57f, %esi            # imm = 0x57F
callq 0x5f5b
movq 0x20(%r15), %rdi
movl $0x581, %esi            # imm = 0x581
callq 0x5f5b
movq %r15, %rdi
movl $0x579, %esi            # imm = 0x579
callq 0x5f5b
movq %rbx, %r15
cmpq %r14, %rbx
jne 0x4882
leaq 0x12a55(%rip), %rcx     # 0x17320
movq (%rcx), %rbx
cmpq %rcx, %rbx
je 0x4b0e
cmpl $0x0, 0x18(%rbx)
jne 0x48ea
movq 0x8(%rbx), %rax
cmpq %r12, %rax
je 0x4ad5
movq (%rbx), %rbx
jmp 0x48ce
movq %rbp, 0x28(%rsp)
leaq 0x48(%r12), %rbp
movq 0x48(%r12), %rbx
cmpq %rbp, %rbx
je 0x4a7b
movq $0x0, 0x10(%rsp)
xorl %eax, %eax
movq $0x0, 0x8(%rsp)
movq %rbp, 0x18(%rsp)
movq %rbx, %r15
movl 0x1c(%rbx), %ecx
leaq 0x12963(%rip), %rdx     # 0x17290
addl (%rdx), %ecx
movq (%rbx), %rbx
movl 0x4(%rsp), %edx
subl %ecx, %edx
testl %edx, %edx
jle 0x4a3e
movl (%r12), %edi
leaq 0x30(%rsp), %r13
movq %r13, %rsi
movl $0x10, %edx
callq 0xb8b8
movl 0x8(%r15), %edi
leaq 0x40(%rsp), %r14
movq %r14, %rsi
movl $0x10, %edx
callq 0xb8b8
movl 0x1c(%r15), %r8d
movl $0x4, %edi
leaq 0xb827(%rip), %rsi      # 0x101a3
movq %r13, %rdx
movq %r14, %rcx
xorl %eax, %eax
callq 0xd5b0
cmpq %r15, 0x8(%r12)
jne 0x4a01
movl $0x4, %edi
leaq 0xb848(%rip), %rsi      # 0x101e4
xorl %eax, %eax
callq 0xd5b0
movq %r12, %rdi
callq 0x75e8
movl (%r12), %edi
movq 0x8(%r12), %rax
movl 0x8(%rax), %edx
movq 0x10(%r12), %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
leaq 0x128db(%rip), %rax     # 0x172c0
movq (%rax), %rax
testq %rax, %rax
je 0x49f8
cmpq %r12, 0x8(%rax)
jne 0x49f8
callq 0xb900
movq $0x0, 0x8(%r12)
movq 0x18(%rsp), %rdi
movq %r15, %rsi
movq %rbp, %rdx
callq 0x5bcf
movq 0x10(%r15), %rdi
movl $0x580, %esi            # imm = 0x580
callq 0x5f5b
movq 0x20(%r15), %rdi
movl $0x582, %esi            # imm = 0x582
callq 0x5f5b
movq %r15, %rdi
movl $0x57e, %esi            # imm = 0x57E
callq 0x5f5b
movb $0x1, %al
jmp 0x4a6b
cmpq $0x0, 0x8(%rsp)
je 0x4a51
movq 0x10(%rsp), %rcx
cmpb %cl, 0x19(%r15)
jbe 0x4a66
movb 0x19(%r15), %cl
movq %rcx, 0x10(%rsp)
movq %r15, 0x18(%rsp)
movq %r15, 0x8(%rsp)
jmp 0x4a6b
movq %r15, 0x18(%rsp)
cmpq %rbp, %rbx
jne 0x4920
testb %al, %al
sete %al
jmp 0x4a8f
movb $0x1, %al
movq $0x0, 0x8(%rsp)
movq $0x0, 0x10(%rsp)
testb %al, %al
movq 0x28(%rsp), %rbp
jne 0x4acc
cmpq $0x0, 0x8(%rsp)
je 0x4ab4
movq 0x8(%r12), %rax
testq %rax, %rax
je 0x4ab4
movq 0x10(%rsp), %rcx
cmpb 0x19(%rax), %cl
jbe 0x4acc
movq 0x38(%r12), %rdx
movswl 0x40(%r12), %ecx
movq %r12, %rdi
movq 0x8(%rsp), %rsi
callq 0x302c
leaq 0x1285d(%rip), %r13     # 0x17330
jmp 0x4b47
movl (%rax), %edi
leaq 0x30(%rsp), %r14
movq %r14, %rsi
movl $0x10, %edx
callq 0xb8b8
movl $0x3, %edi
leaq 0xb686(%rip), %rsi      # 0x1017b
movq %r14, %rdx
xorl %eax, %eax
callq 0xd5b0
callq 0xb0fc
movl %eax, 0x18(%rbx)
movb $0x1, %al
movq %rax, 0x20(%rsp)
movq %r12, %rdi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x302c
movq 0x18(%r12), %rdi
movl $0x57b, %esi            # imm = 0x57B
callq 0x5f5b
movq 0x20(%r12), %rdi
movl $0x57c, %esi            # imm = 0x57C
callq 0x5f5b
movq %r12, %rdi
movl $0x57d, %esi            # imm = 0x57D
callq 0x5f5b
movq (%r13), %rdi
movq %rbp, %rsi
callq 0x6416
movq %rax, %rbp
testq %rax, %rax
jne 0x4811
cmpb $0x0, 0x20(%rsp)
sete %bl
jmp 0x4b6b
movb $0x1, %bl
leaq 0x127ae(%rip), %r14     # 0x17320
movq (%r14), %r13
cmpq %r14, %r13
je 0x4bc9
leaq 0x1270f(%rip), %rbp     # 0x17290
movq %r14, %r15
movq %r13, %r12
movq (%r13), %r13
movl 0x18(%r12), %eax
testl %eax, %eax
je 0x4bc1
movl (%rbp), %ecx
leal (%rax,%rcx,2), %eax
movl 0x4(%rsp), %ecx
subl %eax, %ecx
testl %ecx, %ecx
jle 0x4bc1
movq %r15, %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0x5bcf
movq %r12, %rdi
movl $0x57e, %esi            # imm = 0x57E
callq 0x5f5b
jmp 0x4bc4
movq %r12, %r15
cmpq %r14, %r13
jne 0x4b84
movl $0x6, %edi
callq 0x689c
testb %bl, %bl
jne 0x4bdc
callq 0x2d46
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq