// File: /workspace/github_c_makefile_star2_O1/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, %r13
movq %rdi, %r14
movq %fs:0x28, %rax
movq %rax, -0x30(%rbp)
movq 0x8(%rsi), %rdi
leaq 0x44cd(%rip), %rsi      # 0x708f
callq 0x2340
testq %rax, %rax
je 0x2f68
movq %rax, %rbx
leaq -0x1030(%rbp), %r12
movq %r12, %rdi
movl $0xfff, %esi            # imm = 0xFFF
movq %rax, %rdx
callq 0x21e0
testq %rax, %rax
sete %r15b
je 0x2f94
movq %r14, -0x1038(%rbp)
movw $0x1, %ax
movl %eax, -0x104c(%rbp)
movabsq $0x100002200, %r14      # imm = 0x100002200
movq %r13, -0x1048(%rbp)
movq %r12, %rdi
callq 0x2100
cmpb $0xa, -0x1(%rax,%r12)
je 0x2c33
movw $0xa, -0x1030(%rbp,%rax)
movl $0x1, %ecx
leaq -0x102f(%rbp), %rax
movq %r12, %rdx
xorl %esi, %esi
movzbl -0x1030(%rbp,%rsi), %edi
cmpq $0x23, %rdi
ja 0x2c94
btq %rdi, %r14
jae 0x2c66
incq %rsi
incq %rdx
incq %rcx
incq %rax
jmp 0x2c44
movabsq $0x800000400, %r8       # imm = 0x800000400
btq %rdi, %r8
jae 0x2c94
movq %r12, %rdi
movl $0xfff, %esi            # imm = 0xFFF
movq %rbx, %rdx
callq 0x21e0
testq %rax, %rax
sete %r15b
jne 0x2c19
jmp 0x2f94
leaq (%rsi,%rbp), %r13
addq $-0x1030, %r13          # imm = 0xEFD0
movzbl -0x1030(%rbp,%rsi), %edi
cmpl $0x3d, %edi
je 0x2cc3
cmpl $0xa, %edi
je 0x2f7f
incq %rsi
incq %rdx
incq %rcx
incq %rax
jmp 0x2c9f
movb $0x0, -0x1030(%rbp,%rsi)
xorl %esi, %esi
movzbl -0x1(%rdx), %edi
cmpq $0x20, %rdi
ja 0x2ce5
btq %rdi, %r14
jae 0x2ce5
decq %rdx
movb $0x1, %sil
jmp 0x2ccd
testb $0x1, %sil
je 0x2cf6
movb $0x0, (%rdx)
jmp 0x2cf6
incq %rcx
incq %rax
movzbl -0x1030(%rbp,%rcx), %edx
cmpl $0xc, %edx
jle 0x2d0f
cmpl $0x20, %edx
je 0x2cf0
cmpl $0xd, %edx
je 0x2cf0
jmp 0x2d2c
cmpl $0x9, %edx
je 0x2cf0
cmpl $0xa, %edx
jne 0x2d2c
leaq (%rcx,%rbp), %rax
addq $-0x1030, %rax          # imm = 0xEFD0
movq %rax, -0x1040(%rbp)
jmp 0x2d73
addq %rbp, %rcx
addq $-0x1030, %rcx          # imm = 0xEFD0
movq %rcx, -0x1040(%rbp)
movzbl (%rax), %ecx
cmpl $0xa, %ecx
je 0x2d52
cmpl $0x23, %ecx
je 0x2d4f
incq %rax
jmp 0x2d3d
movb $0xa, (%rax)
movb $0x0, (%rax)
xorl %ecx, %ecx
movzbl -0x1(%rax), %edx
cmpq $0x20, %rdx
ja 0x2d6e
btq %rdx, %r14
jae 0x2d6e
decq %rax
movb $0x1, %cl
jmp 0x2d57
testb $0x1, %cl
je 0x2d76
movb $0x0, (%rax)
movq %r13, %rdi
leaq 0x42ec(%rip), %rsi      # 0x706c
callq 0x2220
testl %eax, %eax
je 0x2e4b
movq %r13, %rdi
leaq 0x42d9(%rip), %rsi      # 0x7070
callq 0x2220
testl %eax, %eax
je 0x2e71
movq %r13, %rdi
leaq 0x42c6(%rip), %rsi      # 0x7074
callq 0x2220
testl %eax, %eax
je 0x2e8b
movq %r13, %rdi
leaq 0x42b5(%rip), %rsi      # 0x707a
callq 0x2220
testl %eax, %eax
je 0x2eae
movq %r13, %rdi
leaq 0x42ab(%rip), %rsi      # 0x7087
callq 0x2220
testl %eax, %eax
je 0x2ed1
movq %r13, %rdi
leaq 0x429e(%rip), %rsi      # 0x7091
callq 0x2220
testl %eax, %eax
je 0x2f12
movq %r13, %rdi
leaq 0x4291(%rip), %rsi      # 0x709b
callq 0x2220
testl %eax, %eax
je 0x2f2c
movq %r13, %rdi
leaq 0x4282(%rip), %rsi      # 0x70a3
callq 0x2220
testl %eax, %eax
je 0x2f45
movzwl -0x104c(%rbp), %edx
leaq 0x4270(%rip), %rdi      # 0x70ac
movq %r13, %rsi
xorl %eax, %eax
callq 0x2150
jmp 0x2f07
movq -0x1038(%rbp), %r15
movq %r15, %rdi
movq -0x1040(%rbp), %rsi
callq 0x2070
movq -0x1048(%rbp), %r13
movq %r15, 0x20(%r13)
jmp 0x2ef2
movq -0x1040(%rbp), %rdi
callq 0x2380
movq -0x1048(%rbp), %r13
movw %ax, 0x38(%r13)
jmp 0x2f07
movq -0x1038(%rbp), %r15
movq %r15, %rdi
movq -0x1040(%rbp), %rsi
callq 0x2070
movq -0x1048(%rbp), %r13
movq %r15, 0x28(%r13)
jmp 0x2ef2
movq -0x1038(%rbp), %r15
movq %r15, %rdi
movq -0x1040(%rbp), %rsi
callq 0x2070
movq -0x1048(%rbp), %r13
movq %r15, 0x30(%r13)
jmp 0x2ef2
movq -0x1038(%rbp), %r15
movq %r15, %rdi
movq -0x1040(%rbp), %rsi
callq 0x2070
movq -0x1048(%rbp), %r13
movq %r15, (%r13)
movq %r15, %rdi
callq 0x2100
addq %rax, %r15
incq %r15
movq %r15, -0x1038(%rbp)
incl -0x104c(%rbp)
jmp 0x2c76
movq -0x1040(%rbp), %rdi
callq 0x2380
movq -0x1048(%rbp), %r13
movw %ax, 0x3a(%r13)
jmp 0x2f07
movq -0x1040(%rbp), %rdi
callq 0x2380
movq -0x1048(%rbp), %r13
movb %al, 0x3d(%r13)
jmp 0x2f07
movq -0x1038(%rbp), %r15
movq %r15, %rdi
movq -0x1040(%rbp), %rsi
callq 0x2070
movq -0x1048(%rbp), %r13
movq %r15, 0x18(%r13)
jmp 0x2ef2
movq 0x8(%r13), %rsi
leaq 0x40a5(%rip), %rdi      # 0x7018
xorl %r15d, %r15d
xorl %eax, %eax
callq 0x2150
jmp 0x2f9c
movzwl -0x104c(%rbp), %esi
leaq 0x40a8(%rip), %rdi      # 0x7035
xorl %eax, %eax
callq 0x2150
movq %rbx, %rdi
callq 0x20f0
movq %fs:0x28, %rax
cmpq -0x30(%rbp), %rax
jne 0x2fc4
andb $0x1, %r15b
movl %r15d, %eax
addq $0x1028, %rsp           # imm = 0x1028
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x2110