// File: conchoecia[P]fasta_stats/src/open_compressed.cc
// Function: pfread(int, void*, unsigned long)
ssize_t pfread(const int fd, void *ptr, const size_t size) {
	if (fd < 0 || local.open_max <= fd) {
		fprintf(stderr, "Error: pfgets: fd out of range: %d\n", fd);
		return -1;
	}
	if (local.buffers[fd].empty()) {
		fprintf(stderr, "Error: pfgets: buffer unallocated\n");
		return -1;
	}
	char *s = (char *)ptr;
	size_t k = size;
	char * const buf = local.buffers[fd].array();
	ssize_t &i = local.buffer_start[fd];
	ssize_t &j = local.buffer_length[fd];
	for (;;) {
		if ((size_t)(j - i) >= k) {
			memcpy(s, buf + i, k);
			i += k;
			return size;
		}
		const size_t n = j - i;
		memcpy(s, buf + i, n);
		s += n;
		k -= n;
		i = 0;
		j = read(fd, buf, BUFSIZE);
		if (j <= 0) {
			if (j == -1) {
				fprintf(stderr, "Error: read(%d): %s\n", fd, strerror(errno));
			}
			buf[j = 0] = '\0';
			/* only return -1 if we don't return anything else */
			return k == size ? -1 : (ssize_t)(size - k);
		}
		buf[j] = '\0';
	}
}

// Assembly:
subq $0x68, %rsp
movl %edi, 0x5c(%rsp)
movq %rsi, 0x50(%rsp)
movq %rdx, 0x48(%rsp)
cmpl $0x0, 0x5c(%rsp)
jl 0xb1ba
movq 0xb120(%rip), %rax      # 0x162d0
movslq 0x5c(%rsp), %rcx
cmpq %rcx, %rax
jg 0xb1e4
movq 0xae0f(%rip), %rax      # 0x15fd0
movq (%rax), %rdi
movl 0x5c(%rsp), %edx
leaq 0x4606(%rip), %rsi      # 0xf7d5
movb $0x0, %al
callq 0x2380
movq $-0x1, 0x60(%rsp)
jmp 0xb40a
movq 0xb0ed(%rip), %rdi      # 0x162d8
movslq 0x5c(%rsp), %rax
shlq $0x3, %rax
addq %rax, %rdi
callq 0xbcc0
testb $0x1, %al
jne 0xb202
jmp 0xb228
movq 0xadc7(%rip), %rax      # 0x15fd0
movq (%rax), %rdi
leaq 0x45e6(%rip), %rsi      # 0xf7f9
movb $0x0, %al
callq 0x2380
movq $-0x1, 0x60(%rsp)
jmp 0xb40a
movq 0x50(%rsp), %rax
movq %rax, 0x40(%rsp)
movq 0x48(%rsp), %rax
movq %rax, 0x38(%rsp)
movq 0xb095(%rip), %rdi      # 0x162d8
movslq 0x5c(%rsp), %rax
shlq $0x3, %rax
addq %rax, %rdi
callq 0xbce0
movq %rax, 0x30(%rsp)
movq 0xb080(%rip), %rax      # 0x162e0
movslq 0x5c(%rsp), %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, 0x28(%rsp)
movq 0xb070(%rip), %rax      # 0x162e8
movslq 0x5c(%rsp), %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, 0x20(%rsp)
movq 0x20(%rsp), %rax
movq (%rax), %rax
movq 0x28(%rsp), %rcx
subq (%rcx), %rax
cmpq 0x38(%rsp), %rax
jb 0xb2db
movq 0x40(%rsp), %rdi
movq 0x30(%rsp), %rsi
movq 0x28(%rsp), %rax
addq (%rax), %rsi
movq 0x38(%rsp), %rdx
callq 0x2220
movq 0x38(%rsp), %rcx
movq 0x28(%rsp), %rax
addq (%rax), %rcx
movq %rcx, (%rax)
movq 0x48(%rsp), %rax
movq %rax, 0x60(%rsp)
jmp 0xb40a
movq 0x20(%rsp), %rax
movq (%rax), %rax
movq 0x28(%rsp), %rcx
subq (%rcx), %rax
movq %rax, 0x18(%rsp)
movq 0x40(%rsp), %rdi
movq 0x30(%rsp), %rsi
movq 0x28(%rsp), %rax
addq (%rax), %rsi
movq 0x18(%rsp), %rdx
callq 0x2220
movq 0x18(%rsp), %rax
addq 0x40(%rsp), %rax
movq %rax, 0x40(%rsp)
movq 0x18(%rsp), %rcx
movq 0x38(%rsp), %rax
subq %rcx, %rax
movq %rax, 0x38(%rsp)
movq 0x28(%rsp), %rax
movq $0x0, (%rax)
movl 0x5c(%rsp), %edi
movq 0x30(%rsp), %rsi
movl $0x8000, %edx           # imm = 0x8000
callq 0x2390
movq %rax, %rcx
movq 0x20(%rsp), %rax
movq %rcx, (%rax)
movq 0x20(%rsp), %rax
cmpq $0x0, (%rax)
jg 0xb3f4
movq 0x20(%rsp), %rax
cmpq $-0x1, (%rax)
jne 0xb3ae
movq 0xac58(%rip), %rax      # 0x15fd0
movq (%rax), %rax
movq %rax, 0x8(%rsp)
movl 0x5c(%rsp), %eax
movl %eax, 0x14(%rsp)
callq 0x2040
movl (%rax), %edi
callq 0x20d0
movq 0x8(%rsp), %rdi
movl 0x14(%rsp), %edx
movq %rax, %rcx
leaq 0x4475(%rip), %rsi      # 0xf81c
movb $0x0, %al
callq 0x2380
movq 0x30(%rsp), %rax
movq 0x20(%rsp), %rcx
movq $0x0, (%rcx)
movb $0x0, (%rax)
movq 0x38(%rsp), %rax
cmpq 0x48(%rsp), %rax
jne 0xb3db
movq $-0x1, %rax
movq %rax, (%rsp)
jmp 0xb3e9
movq 0x48(%rsp), %rax
subq 0x38(%rsp), %rax
movq %rax, (%rsp)
movq (%rsp), %rax
movq %rax, 0x60(%rsp)
jmp 0xb40a
movq 0x30(%rsp), %rax
movq 0x20(%rsp), %rcx
movq (%rcx), %rcx
movb $0x0, (%rax,%rcx)
jmp 0xb289
movq 0x60(%rsp), %rax
addq $0x68, %rsp
retq
nopw %cs:(%rax,%rax)