// File: /workspace/github_c_makefile_star2_O1/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 0x4094(%rip), %rsi      # 0x708f
callq 0x2340
testq %rax, %rax
je 0x3338
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 0x3364
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 0x306c
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 0x30cd
btq %rdi, %r14
jae 0x309f
incq %rsi
incq %rdx
incq %rcx
incq %rax
jmp 0x307d
movabsq $0x800000400, %r8       # imm = 0x800000400
btq %rdi, %r8
jae 0x30cd
movq %r12, %rdi
movl $0xfff, %esi            # imm = 0xFFF
movq %rbx, %rdx
callq 0x21e0
testq %rax, %rax
sete %r15b
jne 0x3052
jmp 0x3364
leaq (%rsi,%rbp), %r13
addq $-0x1030, %r13          # imm = 0xEFD0
movzbl -0x1030(%rbp,%rsi), %edi
cmpl $0x3d, %edi
je 0x30fc
cmpl $0xa, %edi
je 0x334f
incq %rsi
incq %rdx
incq %rcx
incq %rax
jmp 0x30d8
movb $0x0, -0x1030(%rbp,%rsi)
xorl %esi, %esi
movzbl -0x1(%rdx), %edi
cmpq $0x20, %rdi
ja 0x311e
btq %rdi, %r14
jae 0x311e
decq %rdx
movb $0x1, %sil
jmp 0x3106
testb $0x1, %sil
je 0x312f
movb $0x0, (%rdx)
jmp 0x312f
incq %rcx
incq %rax
movzbl -0x1030(%rbp,%rcx), %edx
cmpl $0xc, %edx
jle 0x3148
cmpl $0x20, %edx
je 0x3129
cmpl $0xd, %edx
je 0x3129
jmp 0x3165
cmpl $0x9, %edx
je 0x3129
cmpl $0xa, %edx
jne 0x3165
leaq (%rcx,%rbp), %rax
addq $-0x1030, %rax          # imm = 0xEFD0
movq %rax, -0x1040(%rbp)
jmp 0x31ac
addq %rbp, %rcx
addq $-0x1030, %rcx          # imm = 0xEFD0
movq %rcx, -0x1040(%rbp)
movzbl (%rax), %ecx
cmpl $0xa, %ecx
je 0x318b
cmpl $0x23, %ecx
je 0x3188
incq %rax
jmp 0x3176
movb $0xa, (%rax)
movb $0x0, (%rax)
xorl %ecx, %ecx
movzbl -0x1(%rax), %edx
cmpq $0x20, %rdx
ja 0x31a7
btq %rdx, %r14
jae 0x31a7
decq %rax
movb $0x1, %cl
jmp 0x3190
testb $0x1, %cl
je 0x31af
movb $0x0, (%rax)
movq %r13, %rdi
leaq 0x3eb3(%rip), %rsi      # 0x706c
callq 0x2220
testl %eax, %eax
je 0x326e
movq %r13, %rdi
leaq 0x3ea0(%rip), %rsi      # 0x7070
callq 0x2220
testl %eax, %eax
je 0x3291
movq %r13, %rdi
leaq 0x3ee9(%rip), %rsi      # 0x70d0
callq 0x2220
testl %eax, %eax
je 0x32ab
movq %r13, %rdi
leaq 0x3edc(%rip), %rsi      # 0x70da
callq 0x2220
testl %eax, %eax
je 0x32ec
movq %r13, %rdi
leaq 0x3ed1(%rip), %rsi      # 0x70e6
callq 0x2220
testl %eax, %eax
je 0x3306
movq %r13, %rdi
leaq 0x46ca(%rip), %rsi      # 0x78f6
callq 0x2220
testl %eax, %eax
jne 0x32e1
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 0x32da
movq -0x1038(%rbp), %r15
movq %r15, %rdi
movq -0x1040(%rbp), %rsi
callq 0x2070
movq -0x1048(%rbp), %r13
movq %r15, 0x18(%r13)
jmp 0x32cc
movq -0x1040(%rbp), %rdi
callq 0x2380
movq -0x1048(%rbp), %r13
movw %ax, 0x20(%r13)
jmp 0x32e1
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 0x30af
movq -0x1040(%rbp), %rdi
callq 0x2380
movq -0x1048(%rbp), %r13
movw %ax, 0x3a(%r13)
jmp 0x32e1
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 0x32d4
movq 0x8(%r13), %rsi
leaq 0x3cd5(%rip), %rdi      # 0x7018
xorl %r15d, %r15d
xorl %eax, %eax
callq 0x2150
jmp 0x336c
movzwl -0x104c(%rbp), %esi
leaq 0x3cd8(%rip), %rdi      # 0x7035
xorl %eax, %eax
callq 0x2150
movq %rbx, %rdi
callq 0x20f0
movq %fs:0x28, %rax
cmpq -0x30(%rbp), %rax
jne 0x3394
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
nopl (%rax)