// File: /workspace/github_c_makefile_star2_O0/ToniWestbrook[P]paladin/kopen.c
// Function: http_open
static int http_open(const char *fn)
{
	char *p, *proxy, *q, *http_host, *host, *port, *path, *buf;
	int fd, ret, l;
	ssize_t bytes = 0, bufsz = 0x10000;

	/* parse URL; adapted from khttp_parse_url() in knetfile.c */
	if (strstr(fn, "http://") != fn) return 0;
	// set ->http_host
	for (p = (char*)fn + 7; *p && *p != '/'; ++p);
	l = p - fn - 7;
	http_host = calloc(l + 1, 1);
	strncpy(http_host, fn + 7, l);
	http_host[l] = 0;
	for (q = http_host; *q && *q != ':'; ++q);
	if (*q == ':') *q++ = 0;
	// get http_proxy
	proxy = getenv("http_proxy");
	// set host, port and path
	if (proxy == 0) {
		host = strdup(http_host); // when there is no proxy, server name is identical to http_host name.
		port = strdup(*q? q : "80");
		path = strdup(*p? p : "/");
	} else {
		host = (strstr(proxy, "http://") == proxy)? strdup(proxy + 7) : strdup(proxy);
		for (q = host; *q && *q != ':'; ++q);
		if (*q == ':') *q++ = 0; 
		port = strdup(*q? q : "80");
		path = strdup(fn);
	}

	/* connect; adapted from khttp_connect() in knetfile.c */
	l = 0;
	fd = socket_connect(host, port);
	buf = calloc(bufsz, 1); // FIXME: I am lazy... But in principle, 64KB should be large enough.
	l += snprintf(buf + l, bufsz, "GET %s HTTP/1.0\r\nHost: %s\r\n\r\n",
				 path, http_host);
	if (write_bytes(fd, buf, l) != 0) {
		close(fd);
		fd = -1;
		goto out;
	}
	l = 0;
 retry:
	while (l < bufsz && (bytes = read(fd, buf + l, 1)) > 0) { // read HTTP header; FIXME: bad efficiency
		if (buf[l] == '\n' && l >= 3)
			if (strncmp(buf + l - 3, "\r\n\r\n", 4) == 0) break;
		++l;
	}
	if (bytes < 0 && (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)) goto retry;

	buf[l] = 0;
	if (bytes < 0 || l < 14) { // prematured header
		close(fd);
		fd = -1;
		goto out;
	}
	ret = strtol(buf + 8, &p, 0); // HTTP return code
	if (ret != 200) {
		close(fd);
		fd = -1;
	}
 out:
	free(buf); free(http_host); free(host); free(port); free(path);
	return fd;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xb0, %rsp
movq %rdi, -0x10(%rbp)
movq $0x0, -0x68(%rbp)
movq $0x10000, -0x70(%rbp)   # imm = 0x10000
movq -0x10(%rbp), %rdi
leaq 0x3111d(%rip), %rsi     # 0x37687
callq 0x3080
cmpq -0x10(%rbp), %rax
je 0x6581
movl $0x0, -0x4(%rbp)
jmp 0x6a7a
movq -0x10(%rbp), %rax
addq $0x7, %rax
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rax
movsbl (%rax), %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
movb %al, -0x71(%rbp)
je 0x65ae
movq -0x18(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x2f, %eax
setne %al
movb %al, -0x71(%rbp)
movb -0x71(%rbp), %al
testb $0x1, %al
jne 0x65b7
jmp 0x65c7
jmp 0x65b9
movq -0x18(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x18(%rbp)
jmp 0x658d
movq -0x18(%rbp), %rax
movq -0x10(%rbp), %rcx
subq %rcx, %rax
subq $0x7, %rax
movl %eax, -0x5c(%rbp)
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movslq %eax, %rdi
movl $0x1, %esi
leaq 0x310a1(%rip), %rdx     # 0x3768f
movl $0x57, %ecx
leaq 0x310b5(%rip), %r8      # 0x376af
callq 0x31bf0
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rdi
movq -0x10(%rbp), %rsi
addq $0x7, %rsi
movslq -0x5c(%rbp), %rdx
callq 0x3120
movq -0x30(%rbp), %rax
movslq -0x5c(%rbp), %rcx
movb $0x0, (%rax,%rcx)
movq -0x30(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
movsbl (%rax), %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
movb %al, -0x72(%rbp)
je 0x664d
movq -0x28(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x3a, %eax
setne %al
movb %al, -0x72(%rbp)
movb -0x72(%rbp), %al
testb $0x1, %al
jne 0x6656
jmp 0x6666
jmp 0x6658
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
jmp 0x662c
movq -0x28(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x3a, %eax
jne 0x6684
movq -0x28(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x28(%rbp)
movb $0x0, (%rax)
leaq 0x3102e(%rip), %rdi     # 0x376b9
callq 0x33c0
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
jne 0x6753
movq -0x30(%rbp), %rdi
leaq 0x30fe5(%rip), %rsi     # 0x3768f
movl $0x60, %edx
leaq 0x30ff9(%rip), %rcx     # 0x376af
callq 0x31df0
movq %rax, -0x38(%rbp)
movq -0x28(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x0, %eax
je 0x66d5
movq -0x28(%rbp), %rax
movq %rax, -0x80(%rbp)
jmp 0x66e2
leaq 0x30fe8(%rip), %rax     # 0x376c4
movq %rax, -0x80(%rbp)
jmp 0x66e2
movq -0x80(%rbp), %rdi
leaq 0x30fa2(%rip), %rsi     # 0x3768f
movl $0x61, %edx
leaq 0x30fb6(%rip), %rcx     # 0x376af
callq 0x31df0
movq %rax, -0x40(%rbp)
movq -0x18(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x0, %eax
je 0x671b
movq -0x18(%rbp), %rax
movq %rax, -0x88(%rbp)
jmp 0x672b
leaq 0x30f80(%rip), %rax     # 0x376a2
movq %rax, -0x88(%rbp)
jmp 0x672b
movq -0x88(%rbp), %rdi
leaq 0x30f56(%rip), %rsi     # 0x3768f
movl $0x62, %edx
leaq 0x30f6a(%rip), %rcx     # 0x376af
callq 0x31df0
movq %rax, -0x48(%rbp)
jmp 0x6895
movq -0x20(%rbp), %rdi
leaq 0x30f29(%rip), %rsi     # 0x37687
callq 0x3080
cmpq -0x20(%rbp), %rax
jne 0x6792
movq -0x20(%rbp), %rdi
addq $0x7, %rdi
leaq 0x30f17(%rip), %rsi     # 0x3768f
movl $0x64, %edx
leaq 0x30f2b(%rip), %rcx     # 0x376af
callq 0x31df0
movq %rax, -0x90(%rbp)
jmp 0x67b5
movq -0x20(%rbp), %rdi
leaq 0x30ef2(%rip), %rsi     # 0x3768f
movl $0x64, %edx
leaq 0x30f06(%rip), %rcx     # 0x376af
callq 0x31df0
movq %rax, -0x90(%rbp)
movq -0x90(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
movsbl (%rax), %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
movb %al, -0x91(%rbp)
je 0x67ef
movq -0x28(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x3a, %eax
setne %al
movb %al, -0x91(%rbp)
movb -0x91(%rbp), %al
testb $0x1, %al
jne 0x67fb
jmp 0x680b
jmp 0x67fd
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
jmp 0x67c8
movq -0x28(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x3a, %eax
jne 0x6829
movq -0x28(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x28(%rbp)
movb $0x0, (%rax)
movq -0x28(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x0, %eax
je 0x6842
movq -0x28(%rbp), %rax
movq %rax, -0xa0(%rbp)
jmp 0x6852
leaq 0x30e7b(%rip), %rax     # 0x376c4
movq %rax, -0xa0(%rbp)
jmp 0x6852
movq -0xa0(%rbp), %rdi
leaq 0x30e2f(%rip), %rsi     # 0x3768f
movl $0x67, %edx
leaq 0x30e43(%rip), %rcx     # 0x376af
callq 0x31df0
movq %rax, -0x40(%rbp)
movq -0x10(%rbp), %rdi
leaq 0x30e0f(%rip), %rsi     # 0x3768f
movl $0x68, %edx
leaq 0x30e23(%rip), %rcx     # 0x376af
callq 0x31df0
movq %rax, -0x48(%rbp)
movl $0x0, -0x5c(%rbp)
movq -0x38(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x7190
movl %eax, -0x54(%rbp)
movq -0x70(%rbp), %rdi
movl $0x1, %esi
leaq 0x30dd3(%rip), %rdx     # 0x3768f
movl $0x6e, %ecx
leaq 0x30de7(%rip), %r8      # 0x376af
callq 0x31bf0
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rdi
movslq -0x5c(%rbp), %rax
addq %rax, %rdi
movq -0x70(%rbp), %rsi
movq -0x48(%rbp), %rcx
movq -0x30(%rbp), %r8
leaq 0x30dd8(%rip), %rdx     # 0x376c7
movb $0x0, %al
callq 0x35c0
addl -0x5c(%rbp), %eax
movl %eax, -0x5c(%rbp)
movl -0x54(%rbp), %edi
movq -0x50(%rbp), %rsi
movslq -0x5c(%rbp), %rdx
callq 0x7340
cmpl $0x0, %eax
je 0x6925
movl -0x54(%rbp), %edi
callq 0x3780
movl $0xffffffff, -0x54(%rbp) # imm = 0xFFFFFFFF
jmp 0x6a47
movl $0x0, -0x5c(%rbp)
jmp 0x692e
movslq -0x5c(%rbp), %rcx
xorl %eax, %eax
cmpq -0x70(%rbp), %rcx
movb %al, -0xa1(%rbp)
jge 0x6969
movl -0x54(%rbp), %edi
movq -0x50(%rbp), %rsi
movslq -0x5c(%rbp), %rax
addq %rax, %rsi
movl $0x1, %edx
callq 0x34f0
movq %rax, -0x68(%rbp)
cmpq $0x0, %rax
setg %al
movb %al, -0xa1(%rbp)
movb -0xa1(%rbp), %al
testb $0x1, %al
jne 0x6975
jmp 0x69c3
movq -0x50(%rbp), %rax
movslq -0x5c(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0xa, %eax
jne 0x69b5
cmpl $0x3, -0x5c(%rbp)
jl 0x69b5
movq -0x50(%rbp), %rdi
movslq -0x5c(%rbp), %rax
addq %rax, %rdi
addq $-0x3, %rdi
leaq 0x30d3e(%rip), %rsi     # 0x376e0
movl $0x4, %edx
callq 0x3110
cmpl $0x0, %eax
jne 0x69b3
jmp 0x69c3
jmp 0x69b5
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0x692e
cmpq $0x0, -0x68(%rbp)
jge 0x69ed
callq 0x3050
cmpl $0xb, (%rax)
je 0x69e8
callq 0x3050
cmpl $0xb, (%rax)
je 0x69e8
callq 0x3050
cmpl $0x4, (%rax)
jne 0x69ed
jmp 0x692c
movq -0x50(%rbp), %rax
movslq -0x5c(%rbp), %rcx
movb $0x0, (%rax,%rcx)
cmpq $0x0, -0x68(%rbp)
jl 0x6a06
cmpl $0xe, -0x5c(%rbp)
jge 0x6a17
movl -0x54(%rbp), %edi
callq 0x3780
movl $0xffffffff, -0x54(%rbp) # imm = 0xFFFFFFFF
jmp 0x6a47
movq -0x50(%rbp), %rdi
addq $0x8, %rdi
leaq -0x18(%rbp), %rsi
xorl %edx, %edx
callq 0x34c0
movl %eax, -0x58(%rbp)
cmpl $0xc8, -0x58(%rbp)
je 0x6a45
movl -0x54(%rbp), %edi
callq 0x3780
movl $0xffffffff, -0x54(%rbp) # imm = 0xFFFFFFFF
jmp 0x6a47
movq -0x50(%rbp), %rdi
callq 0x3380
movq -0x30(%rbp), %rdi
callq 0x3380
movq -0x38(%rbp), %rdi
callq 0x3380
movq -0x40(%rbp), %rdi
callq 0x3380
movq -0x48(%rbp), %rdi
callq 0x3380
movl -0x54(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xb0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)