// File: /workspace/github_c_makefile_star2_O3/TeaInside[P]teavpn/src/teavpn/teavpn_config_parser.c
// Function: teavpn_server_config_parser
bool teavpn_server_config_parser(char *internal_buf, server_config *config)
{
	uint16_t line = 1;
	char buffer[4096];
	bool ret = true, sp;
	size_t i, j, k, l, len;

	FILE *h = fopen(config->config_file, "r");

	if (h == NULL) {
		printf("Cannot open config file: %s\n", config->config_file);
		return false;
	}

	while (fgets(buffer, 4095, h)) {

		len = strlen(buffer);
		if (buffer[len - 1] != '\n') {
			buffer[len] = '\n';
			buffer[len + 1] = '\0';
		}
		i = j = k = l = 0;

		// ltrim key
		while ((buffer[i] == ' ') || (buffer[i] == '\t') || (buffer[i] == '\r')) i++;
		j = i;

		if ((buffer[i] == '#') || (buffer[i] == '\n')) continue;

		while (buffer[i] != '=') {
			if (buffer[i] == '\n') {
				printf("Parse error on line %d: couldn't find equal separator\n", line);
				ret = false;
				goto ret;
			}
			i++;
		}
		buffer[i] = '\0';
		k = i + 1;
		i--;

		sp = false;
		// rtrim key
		while ((buffer[i] == ' ') || (buffer[i] == '\t') || (buffer[i] == '\r')) {
			i--;
			sp = true;
		}

		if (sp) buffer[i + 1] = '\0';

		// ltrim value
		while ((buffer[k] == ' ') || (buffer[k] == '\t') || (buffer[k] == '\r')) k++;

		l = k;

		if (buffer[k] == '\n') {
			buffer[k] = '\0';
		} else {
			while (buffer[l] != '\n') {
				if (buffer[l] == '#') {
					len = l + 1;
					buffer[l] = '\n';
					break;
				}
				l++;
			}
			buffer[l] = '\0';
			l--;

			sp = false;
			// rtrim key
			while ((buffer[l] == ' ') || (buffer[l] == '\t') || (buffer[l] == '\r')) {
				l--;
				sp = true;
			}
			if (sp) buffer[l + 1] = '\0';
		}

		// printf("\"%s\" = \"%s\"\n", &(buffer[j]), &(buffer[k]));
		if (!strcmp(&(buffer[j]), "dev")) {
			strcpy(internal_buf, &(buffer[k]));
			config->dev = internal_buf;
			internal_buf += strlen(internal_buf) + 1;
		} else if (!strcmp(&(buffer[j]), "mtu")) {
			config->mtu = (uint16_t)atoi(&(buffer[k]));
		} else if (!strcmp(&(buffer[j]), "inet4")) {
			strcpy(internal_buf, &(buffer[k]));
			config->inet4 = internal_buf;
			internal_buf += strlen(internal_buf) + 1;
		} else if (!strcmp(&(buffer[j]), "inet4_bcmask")) {
			strcpy(internal_buf, &(buffer[k]));
			config->inet4_broadcast = internal_buf;
			internal_buf += strlen(internal_buf) + 1;
		} else if (!strcmp(&(buffer[j]), "bind_addr")) {
			strcpy(internal_buf, &(buffer[k]));
			config->bind_addr = internal_buf;
			internal_buf += strlen(internal_buf) + 1;
		} else if (!strcmp(&(buffer[j]), "bind_port")) {
			config->bind_port = (uint16_t)atoi(&(buffer[k]));
		} else if (!strcmp(&(buffer[j]), "threads")) {
			config->threads = (uint8_t)atoi(&(buffer[k]));
		} else if (!strcmp(&(buffer[j]), "data_dir")) {
			strcpy(internal_buf, &(buffer[k]));
			config->data_dir = internal_buf;
			internal_buf += strlen(internal_buf) + 1;
		} else {
			printf("Invalid config key \"%s\" on line %d\n", &(buffer[j]), line);
		}

		line++;
	}


	ret:
	fclose(h);

	return ret;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1028, %rsp           # imm = 0x1028
movq %rsi, %r12
movq %rdi, %r15
movq %fs:0x28, %rax
movq %rax, -0x30(%rbp)
movq 0x8(%rsi), %rdi
leaq 0x44cd(%rip), %rsi      # 0x708f
callq 0x2340
testq %rax, %rax
je 0x2f5a
movq %rax, %r14
leaq -0x1030(%rbp), %r13
movq %r13, %rdi
movl $0xfff, %esi            # imm = 0xFFF
movq %rax, %rdx
callq 0x21e0
movb $0x1, %bl
testq %rax, %rax
je 0x2f88
movq %r15, %rbx
movw $0x1, %ax
movl %eax, -0x1044(%rbp)
movabsq $0x100002200, %r15      # imm = 0x100002200
movq %r12, -0x1040(%rbp)
movq %r13, %rdi
callq 0x2100
cmpb $0xa, -0x1(%rax,%r13)
je 0x2c2d
movw $0xa, -0x1030(%rbp,%rax)
movl $0x1, %edx
leaq -0x102f(%rbp), %rax
leaq -0x102e(%rbp), %rcx
movq %r13, %rsi
xorl %edi, %edi
movzbl -0x1030(%rbp,%rdi), %r8d
cmpq $0x23, %r8
ja 0x2c95
btq %r8, %r15
jae 0x2c6b
incq %rdi
incq %rsi
incq %rdx
incq %rcx
incq %rax
jmp 0x2c45
movabsq $0x800000400, %r9       # imm = 0x800000400
btq %r8, %r9
jae 0x2c95
movq %r13, %rdi
movl $0xfff, %esi            # imm = 0xFFF
movq %r14, %rdx
callq 0x21e0
testq %rax, %rax
jne 0x2c13
jmp 0x2fb3
leaq (%rdi,%rbp), %r12
addq $-0x1030, %r12          # imm = 0xEFD0
cmpb $0x3d, %r8b
je 0x2ccd
movzbl %r8b, %r8d
cmpl $0xa, %r8d
je 0x2f71
movb -0x102f(%rbp,%rdi), %r8b
incq %rdi
incq %rsi
incq %rdx
incq %rcx
incq %rax
jmp 0x2ca0
movb $0x0, -0x1030(%rbp,%rdi)
xorl %edi, %edi
movzbl -0x1(%rsi), %r8d
cmpq $0x20, %r8
ja 0x2cf0
btq %r8, %r15
jae 0x2cf0
decq %rsi
movb $0x1, %dil
jmp 0x2cd7
testb $0x1, %dil
je 0x2d04
movb $0x0, (%rsi)
jmp 0x2d04
incq %rdx
incq %rcx
incq %rax
movb -0x1030(%rbp,%rdx), %sil
movzbl %sil, %edi
cmpl $0xc, %edi
jle 0x2d21
cmpl $0x20, %edi
je 0x2cfb
cmpl $0xd, %edi
je 0x2cfb
jmp 0x2d3e
cmpl $0x9, %edi
je 0x2cfb
cmpl $0xa, %edi
jne 0x2d3e
leaq (%rdx,%rbp), %rax
addq $-0x1030, %rax          # imm = 0xEFD0
movq %rax, -0x1038(%rbp)
jmp 0x2d8b
addq %rbp, %rdx
addq $-0x1030, %rdx          # imm = 0xEFD0
movq %rdx, -0x1038(%rbp)
cmpb $0xa, %sil
je 0x2d69
movzbl %sil, %edx
cmpl $0x23, %edx
je 0x2d69
movb (%rcx), %sil
incq %rcx
incq %rax
jmp 0x2d4f
movb $0x0, -0x1(%rcx)
xorl %ecx, %ecx
movzbl -0x1(%rax), %edx
cmpq $0x20, %rdx
ja 0x2d86
btq %rdx, %r15
jae 0x2d86
decq %rax
movb $0x1, %cl
jmp 0x2d6f
testb $0x1, %cl
je 0x2d8e
movb $0x0, (%rax)
movq %r12, %rdi
leaq 0x42d4(%rip), %rsi      # 0x706c
callq 0x2220
testl %eax, %eax
je 0x2e63
movq %r12, %rdi
leaq 0x42c1(%rip), %rsi      # 0x7070
callq 0x2220
testl %eax, %eax
je 0x2e80
movq %r12, %rdi
leaq 0x42ae(%rip), %rsi      # 0x7074
callq 0x2220
testl %eax, %eax
je 0x2e9b
movq %r12, %rdi
leaq 0x429d(%rip), %rsi      # 0x707a
callq 0x2220
testl %eax, %eax
je 0x2eb8
movq %r12, %rdi
leaq 0x4293(%rip), %rsi      # 0x7087
callq 0x2220
testl %eax, %eax
je 0x2ed5
movq %r12, %rdi
leaq 0x4286(%rip), %rsi      # 0x7091
callq 0x2220
testl %eax, %eax
je 0x2f08
movq %r12, %rdi
leaq 0x4279(%rip), %rsi      # 0x709b
callq 0x2220
testl %eax, %eax
je 0x2f23
movq %r12, %rdi
leaq 0x426a(%rip), %rsi      # 0x70a3
callq 0x2220
testl %eax, %eax
je 0x2f3d
movzwl -0x1044(%rbp), %edx
leaq 0x4258(%rip), %rdi      # 0x70ac
movq %r12, %rsi
xorl %eax, %eax
callq 0x2150
jmp 0x2efd
movq %rbx, %rdi
movq -0x1038(%rbp), %rsi
callq 0x2070
movq -0x1040(%rbp), %r12
movq %rbx, 0x20(%r12)
jmp 0x2eef
movq -0x1038(%rbp), %rdi
callq 0x2380
movq -0x1040(%rbp), %r12
movw %ax, 0x38(%r12)
jmp 0x2efd
movq %rbx, %rdi
movq -0x1038(%rbp), %rsi
callq 0x2070
movq -0x1040(%rbp), %r12
movq %rbx, 0x28(%r12)
jmp 0x2eef
movq %rbx, %rdi
movq -0x1038(%rbp), %rsi
callq 0x2070
movq -0x1040(%rbp), %r12
movq %rbx, 0x30(%r12)
jmp 0x2eef
movq %rbx, %rdi
movq -0x1038(%rbp), %rsi
callq 0x2070
movq -0x1040(%rbp), %r12
movq %rbx, (%r12)
movq %rbx, %rdi
callq 0x2100
addq %rax, %rbx
incq %rbx
incl -0x1044(%rbp)
jmp 0x2c7b
movq -0x1038(%rbp), %rdi
callq 0x2380
movq -0x1040(%rbp), %r12
movw %ax, 0x3a(%r12)
jmp 0x2efd
movq -0x1038(%rbp), %rdi
callq 0x2380
movq -0x1040(%rbp), %r12
movb %al, 0x3d(%r12)
jmp 0x2efd
movq %rbx, %rdi
movq -0x1038(%rbp), %rsi
callq 0x2070
movq -0x1040(%rbp), %r12
movq %rbx, 0x18(%r12)
jmp 0x2eef
movq 0x8(%r12), %rsi
leaq 0x40b2(%rip), %rdi      # 0x7018
xorl %ebx, %ebx
xorl %eax, %eax
callq 0x2150
jmp 0x2f90
movzwl -0x1044(%rbp), %esi
leaq 0x40b6(%rip), %rdi      # 0x7035
xorl %ebx, %ebx
xorl %eax, %eax
callq 0x2150
movq %r14, %rdi
callq 0x20f0
movq %fs:0x28, %rax
cmpq -0x30(%rbp), %rax
jne 0x2fb7
movl %ebx, %eax
addq $0x1028, %rsp           # imm = 0x1028
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movb $0x1, %bl
jmp 0x2f88
callq 0x2110