// File: /workspace/github_c_makefile_star2_O2/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, %rbx
movq %rdi, %r14
movq %fs:0x28, %rax
movq %rax, -0x30(%rbp)
movq 0x8(%rsi), %rdi
leaq 0x49b5(%rip), %rsi      # 0x708f
callq 0x2340
testq %rax, %rax
je 0x2a9f
movq %rax, %r15
movq %r14, -0x1038(%rbp)
movq %rbx, -0x1048(%rbp)
movw $0x1, %r13w
leaq -0x1030(%rbp), %r12
movabsq $0x100002200, %rbx      # imm = 0x100002200
movq %rax, -0x1050(%rbp)
movq %r12, %rdi
movl $0xfff, %esi            # imm = 0xFFF
movq %r15, %rdx
callq 0x21e0
testq %rax, %rax
sete %r14b
je 0x2a95
movq %r12, %rdi
callq 0x2100
cmpb $0xa, -0x1(%rax,%r12)
je 0x274d
movw $0xa, -0x1030(%rbp,%rax)
leaq -0x102f(%rbp), %rax
leaq -0x102e(%rbp), %rcx
pushq $0x1
popq %rdx
movq %r12, %rsi
xorl %edi, %edi
movzbl -0x1030(%rbp,%rdi), %r8d
cmpq $0x23, %r8
ja 0x279d
btq %r8, %rbx
jae 0x2789
incq %rdi
incq %rsi
incq %rdx
incq %rcx
incq %rax
jmp 0x2763
movabsq $0x800000400, %r9       # imm = 0x800000400
btq %r8, %r9
jb 0x2716
leaq (%rdi,%rbp), %r9
addq $-0x1030, %r9           # imm = 0xEFD0
movzwl %r13w, %r15d
cmpb $0x3d, %r8b
je 0x27d9
movzbl %r8b, %r8d
cmpl $0xa, %r8d
je 0x2a7d
movb -0x102f(%rbp,%rdi), %r8b
incq %rdi
incq %rsi
incq %rdx
incq %rcx
incq %rax
jmp 0x27a8
movb $0x0, -0x1030(%rbp,%rdi)
xorl %edi, %edi
movq %r9, %r14
movzbl -0x1(%rsi), %r8d
cmpq $0x20, %r8
ja 0x27ff
btq %r8, %rbx
jae 0x27ff
decq %rsi
movb $0x1, %dil
jmp 0x27e6
testb $0x1, %dil
je 0x2813
movb $0x0, (%rsi)
jmp 0x2813
incq %rdx
incq %rcx
incq %rax
movzbl -0x1030(%rbp,%rdx), %esi
cmpl $0x9, %esi
je 0x280a
cmpl $0xd, %esi
je 0x280a
cmpl $0x20, %esi
je 0x280a
cmpl $0xa, %esi
jne 0x2842
leaq (%rdx,%rbp), %rax
addq $-0x1030, %rax          # imm = 0xEFD0
movq %rax, -0x1040(%rbp)
jmp 0x288f
addq %rbp, %rdx
addq $-0x1030, %rdx          # imm = 0xEFD0
movq %rdx, -0x1040(%rbp)
cmpb $0xa, %sil
je 0x286d
movzbl %sil, %edx
cmpl $0x23, %edx
je 0x286d
movb (%rcx), %sil
incq %rcx
incq %rax
jmp 0x2853
movb $0x0, -0x1(%rcx)
xorl %ecx, %ecx
movzbl -0x1(%rax), %edx
cmpq $0x20, %rdx
ja 0x288a
btq %rdx, %rbx
jae 0x288a
decq %rax
movb $0x1, %cl
jmp 0x2873
testb $0x1, %cl
je 0x2892
movb $0x0, (%rax)
movq %r14, %rdi
leaq 0x47d0(%rip), %rsi      # 0x706c
callq 0x2220
testl %eax, %eax
je 0x2963
movq %r14, %rdi
leaq 0x47bd(%rip), %rsi      # 0x7070
callq 0x2220
testl %eax, %eax
je 0x2986
movq %r14, %rdi
leaq 0x47aa(%rip), %rsi      # 0x7074
callq 0x2220
testl %eax, %eax
je 0x299f
movq %r14, %rdi
leaq 0x4799(%rip), %rsi      # 0x707a
callq 0x2220
testl %eax, %eax
je 0x29c2
movq %r14, %rdi
leaq 0x478f(%rip), %rsi      # 0x7087
callq 0x2220
testl %eax, %eax
je 0x29e5
movq %r14, %rdi
leaq 0x4782(%rip), %rsi      # 0x7091
callq 0x2220
testl %eax, %eax
je 0x2a29
movq %r14, %rdi
leaq 0x4775(%rip), %rsi      # 0x709b
callq 0x2220
testl %eax, %eax
je 0x2a42
movq %r14, %rdi
leaq 0x4766(%rip), %rsi      # 0x70a3
callq 0x2220
testl %eax, %eax
je 0x2a5a
leaq 0x475b(%rip), %rdi      # 0x70ac
movq %r14, %rsi
movl %r15d, %edx
xorl %eax, %eax
callq 0x2150
jmp 0x2a1a
movq -0x1038(%rbp), %r14
movq %r14, %rdi
movq -0x1040(%rbp), %rsi
callq 0x2070
movq -0x1048(%rbp), %rax
movq %r14, 0x20(%rax)
jmp 0x2a05
movq -0x1040(%rbp), %rdi
callq 0x2380
movq -0x1048(%rbp), %rcx
movw %ax, 0x38(%rcx)
jmp 0x2a1a
movq -0x1038(%rbp), %r14
movq %r14, %rdi
movq -0x1040(%rbp), %rsi
callq 0x2070
movq -0x1048(%rbp), %rax
movq %r14, 0x28(%rax)
jmp 0x2a05
movq -0x1038(%rbp), %r14
movq %r14, %rdi
movq -0x1040(%rbp), %rsi
callq 0x2070
movq -0x1048(%rbp), %rax
movq %r14, 0x30(%rax)
jmp 0x2a05
movq -0x1038(%rbp), %r14
movq %r14, %rdi
movq -0x1040(%rbp), %rsi
callq 0x2070
movq -0x1048(%rbp), %rax
movq %r14, (%rax)
movq %r14, %rdi
callq 0x2100
addq %rax, %r14
incq %r14
movq %r14, -0x1038(%rbp)
incl %r13d
movq -0x1050(%rbp), %r15
jmp 0x2716
movq -0x1040(%rbp), %rdi
callq 0x2380
movq -0x1048(%rbp), %rcx
movw %ax, 0x3a(%rcx)
jmp 0x2a1a
movq -0x1040(%rbp), %rdi
callq 0x2380
movq -0x1048(%rbp), %rcx
movb %al, 0x3d(%rcx)
jmp 0x2a1a
movq -0x1038(%rbp), %r14
movq %r14, %rdi
movq -0x1040(%rbp), %rsi
callq 0x2070
movq -0x1048(%rbp), %rax
movq %r14, 0x18(%rax)
jmp 0x2a05
leaq 0x45b1(%rip), %rdi      # 0x7035
movl %r15d, %esi
xorl %eax, %eax
callq 0x2150
movq -0x1050(%rbp), %r15
movq %r15, %rdi
callq 0x20f0
jmp 0x2ab4
movq 0x8(%rbx), %rsi
leaq 0x456e(%rip), %rdi      # 0x7018
xorl %r14d, %r14d
xorl %eax, %eax
callq 0x2150
movq %fs:0x28, %rax
cmpq -0x30(%rbp), %rax
jne 0x2ad8
movl %r14d, %eax
addq $0x1028, %rsp           # imm = 0x1028
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x2110