// File: /workspace/github_c_makefile_star2_O3/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 0x6bbc
leaq 0x12b2b(%rip), %r13     # 0x17330
movq (%r13), %rdi
xorl %esi, %esi
callq 0x6741
testq %rax, %rax
je 0x4b7b
movq %rax, %rbp
leaq 0x12afd(%rip), %r12     # 0x17320
movq $0x0, 0x18(%rsp)
movq 0x8(%rbp), %rax
movq (%rax), %r15
leaq 0x12a56(%rip), %rax     # 0x17290
movl (%rax), %eax
leal (%rax,%rax), %ecx
addl 0x30(%r15), %ecx
movl 0x4(%rsp), %edi
movl %edi, %edx
subl %ecx, %edx
testl %edx, %edx
movq %r15, 0x8(%rsp)
jle 0x4914
movl (%r15), %edi
leaq 0x20(%rsp), %rbx
movq %rbx, %rsi
movl $0x10, %edx
callq 0xbbac
movl 0x30(%r15), %ecx
movl $0x4, %edi
leaq 0xb8cb(%rip), %rsi      # 0x10148
movq %rbx, %rdx
xorl %eax, %eax
callq 0xd88c
movq (%r13), %rdi
movq %rbp, %rsi
callq 0x6a25
leaq 0x48(%r15), %r14
movq 0x48(%r15), %r15
cmpq %r14, %r15
je 0x48e2
movq (%r15), %rbx
movq %r14, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x5be3
movq 0x10(%r15), %rdi
movl $0x57f, %esi            # imm = 0x57F
callq 0x628b
movq 0x20(%r15), %rdi
movl $0x581, %esi            # imm = 0x581
callq 0x628b
movq %r15, %rdi
movl $0x579, %esi            # imm = 0x579
callq 0x628b
movq %rbx, %r15
cmpq %r14, %rbx
jne 0x48a0
movq (%r12), %rbx
cmpq %r12, %rbx
movq 0x8(%rsp), %r15
je 0x4b22
cmpl $0x0, 0x18(%rbx)
jne 0x4907
movq 0x8(%rbx), %rax
cmpq %r15, %rax
je 0x4ae9
movq (%rbx), %rbx
cmpq %r12, %rbx
jne 0x48f4
jmp 0x4b22
leaq 0x48(%r15), %r14
movq 0x48(%r15), %r12
cmpq %r14, %r12
je 0x4ae0
movq %rbp, 0x10(%rsp)
movb $0x1, %cl
xorl %ebx, %ebx
xorl %r13d, %r13d
movq %r14, %r15
movq %r14, 0x38(%rsp)
movl 0x1c(%r12), %edx
movq %r12, %rbp
movq (%r12), %r12
addl %eax, %edx
movl %edi, %esi
subl %edx, %esi
testl %esi, %esi
jg 0x4984
movb 0x19(%rbp), %dl
cmpb %bl, %dl
movzbl %bl, %ebx
movzbl %dl, %edx
cmoval %edx, %ebx
movq %r13, %rsi
cmovaq %rbp, %rsi
testq %r13, %r13
movq %rsi, %r13
cmoveq %rbp, %r13
cmovel %edx, %ebx
cmpq %r14, %r12
je 0x4aa7
movl 0x1c(%r12), %edx
movq %rbp, %r15
jmp 0x493e
movq 0x8(%rsp), %r14
movl (%r14), %edi
leaq 0x20(%rsp), %rsi
movl $0x10, %edx
callq 0xbbac
movl 0x8(%rbp), %edi
leaq 0x40(%rsp), %rsi
movl $0x10, %edx
callq 0xbbac
movl 0x1c(%rbp), %r8d
movl $0x4, %edi
leaq 0xb7e6(%rip), %rsi      # 0x101a3
leaq 0x20(%rsp), %rdx
leaq 0x40(%rsp), %rcx
xorl %eax, %eax
callq 0xd88c
cmpq %rbp, 0x8(%r14)
movq 0x38(%rsp), %r14
jne 0x4a56
movl $0x4, %edi
leaq 0xb7ff(%rip), %rsi      # 0x101e4
xorl %eax, %eax
callq 0xd88c
movq 0x8(%rsp), %rdi
callq 0x78ff
movq 0x8(%rsp), %rcx
movl (%rcx), %edi
movq 0x8(%rcx), %rax
movl 0x8(%rax), %edx
movq 0x10(%rcx), %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 0x7cd4
movq 0x28(%rsp), %rcx
addq $0x20, %rsp
leaq 0x1288a(%rip), %rax     # 0x172c0
movq (%rax), %rax
testq %rax, %rax
je 0x4a4e
cmpq %rcx, 0x8(%rax)
jne 0x4a4e
callq 0xbbf4
movq 0x8(%rsp), %rcx
movq $0x0, 0x8(%rcx)
movq %r15, %rdi
movq %rbp, %rsi
movq %r14, %rdx
callq 0x5be3
movq 0x10(%rbp), %rdi
movl $0x580, %esi            # imm = 0x580
callq 0x628b
movq 0x20(%rbp), %rdi
movl $0x582, %esi            # imm = 0x582
callq 0x628b
movq %rbp, %rdi
movl $0x57e, %esi            # imm = 0x57E
callq 0x628b
leaq 0x127fc(%rip), %rax     # 0x17290
movl (%rax), %eax
xorl %ecx, %ecx
cmpq %r14, %r12
movl 0x4(%rsp), %edi
jne 0x4939
jmp 0x4aac
testb $0x1, %cl
jne 0x4ad4
testq %r13, %r13
movq 0x8(%rsp), %rdi
je 0x4ac4
movq 0x8(%rdi), %rax
testq %rax, %rax
je 0x4ac4
cmpb 0x19(%rax), %bl
jbe 0x4ad4
movq 0x38(%rdi), %rdx
movswl 0x40(%rdi), %ecx
movq %r13, %rsi
callq 0x3034
leaq 0x12855(%rip), %r13     # 0x17330
movq 0x10(%rsp), %rbp
leaq 0x12839(%rip), %r12     # 0x17320
jmp 0x4b59
movl (%rax), %edi
leaq 0x20(%rsp), %r14
movq %r14, %rsi
movl $0x10, %edx
callq 0xbbac
movl $0x3, %edi
leaq 0xb672(%rip), %rsi      # 0x1017b
movq %r14, %rdx
xorl %eax, %eax
callq 0xd88c
callq 0xb3f0
movl %eax, 0x18(%rbx)
movb $0x1, %al
movq %rax, 0x18(%rsp)
movq %r15, %rdi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x3034
movq 0x18(%r15), %rdi
movl $0x57b, %esi            # imm = 0x57B
callq 0x628b
movq 0x20(%r15), %rdi
movl $0x57c, %esi            # imm = 0x57C
callq 0x628b
movq %r15, %rdi
movl $0x57d, %esi            # imm = 0x57D
callq 0x628b
movq (%r13), %rdi
movq %rbp, %rsi
callq 0x6741
movq %rax, %rbp
testq %rax, %rax
jne 0x482c
cmpb $0x0, 0x18(%rsp)
sete %bl
jmp 0x4b7d
movb $0x1, %bl
movl 0x4(%rsp), %edx
leaq 0x12798(%rip), %r14     # 0x17320
movq (%r14), %r13
cmpq %r14, %r13
je 0x4be1
leaq 0x126f9(%rip), %rbp     # 0x17290
movq %r14, %r15
movq %r13, %r12
movq (%r13), %r13
movl 0x18(%r12), %eax
testl %eax, %eax
je 0x4bd9
movl (%rbp), %ecx
leal (%rax,%rcx,2), %eax
movl %edx, %ecx
subl %eax, %ecx
testl %ecx, %ecx
jle 0x4bd9
movq %r15, %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0x5be3
movq %r12, %rdi
movl $0x57e, %esi            # imm = 0x57E
callq 0x628b
movl 0x4(%rsp), %edx
jmp 0x4bdc
movq %r12, %r15
cmpq %r14, %r13
jne 0x4b9a
movl $0x6, %edi
callq 0x6bd8
testb %bl, %bl
jne 0x4bf4
callq 0x2d46
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq