// File: /workspace/github_c_makefile_star2_O0/TeaInside[P]teavpn/src/teavpn/server/tcp.c
// Function: teavpn_tcp_server_init
static uint8_t teavpn_tcp_server_init(char *config_buffer, server_config *config)
{
	// Initialize buffer channel value.
	for (register uint16_t i = 0; i < BUFCHAN_ALLOC; ++i) {
		bufchan[i].ref_count = 0;
	}

	// Initialize queues.
	for (register uint16_t  i = 0; i < QUEUE_AMOUNT; ++i) {
		queue_zero(i);
	}

	// Initialize connection entry value.
	for (register uint16_t i = 0; i < CONNECTION_ALLOC; ++i) {
		connection_zero(i);
	}

	// Load config file.
	if (config->config_file != NULL) {
		if (!teavpn_server_config_parser(config_buffer, config)) {
			debug_log(0, "Config error!");
			return 1;
		}
	}

	// Set verbose_level (global var).
	verbose_level = config->verbose_level;

	// data_dir is a directory that saves TeaVPN data
	// such as user, password, etc.
	if (config->data_dir == NULL) {
		debug_log(0, "Data dir cannot be empty!");
		return 1;
	}

	/**
	 * This pipe is purposed to interrupt main
	 * process when a new connection is made.
	 */
	if (pipe(m_pipe_fd) < 0) {
		debug_log(0, "Cannot open pipe for m_pipe_fd");
		perror("Cannot open pipe for m_pipe_fd");
		return 1;
	}

	/**
	 * Create TUN/TAP interface.
	 */
	debug_log(2, "Allocating TUN/TAP interface...");
	if ((tap_fd = tun_alloc(config->dev, IFF_TUN)) < 0) {
		debug_log(0, "Error connecting to TUN/TAP interface \"%s\"!", config->dev);
		close(m_pipe_fd[0]);
		close(m_pipe_fd[1]);
		return 1;
	}
	debug_log(0, "Successfully created a new interface \"%s\".", config->dev);

	/**
	 * Initialize TUN/TAP interface.
	 */
	if (!teavpn_tcp_server_init_iface(config)) {
		debug_log(0, "Cannot init interface");
		close(m_pipe_fd[0]);
		close(m_pipe_fd[1]);
		close(tap_fd);
		return 1;
	}

	/**
	 * Create TCP socket.
	 */
	debug_log(1, "Creating TCP socket...");
	if ((net_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
		debug_log(0, "Cannot create TCP socket");
		perror("Socket creation failed");
		close(m_pipe_fd[0]);
		close(m_pipe_fd[1]);
		close(tap_fd);
		return 1;
	}
	debug_log(1, "TCP socket created successfully");

	/**
	 * Setting up socket.
	 */
	debug_log(1, "Setting up socket file descriptor...");
	if (!teavpn_tcp_server_socket_setup(net_fd)) {
		debug_log(0, "Cannot setup socket");
		close(m_pipe_fd[0]);
		close(m_pipe_fd[1]);
		close(net_fd);
		close(tap_fd);
		return 1;
	}
	debug_log(1, "Socket file descriptor set up successfully");

	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x20, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movw $0x0, -0x1a(%rbp)
movzwl -0x1a(%rbp), %eax
cmpl $0x18, %eax
jge 0x65d7
movq 0x7dba(%rip), %rax      # 0xe370
movzwl -0x1a(%rbp), %ecx
imulq $0x1030, %rcx, %rcx     # imm = 0x1030
addq %rcx, %rax
movw $0x0, (%rax)
movw -0x1a(%rbp), %ax
addw $0x1, %ax
movw %ax, -0x1a(%rbp)
jmp 0x65a6
movw $0x0, -0x1c(%rbp)
movzwl -0x1c(%rbp), %eax
cmpl $0x30, %eax
jge 0x65fd
movzwl -0x1c(%rbp), %edi
callq 0x7f60
movw -0x1c(%rbp), %ax
addw $0x1, %ax
movw %ax, -0x1c(%rbp)
jmp 0x65dd
movw $0x0, -0x1e(%rbp)
movzwl -0x1e(%rbp), %eax
cmpl $0x18, %eax
jge 0x6623
movzwl -0x1e(%rbp), %edi
callq 0x7cf0
movw -0x1e(%rbp), %ax
addw $0x1, %ax
movw %ax, -0x1e(%rbp)
jmp 0x6603
movq -0x18(%rbp), %rax
cmpq $0x0, 0x8(%rax)
je 0x6668
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x2840
testb $0x1, %al
jne 0x6666
leaq 0x7dde(%rip), %rax      # 0xe424
movzbl (%rax), %ecx
xorl %eax, %eax
cmpl %ecx, %eax
jg 0x665d
leaq 0x40f8(%rip), %rdi      # 0xa74e
movb $0x0, %al
callq 0x84c0
movb $0x1, -0x1(%rbp)
jmp 0x6943
jmp 0x6668
movq -0x18(%rbp), %rax
movb 0x3c(%rax), %cl
leaq 0x7dae(%rip), %rax      # 0xe424
movb %cl, (%rax)
movq -0x18(%rbp), %rax
cmpq $0x0, 0x18(%rax)
jne 0x66aa
leaq 0x7d9a(%rip), %rax      # 0xe424
movzbl (%rax), %ecx
xorl %eax, %eax
cmpl %ecx, %eax
jg 0x66a1
leaq 0x4b04(%rip), %rdi      # 0xb19e
movb $0x0, %al
callq 0x84c0
movb $0x1, -0x1(%rbp)
jmp 0x6943
leaq 0x7ce7(%rip), %rdi      # 0xe398
callq 0x21c0
cmpl $0x0, %eax
jge 0x66ee
leaq 0x7d62(%rip), %rax      # 0xe424
movzbl (%rax), %ecx
xorl %eax, %eax
cmpl %ecx, %eax
jg 0x66d9
leaq 0x4ae6(%rip), %rdi      # 0xb1b8
movb $0x0, %al
callq 0x84c0
leaq 0x4ad8(%rip), %rdi      # 0xb1b8
callq 0x2360
movb $0x1, -0x1(%rbp)
jmp 0x6943
leaq 0x7d2f(%rip), %rax      # 0xe424
movzbl (%rax), %ecx
movl $0x2, %eax
cmpl %ecx, %eax
jg 0x670f
leaq 0x40d3(%rip), %rdi      # 0xa7db
movb $0x0, %al
callq 0x84c0
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rdi
movl $0x1, %esi
callq 0x8640
movl %eax, 0x7c6d(%rip)      # 0xe394
cmpl $0x0, %eax
jge 0x6771
leaq 0x7cf1(%rip), %rax      # 0xe424
movzbl (%rax), %ecx
xorl %eax, %eax
cmpl %ecx, %eax
jg 0x6752
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rsi
leaq 0x4a8c(%rip), %rdi      # 0xb1d7
movb $0x0, %al
callq 0x84c0
movl 0x7c40(%rip), %edi      # 0xe398
callq 0x21b0
movl 0x7c39(%rip), %edi      # 0xe39c
callq 0x21b0
movb $0x1, -0x1(%rbp)
jmp 0x6943
leaq 0x7cac(%rip), %rax      # 0xe424
movzbl (%rax), %ecx
xorl %eax, %eax
cmpl %ecx, %eax
jg 0x6797
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rsi
leaq 0x4095(%rip), %rdi      # 0xa825
movb $0x0, %al
callq 0x84c0
movq -0x18(%rbp), %rdi
callq 0x7fd0
testb $0x1, %al
jne 0x67ec
leaq 0x7c79(%rip), %rax      # 0xe424
movzbl (%rax), %ecx
xorl %eax, %eax
cmpl %ecx, %eax
jg 0x67c2
leaq 0x4a48(%rip), %rdi      # 0xb203
movb $0x0, %al
callq 0x84c0
movl 0x7bd0(%rip), %edi      # 0xe398
callq 0x21b0
movl 0x7bc9(%rip), %edi      # 0xe39c
callq 0x21b0
movl 0x7bb6(%rip), %edi      # 0xe394
callq 0x21b0
movb $0x1, -0x1(%rbp)
jmp 0x6943
leaq 0x7c31(%rip), %rax      # 0xe424
movzbl (%rax), %ecx
movl $0x1, %eax
cmpl %ecx, %eax
jg 0x680d
leaq 0x404a(%rip), %rdi      # 0xa850
movb $0x0, %al
callq 0x84c0
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x2420
movl %eax, 0x7b6c(%rip)      # 0xe390
cmpl $0x0, %eax
jge 0x687d
leaq 0x7bf4(%rip), %rax      # 0xe424
movzbl (%rax), %ecx
xorl %eax, %eax
cmpl %ecx, %eax
jg 0x6847
leaq 0x49d9(%rip), %rdi      # 0xb219
movb $0x0, %al
callq 0x84c0
leaq 0x4019(%rip), %rdi      # 0xa867
callq 0x2360
movl 0x7b3f(%rip), %edi      # 0xe398
callq 0x21b0
movl 0x7b38(%rip), %edi      # 0xe39c
callq 0x21b0
movl 0x7b25(%rip), %edi      # 0xe394
callq 0x21b0
movb $0x1, -0x1(%rbp)
jmp 0x6943
leaq 0x7ba0(%rip), %rax      # 0xe424
movzbl (%rax), %ecx
movl $0x1, %eax
cmpl %ecx, %eax
jg 0x689e
leaq 0x3fe7(%rip), %rdi      # 0xa87e
movb $0x0, %al
callq 0x84c0
leaq 0x7b7f(%rip), %rax      # 0xe424
movzbl (%rax), %ecx
movl $0x1, %eax
cmpl %ecx, %eax
jg 0x68bf
leaq 0x497a(%rip), %rdi      # 0xb232
movb $0x0, %al
callq 0x84c0
movl 0x7acb(%rip), %edi      # 0xe390
callq 0x8160
testb $0x1, %al
jne 0x691e
leaq 0x7b4f(%rip), %rax      # 0xe424
movzbl (%rax), %ecx
xorl %eax, %eax
cmpl %ecx, %eax
jg 0x68ec
leaq 0x4972(%rip), %rdi      # 0xb257
movb $0x0, %al
callq 0x84c0
movl 0x7aa6(%rip), %edi      # 0xe398
callq 0x21b0
movl 0x7a9f(%rip), %edi      # 0xe39c
callq 0x21b0
movl 0x7a88(%rip), %edi      # 0xe390
callq 0x21b0
movl 0x7a81(%rip), %edi      # 0xe394
callq 0x21b0
movb $0x1, -0x1(%rbp)
jmp 0x6943
leaq 0x7aff(%rip), %rax      # 0xe424
movzbl (%rax), %ecx
movl $0x1, %eax
cmpl %ecx, %eax
jg 0x693f
leaq 0x4933(%rip), %rdi      # 0xb26b
movb $0x0, %al
callq 0x84c0
movb $0x0, -0x1(%rbp)
movb -0x1(%rbp), %al
addq $0x20, %rsp
popq %rbp
retq
nopl (%rax)