// File: /workspace/github_c_makefile_star2_O3/ToniWestbrook[P]paladin/kopen.c
// Function: kopen
void *kopen(const char *fn, int *_fd)
{
	koaux_t *aux = 0;
	*_fd = -1;
	if (strstr(fn, "http://") == fn) {
		aux = calloc(1, sizeof(koaux_t));
		aux->type = KO_HTTP;
		aux->fd = http_open(fn);
	} else if (strstr(fn, "ftp://") == fn) {
		aux = calloc(1, sizeof(koaux_t));
		aux->type = KO_FTP;
		aux->fd = ftp_open(fn);
	} else if (strcmp(fn, "-") == 0) {
		aux = calloc(1, sizeof(koaux_t));
		aux->type = KO_STDIN;
		aux->fd = STDIN_FILENO;
	} else {
		const char *p, *q;
		for (p = fn; *p; ++p)
			if (!isspace(*p)) break;
		if (*p == '<') { // pipe open
			int need_shell, pfd[2];
			pid_t pid;
			// a simple check to see if we need to invoke a shell; not always working
			for (q = p + 1; *q; ++q)
				if (ispunct(*q) && *q != '.' && *q != '_' && *q != '-' && *q != ':')
					break;
			need_shell = (*q != 0);
			if (pipe(pfd) != 0) return 0;
			pid = vfork();
			if (pid == -1) { /* vfork() error */
				close(pfd[0]); close(pfd[1]);
				return 0;
			}
			if (pid == 0) { /* the child process */
				char **argv; /* FIXME: I do not know if this will lead to a memory leak */
				close(pfd[0]);
				dup2(pfd[1], STDOUT_FILENO);
				close(pfd[1]);
				if (!need_shell) {
					argv = cmd2argv(p + 1);
					execvp(argv[0], argv);
					free(argv[0]); free(argv);
				} else execl("/bin/sh", "sh", "-c", p + 1, NULL);
				exit(1);
			} else { /* parent process */
				close(pfd[1]);
				aux = calloc(1, sizeof(koaux_t));
				aux->type = KO_PIPE;
				aux->fd = pfd[0];
				aux->pid = pid;
			}
		} else {
#ifdef _WIN32
			*_fd = open(fn, O_RDONLY | O_BINARY);
#else
			*_fd = open(fn, O_RDONLY);
#endif
			if (*_fd >= 0) {
				aux = calloc(1, sizeof(koaux_t));
				aux->type = KO_FILE;
				aux->fd = *_fd;
			}
		}
	}
	if (aux) *_fd = aux->fd;
	return aux;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x108, %rsp            # imm = 0x108
movq %rsi, %r12
movq %rdi, %r15
movl $0xffffffff, (%rsi)     # imm = 0xFFFFFFFF
leaq 0x1f0a7(%rip), %rsi     # 0x25683
movl $0x7, %edx
callq 0x3120
testl %eax, %eax
je 0x6652
leaq 0x1f0a8(%rip), %rsi     # 0x25699
movl $0x6, %edx
movq %r15, %rdi
callq 0x3120
testl %eax, %eax
je 0x66d0
movzbl (%r15), %eax
testl %eax, %eax
je 0x689e
cmpl $0x2d, %eax
jne 0x6622
cmpb $0x0, 0x1(%r15)
je 0x69da
movl %eax, %ebx
callq 0x3320
movq (%rax), %rax
leaq 0x1(%r15), %r14
movl %ebx, %ecx
movsbq %cl, %rdx
testb $0x20, 0x1(%rax,%rdx,2)
je 0x67cd
movb (%r14), %dl
incq %r14
movl %edx, %ecx
testb %dl, %dl
jne 0x6632
jmp 0x689e
leaq 0x1f032(%rip), %rdx     # 0x2568b
leaq 0x1f033(%rip), %r8      # 0x25693
movl $0x1, %edi
movl $0xc, %esi
movl $0x10f, %ecx            # imm = 0x10F
callq 0x2319c
movl $0x4, (%rax)
movq %rax, %rbx
leaq 0x1efff(%rip), %rsi     # 0x25683
movl $0x7, %edx
movq %r15, %rdi
callq 0x3120
xorl %ecx, %ecx
testl %eax, %eax
jne 0x6c9a
movabsq $0x100000000, %rax      # imm = 0x100000000
leaq 0x7(%r15), %rbp
xorl %ecx, %ecx
movl %ecx, %r13d
movq %rbp, 0x70(%rsp)
movzbl (%rbp), %ecx
testl %ecx, %ecx
je 0x6754
cmpl $0x2f, %ecx
je 0x6754
incq %rbp
addq %rax, %r13
jmp 0x66b3
leaq 0x1efb4(%rip), %rdx     # 0x2568b
leaq 0x1efb5(%rip), %r8      # 0x25693
movl $0x1, %edi
movl $0xc, %esi
movl $0x113, %ecx            # imm = 0x113
callq 0x2319c
movq %rax, %rbp
movl $0x5, (%rax)
leaq 0x1ef97(%rip), %rsi     # 0x25699
movl $0x6, %edx
movq %r15, %rdi
callq 0x3120
xorl %ecx, %ecx
testl %eax, %eax
jne 0x6f25
movq %r12, (%rsp)
movabsq $0x100000000, %r14      # imm = 0x100000000
movq %r15, %r12
addq $0x6, %r12
xorl %eax, %eax
movl %eax, %r15d
movq %r12, %r13
movzbl (%r13), %eax
testl %eax, %eax
je 0x6b84
cmpl $0x2f, %eax
je 0x6a0d
incq %r13
addq %r14, %r15
jmp 0x6736
movq %r12, (%rsp)
movq %rbp, 0xa0(%rsp)
addq %r13, %rax
movq %rax, %rdi
sarq $0x20, %rdi
leaq 0x1ef1a(%rip), %rdx     # 0x2568b
leaq 0x1ef33(%rip), %r8      # 0x256ab
movl $0x1, %esi
movl $0x57, %ecx
callq 0x2319c
movq %rax, %r12
movq %rax, %r14
sarq $0x20, %r13
movq %rax, %rdi
movq 0x70(%rsp), %rsi
movq %r13, %rdx
callq 0x3130
movb $0x0, (%r12,%r13)
movq %r14, %r12
movq %r14, %rax
movq %rbx, 0x98(%rsp)
movzbl (%rax), %ecx
testl %ecx, %ecx
je 0x68f8
cmpl $0x3a, %ecx
je 0x68f2
incq %rax
jmp 0x67b4
cmpb $0x3c, %cl
jne 0x689e
movb (%r14), %dil
movb $0x1, %cl
testb %dil, %dil
je 0x6821
leaq 0x1(%r14), %rsi
movabsq $0x4000000002003, %rdx  # imm = 0x4000000002003
movsbq %dil, %r8
movzwl (%rax,%r8,2), %r8d
testb $0x4, %r8b
je 0x6813
movzbl %dil, %r8d
xorl %edi, %edi
addl $-0x2d, %r8d
cmpl $0x32, %r8d
ja 0x6823
btq %r8, %rdx
jae 0x6823
movb (%rsi), %r8b
incq %rsi
movl %r8d, %edi
testb %r8b, %r8b
jne 0x67ee
movl %ecx, %edi
movl %edi, %ebp
leaq 0xa0(%rsp), %rdi
callq 0x3490
testl %eax, %eax
jne 0x6f5b
callq 0x34b0
cmpl $-0x1, %eax
je 0x6f43
testl %eax, %eax
je 0x6f6a
movl %eax, %ebx
movl 0xa4(%rsp), %edi
callq 0x37a0
leaq 0x1ee26(%rip), %rdx     # 0x2568b
leaq 0x1ee27(%rip), %r8      # 0x25693
movl $0x1, %edi
movl $0xc, %esi
movl $0x139, %ecx            # imm = 0x139
callq 0x2319c
movq %rax, %rbp
movl $0x3, (%rax)
movl 0xa0(%rsp), %ecx
movl %ecx, 0x4(%rax)
movl %ebx, 0x8(%rax)
movl 0x4(%rax), %eax
jmp 0x6f2a
xorl %ebp, %ebp
movl %ebp, %eax
movq %r15, %rdi
movl %ebp, %esi
callq 0x31d0
movl %eax, (%r12)
testl %eax, %eax
js 0x68eb
leaq 0x1edd0(%rip), %rdx     # 0x2568b
leaq 0x1edd1(%rip), %r8      # 0x25693
movl $0x1, %edi
movl $0xc, %esi
movl $0x145, %ecx            # imm = 0x145
callq 0x2319c
movq %rax, %rbp
movl $0x2, (%rax)
movl (%r12), %ecx
movl %ecx, 0x4(%rax)
jmp 0x6f28
movl %ebp, %eax
jmp 0x6f31
movb $0x0, (%rax)
incq %rax
movq %rax, %rbx
leaq 0x1edb3(%rip), %rdi     # 0x256b5
callq 0x33d0
testq %rax, %rax
je 0x696b
movq %rax, %r14
movq %rax, %rdi
leaq 0x1ed6a(%rip), %rsi     # 0x25683
movl $0x7, %edx
callq 0x3120
leaq 0x7(%r14), %rdi
testl %eax, %eax
cmovneq %r14, %rdi
leaq 0x1ed57(%rip), %rsi     # 0x2568b
leaq 0x1ed70(%rip), %rcx     # 0x256ab
movl $0x64, %edx
callq 0x23285
movq %rax, %rcx
movq %rax, %r13
incq %rcx
movq %rcx, %rax
movzbl -0x1(%rax), %ecx
testl %ecx, %ecx
je 0x6b8f
cmpl $0x3a, %ecx
je 0x6b6a
incq %rax
jmp 0x6951
leaq 0x1ed19(%rip), %r14     # 0x2568b
leaq 0x1ed32(%rip), %r15     # 0x256ab
movq %r12, %rdi
movq %r14, %rsi
movl $0x60, %edx
movq %r15, %rcx
callq 0x23285
movq %rax, %r13
cmpb $0x0, (%rbx)
leaq 0x1ed27(%rip), %rdi     # 0x256c0
cmovneq %rbx, %rdi
movq %r14, %rsi
movl $0x61, %edx
movq %r15, %rcx
callq 0x23285
movq %rax, %rbx
cmpb $0x0, (%rbp)
leaq 0x1ece3(%rip), %rdi     # 0x2569e
cmovneq %rbp, %rdi
movq %r14, %rsi
movl $0x62, %edx
movq %r15, %rcx
callq 0x23285
movq %rax, %rcx
movq %rbx, %rax
jmp 0x6bd6
leaq 0x1ecaa(%rip), %rdx     # 0x2568b
leaq 0x1ecab(%rip), %r8      # 0x25693
movl $0x1, %edi
movl $0xc, %esi
movl $0x117, %ecx            # imm = 0x117
callq 0x2319c
movq %rax, %rbp
movq $0x1, (%rax)
xorl %eax, %eax
jmp 0x6f2a
leaq 0x1ecf3(%rip), %rdi     # 0x25707
leaq 0x1ec70(%rip), %rsi     # 0x2568b
leaq 0x1ece8(%rip), %rbx     # 0x2570a
movl $0xbf, %edx
movq %rbx, %rcx
callq 0x23285
movq %rax, 0x68(%rsp)
movq %rax, 0x90(%rsp)
addq %r15, %r14
movq %r14, %rdi
sarq $0x20, %rdi
movl $0x1, %esi
leaq 0x1ec39(%rip), %rdx     # 0x2568b
movl $0xc0, %ecx
movq %rbx, %r8
callq 0x2319c
movq %rax, %r14
movq %rax, 0x88(%rsp)
sarq $0x20, %r15
movq %rax, %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x3130
movq %r13, %rdi
callq 0x3100
leaq 0x8(%rax), %rdi
movl $0x1, %esi
leaq 0x1ebf7(%rip), %rdx     # 0x2568b
movl $0xc2, %ecx
movq %rbx, %r8
callq 0x2319c
movq %rax, %r15
leaq 0x1ec68(%rip), %rsi     # 0x25713
xorl %ecx, %ecx
movq %rax, %rdi
movq %r13, %rdx
movl %ecx, %eax
callq 0x3070
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
movq %r14, %rdi
movq 0x68(%rsp), %rsi
callq 0x71c9
movl %eax, 0x34(%rsp)
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
cmpl $-0x1, %eax
je 0x6d92
movq %r15, 0x48(%rsp)
leaq 0x30(%rsp), %r14
movq %r14, %rdi
callq 0x7306
leaq 0x1ec22(%rip), %rsi     # 0x2571d
movl $0x1, %r15d
movq %r14, %rdi
movl %r15d, %edx
callq 0x744c
leaq 0x1ec1b(%rip), %rsi     # 0x2572e
movq %r14, %rdi
movl %r15d, %edx
callq 0x744c
leaq 0x1ec17(%rip), %rsi     # 0x2573c
movq %r14, %rdi
movl %r15d, %edx
callq 0x744c
leaq 0x1ec0e(%rip), %rsi     # 0x25745
movq %r14, %rdi
movl %r15d, %edx
callq 0x744c
movq 0x8(%r14), %rax
incq %rax
movq %rax, %rdi
movq (%rsp), %r12
movzbl -0x1(%rdi), %eax
testl %eax, %eax
je 0x6eed
cmpl $0x28, %eax
je 0x6de3
incq %rdi
jmp 0x6b50
movq %r12, %rbp
movb $0x0, -0x1(%rax)
cmpb $0x0, (%rax)
leaq 0x1eb45(%rip), %rcx     # 0x256c0
cmovneq %rax, %rcx
movq %rcx, %rdi
jmp 0x6b99
xorl %ecx, %ecx
movq (%rsp), %r12
jmp 0x6f25
movq %r12, %rbp
leaq 0x1eb27(%rip), %rdi     # 0x256c0
leaq 0x1eaeb(%rip), %r14     # 0x2568b
leaq 0x1eb04(%rip), %r12     # 0x256ab
movq %r14, %rsi
movl $0x67, %edx
movq %r12, %rcx
callq 0x23285
movq %rax, %rbx
movq %r15, %rdi
movq %r14, %rsi
movl $0x68, %edx
movq %r12, %rcx
callq 0x23285
movq %rax, %rcx
movq %rbx, %rax
movq %rbp, %r12
movq %r13, %rdi
movq %rcx, %r13
movq %rax, %rsi
movq %rdi, 0x78(%rsp)
movq %rax, 0x80(%rsp)
callq 0x71c9
movl %eax, %r15d
movl %eax, %ebp
leaq 0x1ea8e(%rip), %rdx     # 0x2568b
leaq 0x1eaa7(%rip), %r8      # 0x256ab
movl $0x10000, %ebx          # imm = 0x10000
movl $0x1, %esi
movq %rbx, %rdi
movl $0x6e, %ecx
callq 0x2319c
movq %rax, %r14
movq %rax, 0x8(%rsp)
leaq 0x1ea99(%rip), %rdx     # 0x256c3
xorl %eax, %eax
movq %r14, %rdi
movq %rbx, %rsi
movq %r13, %rcx
movq %r12, %r8
callq 0x35e0
movslq %eax, %rdx
movl %r15d, %edi
movq %r14, %rsi
callq 0x72bf
testl %eax, %eax
je 0x6ca7
movl %ebp, %edi
callq 0x37a0
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movq 0x8(%rsp), %rdi
callq 0x3390
movq %r12, %rdi
callq 0x3390
movq 0x78(%rsp), %rdi
callq 0x3390
movq 0x80(%rsp), %rdi
callq 0x3390
movq %r13, %rdi
callq 0x3390
movl %ebp, %ecx
movq (%rsp), %r12
movq 0x98(%rsp), %rbx
movl %ecx, 0x4(%rbx)
movl %ecx, %eax
movq %rbx, %rbp
jmp 0x6f2a
movq %r13, 0x18(%rsp)
movq %r12, 0x10(%rsp)
xorl %eax, %eax
movl %eax, %edx
movl $0x1, %r15d
movslq %eax, %r13
cmpl $0xffff, %r13d          # imm = 0xFFFF
jg 0x6d21
movl %r13d, %ebx
leaq (%r14,%r13), %r12
movl %ebp, %edi
movq %r12, %rsi
movq %r15, %rdx
callq 0x3510
testq %rax, %rax
jle 0x6d29
cmpq $0x3, %r13
jl 0x6d0e
cmpb $0xa, (%r12)
jne 0x6d0e
leaq (%r14,%r13), %rdi
addq $-0x3, %rdi
leaq 0x1e9e0(%rip), %rsi     # 0x256dc
movl $0x4, %edx
callq 0x3120
testl %eax, %eax
je 0x6d9e
movq %r13, %rax
incq %rax
movq %rax, %r13
cmpq $0x10000, %rax          # imm = 0x10000
jne 0x6cc7
jmp 0x6d7a
movq %r13, %rbx
movq %rdx, %r12
jmp 0x6d31
movq %rax, %r12
movl %ebx, %eax
movq %r13, %rbx
movl %eax, %r13d
testq %r12, %r12
jns 0x6f5f
callq 0x3050
movl (%rax), %ecx
movl %r13d, %eax
movq %r12, %rdx
cmpl $0x4, %ecx
je 0x6cbb
movl %r13d, %eax
movq %r12, %rdx
cmpl $0xb, %ecx
je 0x6cbb
movq 0x8(%rsp), %rax
movb $0x0, (%rax,%rbx)
movq 0x10(%rsp), %r12
movq 0x18(%rsp), %r13
jmp 0x6c4f
movq 0x8(%rsp), %rax
movb $0x0, 0x10000(%rax)
movq 0x10(%rsp), %r12
movq 0x18(%rsp), %r13
jmp 0x6dbd
movl %r13d, %ecx
movq (%rsp), %r12
jmp 0x6ef5
movl %ebx, %eax
movq 0x10(%rsp), %r12
movq 0x8(%rsp), %rcx
movb $0x0, (%rcx,%r13)
cmpl $0xe, %eax
movq 0x18(%rsp), %r13
jl 0x6c4f
addq $0x8, %r14
leaq 0xa0(%rsp), %rsi
movq %r14, %rdi
xorl %edx, %edx
callq 0x34e0
cmpl $0xc8, %eax
jne 0x6c4f
jmp 0x6c5b
leaq 0x54(%rsp), %rcx
leaq 0x58(%rsp), %r8
leaq 0x5c(%rsp), %r9
leaq 0x60(%rsp), %r10
leaq 0x64(%rsp), %r11
leaq 0x1e949(%rip), %rsi     # 0x2574c
xorl %ebx, %ebx
movl %ebx, %r13d
leaq 0x50(%rsp), %r15
movq %r15, %rdx
movl %ebx, %eax
pushq %r11
pushq %r10
callq 0x32e0
addq $0x10, %rsp
movl (%r15), %eax
movl %eax, 0x2c(%rsp)
movl 0x4(%r15), %eax
movl %eax, 0x28(%rsp)
movl 0x8(%r15), %r12d
movl 0xc(%r15), %eax
movl %eax, 0x24(%rsp)
movl 0x10(%r15), %eax
shll $0x8, %eax
movzwl %ax, %r14d
addl 0x14(%r15), %r14d
leaq 0x30(%rsp), %rdi
movq 0x48(%rsp), %r15
movq %r15, %rsi
movl %ebx, %edx
callq 0x744c
leaq 0x1e8fa(%rip), %rsi     # 0x2575e
leaq 0xa0(%rsp), %rbx
movq %rbx, %rdi
movl 0x2c(%rsp), %edx
movl 0x28(%rsp), %ecx
movl %r12d, %r8d
movl 0x24(%rsp), %r9d
movl %r13d, %eax
callq 0x3070
leaq 0x1e8cd(%rip), %rsi     # 0x2575b
leaq 0xfe(%rsp), %r12
movq %r12, %rdi
movl %r14d, %edx
movl %r13d, %eax
callq 0x3070
movq %rbx, %rdi
movq %r12, %rsi
movq (%rsp), %r12
callq 0x71c9
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
cmpl $-0x1, %eax
je 0x6ef5
movl %eax, %r14d
leaq 0x30(%rsp), %rdi
callq 0x7306
cmpl $0x96, %eax
je 0x6edf
movl %r14d, %edi
callq 0x37a0
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
movl 0x34(%rsp), %edi
callq 0x37a0
movl %r14d, %ecx
jmp 0x6ef5
movl %r13d, %ecx
movq 0x48(%rsp), %r15
movl %ecx, %ebx
movq 0x88(%rsp), %rdi
callq 0x3390
movq 0x90(%rsp), %rdi
callq 0x3390
movq %r15, %rdi
callq 0x3390
movq 0x38(%rsp), %rdi
callq 0x3390
movl %ebx, %ecx
movl %ecx, 0x4(%rbp)
movl %ecx, %eax
movl %eax, (%r12)
movq %rbp, %rax
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0xa0(%rsp), %edi
callq 0x37a0
movl 0xa4(%rsp), %edi
callq 0x37a0
xorl %eax, %eax
jmp 0x6f31
movl %r13d, %eax
movq %rbx, %r13
jmp 0x6da0
movl 0xa0(%rsp), %edi
callq 0x37a0
movl 0xa4(%rsp), %edi
movl $0x1, %esi
callq 0x32b0
movl 0xa4(%rsp), %edi
callq 0x37a0
testb %bpl, %bpl
je 0x6fc0
movq %r14, %rdi
callq 0x6fec
movq %rax, %rbx
movq (%rax), %rdi
movq %rax, %rsi
callq 0x3220
movq (%rbx), %rdi
callq 0x3390
movq %rbx, %rdi
callq 0x3390
jmp 0x6fe2
leaq 0x1e6d9(%rip), %rdi     # 0x256a0
leaq 0x1e6d7(%rip), %rsi     # 0x256a5
leaq 0x1e6d3(%rip), %rdx     # 0x256a8
xorl %eax, %eax
movl %eax, %r8d
movq %r14, %rcx
callq 0x3400
movl $0x1, %edi
callq 0x33c0