// File: karel-brinda[P]ococo/ext/htslib/knetfile.c
// Function: khttp_connect_file
int khttp_connect_file(knetFile *fp)
{
	int ret, l = 0;
	char *buf, *p;
	if (fp->fd != -1) netclose(fp->fd);
	fp->fd = socket_connect(fp->host, fp->port);
	buf = (char*)calloc(0x10000, 1); // FIXME: I am lazy... But in principle, 64KB should be large enough.
	l += sprintf(buf + l, "GET %s HTTP/1.0\r\nHost: %s\r\n", fp->path, fp->http_host);
	if (fp->offset != 0) l += sprintf(buf + l, "Range: bytes=%lld-\r\n", (long long)fp->offset);
	l += sprintf(buf + l, "\r\n");
	if ( netwrite(fp->fd, buf, l) != l ) { free(buf); return -1; }
	l = 0;
	while (netread(fp->fd, buf + l, 1)) { // 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;
	}
	buf[l] = 0;
	if (l < 14) { // prematured header
		free(buf);
		netclose(fp->fd);
		fp->fd = -1;
		return -1;
	}
	ret = strtol(buf + 8, &p, 0); // HTTP return code
	if (ret == 200 && fp->offset>0) { // 200 (complete result); then skip beginning of the file
		off_t rest = fp->offset;
		while (rest) {
			off_t l = rest < 0x10000? rest : 0x10000;
			rest -= my_netread(fp->fd, buf, l);
		}
	} else if (ret != 206 && ret != 200) {
		// failed to open file
		free(buf);
		netclose(fp->fd);
		switch (ret) {
		case 401: errno = EPERM; break;
		case 403: errno = EACCES; break;
		case 404: errno = ENOENT; break;
		case 407: errno = EPERM; break;
		case 408: errno = ETIMEDOUT; break;
		case 410: errno = ENOENT; break;
		case 503: errno = EAGAIN; break;
		case 504: errno = ETIMEDOUT; break;
		default:  errno = (ret >= 400 && ret < 500)? EINVAL : EIO; break;
		}
		fp->fd = -1;
		return -1;
	}
	free(buf);
	fp->is_ready = 1;
	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movl $0x0, -0x18(%rbp)
movq -0x10(%rbp), %rax
cmpl $-0x1, 0x4(%rax)
je 0x76e79
movq -0x10(%rbp), %rax
movl 0x4(%rax), %edi
callq 0x4b40
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rsi
callq 0x761a0
movl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x4(%rax)
movl $0x10000, %edi          # imm = 0x10000
movl $0x1, %esi
callq 0x4370
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rdi
movslq -0x18(%rbp), %rax
addq %rax, %rdi
movq -0x10(%rbp), %rax
movq 0x70(%rax), %rdx
movq -0x10(%rbp), %rax
movq 0x78(%rax), %rcx
leaq 0x7f4f(%rip), %rsi      # 0x7ee1b
movb $0x0, %al
callq 0x4100
addl -0x18(%rbp), %eax
movl %eax, -0x18(%rbp)
movq -0x10(%rbp), %rax
cmpq $0x0, 0x8(%rax)
je 0x76f0b
movq -0x20(%rbp), %rdi
movslq -0x18(%rbp), %rax
addq %rax, %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdx
leaq 0x7f39(%rip), %rsi      # 0x7ee37
movb $0x0, %al
callq 0x4100
addl -0x18(%rbp), %eax
movl %eax, -0x18(%rbp)
movq -0x20(%rbp), %rdi
movslq -0x18(%rbp), %rax
addq %rax, %rdi
leaq 0x7f31(%rip), %rsi      # 0x7ee4e
movb $0x0, %al
callq 0x4100
addl -0x18(%rbp), %eax
movl %eax, -0x18(%rbp)
movq -0x10(%rbp), %rax
movl 0x4(%rax), %edi
movq -0x20(%rbp), %rsi
movslq -0x18(%rbp), %rdx
callq 0x41d0
movslq -0x18(%rbp), %rcx
cmpq %rcx, %rax
je 0x76f5c
movq -0x20(%rbp), %rdi
callq 0x46a0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x77207
movl $0x0, -0x18(%rbp)
movq -0x10(%rbp), %rax
movl 0x4(%rax), %edi
movq -0x20(%rbp), %rsi
movslq -0x18(%rbp), %rax
addq %rax, %rsi
movl $0x1, %edx
callq 0x4840
cmpq $0x0, %rax
je 0x76fd0
movq -0x20(%rbp), %rax
movslq -0x18(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0xa, %eax
jne 0x76fc5
cmpl $0x3, -0x18(%rbp)
jl 0x76fc5
movq -0x20(%rbp), %rdi
movslq -0x18(%rbp), %rax
addq %rax, %rdi
addq $-0x3, %rdi
leaq 0x7e9a(%rip), %rsi      # 0x7ee4c
movl $0x4, %edx
callq 0x4290
cmpl $0x0, %eax
jne 0x76fc3
jmp 0x76fd0
jmp 0x76fc5
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x76f63
movq -0x20(%rbp), %rax
movslq -0x18(%rbp), %rcx
movb $0x0, (%rax,%rcx)
cmpl $0xe, -0x18(%rbp)
jge 0x7700e
movq -0x20(%rbp), %rdi
callq 0x46a0
movq -0x10(%rbp), %rax
movl 0x4(%rax), %edi
callq 0x4b40
movq -0x10(%rbp), %rax
movl $0xffffffff, 0x4(%rax)  # imm = 0xFFFFFFFF
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x77207
movq -0x20(%rbp), %rdi
addq $0x8, %rdi
leaq -0x28(%rbp), %rsi
xorl %edx, %edx
callq 0x4820
movl %eax, -0x14(%rbp)
cmpl $0xc8, -0x14(%rbp)
jne 0x7709b
movq -0x10(%rbp), %rax
cmpq $0x0, 0x8(%rax)
jle 0x7709b
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
je 0x77096
cmpq $0x10000, -0x30(%rbp)   # imm = 0x10000
jge 0x7705f
movq -0x30(%rbp), %rax
movq %rax, -0x40(%rbp)
jmp 0x7706a
movl $0x10000, %eax          # imm = 0x10000
movq %rax, -0x40(%rbp)
jmp 0x7706a
movq -0x40(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rax
movl 0x4(%rax), %edi
movq -0x20(%rbp), %rsi
movq -0x38(%rbp), %rdx
callq 0x77210
movq %rax, %rcx
movq -0x30(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x30(%rbp)
jmp 0x77044
jmp 0x771ec
cmpl $0xce, -0x14(%rbp)
je 0x771ea
cmpl $0xc8, -0x14(%rbp)
je 0x771ea
movq -0x20(%rbp), %rdi
callq 0x46a0
movq -0x10(%rbp), %rax
movl 0x4(%rax), %edi
callq 0x4b40
movl -0x14(%rbp), %eax
movl %eax, -0x44(%rbp)
subl $0x191, %eax            # imm = 0x191
je 0x7712d
jmp 0x770d9
movl -0x44(%rbp), %eax
subl $0x193, %eax            # imm = 0x193
je 0x7713d
jmp 0x770e5
movl -0x44(%rbp), %eax
subl $0x194, %eax            # imm = 0x194
je 0x7714d
jmp 0x770f1
movl -0x44(%rbp), %eax
subl $0x197, %eax            # imm = 0x197
je 0x7715a
jmp 0x770fd
movl -0x44(%rbp), %eax
subl $0x198, %eax            # imm = 0x198
je 0x77167
jmp 0x77109
movl -0x44(%rbp), %eax
subl $0x19a, %eax            # imm = 0x19A
je 0x77174
jmp 0x77115
movl -0x44(%rbp), %eax
subl $0x1f7, %eax            # imm = 0x1F7
je 0x77181
jmp 0x77121
movl -0x44(%rbp), %eax
subl $0x1f8, %eax            # imm = 0x1F8
je 0x7718e
jmp 0x7719b
callq 0x4070
movl $0x1, (%rax)
jmp 0x771d6
callq 0x4070
movl $0xd, (%rax)
jmp 0x771d6
callq 0x4070
movl $0x2, (%rax)
jmp 0x771d6
callq 0x4070
movl $0x1, (%rax)
jmp 0x771d6
callq 0x4070
movl $0x6e, (%rax)
jmp 0x771d6
callq 0x4070
movl $0x2, (%rax)
jmp 0x771d6
callq 0x4070
movl $0xb, (%rax)
jmp 0x771d6
callq 0x4070
movl $0x6e, (%rax)
jmp 0x771d6
xorl %eax, %eax
cmpl $0x190, -0x14(%rbp)     # imm = 0x190
movb %al, -0x45(%rbp)
jl 0x771b6
cmpl $0x1f4, -0x14(%rbp)     # imm = 0x1F4
setl %al
movb %al, -0x45(%rbp)
movb -0x45(%rbp), %dl
movl $0x5, %eax
movl $0x16, %ecx
testb $0x1, %dl
cmovnel %ecx, %eax
movl %eax, -0x4c(%rbp)
callq 0x4070
movl -0x4c(%rbp), %ecx
movl %ecx, (%rax)
movq -0x10(%rbp), %rax
movl $0xffffffff, 0x4(%rax)  # imm = 0xFFFFFFFF
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x77207
jmp 0x771ec
movq -0x20(%rbp), %rdi
callq 0x46a0
movq -0x10(%rbp), %rax
movl $0x1, 0x40(%rax)
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq