// File: conchoecia[P]fasta_stats/src/open_compressed.cc
// Function: open_compressed(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&)
int open_compressed(const std::string &filename) {
	int fd = -1;
	std::string s(filename);
	std::string suffix;
	/* see if file exists */
	if (!s.empty() && s.compare("-") != 0 && find_suffix(s, suffix) == -1) {
		return -1;
	}
	if (!suffix.empty()) {
		pid_t pid;
		int pipefd[2];
		if (pipe(pipefd) == -1) {
			fprintf(stderr, "Error: pipe: %s\n", strerror(errno));
			return -1;
		} else if (pipefd[0] >= local.open_max) { // too many open files
			close(pipefd[0]);
			close(pipefd[1]);
			errno = ENFILE;
			fprintf(stderr, "Error: open: %s\n", strerror(errno));
			return -1;
		} else if ((pid = fork()) == -1) {
			fprintf(stderr, "Error: fork: %s\n", strerror(errno));
			close(pipefd[0]);
			close(pipefd[1]);
			return -1;
		} else if (pid == 0) {	/* child */
			close(0);
			close(pipefd[0]);
			if (dup2(pipefd[1], 1) == -1) {
				fprintf(stderr, "Error: dup2: %s\n", strerror(errno));
				_exit(1);
			}
			close(pipefd[1]);
			for (int i = 3; i < local.open_max; ++i) {
				close(i);
			}
			if (suffix == ".bz2") {
				if (execlp("bzip2", "bzip2", "-d", "-c", s.c_str(), NULL) == -1) {
					fprintf(stderr, "Error: execlp bzip2 -d -c: %s\n", strerror(errno));
				}
			} else {
				if (execlp("gzip", "gzip", "-d", "-c", s.c_str(), NULL) == -1) {
					fprintf(stderr, "Error: execlp gzip -d -c: %s\n", strerror(errno));
				}
			}
			_exit(1);
		} else {		/* parent */
			close(pipefd[1]);
			fd = pipefd[0];
			local.add_open(fd, pid);
		}
	} else if (s.empty() || s.compare("-") == 0) {
		fd = 0;
	} else if ((fd = open(s.c_str(), O_RDONLY)) == -1) {
		fprintf(stderr, "Error: open: %s\n", strerror(errno));
		return -1;
	} else if (fd >= local.open_max) {	// too many open files
		close(fd);
		errno = ENFILE;
		fprintf(stderr, "Error: open: %s\n", strerror(errno));
		return -1;
	}
	// make sure to leave extra spot for trailing null
	local.buffers[fd].resize(BUFSIZE + 1);
	local.buffers[fd][0] = '\0';
	local.buffer_start[fd] = 0;
	local.buffer_length[fd] = 0;
	return fd;
}

