// File: /workspace/github_cpp_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %rbx
movl 0x4(%rdi), %edi
cmpl $-0x1, %edi
je 0x4ea7a
callq 0x4a50
movq 0x10(%rbx), %rdi
movq 0x18(%rbx), %rsi
callq 0x4e2d8
movl %eax, 0x4(%rbx)
movl $0x10000, %edi          # imm = 0x10000
movl $0x1, %esi
callq 0x4360
movq %rax, %r14
movq 0x70(%rbx), %rdx
movq 0x78(%rbx), %rcx
leaq 0x7eb1(%rip), %rsi      # 0x5695c
movq %rax, %rdi
xorl %eax, %eax
callq 0x4110
movl %eax, %ebp
movq 0x8(%rbx), %rdx
testq %rdx, %rdx
je 0x4ead6
movslq %ebp, %rdi
addq %r14, %rdi
leaq 0x7eab(%rip), %rsi      # 0x56978
xorl %eax, %eax
callq 0x4110
addl %eax, %ebp
movslq %ebp, %rax
movb $0x0, 0x2(%r14,%rax)
movw $0xa0d, (%r14,%rax)     # imm = 0xA0D
addl $0x2, %eax
movl 0x4(%rbx), %edi
movslq %eax, %r15
movq %r14, %rsi
movq %r15, %rdx
callq 0x41f0
cmpq %r15, %rax
jne 0x4ebdd
movl 0x4(%rbx), %edi
movl $0x1, %edx
movq %r14, %rsi
callq 0x4780
testq %rax, %rax
je 0x4ebe7
leaq -0x3(%r14), %r13
xorl %r12d, %r12d
leaq 0x7e63(%rip), %r15      # 0x5698d
cmpq $0x3, %r12
jb 0x4eb4c
cmpb $0xa, (%r14,%r12)
jne 0x4eb4c
leaq (%r12,%r13), %rdi
movl $0x4, %edx
movq %r15, %rsi
callq 0x4280
testl %eax, %eax
je 0x4eb68
movl 0x4(%rbx), %edi
leaq (%r12,%r14), %rsi
incq %rsi
incq %r12
movl $0x1, %edx
callq 0x4780
testq %rax, %rax
jne 0x4eb2a
leaq (%r14,%r12), %rax
movb $0x0, (%rax)
cmpl $0xe, %r12d
jb 0x4ebeb
movq %r14, %rdi
addq $0x8, %rdi
movq %rsp, %rsi
xorl %ebp, %ebp
xorl %edx, %edx
callq 0x4760
movq %rax, %r15
cmpl $0xce, %r15d
je 0x4ebcc
cmpl $0xc8, %r15d
jne 0x4ec2a
movq 0x8(%rbx), %r12
testq %r12, %r12
jle 0x4ec18
movl $0x10000, %r15d         # imm = 0x10000
cmpq %r15, %r12
movl $0x10000, %edx          # imm = 0x10000
cmovlq %r12, %rdx
movl 0x4(%rbx), %edi
movq %r14, %rsi
callq 0x4ecaf
subq %rax, %r12
jne 0x4ebb0
movq %r14, %rdi
callq 0x4610
movl $0x1, 0x40(%rbx)
jmp 0x4ec07
movq %r14, %rdi
callq 0x4610
jmp 0x4ec02
movb $0x0, (%r14)
movq %r14, %rdi
callq 0x4610
movl 0x4(%rbx), %edi
callq 0x4a50
movl $0xffffffff, 0x4(%rbx)  # imm = 0xFFFFFFFF
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0xc8, %r15d
je 0x4ebcc
cmpl $0xce, %r15d
je 0x4ebcc
movq %r14, %rdi
callq 0x4610
movl 0x4(%rbx), %edi
callq 0x4a50
leal -0x191(%r15), %eax
cmpl $0x9, %eax
ja 0x4ec5d
leaq 0x7c5f(%rip), %rcx      # 0x568ac
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl $0x1, %ebp
jmp 0x4eca3
cmpl $0x1f7, %r15d           # imm = 0x1F7
je 0x4ec9e
cmpl $0x1f8, %r15d           # imm = 0x1F8
jne 0x4ec7d
movl $0x6e, %ebp
jmp 0x4eca3
movl $0x2, %ebp
jmp 0x4eca3
addl $0xfffffe70, %r15d      # imm = 0xFFFFFE70
cmpl $0x64, %r15d
movl $0x16, %eax
movl $0x5, %ebp
cmovbl %eax, %ebp
jmp 0x4eca3
movl $0xd, %ebp
jmp 0x4eca3
movl $0xb, %ebp
callq 0x4070
movl %ebp, (%rax)
jmp 0x4ebfb