// File: /workspace/github_c_makefile_star2_O0/icebert[P]pblat/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);
    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 0xcc5a9
movq -0x10(%rbp), %rax
movl 0x4(%rax), %edi
callq 0x6e90
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rsi
callq 0xcb8d0
movl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x4(%rax)
movl $0x10000, %edi          # imm = 0x10000
movl $0x1, %esi
callq 0x6460
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 0x192cd(%rip), %rsi     # 0xe58c9
movb $0x0, %al
callq 0x6160
addl -0x18(%rbp), %eax
movl %eax, -0x18(%rbp)
movq -0x20(%rbp), %rdi
movslq -0x18(%rbp), %rax
addq %rax, %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdx
leaq 0xf8ae(%rip), %rsi      # 0xdbed1
movb $0x0, %al
callq 0x6160
addl -0x18(%rbp), %eax
movl %eax, -0x18(%rbp)
movq -0x20(%rbp), %rdi
movslq -0x18(%rbp), %rax
addq %rax, %rdi
leaq 0x15532(%rip), %rsi     # 0xe1b74
movb $0x0, %al
callq 0x6160
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 0x6250
movslq -0x18(%rbp), %rcx
cmpq %rcx, %rax
je 0xcc681
movq -0x20(%rbp), %rdi
callq 0x6880
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xcc92c
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 0x6a90
cmpq $0x0, %rax
je 0xcc6f5
movq -0x20(%rbp), %rax
movslq -0x18(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0xa, %eax
jne 0xcc6ea
cmpl $0x3, -0x18(%rbp)
jl 0xcc6ea
movq -0x20(%rbp), %rdi
movslq -0x18(%rbp), %rax
addq %rax, %rdi
addq $-0x3, %rdi
leaq 0x1920e(%rip), %rsi     # 0xe58e5
movl $0x4, %edx
callq 0x6320
cmpl $0x0, %eax
jne 0xcc6e8
jmp 0xcc6f5
jmp 0xcc6ea
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0xcc688
movq -0x20(%rbp), %rax
movslq -0x18(%rbp), %rcx
movb $0x0, (%rax,%rcx)
cmpl $0xe, -0x18(%rbp)
jge 0xcc733
movq -0x20(%rbp), %rdi
callq 0x6880
movq -0x10(%rbp), %rax
movl 0x4(%rax), %edi
callq 0x6e90
movq -0x10(%rbp), %rax
movl $0xffffffff, 0x4(%rax)  # imm = 0xFFFFFFFF
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xcc92c
movq -0x20(%rbp), %rdi
addq $0x8, %rdi
leaq -0x28(%rbp), %rsi
xorl %edx, %edx
callq 0x6a50
movl %eax, -0x14(%rbp)
cmpl $0xc8, -0x14(%rbp)
jne 0xcc7c0
movq -0x10(%rbp), %rax
cmpq $0x0, 0x8(%rax)
jle 0xcc7c0
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
je 0xcc7bb
cmpq $0x10000, -0x30(%rbp)   # imm = 0x10000
jge 0xcc784
movq -0x30(%rbp), %rax
movq %rax, -0x40(%rbp)
jmp 0xcc78f
movl $0x10000, %eax          # imm = 0x10000
movq %rax, -0x40(%rbp)
jmp 0xcc78f
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 0xcc940
movq %rax, %rcx
movq -0x30(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x30(%rbp)
jmp 0xcc769
jmp 0xcc911
cmpl $0xce, -0x14(%rbp)
je 0xcc90f
cmpl $0xc8, -0x14(%rbp)
je 0xcc90f
movq -0x20(%rbp), %rdi
callq 0x6880
movq -0x10(%rbp), %rax
movl 0x4(%rax), %edi
callq 0x6e90
movl -0x14(%rbp), %eax
movl %eax, -0x44(%rbp)
subl $0x191, %eax            # imm = 0x191
je 0xcc852
jmp 0xcc7fe
movl -0x44(%rbp), %eax
subl $0x193, %eax            # imm = 0x193
je 0xcc862
jmp 0xcc80a
movl -0x44(%rbp), %eax
subl $0x194, %eax            # imm = 0x194
je 0xcc872
jmp 0xcc816
movl -0x44(%rbp), %eax
subl $0x197, %eax            # imm = 0x197
je 0xcc87f
jmp 0xcc822
movl -0x44(%rbp), %eax
subl $0x198, %eax            # imm = 0x198
je 0xcc88c
jmp 0xcc82e
movl -0x44(%rbp), %eax
subl $0x19a, %eax            # imm = 0x19A
je 0xcc899
jmp 0xcc83a
movl -0x44(%rbp), %eax
subl $0x1f7, %eax            # imm = 0x1F7
je 0xcc8a6
jmp 0xcc846
movl -0x44(%rbp), %eax
subl $0x1f8, %eax            # imm = 0x1F8
je 0xcc8b3
jmp 0xcc8c0
callq 0x6060
movl $0x1, (%rax)
jmp 0xcc8fb
callq 0x6060
movl $0xd, (%rax)
jmp 0xcc8fb
callq 0x6060
movl $0x2, (%rax)
jmp 0xcc8fb
callq 0x6060
movl $0x1, (%rax)
jmp 0xcc8fb
callq 0x6060
movl $0x6e, (%rax)
jmp 0xcc8fb
callq 0x6060
movl $0x2, (%rax)
jmp 0xcc8fb
callq 0x6060
movl $0xb, (%rax)
jmp 0xcc8fb
callq 0x6060
movl $0x6e, (%rax)
jmp 0xcc8fb
xorl %eax, %eax
cmpl $0x190, -0x14(%rbp)     # imm = 0x190
movb %al, -0x45(%rbp)
jl 0xcc8db
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 0x6060
movl -0x4c(%rbp), %ecx
movl %ecx, (%rax)
movq -0x10(%rbp), %rax
movl $0xffffffff, 0x4(%rax)  # imm = 0xFFFFFFFF
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xcc92c
jmp 0xcc911
movq -0x20(%rbp), %rdi
callq 0x6880
movq -0x10(%rbp), %rax
movl $0x1, 0x40(%rax)
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)