// File: /workspace/github_c_makefile_star2_O0/open-mesh-mirror[P]batmand/batman.c
// Function: choose_gw
void choose_gw(void)
{
	struct list_head *pos;
	struct gw_node *gw_node, *tmp_curr_gw = NULL;
	uint8_t max_gw_class = 0, max_tq = 0;
	uint32_t current_time, max_gw_factor = 0, tmp_gw_factor = 0;
	int download_speed, upload_speed;
	char orig_str[ADDR_STR_LEN];
	prof_start( PROF_choose_gw );


	current_time = get_time_msec();
	if ((routing_class == 0) || ((routing_class < 4) && ((int64_t)(get_time_msec64() - (originator_interval * local_win_size)) < 0))) {

		prof_stop( PROF_choose_gw );
		return;

	}

	if ( list_empty( &gw_list ) ) {

		if ( curr_gateway != NULL ) {

			debug_output( 3, "Removing default route - no gateway in range\n" );

			del_default_route();

		}

		prof_stop( PROF_choose_gw );
		return;

	}

	list_for_each( pos, &gw_list ) {

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

		/* ignore this gateway if recent connection attempts were unsuccessful */
		/* if it is our only gateway retry immediately */
		if ((gw_node != (struct gw_node *)gw_list.next) || (gw_node->list.next != (struct list_head *)&gw_list)) {

			if ((int)(current_time - (gw_node->last_failure + 30000)) < 0)
				continue;

		}

		if ( gw_node->orig_node->router == NULL )
			continue;

		if ( gw_node->deleted )
			continue;

		switch ( routing_class ) {

			case 1: /* fast connection */
				get_gw_speeds( gw_node->orig_node->gwflags, &download_speed, &upload_speed );

				if (((tmp_gw_factor = (((gw_node->orig_node->router->tq_avg * 100 ) / local_win_size) *
								  ((gw_node->orig_node->router->tq_avg * 100) / local_win_size) *
								     (download_speed / 64))) > max_gw_factor) ||
								  ((tmp_gw_factor == max_gw_factor) && (gw_node->orig_node->router->tq_avg > max_tq)))
					tmp_curr_gw = gw_node;
				break;

			default: /* stable connection (use best statistic) */
				 /* fast-switch (use best statistic but change as soon as a better gateway appears) */
				 /* late-switch (use best statistic but change as soon as a better gateway appears which has $routing_class more tq points) */
				if (gw_node->orig_node->router->tq_avg > max_tq)
					tmp_curr_gw = gw_node;
				break;

		}

		if ( gw_node->orig_node->gwflags > max_gw_class )
			max_gw_class = gw_node->orig_node->gwflags;

		if (gw_node->orig_node->router->tq_avg > max_tq)
			max_tq = gw_node->orig_node->router->tq_avg;

		if ( tmp_gw_factor > max_gw_factor )
			max_gw_factor = tmp_gw_factor;

		if ( ( pref_gateway != 0 ) && ( pref_gateway == gw_node->orig_node->orig ) ) {

			tmp_curr_gw = gw_node;

			addr_to_string( tmp_curr_gw->orig_node->orig, orig_str, ADDR_STR_LEN );
			debug_output( 3, "Preferred gateway found: %s (gw_flags: %i, tq: %i, gw_product: %i)\n", orig_str, gw_node->orig_node->gwflags, gw_node->orig_node->router->tq_avg, tmp_gw_factor );

			break;

		}

	}


	if ( curr_gateway != tmp_curr_gw ) {

		if ( curr_gateway != NULL ) {

			if ( tmp_curr_gw != NULL )
				debug_output( 3, "Removing default route - better gateway found\n" );
			else
				debug_output( 3, "Removing default route - no gateway in range\n" );

			del_default_route();

		}

		curr_gateway = tmp_curr_gw;

		/* may be the last gateway is now gone */
		if ( ( curr_gateway != NULL ) && ( !is_aborted() ) ) {

			addr_to_string( curr_gateway->orig_node->orig, orig_str, ADDR_STR_LEN );
			debug_output( 3, "Adding default route to %s (gw_flags: %i, tq: %i, gw_product: %i)\n", orig_str, max_gw_class, max_tq, max_gw_factor );

			add_default_route();

		}

	}

	prof_stop( PROF_choose_gw );
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq $0x0, -0x18(%rbp)
movb $0x0, -0x19(%rbp)
movb $0x0, -0x1a(%rbp)
movl $0x0, -0x24(%rbp)
movl $0x0, -0x28(%rbp)
xorl %edi, %edi
callq 0x8760
callq 0xf260
movl %eax, -0x20(%rbp)
movzbl 0x186ae(%rip), %eax     # 0x1b35a
cmpl $0x0, %eax
je 0x2cdf
movzbl 0x186a2(%rip), %eax     # 0x1b35a
cmpl $0x4, %eax
jge 0x2ceb
callq 0xf350
movswl 0x185d9(%rip), %ecx     # 0x1b2a2
movzbl 0x185df(%rip), %edx     # 0x1b2af
imull %edx, %ecx
movslq %ecx, %rcx
subq %rcx, %rax
cmpq $0x0, %rax
jge 0x2ceb
xorl %edi, %edi
callq 0x8790
jmp 0x301f
leaq 0x186ce(%rip), %rdi     # 0x1b3c0
callq 0x6f40
cmpl $0x0, %eax
je 0x2d2a
cmpq $0x0, 0x1865c(%rip)     # 0x1b360
je 0x2d1e
movl $0x3, %edi
leaq 0x11c9d(%rip), %rsi     # 0x149af
movb $0x0, %al
callq 0x123f0
callq 0xfb30
xorl %edi, %edi
callq 0x8790
jmp 0x301f
movq 0x1868f(%rip), %rax     # 0x1b3c0
movq %rax, -0x8(%rbp)
leaq 0x18684(%rip), %rax     # 0x1b3c0
cmpq %rax, -0x8(%rbp)
je 0x2f65
movq -0x8(%rbp), %rax
movq %rax, -0x10(%rbp)
movq -0x10(%rbp), %rax
cmpq 0x18667(%rip), %rax     # 0x1b3c0
jne 0x2d6b
movq -0x10(%rbp), %rax
leaq 0x1865a(%rip), %rcx     # 0x1b3c0
cmpq %rcx, (%rax)
je 0x2d89
movl -0x20(%rbp), %eax
movq -0x10(%rbp), %rcx
movl 0x14(%rcx), %ecx
addl $0x7530, %ecx           # imm = 0x7530
subl %ecx, %eax
cmpl $0x0, %eax
jge 0x2d87
jmp 0x2f55
jmp 0x2d89
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
cmpq $0x0, 0x8(%rax)
jne 0x2d9d
jmp 0x2f55
movq -0x10(%rbp), %rax
cmpl $0x0, 0x18(%rax)
je 0x2dac
jmp 0x2f55
movb 0x185a8(%rip), %al      # 0x1b35a
subb $0x1, %al
jne 0x2e62
jmp 0x2dbc
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
leaq -0x2c(%rbp), %rsi
leaq -0x30(%rbp), %rdx
movzbl 0x34(%rax), %edi
callq 0x3030
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x8(%rax), %rax
movzbl 0x19(%rax), %eax
imull $0x64, %eax, %eax
movzbl 0x184c0(%rip), %ecx     # 0x1b2af
cltd
idivl %ecx
movl %eax, -0x48(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x8(%rax), %rax
movzbl 0x19(%rax), %eax
imull $0x64, %eax, %eax
movzbl 0x184a0(%rip), %ecx     # 0x1b2af
cltd
idivl %ecx
movl %eax, %ecx
movl -0x48(%rbp), %eax
imull %ecx, %eax
movl %eax, -0x44(%rbp)
movl -0x2c(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movl -0x44(%rbp), %eax
imull %ecx, %eax
movl %eax, -0x28(%rbp)
cmpl -0x24(%rbp), %eax
ja 0x2e58
movl -0x28(%rbp), %eax
cmpl -0x24(%rbp), %eax
jne 0x2e60
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x8(%rax), %rax
movzbl 0x19(%rax), %eax
movzbl -0x1a(%rbp), %ecx
cmpl %ecx, %eax
jle 0x2e60
movq -0x10(%rbp), %rax
movq %rax, -0x18(%rbp)
jmp 0x2e84
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x8(%rax), %rax
movzbl 0x19(%rax), %eax
movzbl -0x1a(%rbp), %ecx
cmpl %ecx, %eax
jle 0x2e82
movq -0x10(%rbp), %rax
movq %rax, -0x18(%rbp)
jmp 0x2e84
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movzbl 0x34(%rax), %eax
movzbl -0x19(%rbp), %ecx
cmpl %ecx, %eax
jle 0x2ea6
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movb 0x34(%rax), %al
movb %al, -0x19(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x8(%rax), %rax
movzbl 0x19(%rax), %eax
movzbl -0x1a(%rbp), %ecx
cmpl %ecx, %eax
jle 0x2ed0
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x8(%rax), %rax
movb 0x19(%rax), %al
movb %al, -0x1a(%rbp)
movl -0x28(%rbp), %eax
cmpl -0x24(%rbp), %eax
jbe 0x2ede
movl -0x28(%rbp), %eax
movl %eax, -0x24(%rbp)
cmpl $0x0, 0x1848b(%rip)     # 0x1b370
je 0x2f53
movl 0x18483(%rip), %eax     # 0x1b370
movq -0x10(%rbp), %rcx
movq 0x8(%rcx), %rcx
cmpl (%rcx), %eax
jne 0x2f53
movq -0x10(%rbp), %rax
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movl (%rax), %edi
leaq -0x40(%rbp), %rsi
movl $0x10, %edx
callq 0xfa90
leaq -0x40(%rbp), %rdx
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movzbl 0x34(%rax), %ecx
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x8(%rax), %rax
movzbl 0x19(%rax), %r8d
movl -0x28(%rbp), %r9d
movl $0x3, %edi
leaq 0x11a93(%rip), %rsi     # 0x149dd
movb $0x0, %al
callq 0x123f0
jmp 0x2f65
jmp 0x2f55
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x8(%rbp)
jmp 0x2d35
movq 0x183f4(%rip), %rax     # 0x1b360
cmpq -0x18(%rbp), %rax
je 0x3018
cmpq $0x0, 0x183e2(%rip)     # 0x1b360
je 0x2fb4
cmpq $0x0, -0x18(%rbp)
je 0x2f9c
movl $0x3, %edi
leaq 0x11a8e(%rip), %rsi     # 0x14a21
movb $0x0, %al
callq 0x123f0
jmp 0x2faf
movl $0x3, %edi
leaq 0x11a07(%rip), %rsi     # 0x149af
movb $0x0, %al
callq 0x123f0
callq 0xfb30
movq -0x18(%rbp), %rax
movq %rax, 0x183a1(%rip)     # 0x1b360
cmpq $0x0, 0x18399(%rip)     # 0x1b360
je 0x3016
callq 0xfaf0
cmpb $0x0, %al
jne 0x3016
movq 0x18387(%rip), %rax     # 0x1b360
movq 0x8(%rax), %rax
movl (%rax), %edi
leaq -0x40(%rbp), %rsi
movl $0x10, %edx
callq 0xfa90
leaq -0x40(%rbp), %rdx
movzbl -0x19(%rbp), %ecx
movzbl -0x1a(%rbp), %r8d
movl -0x24(%rbp), %r9d
movl $0x3, %edi
leaq 0x11a46(%rip), %rsi     # 0x14a50
movb $0x0, %al
callq 0x123f0
callq 0xfb50
jmp 0x3018
xorl %edi, %edi
callq 0x8790
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)