// File: /workspace/github_c_makefile_star2_O1/TeaInside[P]teavpn/src/teavpn/client/tcp.c
// Function: teavpn_tcp_client_init_iface
static bool teavpn_tcp_client_init_iface(client_config *config, struct teavpn_client_ip *ip)
{
	bool ret;
	char cmd[100],
		data[100],
		*escaped_dev,
		*escaped_inet4,
		*escaped_inet4_broadcast,
		*p, *q;
	FILE *fp = NULL;

	escaped_dev = escapeshellarg(config->dev);
	escaped_inet4 = escapeshellarg(ip->inet4);
	escaped_inet4_broadcast = escapeshellarg(ip->inet4_broadcast);

	/**
	 * Set interface up.
	 */
	sprintf(
		cmd,
		"/sbin/ip link set dev %s up mtu %d",
		escaped_dev,
		config->mtu
	);
	debug_log(1, "Executing: %s\n", cmd);
	if (system(cmd)) {
		ret = false;
		goto ret;
	}


	/**
	 * Assign private IP.
	 */
	sprintf(
		cmd,
		"/sbin/ip addr add dev %s %s broadcast %s",
		escaped_dev,
		escaped_inet4,
		escaped_inet4_broadcast
	);
	debug_log(0, "Executing: %s", cmd);
	if (system(cmd)) {
		ret = false;
		goto ret;
	}

	/**
	 * Get server route data.
	 */
	sprintf(cmd, "/sbin/ip route get %s", config->server_ip);
	debug_log(0, "Executing: %s", cmd);
	fp = popen(cmd, "r");
	q = fgets(data, 99, fp);
	pclose(fp);

	if (q == NULL) {
		debug_log(0, "Cannot get server route via");

		// Commented for debug only.
		ret = false;
		goto ret;
	}

	p = strstr(data, "via");
	if (p == NULL) {
		p = strstr(data, "src");
		if (p == NULL) {
			debug_log(0, "Cannot get server route via");

			// Commented for debug only.
			ret = false;
			goto ret;
		}
	}

	while ((*p) != ' ') p++;
	p++;
	q = p;
	while ((*q) != ' ') q++;
	*q = '\0';

	// Commented for debug only.
	sprintf(cmd, "/sbin/ip route add %s/32 via %s", config->server_ip, p);
	debug_log(1, "Executing: %s", cmd);
	if (system(cmd)) {
		debug_log(3, "Exit code is not zero");
	}

	sprintf(cmd, "/sbin/ip route add 0.0.0.0/1 via %s", "5.5.0.1");
	debug_log(0, "Executing: %s", cmd);

	if (system(cmd)) {
		debug_log(3, "Exit code is not zero");
		// ret = false;
		// goto ret;
	}

	sprintf(cmd, "/sbin/ip route add 128.0.0.0/1 via %s", "5.5.0.1");
	debug_log(0, "Executing: %s", cmd);

	if (system(cmd)) {
		debug_log(3, "Exit code is not zero");
		// ret = false;
		// goto ret;
	}

	ret = true;
ret:
	free(escaped_dev);
	free(escaped_inet4);
	free(escaped_inet4_broadcast);
	return ret;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf8, %rsp
movq %rsi, %r15
movq %rdi, %r12
movq %fs:0x28, %rax
movq %rax, -0x30(%rbp)
movq 0x18(%rdi), %rdi
callq 0x2a80
movq %rax, %rbx
movq %r15, %rdi
callq 0x2a80
movq %rax, %r14
addq $0x13, %r15
movq %r15, %rdi
callq 0x2a80
movq %rax, %r15
movzwl 0x20(%r12), %ecx
leaq 0x364d(%rip), %rsi      # 0x7958
leaq -0xa0(%rbp), %rdi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x2390
leaq 0x7101(%rip), %rax      # 0xb424
cmpb $0x0, (%rax)
je 0x433d
leaq 0x364c(%rip), %rdi      # 0x797b
leaq -0xa0(%rbp), %rsi
xorl %eax, %eax
callq 0x605c
leaq -0xa0(%rbp), %rdi
callq 0x2140
testl %eax, %eax
je 0x4355
xorl %r13d, %r13d
jmp 0x45ba
leaq 0x362e(%rip), %rsi      # 0x798a
xorl %r13d, %r13d
movq %r14, -0x118(%rbp)
leaq -0xa0(%rbp), %r14
movq %r14, %rdi
movq %rbx, %rdx
movq -0x118(%rbp), %rcx
movq %r15, %r8
xorl %eax, %eax
callq 0x2390
leaq 0x3628(%rip), %rdi      # 0x79b3
movq %r14, %rsi
xorl %eax, %eax
callq 0x605c
movq %r14, %rdi
movq -0x118(%rbp), %r14
callq 0x2140
testl %eax, %eax
jne 0x45ba
movq %r15, -0x120(%rbp)
movq (%r12), %rdx
leaq 0x3603(%rip), %rsi      # 0x79c1
xorl %r13d, %r13d
leaq -0xa0(%rbp), %r14
movq %r14, %rdi
xorl %eax, %eax
callq 0x2390
leaq 0x35da(%rip), %rdi      # 0x79b3
movq %r14, %rsi
xorl %eax, %eax
callq 0x605c
leaq 0x2ca5(%rip), %rsi      # 0x708f
movq %r14, %rdi
callq 0x2330
movq %rax, %r14
leaq -0x110(%rbp), %rdi
movl $0x63, %esi
movq %rax, %rdx
callq 0x21e0
movq %rax, %r15
movq %r14, %rdi
callq 0x2180
leaq 0x35bc(%rip), %rdi      # 0x79d7
testq %r15, %r15
je 0x444f
leaq 0x35c8(%rip), %rsi      # 0x79ef
leaq -0x110(%rbp), %rdi
callq 0x23e0
testq %rax, %rax
movq -0x118(%rbp), %r14
movq -0x120(%rbp), %r15
je 0x4462
leaq 0x6fd7(%rip), %r13      # 0xb424
jmp 0x4485
movq -0x118(%rbp), %r14
movq -0x120(%rbp), %r15
jmp 0x45b3
leaq 0x358a(%rip), %rsi      # 0x79f3
leaq -0x110(%rbp), %rdi
callq 0x23e0
testq %rax, %rax
leaq 0x6fa5(%rip), %r13      # 0xb424
je 0x45a9
cmpb $0x20, (%rax)
leaq 0x1(%rax), %rax
jne 0x4485
movq %rax, %rcx
cmpb $0x20, (%rcx)
leaq 0x1(%rcx), %rcx
jne 0x4491
movb $0x0, -0x1(%rcx)
movq (%r12), %rdx
leaq 0x354e(%rip), %rsi      # 0x79f7
leaq -0xa0(%rbp), %rdi
movq %rax, %rcx
xorl %eax, %eax
callq 0x2390
cmpb $0x0, (%r13)
je 0x44d6
leaq 0x34eb(%rip), %rdi      # 0x79b3
leaq -0xa0(%rbp), %rsi
xorl %eax, %eax
callq 0x605c
leaq -0xa0(%rbp), %rdi
callq 0x2140
testl %eax, %eax
je 0x44fb
cmpb $0x3, (%r13)
jb 0x44fb
leaq 0x3523(%rip), %rdi      # 0x7a17
xorl %eax, %eax
callq 0x605c
leaq 0x352b(%rip), %rsi      # 0x7a2d
leaq 0x3548(%rip), %rdx      # 0x7a51
leaq -0xa0(%rbp), %r14
movq %r14, %rdi
xorl %eax, %eax
callq 0x2390
leaq 0x3492(%rip), %rdi      # 0x79b3
movq %r14, %rsi
xorl %eax, %eax
callq 0x605c
movq %r14, %rdi
callq 0x2140
testl %eax, %eax
je 0x454c
cmpb $0x3, (%r13)
jb 0x454c
leaq 0x34d2(%rip), %rdi      # 0x7a17
xorl %eax, %eax
callq 0x605c
leaq 0x3506(%rip), %rsi      # 0x7a59
leaq 0x34f7(%rip), %rdx      # 0x7a51
movq %r14, %rdi
xorl %eax, %eax
callq 0x2390
leaq 0x3448(%rip), %rdi      # 0x79b3
movq %r14, %rsi
xorl %eax, %eax
callq 0x605c
movq %r14, %rdi
callq 0x2140
movb $0x1, %r13b
testl %eax, %eax
je 0x45a0
leaq 0x6e99(%rip), %rax      # 0xb424
cmpb $0x3, (%rax)
movq -0x118(%rbp), %r14
jb 0x45ba
leaq 0x3479(%rip), %rdi      # 0x7a17
jmp 0x45b3
movq -0x118(%rbp), %r14
jmp 0x45ba
xorl %r13d, %r13d
leaq 0x3424(%rip), %rdi      # 0x79d7
xorl %eax, %eax
callq 0x605c
movq %rbx, %rdi
callq 0x2030
movq %r14, %rdi
callq 0x2030
movq %r15, %rdi
callq 0x2030
movq %fs:0x28, %rax
cmpq -0x30(%rbp), %rax
jne 0x45f6
movl %r13d, %eax
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x2110
nop