// File: /workspace/github_c_makefile_star2_O3/TeaInside[P]teavpn/src/teavpn/teavpn_config_parser.c
// Function: teavpn_client_config_parser
bool teavpn_client_config_parser(char *internal_buf, client_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]), "server_ip")) {
			strcpy(internal_buf, &(buffer[k]));
			config->server_ip = internal_buf;
			internal_buf += strlen(internal_buf) + 1;
		} else if (!strcmp(&(buffer[j]), "server_port")) {
			config->server_port = (uint16_t)atoi(&(buffer[k]));
		} else if (!strcmp(&(buffer[j]), "username")) {
			strcpy(internal_buf, &(buffer[k]));
			config->username = internal_buf;
			config->username_len = strlen(internal_buf);
			internal_buf +=  config->username_len + 1;
		} else if (!strcmp(&(buffer[j]), "password")) {
			strcpy(internal_buf, &(buffer[k]));
			config->password = internal_buf;
			config->password_len = strlen(internal_buf);
			internal_buf += config->password_len;
		}

		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 0x40a1(%rip), %rsi      # 0x708f
callq 0x2340
testq %rax, %rax
je 0x3327
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 0x3355
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 0x3059
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 0x30c1
btq %r8, %r15
jae 0x3097
incq %rdi
incq %rsi
incq %rdx
incq %rcx
incq %rax
jmp 0x3071
movabsq $0x800000400, %r9       # imm = 0x800000400
btq %r8, %r9
jae 0x30c1
movq %r13, %rdi
movl $0xfff, %esi            # imm = 0xFFF
movq %r14, %rdx
callq 0x21e0
testq %rax, %rax
jne 0x303f
jmp 0x3380
leaq (%rdi,%rbp), %r12
addq $-0x1030, %r12          # imm = 0xEFD0
cmpb $0x3d, %r8b
je 0x30f9
movzbl %r8b, %r8d
cmpl $0xa, %r8d
je 0x333e
movb -0x102f(%rbp,%rdi), %r8b
incq %rdi
incq %rsi
incq %rdx
incq %rcx
incq %rax
jmp 0x30cc
movb $0x0, -0x1030(%rbp,%rdi)
xorl %edi, %edi
movzbl -0x1(%rsi), %r8d
cmpq $0x20, %r8
ja 0x311c
btq %r8, %r15
jae 0x311c
decq %rsi
movb $0x1, %dil
jmp 0x3103
testb $0x1, %dil
je 0x3130
movb $0x0, (%rsi)
jmp 0x3130
incq %rdx
incq %rcx
incq %rax
movb -0x1030(%rbp,%rdx), %sil
movzbl %sil, %edi
cmpl $0xc, %edi
jle 0x314d
cmpl $0x20, %edi
je 0x3127
cmpl $0xd, %edi
je 0x3127
jmp 0x316a
cmpl $0x9, %edi
je 0x3127
cmpl $0xa, %edi
jne 0x316a
leaq (%rdx,%rbp), %rax
addq $-0x1030, %rax          # imm = 0xEFD0
movq %rax, -0x1038(%rbp)
jmp 0x31b7
addq %rbp, %rdx
addq $-0x1030, %rdx          # imm = 0xEFD0
movq %rdx, -0x1038(%rbp)
cmpb $0xa, %sil
je 0x3195
movzbl %sil, %edx
cmpl $0x23, %edx
je 0x3195
movb (%rcx), %sil
incq %rcx
incq %rax
jmp 0x317b
movb $0x0, -0x1(%rcx)
xorl %ecx, %ecx
movzbl -0x1(%rax), %edx
cmpq $0x20, %rdx
ja 0x31b2
btq %rdx, %r15
jae 0x31b2
decq %rax
movb $0x1, %cl
jmp 0x319b
testb $0x1, %cl
je 0x31ba
movb $0x0, (%rax)
movq %r12, %rdi
leaq 0x3ea8(%rip), %rsi      # 0x706c
callq 0x2220
testl %eax, %eax
je 0x3274
movq %r12, %rdi
leaq 0x3e95(%rip), %rsi      # 0x7070
callq 0x2220
testl %eax, %eax
je 0x3291
movq %r12, %rdi
leaq 0x3ede(%rip), %rsi      # 0x70d0
callq 0x2220
testl %eax, %eax
je 0x32ac
movq %r12, %rdi
leaq 0x3ed1(%rip), %rsi      # 0x70da
callq 0x2220
testl %eax, %eax
je 0x32df
movq %r12, %rdi
leaq 0x3ec6(%rip), %rsi      # 0x70e6
callq 0x2220
testl %eax, %eax
je 0x32fa
movq %r12, %rdi
leaq 0x469c(%rip), %rsi      # 0x78d3
callq 0x2220
testl %eax, %eax
jne 0x32d4
movq %rbx, %rdi
movq -0x1038(%rbp), %rsi
callq 0x2070
movq -0x1040(%rbp), %r12
movq %rbx, 0x30(%r12)
movq %rbx, %rdi
callq 0x2100
movb %al, 0x39(%r12)
movzbl %al, %eax
addq %rax, %rbx
jmp 0x32d4
movq %rbx, %rdi
movq -0x1038(%rbp), %rsi
callq 0x2070
movq -0x1040(%rbp), %r12
movq %rbx, 0x18(%r12)
jmp 0x32c6
movq -0x1038(%rbp), %rdi
callq 0x2380
movq -0x1040(%rbp), %r12
movw %ax, 0x20(%r12)
jmp 0x32d4
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 0x30a7
movq -0x1038(%rbp), %rdi
callq 0x2380
movq -0x1040(%rbp), %r12
movw %ax, 0x3a(%r12)
jmp 0x32d4
movq %rbx, %rdi
movq -0x1038(%rbp), %rsi
callq 0x2070
movq -0x1040(%rbp), %r12
movq %rbx, 0x28(%r12)
movq %rbx, %rdi
callq 0x2100
movb %al, 0x38(%r12)
movzbl %al, %eax
jmp 0x32ce
movq 0x8(%r12), %rsi
leaq 0x3ce5(%rip), %rdi      # 0x7018
xorl %ebx, %ebx
xorl %eax, %eax
callq 0x2150
jmp 0x335d
movzwl -0x1044(%rbp), %esi
leaq 0x3ce9(%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 0x3384
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 0x3355
callq 0x2110
nopl (%rax)