// File: /workspace/github_c_makefile_star2_O2/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, %r13
movq %rdi, %r14
movq %fs:0x28, %rax
movq %rax, -0x30(%rbp)
movq 0x8(%rsi), %rdi
leaq 0x4580(%rip), %rsi      # 0x708f
callq 0x2340
testq %rax, %rax
je 0x2e66
movq %rax, %rbx
movq %r14, -0x1038(%rbp)
movw $0x1, %ax
movl %eax, -0x104c(%rbp)
leaq -0x1030(%rbp), %r12
movabsq $0x100002200, %r14      # imm = 0x100002200
movq %r13, -0x1048(%rbp)
movq %r12, %rdi
movl $0xfff, %esi            # imm = 0xFFF
movq %rbx, %rdx
callq 0x21e0
testq %rax, %rax
sete %r15b
je 0x2e5c
movq %r12, %rdi
callq 0x2100
cmpb $0xa, -0x1(%rax,%r12)
je 0x2b80
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 0x2bd0
btq %r8, %r14
jae 0x2bbc
incq %rdi
incq %rsi
incq %rdx
incq %rcx
incq %rax
jmp 0x2b96
movabsq $0x800000400, %r9       # imm = 0x800000400
btq %r8, %r9
jb 0x2b49
leaq (%rdi,%rbp), %r13
addq $-0x1030, %r13          # imm = 0xEFD0
cmpb $0x3d, %r8b
je 0x2c08
movzbl %r8b, %r8d
cmpl $0xa, %r8d
je 0x2e47
movb -0x102f(%rbp,%rdi), %r8b
incq %rdi
incq %rsi
incq %rdx
incq %rcx
incq %rax
jmp 0x2bdb
movb $0x0, -0x1030(%rbp,%rdi)
xorl %edi, %edi
movzbl -0x1(%rsi), %r8d
cmpq $0x20, %r8
ja 0x2c2b
btq %r8, %r14
jae 0x2c2b
decq %rsi
movb $0x1, %dil
jmp 0x2c12
testb $0x1, %dil
je 0x2c3f
movb $0x0, (%rsi)
jmp 0x2c3f
incq %rdx
incq %rcx
incq %rax
movzbl -0x1030(%rbp,%rdx), %esi
cmpl $0x9, %esi
je 0x2c36
cmpl $0xd, %esi
je 0x2c36
cmpl $0x20, %esi
je 0x2c36
cmpl $0xa, %esi
jne 0x2c6e
leaq (%rdx,%rbp), %rax
addq $-0x1030, %rax          # imm = 0xEFD0
movq %rax, -0x1040(%rbp)
jmp 0x2cbb
addq %rbp, %rdx
addq $-0x1030, %rdx          # imm = 0xEFD0
movq %rdx, -0x1040(%rbp)
cmpb $0xa, %sil
je 0x2c99
movzbl %sil, %edx
cmpl $0x23, %edx
je 0x2c99
movb (%rcx), %sil
incq %rcx
incq %rax
jmp 0x2c7f
movb $0x0, -0x1(%rcx)
xorl %ecx, %ecx
movzbl -0x1(%rax), %edx
cmpq $0x20, %rdx
ja 0x2cb6
btq %rdx, %r14
jae 0x2cb6
decq %rax
movb $0x1, %cl
jmp 0x2c9f
testb $0x1, %cl
je 0x2cbe
movb $0x0, (%rax)
movq %r13, %rdi
leaq 0x43a4(%rip), %rsi      # 0x706c
callq 0x2220
testl %eax, %eax
je 0x2d7d
movq %r13, %rdi
leaq 0x4391(%rip), %rsi      # 0x7070
callq 0x2220
testl %eax, %eax
je 0x2da0
movq %r13, %rdi
leaq 0x43da(%rip), %rsi      # 0x70d0
callq 0x2220
testl %eax, %eax
je 0x2dba
movq %r13, %rdi
leaq 0x43cd(%rip), %rsi      # 0x70da
callq 0x2220
testl %eax, %eax
je 0x2dfb
movq %r13, %rdi
leaq 0x43c2(%rip), %rsi      # 0x70e6
callq 0x2220
testl %eax, %eax
je 0x2e15
movq %r13, %rdi
leaq 0x4b98(%rip), %rsi      # 0x78d3
callq 0x2220
testl %eax, %eax
jne 0x2df0
movq -0x1038(%rbp), %r15
movq %r15, %rdi
movq -0x1040(%rbp), %rsi
callq 0x2070
movq -0x1048(%rbp), %r13
movq %r15, 0x30(%r13)
movq %r15, %rdi
callq 0x2100
movb %al, 0x39(%r13)
movzbl %al, %eax
addq %rax, %r15
jmp 0x2de9
movq -0x1038(%rbp), %r15
movq %r15, %rdi
movq -0x1040(%rbp), %rsi
callq 0x2070
movq -0x1048(%rbp), %r13
movq %r15, 0x18(%r13)
jmp 0x2ddb
movq -0x1040(%rbp), %rdi
callq 0x2380
movq -0x1048(%rbp), %r13
movw %ax, 0x20(%r13)
jmp 0x2df0
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 0x2b49
movq -0x1040(%rbp), %rdi
callq 0x2380
movq -0x1048(%rbp), %r13
movw %ax, 0x3a(%r13)
jmp 0x2df0
movq -0x1038(%rbp), %r15
movq %r15, %rdi
movq -0x1040(%rbp), %rsi
callq 0x2070
movq -0x1048(%rbp), %r13
movq %r15, 0x28(%r13)
movq %r15, %rdi
callq 0x2100
movb %al, 0x38(%r13)
movzbl %al, %eax
jmp 0x2de3
movzwl -0x104c(%rbp), %esi
leaq 0x41e0(%rip), %rdi      # 0x7035
xorl %eax, %eax
callq 0x2150
movq %rbx, %rdi
callq 0x20f0
jmp 0x2e7b
movq 0x8(%r13), %rsi
leaq 0x41a7(%rip), %rdi      # 0x7018
xorl %r15d, %r15d
xorl %eax, %eax
callq 0x2150
movq %fs:0x28, %rax
cmpq -0x30(%rbp), %rax
jne 0x2e9f
movl %r15d, %eax
addq $0x1028, %rsp           # imm = 0x1028
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x2110