// File: /workspace/github_c_makefile_star2_O2/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)
pushq $0x6
popq %rdi
callq 0x63a8
xorl %r13d, %r13d
movq $0x0, 0x10(%rsp)
leaq 0x12de7(%rip), %rax     # 0x17330
movq (%rax), %rdi
movq %r13, %rsi
callq 0x5f8c
testq %rax, %rax
je 0x4863
movq %rax, %r13
movq 0x8(%rax), %rax
movq (%rax), %rbp
leaq 0x12d22(%rip), %rax     # 0x17290
movl (%rax), %eax
addl %eax, %eax
addl 0x30(%rbp), %eax
movl 0x4(%rsp), %ecx
subl %eax, %ecx
testl %ecx, %ecx
jg 0x4749
movq %r13, 0x20(%rsp)
leaq 0x48(%rbp), %r14
movq 0x48(%rbp), %r8
xorl %ebx, %ebx
movq %r14, 0x8(%rsp)
xorl %eax, %eax
xorl %ecx, %ecx
movb %cl, 0x3(%rsp)
movq %rbx, 0x18(%rsp)
movq %r8, %rbx
cmpq %r14, %rbx
je 0x4707
movl 0x1c(%rbx), %ecx
leaq 0x12cd6(%rip), %rdx     # 0x17290
addl (%rdx), %ecx
movq (%rbx), %r8
movl 0x4(%rsp), %edx
subl %ecx, %edx
testl %edx, %edx
jle 0x46ce
movq %r8, 0x28(%rsp)
movl (%rbp), %edi
leaq 0x30(%rsp), %r13
movq %r13, %rsi
movq %r14, %r15
pushq $0x10
popq %r14
movl %r14d, %edx
callq 0xb04f
movl 0x8(%rbx), %edi
leaq 0x40(%rsp), %r12
movq %r12, %rsi
movl %r14d, %edx
movq %r15, %r14
callq 0xb04f
movl 0x1c(%rbx), %r8d
pushq $0x4
popq %rdi
leaq 0xab93(%rip), %rsi      # 0xf1a3
movq %r13, %rdx
movq %r12, %rcx
xorl %eax, %eax
callq 0xcb1c
cmpq %rbx, 0x8(%rbp)
jne 0x4689
pushq $0x4
popq %rdi
leaq 0xabb7(%rip), %rsi      # 0xf1e4
xorl %eax, %eax
callq 0xcb1c
movq %rbp, %rdi
callq 0x70b9
movl (%rbp), %edi
movq 0x8(%rbp), %rax
movl 0x8(%rax), %edx
movq 0x10(%rbp), %rax
movl 0x20(%rax), %r8d
movq 0x8(%rax), %r9
subq $0x8, %rsp
pushq $0x20
popq %rsi
xorl %ecx, %ecx
pushq $0x1
pushq $0x0
pushq $0x42
callq 0x745c
addq $0x20, %rsp
leaq 0x12c4f(%rip), %rax     # 0x172c0
movq (%rax), %rax
testq %rax, %rax
je 0x4684
cmpq %rbp, 0x8(%rax)
jne 0x4684
callq 0xb095
andq $0x0, 0x8(%rbp)
movq 0x8(%rsp), %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x5763
movq 0x10(%rbx), %rdi
movl $0x580, %esi            # imm = 0x580
callq 0x5b1a
movq 0x20(%rbx), %rdi
movl $0x582, %esi            # imm = 0x582
callq 0x5b1a
movq %rbx, %rdi
movl $0x57e, %esi            # imm = 0x57E
callq 0x5b1a
movb $0x1, %al
movq 0x28(%rsp), %rbx
jmp 0x45a7
movb 0x19(%rbx), %cl
movq %rbx, 0x8(%rsp)
movq 0x18(%rsp), %rsi
testq %rsi, %rsi
je 0x459b
movb 0x3(%rsp), %dil
cmpb %dil, %cl
cmovaq %rbx, %rsi
movzbl %cl, %edx
movzbl %dil, %ecx
cmoval %edx, %ecx
movq %rbx, 0x8(%rsp)
movq %rsi, %rbx
jmp 0x459b
testb %al, %al
movq 0x20(%rsp), %r13
movq 0x18(%rsp), %rsi
je 0x4542
testq %rsi, %rsi
je 0x4734
movq 0x8(%rbp), %rax
testq %rax, %rax
je 0x4734
movb 0x3(%rsp), %cl
cmpb 0x19(%rax), %cl
jbe 0x4542
movq 0x38(%rbp), %rdx
movswl 0x40(%rbp), %ecx
movq %rbp, %rdi
callq 0x2f26
jmp 0x4542
movl (%rbp), %edi
leaq 0x30(%rsp), %rbx
movq %rbx, %rsi
pushq $0x10
popq %rdx
callq 0xb04f
movl 0x30(%rbp), %ecx
pushq $0x4
popq %rdi
leaq 0xa9df(%rip), %rsi      # 0xf148
movq %rbx, %rdx
xorl %eax, %eax
callq 0xcb1c
leaq 0x12bb6(%rip), %rax     # 0x17330
movq (%rax), %rdi
movq %r13, %rsi
callq 0x6241
leaq 0x48(%rbp), %rbx
movq 0x48(%rbp), %r14
cmpq %rbx, %r14
je 0x47d1
movq (%r14), %r12
movq %rbx, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x5763
movq 0x10(%r14), %rdi
movl $0x57f, %esi            # imm = 0x57F
callq 0x5b1a
movq 0x20(%r14), %rdi
movl $0x581, %esi            # imm = 0x581
callq 0x5b1a
movq %r14, %rdi
movl $0x579, %esi            # imm = 0x579
callq 0x5b1a
movq %r12, %r14
jmp 0x478d
leaq 0x12b48(%rip), %rcx     # 0x17320
movq %rcx, %rbx
movq (%rbx), %rbx
cmpq %rcx, %rbx
je 0x4827
cmpl $0x0, 0x18(%rbx)
jne 0x47db
movq 0x8(%rbx), %rax
cmpq %rbp, %rax
jne 0x47db
movl (%rax), %edi
leaq 0x30(%rsp), %r14
movq %r14, %rsi
pushq $0x10
popq %rdx
callq 0xb04f
pushq $0x3
popq %rdi
leaq 0xa96d(%rip), %rsi      # 0xf17b
movq %r14, %rdx
xorl %eax, %eax
callq 0xcb1c
callq 0xa9e4
movl %eax, 0x18(%rbx)
movb $0x1, %al
movq %rax, 0x10(%rsp)
movq %rbp, %rdi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x2f26
movq 0x18(%rbp), %rdi
movl $0x57b, %esi            # imm = 0x57B
callq 0x5b1a
movq 0x20(%rbp), %rdi
movl $0x57c, %esi            # imm = 0x57C
callq 0x5b1a
movq %rbp, %rdi
movl $0x57d, %esi            # imm = 0x57D
callq 0x5b1a
jmp 0x4542
leaq 0x12ab6(%rip), %rbx     # 0x17320
movq (%rbx), %r12
movq %rbx, %r14
movq %r12, %r15
cmpq %rbx, %r12
je 0x48bb
movq (%r15), %r12
movl 0x18(%r15), %eax
testl %eax, %eax
je 0x48b6
leaq 0x12a06(%rip), %rcx     # 0x17290
movl (%rcx), %ecx
leal (%rax,%rcx,2), %eax
movl 0x4(%rsp), %ecx
subl %eax, %ecx
testl %ecx, %ecx
jle 0x48b6
movq %r14, %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x5763
movq %r15, %rdi
movl $0x57e, %esi            # imm = 0x57E
callq 0x5b1a
jmp 0x4870
movq %r15, %r14
jmp 0x4870
pushq $0x6
popq %rdi
callq 0x63c4
cmpb $0x0, 0x10(%rsp)
je 0x48cf
callq 0x2bd6
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq