// File: /workspace/github_cpp_makefile_star2_O1/statgen[P]statgen/lib/samtools-0.1.7a-hybrid/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 = 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);
    l += sprintf(buf + l, "Range: bytes=%lld-\r\n", (long long)fp->offset);
	l += sprintf(buf + l, "\r\n");
	netwrite(fp->fd, buf, l);
	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
		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) {
		free(buf);
		fprintf(stderr, "[khttp_connect_file] fail to open file (HTTP code: %d).\n", ret);
		netclose(fp->fd);
		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 0x1c15d
callq 0x3090
leaq 0x4(%rbx), %r14
movq 0x10(%rbx), %rdi
movq 0x18(%rbx), %rsi
callq 0x1ba08
movl %eax, 0x4(%rbx)
movl $0x10000, %edi          # imm = 0x10000
movl $0x1, %esi
callq 0x3530
movq %rax, %r15
movq 0x70(%rbx), %rdx
movq 0x78(%rbx), %rcx
leaq 0xb407(%rip), %rsi      # 0x27599
movq %rax, %rdi
xorl %eax, %eax
callq 0x3350
movslq %eax, %r12
leaq (%r15,%r12), %rdi
movq 0x8(%rbx), %rdx
leaq 0xb407(%rip), %rsi      # 0x275b5
xorl %eax, %eax
callq 0x3350
leal (%rax,%r12), %ecx
movslq %ecx, %rcx
movw $0xa0d, (%r15,%rcx)     # imm = 0xA0D
movb $0x0, 0x2(%r15,%rcx)
leal 0x2(%rax,%r12), %eax
movl 0x4(%rbx), %edi
movslq %eax, %rdx
movq %r15, %rsi
callq 0x3640
movl 0x4(%rbx), %edi
movl $0x1, %edx
movq %r15, %rsi
callq 0x31b0
testq %rax, %rax
je 0x1c29e
leaq -0x3(%r15), %rbp
xorl %r13d, %r13d
leaq 0xb3c7(%rip), %r12      # 0x275ca
cmpq $0x3, %r13
jb 0x1c227
cmpb $0xa, (%r15,%r13)
jne 0x1c227
movq %rbp, %rdi
addq %r13, %rdi
movl $0x4, %edx
movq %r12, %rsi
callq 0x31c0
testl %eax, %eax
je 0x1c243
movl (%r14), %edi
leaq (%r15,%r13), %rsi
incq %rsi
incq %r13
movl $0x1, %edx
callq 0x31b0
testq %rax, %rax
jne 0x1c203
leaq (%r15,%r13), %rax
movb $0x0, (%rax)
cmpl $0xe, %r13d
jb 0x1c2a2
movq %r15, %rdi
addq $0x8, %rdi
movq %rsp, %rsi
xorl %ebp, %ebp
xorl %edx, %edx
callq 0x3400
cmpl $0xce, %eax
je 0x1c2c6
cmpl $0xc8, %eax
jne 0x1c2e6
movq 0x8(%rbx), %r12
testq %r12, %r12
jle 0x1c2b8
movl $0x10000, %r13d         # imm = 0x10000
cmpq %r13, %r12
movl $0x10000, %edx          # imm = 0x10000
cmovlq %r12, %rdx
movl (%r14), %edi
movq %r15, %rsi
callq 0x1c2f5
subq %rax, %r12
jne 0x1c280
jmp 0x1c2c6
movb $0x0, (%r15)
movl (%r14), %edi
callq 0x3090
movl $0xffffffff, (%r14)     # imm = 0xFFFFFFFF
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0x1c2d5
cmpl $0xc8, %eax
je 0x1c2c6
cmpl $0xce, %eax
jne 0x1c2e6
movq %r15, %rdi
callq 0x32c0
movl $0x1, 0x40(%rbx)
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r15, %rdi
movl %eax, %esi
movq %r14, %rdx
callq 0x4b24
jmp 0x1c2b1