// Assembly:
subq $0xd8, %rsp
movq %rdi, 0xc8(%rsp)
movl $0xffffffff, 0xc4(%rsp) # imm = 0xFFFFFFFF
movq 0xc8(%rsp), %rsi
leaq 0xa0(%rsp), %rdi
callq 0x2120
leaq 0x80(%rsp), %rdi
callq 0x2370
leaq 0xa0(%rsp), %rdi
callq 0x23f0
testb $0x1, %al
jne 0xa9e3
leaq 0xa0(%rsp), %rdi
leaq 0x4dd1(%rip), %rsi      # 0xf741
callq 0x20b0
cmpl $0x0, %eax
je 0xa9e3
leaq 0xa0(%rsp), %rdi
leaq 0x80(%rsp), %rsi
callq 0xa540
movl %eax, 0x5c(%rsp)
jmp 0xa995
movl 0x5c(%rsp), %eax
cmpl $-0x1, %eax
jne 0xa9e3
movl $0xffffffff, 0xd4(%rsp) # imm = 0xFFFFFFFF
movl $0x1, 0x70(%rsp)
jmp 0xaedb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x78(%rsp)
movl %eax, 0x74(%rsp)
leaq 0x80(%rsp), %rdi
callq 0x21a0
leaq 0xa0(%rsp), %rdi
callq 0x21a0
jmp 0xaf04
leaq 0x80(%rsp), %rdi
callq 0x23f0
testb $0x1, %al
jne 0xad23
leaq 0x64(%rsp), %rdi
callq 0x2340
cmpl $-0x1, %eax
jne 0xaa50
movq 0xb5c2(%rip), %rax      # 0x15fd0
movq (%rax), %rax
movq %rax, 0x50(%rsp)
callq 0x2040
movl (%rax), %edi
callq 0x20d0
movq 0x50(%rsp), %rdi
movq %rax, %rdx
leaq 0x4d12(%rip), %rsi      # 0xf743
movb $0x0, %al
callq 0x2380
movl $0xffffffff, 0xd4(%rsp) # imm = 0xFFFFFFFF
movl $0x1, 0x70(%rsp)
jmp 0xaedb
movslq 0x64(%rsp), %rax
cmpq 0xb874(%rip), %rax      # 0x162d0
jl 0xaac8
movl 0x64(%rsp), %edi
callq 0x2460
jmp 0xaa69
movl 0x68(%rsp), %edi
callq 0x2460
jmp 0xaa74
callq 0x2040
movl $0x17, (%rax)
movq 0xb54a(%rip), %rax      # 0x15fd0
movq (%rax), %rax
movq %rax, 0x48(%rsp)
callq 0x2040
movl (%rax), %edi
callq 0x20d0
movq 0x48(%rsp), %rdi
movq %rax, %rdx
leaq 0x4cab(%rip), %rsi      # 0xf754
movb $0x0, %al
callq 0x2380
movl $0xffffffff, 0xd4(%rsp) # imm = 0xFFFFFFFF
movl $0x1, 0x70(%rsp)
jmp 0xaedb
callq 0x2090
movl %eax, 0x6c(%rsp)
cmpl $-0x1, %eax
jne 0xab35
movq 0xb4f3(%rip), %rax      # 0x15fd0
movq (%rax), %rax
movq %rax, 0x40(%rsp)
callq 0x2040
movl (%rax), %edi
callq 0x20d0
movq 0x40(%rsp), %rdi
movq %rax, %rdx
leaq 0x4c65(%rip), %rsi      # 0xf765
xorl %eax, %eax
callq 0x2380
movl 0x64(%rsp), %edi
callq 0x2460
jmp 0xab12
movl 0x68(%rsp), %edi
callq 0x2460
jmp 0xab1d
movl $0xffffffff, 0xd4(%rsp) # imm = 0xFFFFFFFF
movl $0x1, 0x70(%rsp)
jmp 0xaedb
cmpl $0x0, 0x6c(%rsp)
jne 0xace7
xorl %edi, %edi
callq 0x2460
jmp 0xab49
movl 0x64(%rsp), %edi
callq 0x2460
jmp 0xab54
movl 0x68(%rsp), %edi
movl $0x1, %esi
callq 0x2250
cmpl $-0x1, %eax
jne 0xaba4
movq 0xb462(%rip), %rax      # 0x15fd0
movq (%rax), %rax
movq %rax, 0x38(%rsp)
callq 0x2040
movl (%rax), %edi
callq 0x20d0
movq 0x38(%rsp), %rdi
movq %rax, %rdx
leaq 0x4be5(%rip), %rsi      # 0xf776
xorl %eax, %eax
callq 0x2380
movl $0x1, %edi
callq 0x23c0
jmp 0xaba4
movl 0x68(%rsp), %edi
callq 0x2460
jmp 0xabaf
movl $0x3, 0x60(%rsp)
movslq 0x60(%rsp), %rax
cmpq 0xb70d(%rip), %rax      # 0x162d0
jge 0xabdf
movl 0x60(%rsp), %edi
callq 0x2460
jmp 0xabd0
jmp 0xabd2
movl 0x60(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x60(%rsp)
jmp 0xabb7
leaq 0x4b3e(%rip), %rsi      # 0xf724
leaq 0x80(%rsp), %rdi
callq 0xbad0
movb %al, 0x37(%rsp)
jmp 0xabf9
movb 0x37(%rsp), %al
testb $0x1, %al
jne 0xac03
jmp 0xac6f
leaq 0xa0(%rsp), %rdi
callq 0x2110
movq %rax, %r8
leaq 0x4b6d(%rip), %rsi      # 0xf787
leaq 0x4b6c(%rip), %rdx      # 0xf78d
leaq 0x4b68(%rip), %rcx      # 0xf790
xorl %eax, %eax
movl %eax, %r9d
movq %rsi, %rdi
movb $0x0, %al
callq 0x20c0
cmpl $-0x1, %eax
jne 0xac6d
movq 0xb38d(%rip), %rax      # 0x15fd0
movq (%rax), %rax
movq %rax, 0x28(%rsp)
callq 0x2040
movl (%rax), %edi
callq 0x20d0
movq 0x28(%rsp), %rdi
movq %rax, %rdx
leaq 0x4b2d(%rip), %rsi      # 0xf793
movb $0x0, %al
callq 0x2380
jmp 0xacdb
leaq 0xa0(%rsp), %rdi
callq 0x2110
movq %rax, %r8
leaq 0x4b2c(%rip), %rsi      # 0xf7b2
leaq 0x4b00(%rip), %rdx      # 0xf78d
leaq 0x4afc(%rip), %rcx      # 0xf790
xorl %eax, %eax
movl %eax, %r9d
movq %rsi, %rdi
movb $0x0, %al
callq 0x20c0
cmpl $-0x1, %eax
jne 0xacd9
movq 0xb321(%rip), %rax      # 0x15fd0
movq (%rax), %rax
movq %rax, 0x20(%rsp)
callq 0x2040
movl (%rax), %edi
callq 0x20d0
movq 0x20(%rsp), %rdi
movq %rax, %rdx
leaq 0x4ae5(%rip), %rsi      # 0xf7b7
movb $0x0, %al
callq 0x2380
jmp 0xacdb
movl $0x1, %edi
callq 0x23c0
jmp 0xace7
movl 0x68(%rsp), %edi
callq 0x2460
jmp 0xacf2
movl 0x64(%rsp), %eax
movl %eax, 0xc4(%rsp)
movl 0xc4(%rsp), %esi
movl 0x6c(%rsp), %edx
leaq 0xb579(%rip), %rdi      # 0x16288
callq 0xbb00
jmp 0xad16
jmp 0xad18
jmp 0xad1a
jmp 0xad1c
jmp 0xad1e
jmp 0xae4f
leaq 0xa0(%rsp), %rdi
callq 0x23f0
testb $0x1, %al
jne 0xad4d
leaq 0xa0(%rsp), %rdi
leaq 0x49fe(%rip), %rsi      # 0xf741
callq 0x20b0
cmpl $0x0, %eax
jne 0xad5d
movl $0x0, 0xc4(%rsp)
jmp 0xae4d
leaq 0xa0(%rsp), %rdi
callq 0x2110
movq %rax, %rdi
xorl %esi, %esi
movb %sil, %al
callq 0x21f0
movl %eax, 0x1c(%rsp)
jmp 0xad7d
movl 0x1c(%rsp), %eax
movl %eax, 0xc4(%rsp)
cmpl $-0x1, %eax
jne 0xadd6
movq 0xb23c(%rip), %rax      # 0x15fd0
movq (%rax), %rax
movq %rax, 0x10(%rsp)
callq 0x2040
movl (%rax), %edi
callq 0x20d0
movq 0x10(%rsp), %rdi
movq %rax, %rdx
leaq 0x499d(%rip), %rsi      # 0xf754
movb $0x0, %al
callq 0x2380
movl $0xffffffff, 0xd4(%rsp) # imm = 0xFFFFFFFF
movl $0x1, 0x70(%rsp)
jmp 0xaedb
movslq 0xc4(%rsp), %rax
cmpq 0xb4eb(%rip), %rax      # 0x162d0
jl 0xae49
movl 0xc4(%rsp), %edi
callq 0x2460
jmp 0xadf5
callq 0x2040
movl $0x17, (%rax)
movq 0xb1c9(%rip), %rax      # 0x15fd0
movq (%rax), %rax
movq %rax, 0x8(%rsp)
callq 0x2040
movl (%rax), %edi
callq 0x20d0
movq 0x8(%rsp), %rdi
movq %rax, %rdx
leaq 0x492a(%rip), %rsi      # 0xf754
movb $0x0, %al
callq 0x2380
movl $0xffffffff, 0xd4(%rsp) # imm = 0xFFFFFFFF
movl $0x1, 0x70(%rsp)
jmp 0xaedb
jmp 0xae4b
jmp 0xae4d
jmp 0xae4f
movq 0xb482(%rip), %rax      # 0x162d8
movslq 0xc4(%rsp), %rcx
leaq (%rax,%rcx,8), %rdi
movl $0x8001, %esi           # imm = 0x8001
callq 0xbb40
jmp 0xae6e
movq 0xb463(%rip), %rax      # 0x162d8
movslq 0xc4(%rsp), %rcx
leaq (%rax,%rcx,8), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0xbb70
movq %rax, (%rsp)
jmp 0xae90
movq (%rsp), %rax
movb $0x0, (%rax)
movq 0xb442(%rip), %rax      # 0x162e0
movslq 0xc4(%rsp), %rcx
movq $0x0, (%rax,%rcx,8)
movq 0xb433(%rip), %rax      # 0x162e8
movslq 0xc4(%rsp), %rcx
movq $0x0, (%rax,%rcx,8)
movl 0xc4(%rsp), %eax
movl %eax, 0xd4(%rsp)
movl $0x1, 0x70(%rsp)
leaq 0x80(%rsp), %rdi
callq 0x21a0
leaq 0xa0(%rsp), %rdi
callq 0x21a0
movl 0xd4(%rsp), %eax
addq $0xd8, %rsp
retq
movq 0x78(%rsp), %rdi
callq 0x2420
nop