// File: /workspace/github_c_makefile_star2_O0/genometools[P]genometools/src/external/samtools-0.1.18/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:
subq $0x48, %rsp
movq %rdi, 0x38(%rsp)
movl $0x0, 0x30(%rsp)
movq 0x38(%rsp), %rax
cmpl $-0x1, 0x4(%rax)
je 0x483939
movq 0x38(%rsp), %rax
movl 0x4(%rax), %edi
callq 0x2f750
movq 0x38(%rsp), %rax
movq 0x10(%rax), %rdi
movq 0x38(%rsp), %rax
movq 0x18(%rax), %rsi
callq 0x482b70
movl %eax, %ecx
movq 0x38(%rsp), %rax
movl %ecx, 0x4(%rax)
movl $0x10000, %edi          # imm = 0x10000
movl $0x1, %esi
callq 0x32f50
movq %rax, 0x28(%rsp)
movq 0x28(%rsp), %rdi
movslq 0x30(%rsp), %rax
addq %rax, %rdi
movq 0x38(%rsp), %rax
movq 0x70(%rax), %rdx
movq 0x38(%rsp), %rax
movq 0x78(%rax), %rcx
leaq 0xf75d2(%rip), %rsi     # 0x57af66
movb $0x0, %al
callq 0x31400
addl 0x30(%rsp), %eax
movl %eax, 0x30(%rsp)
movq 0x28(%rsp), %rdi
movslq 0x30(%rsp), %rax
addq %rax, %rdi
movq 0x38(%rsp), %rax
movq 0x8(%rax), %rdx
leaq 0xf75c2(%rip), %rsi     # 0x57af82
movb $0x0, %al
callq 0x31400
addl 0x30(%rsp), %eax
movl %eax, 0x30(%rsp)
movq 0x28(%rsp), %rdi
movslq 0x30(%rsp), %rax
addq %rax, %rdi
leaq 0xf75b6(%rip), %rsi     # 0x57af99
movb $0x0, %al
callq 0x31400
addl 0x30(%rsp), %eax
movl %eax, 0x30(%rsp)
movq 0x38(%rsp), %rax
movl 0x4(%rax), %edi
movq 0x28(%rsp), %rsi
movslq 0x30(%rsp), %rdx
callq 0x33a30
movl $0x0, 0x30(%rsp)
movq 0x38(%rsp), %rax
movl 0x4(%rax), %edi
movq 0x28(%rsp), %rsi
movslq 0x30(%rsp), %rax
addq %rax, %rsi
movl $0x1, %edx
callq 0x30190
cmpq $0x0, %rax
je 0x483a88
movq 0x28(%rsp), %rax
movslq 0x30(%rsp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0xa, %eax
jne 0x483a7b
cmpl $0x3, 0x30(%rsp)
jl 0x483a7b
movq 0x28(%rsp), %rdi
movslq 0x30(%rsp), %rax
addq %rax, %rdi
addq $-0x3, %rdi
leaq 0xf752f(%rip), %rsi     # 0x57af97
movl $0x4, %edx
callq 0x301c0
cmpl $0x0, %eax
jne 0x483a79
jmp 0x483a88
jmp 0x483a7b
movl 0x30(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x30(%rsp)
jmp 0x483a11
movq 0x28(%rsp), %rax
movslq 0x30(%rsp), %rcx
movb $0x0, (%rax,%rcx)
cmpl $0xe, 0x30(%rsp)
jge 0x483ac3
movq 0x38(%rsp), %rax
movl 0x4(%rax), %edi
callq 0x2f750
movq 0x38(%rsp), %rax
movl $0xffffffff, 0x4(%rax)  # imm = 0xFFFFFFFF
movl $0xffffffff, 0x44(%rsp) # imm = 0xFFFFFFFF
jmp 0x483bdd
movq 0x28(%rsp), %rdi
addq $0x8, %rdi
leaq 0x20(%rsp), %rsi
xorl %edx, %edx
callq 0x31c30
movl %eax, 0x34(%rsp)
cmpl $0xc8, 0x34(%rsp)
jne 0x483b60
movq 0x38(%rsp), %rax
cmpq $0x0, 0x8(%rax)
jle 0x483b60
movq 0x38(%rsp), %rax
movq 0x8(%rax), %rax
movq %rax, 0x18(%rsp)
cmpq $0x0, 0x18(%rsp)
je 0x483b5e
cmpq $0x10000, 0x18(%rsp)    # imm = 0x10000
jge 0x483b1f
movq 0x18(%rsp), %rax
movq %rax, 0x8(%rsp)
jmp 0x483b2b
movl $0x10000, %eax          # imm = 0x10000
movq %rax, 0x8(%rsp)
jmp 0x483b2b
movq 0x8(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0x38(%rsp), %rax
movl 0x4(%rax), %edi
movq 0x28(%rsp), %rsi
movq 0x10(%rsp), %rdx
callq 0x483bf0
movq %rax, %rcx
movq 0x18(%rsp), %rax
subq %rcx, %rax
movq %rax, 0x18(%rsp)
jmp 0x483b00
jmp 0x483bbf
cmpl $0xce, 0x34(%rsp)
je 0x483bbd
cmpl $0xc8, 0x34(%rsp)
je 0x483bbd
movq 0x28(%rsp), %rdi
callq 0x34078
movq 0x178443(%rip), %rax    # 0x5fbfc8
movq (%rax), %rdi
movl 0x34(%rsp), %edx
leaq 0xf7409(%rip), %rsi     # 0x57af9c
movb $0x0, %al
callq 0x33960
movq 0x38(%rsp), %rax
movl 0x4(%rax), %edi
callq 0x2f750
movq 0x38(%rsp), %rax
movl $0xffffffff, 0x4(%rax)  # imm = 0xFFFFFFFF
movl $0xffffffff, 0x44(%rsp) # imm = 0xFFFFFFFF
jmp 0x483bdd
jmp 0x483bbf
movq 0x28(%rsp), %rdi
callq 0x34078
movq 0x38(%rsp), %rax
movl $0x1, 0x40(%rax)
movl $0x0, 0x44(%rsp)
movl 0x44(%rsp), %eax
addq $0x48, %rsp
retq
nopw %cs:(%rax,%rax)