// File: /workspace/github_cpp_makefile_star2_O2/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:
pushq %rbp
pushq %r14
pushq %rbx
subq $0x50, %rsp
movq %rdi, %rsi
leaq 0x10(%rsp), %rbx
movq %rbx, %rdi
callq 0x22c0
leaq 0x40(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
xorl %ebp, %ebp
cmpq $0x0, 0x8(%rbx)
je 0x486f
leaq 0x1e3f(%rip), %rsi      # 0x65d9
leaq 0x10(%rsp), %rdi
callq 0x20b0
pushq $-0x1
popq %rbx
testl %eax, %eax
je 0x47c3
leaq 0x10(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x4589
cmpl $-0x1, %eax
je 0x48b7
cmpq $0x0, 0x38(%rsp)
je 0x4829
leaq 0x8(%rsp), %rdi
callq 0x22b0
cmpl $-0x1, %eax
je 0x48d6
movslq 0x8(%rsp), %rdi
cmpq %rdi, 0x5a7e(%rip)      # 0xa268
jle 0x48f5
callq 0x2090
testl %eax, %eax
je 0x4977
movl %eax, %r14d
cmpl $-0x1, %eax
je 0x4936
movl 0xc(%rsp), %edi
callq 0x23d0
movl 0x8(%rsp), %ebp
leaq 0x5a03(%rip), %rdi      # 0xa220
movl %ebp, %esi
movl %r14d, %edx
callq 0x50f2
jmp 0x486f
cmpq $0x0, 0x18(%rsp)
je 0x486f
leaq 0x1da1(%rip), %rsi      # 0x65d9
leaq 0x10(%rsp), %rdi
callq 0x20b0
testl %eax, %eax
je 0x486f
movq 0x10(%rsp), %rdi
xorl %esi, %esi
xorl %eax, %eax
callq 0x21c0
movl %eax, %ebp
cmpl $-0x1, %eax
je 0x4a0f
movslq %ebp, %rax
cmpq %rax, 0x59ff(%rip)      # 0xa268
jle 0x4a25
movslq %ebp, %rbx
leaq (,%rbx,8), %rdi
addq 0x59ef(%rip), %rdi      # 0xa270
movl $0x8001, %esi           # imm = 0x8001
callq 0x5110
movq 0x59de(%rip), %rax      # 0xa270
movq (%rax,%rbx,8), %rax
movq 0x10(%rax), %rax
movb $0x0, (%rax)
movq 0x59d4(%rip), %rax      # 0xa278
andq $0x0, (%rax,%rbx,8)
movq 0x59d0(%rip), %rax      # 0xa280
andq $0x0, (%rax,%rbx,8)
movl %ebp, %ebx
leaq 0x30(%rsp), %rdi
callq 0x2120
leaq 0x10(%rsp), %rdi
callq 0x2120
movl %ebx, %eax
addq $0x50, %rsp
popq %rbx
popq %r14
popq %rbp
retq
movq 0x56f3(%rip), %rax      # 0x9fd0
movq (%rax), %r14
callq 0x2040
movl (%rax), %edi
callq 0x20d0
leaq 0x1dce(%rip), %rsi      # 0x66c1
jmp 0x4927
callq 0x23d0
movl 0xc(%rsp), %edi
callq 0x23d0
callq 0x2040
movl $0x17, (%rax)
movq 0x56bb(%rip), %rax      # 0x9fd0
movq (%rax), %r14
pushq $0x17
popq %rdi
callq 0x20d0
leaq 0x1dab(%rip), %rsi      # 0x66d2
movq %r14, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x22f0
jmp 0x48b7
movq 0x5693(%rip), %rax      # 0x9fd0
movq (%rax), %r14
callq 0x2040
movl (%rax), %edi
callq 0x20d0
leaq 0x1d90(%rip), %rsi      # 0x66e3
movq %r14, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x22f0
movl 0x8(%rsp), %edi
callq 0x23d0
movl 0xc(%rsp), %edi
callq 0x23d0
jmp 0x48b7
xorl %edi, %edi
callq 0x23d0
movl 0x8(%rsp), %edi
callq 0x23d0
movl 0xc(%rsp), %edi
pushq $0x1
popq %rsi
callq 0x2220
cmpl $-0x1, %eax
jne 0x49a4
leaq 0x1d55(%rip), %rbx      # 0x66f4
jmp 0x4a5f
movl 0xc(%rsp), %edi
callq 0x23d0
pushq $0x3
popq %rbx
cmpq %rbx, 0x58b1(%rip)      # 0xa268
jle 0x49c5
movl %ebx, %edi
callq 0x23d0
incq %rbx
jmp 0x49b0
leaq 0x1cd8(%rip), %rsi      # 0x66a4
leaq 0x30(%rsp), %rdi
callq 0x50e5
movq 0x10(%rsp), %r8
testb %al, %al
je 0x4a31
leaq 0x1d1f(%rip), %rdi      # 0x6705
leaq 0x1d1e(%rip), %rdx      # 0x670b
leaq 0x1d1a(%rip), %rcx      # 0x670e
movq %rdi, %rsi
xorl %r9d, %r9d
xorl %eax, %eax
callq 0x20c0
cmpl $-0x1, %eax
jne 0x4a85
leaq 0x1d04(%rip), %rbx      # 0x6711
jmp 0x4a5f
movq 0x55ba(%rip), %rax      # 0x9fd0
movq (%rax), %r14
callq 0x2040
movl (%rax), %edi
jmp 0x491b
movl %ebp, %edi
callq 0x23d0
jmp 0x4903
leaq 0x1cf8(%rip), %rdi      # 0x6730
leaq 0x1ccc(%rip), %rdx      # 0x670b
leaq 0x1cc8(%rip), %rcx      # 0x670e
movq %rdi, %rsi
xorl %r9d, %r9d
xorl %eax, %eax
callq 0x20c0
cmpl $-0x1, %eax
jne 0x4a85
leaq 0x1cd6(%rip), %rbx      # 0x6735
movq 0x556a(%rip), %rax      # 0x9fd0
movq (%rax), %r14
callq 0x2040
movl (%rax), %edi
callq 0x20d0
movq %r14, %rdi
movq %rbx, %rsi
movq %rax, %rdx
xorl %eax, %eax
callq 0x22f0
pushq $0x1
popq %rdi
callq 0x2330
jmp 0x4a91
jmp 0x4a91
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x2120
leaq 0x10(%rsp), %rdi
callq 0x2120
movq %rbx, %rdi
callq 0x2370