// File: /workspace/github_c_makefile_star2_O0/GangZhuo[P]dohclient/src/channel_hosts.c
// Function: hosts_parse_file
static int hosts_parse_file(channel_t *ctx, const char *filename)
{
	char buf[512];
	char *line;
	FILE *fp;
	int rownum = 0;
	sockaddr_t addr;
	void *ip;
	int family;

	fp = fopen(filename, "rb");
	if (fp == NULL) {
		loge("Can't open hosts: %s\n", filename);
		return -1;
	}

	if (fseek(fp, 0, SEEK_SET) != 0) {
		loge("fseek\n");
		fclose(fp);
		return -1;
	}

	while ((line = fgets(buf, sizeof(buf), fp)) != NULL) {
		char *sp_pos;

		rownum++;

		line = ltrim(line);

		/* skip comments */
		if ((*line) == '#') continue;

		/* remove CRLF */
		sp_pos = strrchr(line, '\r');
		if (sp_pos)
			*sp_pos = 0;
		else if ((sp_pos = strrchr(line, '\n')) != NULL)
			*sp_pos = 0;

		/* skip empty line */
		if (!(*line)) continue;

		/* find end position of the ip address */
		sp_pos = strspace(line);

		if (!sp_pos) {
			logw("invalid line %d: %s\n", rownum, line);
			continue;
		}

		*sp_pos = '\0';

		memset(&addr, 0, sizeof(sockaddr_t));

		if (!try_parse_as_ip(&addr, line, "80")) {
			logw("invalid ip address (line %d): %s\n", rownum, line);
			continue;
		}

		/* string that follow the ip address are a domain names */
		line = ltrim(sp_pos + 1);

		if (!(*line)) {
			logw("no domain name (line %d): %s\n", rownum, ltrim(buf));
			continue;
		}

		/* get ip family and bytes */
		family = addr.addr.ss_family;
		if (family == AF_INET)
			ip = &(((struct sockaddr_in*)(&addr.addr))->sin_addr);
		else
			ip = &(((struct sockaddr_in6*)(&addr.addr))->sin6_addr);

		/* parse domains one by one, and add these to dictionary */
		while (*line) {
			sp_pos = strspace(line);
			if (sp_pos)
				*sp_pos = '\0';
			if (hosts_add(ctx, line, ip, family)) {
				loge("hosts_add() error\n");
				fclose(fp);
				return -1;
			}
			if (!sp_pos)
				break;
			else {
				line = ltrim(sp_pos + 1);
			}
		}
	}

	fclose(fp);

	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x2f0, %rsp            # imm = 0x2F0
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl $0x0, -0x234(%rbp)
movq -0x18(%rbp), %rdi
leaq 0x1aaf9(%rip), %rsi     # 0x35db1
callq 0x3440
movq %rax, -0x230(%rbp)
cmpq $0x0, -0x230(%rbp)
jne 0x1b314
jmp 0x1b2d0
movb $0x0, %al
callq 0x242d0
cmpl $0x3, (%rax)
jl 0x1b306
movq -0x18(%rbp), %r9
movl $0x3, %edi
leaq 0x1ba0e(%rip), %rsi     # 0x36cfa
leaq 0x1bad1(%rip), %rdx     # 0x36dc4
movl $0x12f, %ecx            # imm = 0x12F
leaq 0x1bad6(%rip), %r8      # 0x36dd5
movb $0x0, %al
callq 0x243b0
jmp 0x1b308
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x1b6d6
movq -0x230(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x3410
cmpl $0x0, %eax
je 0x1b379
jmp 0x1b32d
movb $0x0, %al
callq 0x242d0
cmpl $0x3, (%rax)
jl 0x1b35f
movl $0x3, %edi
leaq 0x1b9b5(%rip), %rsi     # 0x36cfa
leaq 0x1ba78(%rip), %rdx     # 0x36dc4
movl $0x134, %ecx            # imm = 0x134
leaq 0x1ba93(%rip), %r8      # 0x36deb
movb $0x0, %al
callq 0x243b0
jmp 0x1b361
movq -0x230(%rbp), %rdi
callq 0x33a0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x1b6d6
jmp 0x1b37b
leaq -0x220(%rbp), %rdi
movq -0x230(%rbp), %rdx
movl $0x200, %esi            # imm = 0x200
callq 0x3590
movq %rax, -0x228(%rbp)
cmpq $0x0, %rax
je 0x1b6c3
movl -0x234(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x234(%rbp)
movq -0x228(%rbp), %rdi
callq 0x2ef90
movq %rax, -0x228(%rbp)
movq -0x228(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x23, %eax
jne 0x1b3d7
jmp 0x1b37b
movq -0x228(%rbp), %rdi
movl $0xd, %esi
callq 0x3400
movq %rax, -0x2d8(%rbp)
cmpq $0x0, -0x2d8(%rbp)
je 0x1b405
movq -0x2d8(%rbp), %rax
movb $0x0, (%rax)
jmp 0x1b42f
movq -0x228(%rbp), %rdi
movl $0xa, %esi
callq 0x3400
movq %rax, -0x2d8(%rbp)
cmpq $0x0, %rax
je 0x1b42d
movq -0x2d8(%rbp), %rax
movb $0x0, (%rax)
jmp 0x1b42f
movq -0x228(%rbp), %rax
cmpb $0x0, (%rax)
jne 0x1b440
jmp 0x1b37b
movq -0x228(%rbp), %rdi
callq 0x1b6f0
movq %rax, -0x2d8(%rbp)
cmpq $0x0, -0x2d8(%rbp)
jne 0x1b4aa
jmp 0x1b45f
movb $0x0, %al
callq 0x242d0
cmpl $0x4, (%rax)
jl 0x1b4a3
movl -0x234(%rbp), %r9d
movq -0x228(%rbp), %rax
movl $0x4, %edi
leaq 0x1b875(%rip), %rsi     # 0x36cfa
leaq 0x1b938(%rip), %rdx     # 0x36dc4
movl $0x151, %ecx            # imm = 0x151
leaq 0x1b95a(%rip), %r8      # 0x36df2
movq %rax, (%rsp)
movb $0x0, %al
callq 0x243b0
jmp 0x1b4a5
jmp 0x1b37b
movq -0x2d8(%rbp), %rax
movb $0x0, (%rax)
leaq -0x2c0(%rbp), %rdi
xorl %esi, %esi
movl $0x88, %edx
callq 0x3200
movq -0x228(%rbp), %rsi
leaq -0x2c0(%rbp), %rdi
leaq 0x1cc0f(%rip), %rdx     # 0x380eb
callq 0x24ce0
cmpl $0x0, %eax
jne 0x1b533
jmp 0x1b4e8
movb $0x0, %al
callq 0x242d0
cmpl $0x4, (%rax)
jl 0x1b52c
movl -0x234(%rbp), %r9d
movq -0x228(%rbp), %rax
movl $0x4, %edi
leaq 0x1b7ec(%rip), %rsi     # 0x36cfa
leaq 0x1b8af(%rip), %rdx     # 0x36dc4
movl $0x15a, %ecx            # imm = 0x15A
leaq 0x1b8e6(%rip), %r8      # 0x36e07
movq %rax, (%rsp)
movb $0x0, %al
callq 0x243b0
jmp 0x1b52e
jmp 0x1b37b
movq -0x2d8(%rbp), %rdi
addq $0x1, %rdi
callq 0x2ef90
movq %rax, -0x228(%rbp)
movq -0x228(%rbp), %rax
cmpb $0x0, (%rax)
jne 0x1b5b4
jmp 0x1b558
movb $0x0, %al
callq 0x242d0
cmpl $0x4, (%rax)
jl 0x1b5ad
movl -0x234(%rbp), %eax
movl %eax, -0x2dc(%rbp)
leaq -0x220(%rbp), %rdi
callq 0x2ef90
movl -0x2dc(%rbp), %r9d
movl $0x4, %edi
leaq 0x1b76b(%rip), %rsi     # 0x36cfa
leaq 0x1b82e(%rip), %rdx     # 0x36dc4
movl $0x162, %ecx            # imm = 0x162
leaq 0x1b887(%rip), %r8      # 0x36e29
movq %rax, (%rsp)
movb $0x0, %al
callq 0x243b0
jmp 0x1b5af
jmp 0x1b37b
movzwl -0x2c0(%rbp), %eax
movl %eax, -0x2cc(%rbp)
cmpl $0x2, -0x2cc(%rbp)
jne 0x1b5de
leaq -0x2c0(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x2c8(%rbp)
jmp 0x1b5f0
leaq -0x2c0(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x2c8(%rbp)
jmp 0x1b5f2
movq -0x228(%rbp), %rax
cmpb $0x0, (%rax)
je 0x1b6be
movq -0x228(%rbp), %rdi
callq 0x1b6f0
movq %rax, -0x2d8(%rbp)
cmpq $0x0, -0x2d8(%rbp)
je 0x1b629
movq -0x2d8(%rbp), %rax
movb $0x0, (%rax)
movq -0x10(%rbp), %rdi
movq -0x228(%rbp), %rsi
movq -0x2c8(%rbp), %rdx
movl -0x2cc(%rbp), %ecx
callq 0x1a550
cmpl $0x0, %eax
je 0x1b696
jmp 0x1b64d
movb $0x0, %al
callq 0x242d0
cmpl $0x3, (%rax)
jl 0x1b67f
movl $0x3, %edi
leaq 0x1b695(%rip), %rsi     # 0x36cfa
leaq 0x1b758(%rip), %rdx     # 0x36dc4
movl $0x173, %ecx            # imm = 0x173
leaq 0x1b7cf(%rip), %r8      # 0x36e47
movb $0x0, %al
callq 0x243b0
jmp 0x1b681
movq -0x230(%rbp), %rdi
callq 0x33a0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x1b6d6
cmpq $0x0, -0x2d8(%rbp)
jne 0x1b6a2
jmp 0x1b6be
movq -0x2d8(%rbp), %rdi
addq $0x1, %rdi
callq 0x2ef90
movq %rax, -0x228(%rbp)
jmp 0x1b5f2
jmp 0x1b37b
movq -0x230(%rbp), %rdi
callq 0x33a0
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x2f0, %rsp            # imm = 0x2F0
popq %rbp
retq
nopw %cs:(%rax,%rax)