// File: /workspace/github_c_makefile_star2_O1/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, %r15
movq %rdi, %r12
movl $0xffffffff, (%rsi)     # imm = 0xFFFFFFFF
leaq 0x1f0bf(%rip), %rsi     # 0x25683
movl $0x7, %edx
callq 0x3120
testl %eax, %eax
je 0x6640
leaq 0x1f0c0(%rip), %rsi     # 0x25699
movl $0x6, %edx
movq %r12, %rdi
callq 0x3120
testl %eax, %eax
je 0x66c1
leaq 0x22269(%rip), %rsi     # 0x2885e
movq %r12, %rdi
callq 0x34d0
testl %eax, %eax
je 0x6744
movb (%r12), %bl
testb %bl, %bl
je 0x6851
callq 0x3320
movq (%rax), %rax
leaq 0x1(%r12), %r14
movl %ebx, %ecx
movsbq %cl, %rdx
testb $0x20, 0x1(%rax,%rdx,2)
je 0x67db
movb (%r14), %dl
incq %r14
movl %edx, %ecx
testb %dl, %dl
jne 0x6620
jmp 0x6851
leaq 0x1f044(%rip), %rdx     # 0x2568b
leaq 0x1f045(%rip), %r8      # 0x25693
movl $0x1, %edi
movl $0xc, %esi
movl $0x10f, %ecx            # imm = 0x10F
callq 0x22cb4
movq %rax, %r13
movl $0x4, (%rax)
leaq 0x1f011(%rip), %rsi     # 0x25683
movl $0x7, %edx
movq %r12, %rdi
callq 0x3120
xorl %ecx, %ecx
testl %eax, %eax
jne 0x6c0d
movq %r15, 0x8(%rsp)
movabsq $0x100000000, %rax      # imm = 0x100000000
leaq 0x7(%r12), %r14
xorl %ecx, %ecx
movl %ecx, %r15d
movq %r14, %rcx
movzbl (%rcx), %edx
testl %edx, %edx
je 0x6773
cmpl $0x2f, %edx
je 0x6773
incq %rcx
addq %rax, %r15
jmp 0x66a5
leaq 0x1efc3(%rip), %rdx     # 0x2568b
leaq 0x1efc4(%rip), %r8      # 0x25693
movl $0x1, %edi
movl $0xc, %esi
movl $0x113, %ecx            # imm = 0x113
callq 0x22cb4
movq %rax, %r14
movl $0x5, (%rax)
leaq 0x1efa6(%rip), %rsi     # 0x25699
movl $0x6, %edx
movq %r12, %rdi
callq 0x3120
xorl %ecx, %ecx
testl %eax, %eax
jne 0x6eba
movq %r15, 0x8(%rsp)
movabsq $0x100000000, %r15      # imm = 0x100000000
movq %r12, %r13
addq $0x6, %r13
xorl %eax, %eax
movl %eax, %ebx
movq %r13, %rbp
movzbl (%rbp), %eax
testl %eax, %eax
je 0x6d01
cmpl $0x2f, %eax
je 0x6994
incq %rbp
addq %r15, %rbx
jmp 0x6727
leaq 0x1ef40(%rip), %rdx     # 0x2568b
leaq 0x1ef41(%rip), %r8      # 0x25693
movl $0x1, %edi
movl $0xc, %esi
movl $0x117, %ecx            # imm = 0x117
callq 0x22cb4
movl $0x1, (%rax)
xorl %ecx, %ecx
jmp 0x6ebd
movq %rcx, 0xa0(%rsp)
addq %r15, %rax
movq %rax, %rdi
sarq $0x20, %rdi
leaq 0x1eeff(%rip), %rdx     # 0x2568b
leaq 0x1ef18(%rip), %r8      # 0x256ab
movl $0x1, %esi
movl $0x57, %ecx
callq 0x22cb4
movq %rax, %rbx
movq %rax, %rbp
sarq $0x20, %r15
movq %rax, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x3130
movb $0x0, (%rbx,%r15)
movq %rbp, %rax
movzbl (%rax), %ecx
testl %ecx, %ecx
je 0x68a9
cmpl $0x3a, %ecx
je 0x68a3
incq %rax
jmp 0x67c2
cmpb $0x3c, %cl
jne 0x6851
movb (%r14), %bl
testb %bl, %bl
sete %bpl
je 0x6d19
movq %r15, %r12
leaq 0x1(%r14), %r15
callq 0x3320
movq (%rax), %rax
movabsq $0x4000000002003, %rcx  # imm = 0x4000000002003
movq %r15, %rdx
movq %r12, %r15
movl %ebx, %edi
movsbq %dil, %r8
movzwl (%rax,%r8,2), %r8d
testb $0x4, %r8b
je 0x683b
movl %ebp, %esi
movzbl %dil, %edi
addl $-0x2d, %edi
cmpl $0x32, %edi
ja 0x6d1b
btq %rdi, %rcx
jae 0x6d1b
movb (%rdx), %dil
incq %rdx
testb %dil, %dil
sete %bpl
movl %ebp, %esi
jne 0x6810
jmp 0x6d1b
xorl %ebx, %ebx
movl %ebx, %eax
movq %r12, %rdi
movl %ebx, %esi
callq 0x31d0
movl %eax, (%r15)
movb $0x1, %bpl
testl %eax, %eax
js 0x689c
leaq 0x1ee1b(%rip), %rdx     # 0x2568b
leaq 0x1ee1c(%rip), %r8      # 0x25693
movl $0x1, %edi
movl $0xc, %esi
movl $0x145, %ecx            # imm = 0x145
callq 0x22cb4
movl $0x2, (%rax)
movl (%r15), %ecx
movl %ecx, 0x4(%rax)
jmp 0x6f02
movl %ebx, %eax
jmp 0x6f02
movb $0x0, (%rax)
incq %rax
movq %rax, %r15
leaq 0x1ee02(%rip), %rdi     # 0x256b5
callq 0x33d0
testq %rax, %rax
movq %r13, 0x98(%rsp)
je 0x691a
movq %rax, %r14
movq %rax, %rdi
leaq 0x1edb1(%rip), %rsi     # 0x25683
movl $0x7, %edx
callq 0x3120
leaq 0x7(%r14), %rdi
testl %eax, %eax
cmovneq %r14, %rdi
leaq 0x1ed9e(%rip), %rsi     # 0x2568b
leaq 0x1edb7(%rip), %rcx     # 0x256ab
movl $0x64, %edx
callq 0x22d9d
movq %rax, %r15
movzbl (%rax), %ecx
testl %ecx, %ecx
je 0x6af0
cmpl $0x3a, %ecx
je 0x6aea
incq %rax
jmp 0x6901
leaq 0x1ed6a(%rip), %rbx     # 0x2568b
leaq 0x1ed83(%rip), %r14     # 0x256ab
movq %rbp, %rdi
movq %rbx, %rsi
movl $0x60, %edx
movq %r14, %rcx
callq 0x22d9d
movq %rax, %r12
cmpb $0x0, (%r15)
leaq 0x1ed77(%rip), %rdi     # 0x256c0
cmovneq %r15, %rdi
movq %rbx, %rsi
movl $0x61, %edx
movq %r14, %rcx
callq 0x22d9d
movq %rax, %r15
movq 0xa0(%rsp), %rax
cmpb $0x0, (%rax)
leaq 0x1ed2c(%rip), %rdi     # 0x2569e
cmovneq %rax, %rdi
movq %rbx, %rsi
movl $0x62, %edx
movq %r14, %rcx
callq 0x22d9d
movq %rax, %r14
movq %r15, %rax
movq %r12, %r15
jmp 0x6b38
leaq 0x1ed6c(%rip), %rdi     # 0x25707
leaq 0x1ece9(%rip), %rsi     # 0x2568b
leaq 0x1ed61(%rip), %r12     # 0x2570a
movl $0xbf, %edx
movq %r12, %rcx
callq 0x22d9d
movq %rax, 0x60(%rsp)
movq %rax, 0x88(%rsp)
addq %rbx, %r15
sarq $0x20, %r15
movl $0x1, %esi
movq %r15, %rdi
leaq 0x1ecb2(%rip), %r15     # 0x2568b
movq %r15, %rdx
movl $0xc0, %ecx
movq %r12, %r8
callq 0x22cb4
movq %rax, %r12
movq %rax, 0x80(%rsp)
sarq $0x20, %rbx
movq %rax, %rdi
movq %r13, %rsi
movq %rbx, %rdx
callq 0x3130
movq %rbp, %rdi
callq 0x3100
leaq 0x8(%rax), %rdi
movl $0x1, %esi
movq %r15, %rdx
movl $0xc2, %ecx
leaq 0x1ece4(%rip), %r8      # 0x2570a
callq 0x22cb4
movq %rax, %r13
leaq 0x1ecde(%rip), %rsi     # 0x25713
xorl %ecx, %ecx
movq %rax, %rdi
movq %rbp, %rdx
movl %ecx, %eax
callq 0x3070
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsp)
movq %r12, %rdi
movq 0x60(%rsp), %rsi
callq 0x716a
movl %eax, 0x24(%rsp)
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpl $-0x1, %eax
je 0x6d0d
leaq 0x20(%rsp), %r15
movq %r15, %rdi
callq 0x72a2
leaq 0x1ec9e(%rip), %rsi     # 0x2571d
movl $0x1, %ebx
movq %r15, %rdi
movl %ebx, %edx
callq 0x73dd
leaq 0x1ec99(%rip), %rsi     # 0x2572e
movq %r15, %rdi
movl %ebx, %edx
callq 0x73dd
leaq 0x1ec96(%rip), %rsi     # 0x2573c
movq %r15, %rdi
movl %ebx, %edx
callq 0x73dd
leaq 0x1ec8e(%rip), %rsi     # 0x25745
movq %r15, %rdi
movl %ebx, %edx
callq 0x73dd
movq 0x8(%r15), %rax
incq %rax
movq %rax, %rdi
movq 0x8(%rsp), %r15
movzbl -0x1(%rdi), %eax
testl %eax, %eax
je 0x6e8a
cmpl $0x28, %eax
je 0x6d92
incq %rdi
jmp 0x6ad0
movb $0x0, (%rax)
incq %rax
cmpb $0x0, (%rax)
leaq 0x1ebc6(%rip), %rdi     # 0x256c0
cmovneq %rax, %rdi
leaq 0x1eb86(%rip), %rbx     # 0x2568b
leaq 0x1eb9f(%rip), %r14     # 0x256ab
movq %rbx, %rsi
movl $0x67, %edx
movq %r14, %rcx
callq 0x22d9d
movq %rax, %r13
movq %r12, %rdi
movq %rbx, %rsi
movl $0x68, %edx
movq %r14, %rcx
callq 0x22d9d
movq %rax, %r14
movq %r13, %rax
movq %rbp, %r13
movq %r15, %rdi
movq %rax, %rsi
movq %r15, 0x68(%rsp)
movq %rax, 0x70(%rsp)
callq 0x716a
movl %eax, %ebp
movl %eax, %r15d
leaq 0x1eb2f(%rip), %rdx     # 0x2568b
leaq 0x1eb48(%rip), %r8      # 0x256ab
movl $0x10000, %ebx          # imm = 0x10000
movl $0x1, %esi
movq %rbx, %rdi
movl $0x6e, %ecx
callq 0x22cb4
movq %rax, %r12
movq %rax, 0x38(%rsp)
leaq 0x1eb3a(%rip), %rdx     # 0x256c3
xorl %eax, %eax
movq %r12, %rdi
movq %rbx, %rsi
movq %r14, 0x78(%rsp)
movq %r14, %rcx
movq %r13, 0x90(%rsp)
movq %r13, %r8
callq 0x35e0
movslq %eax, %rdx
movl %ebp, %edi
movq %r12, %rsi
callq 0x725b
testl %eax, %eax
je 0x6c15
movl %r15d, %edi
callq 0x37a0
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
movq 0x38(%rsp), %rdi
callq 0x3390
movq 0x90(%rsp), %rdi
callq 0x3390
movq 0x68(%rsp), %rdi
callq 0x3390
movq 0x70(%rsp), %rdi
callq 0x3390
movq 0x78(%rsp), %rdi
callq 0x3390
movl %r15d, %ecx
movq 0x8(%rsp), %r15
movq 0x98(%rsp), %r13
movq %r13, %rax
jmp 0x6ebd
xorl %eax, %eax
movl %eax, %edx
movslq %eax, %r14
cmpl $0xffff, %r14d          # imm = 0xFFFF
jg 0x6c8e
movl %r14d, %r13d
leaq (%r12,%r14), %rbx
movl %r15d, %edi
movq %rbx, %rsi
movl $0x1, %edx
callq 0x3510
movq %rax, %rbp
testq %rax, %rax
jle 0x6c89
cmpq $0x3, %r14
jl 0x6c6c
cmpb $0xa, (%rbx)
jne 0x6c6c
leaq (%r12,%r14), %rdi
addq $-0x3, %rdi
leaq 0x1ea7e(%rip), %rsi     # 0x256dc
movl $0x4, %edx
callq 0x3120
testl %eax, %eax
je 0x6c89
movq %r14, %rax
incq %rax
movq %rax, %r14
cmpq $0x10000, %rax          # imm = 0x10000
jne 0x6c25
movl $0x10000, %r14d         # imm = 0x10000
movl $0x10000, %r13d         # imm = 0x10000
movq %rbp, %rbx
jmp 0x6c94
movl %eax, %r13d
movq %rdx, %rbx
testq %rbx, %rbx
jns 0x6cbe
callq 0x3050
movl (%rax), %ecx
movl %r13d, %eax
movq %rbx, %rdx
cmpl $0x4, %ecx
je 0x6c19
movl %r13d, %eax
movq %rbx, %rdx
cmpl $0xb, %ecx
je 0x6c19
movq 0x38(%rsp), %rax
movb $0x0, (%rax,%r14)
cmpl $0xe, %r13d
jl 0x6bba
testq %rbx, %rbx
js 0x6bba
addq $0x8, %r12
leaq 0xa0(%rsp), %rsi
movq %r12, %rdi
xorl %edx, %edx
callq 0x34e0
cmpl $0xc8, %eax
jne 0x6bba
jmp 0x6bc8
xorl %ecx, %ecx
movq 0x8(%rsp), %r15
jmp 0x6eba
movl %ebp, %ebx
movq 0x8(%rsp), %r15
jmp 0x6e8c
movl %ebp, %esi
movl %esi, %ebx
leaq 0xa0(%rsp), %rdi
callq 0x3490
testl %eax, %eax
jne 0x6ef5
callq 0x34b0
cmpl $-0x1, %eax
je 0x6edd
testl %eax, %eax
je 0x6f0b
movl %eax, %ebx
movl 0xa4(%rsp), %edi
callq 0x37a0
leaq 0x1e92e(%rip), %rdx     # 0x2568b
leaq 0x1e92f(%rip), %r8      # 0x25693
movl $0x1, %edi
movl $0xc, %esi
movl $0x139, %ecx            # imm = 0x139
callq 0x22cb4
movl $0x3, (%rax)
movl 0xa0(%rsp), %ecx
movl %ecx, 0x4(%rax)
movl %ebx, 0x8(%rax)
movb $0x1, %cl
jmp 0x6f00
leaq 0x44(%rsp), %rcx
leaq 0x48(%rsp), %r8
leaq 0x4c(%rsp), %r9
leaq 0x50(%rsp), %r10
leaq 0x54(%rsp), %r11
leaq 0x1e99a(%rip), %rsi     # 0x2574c
xorl %eax, %eax
movl %eax, %ebp
leaq 0x40(%rsp), %rbx
movq %rbx, %rdx
pushq %r11
pushq %r10
callq 0x32e0
addq $0x10, %rsp
movl (%rbx), %r12d
movl 0x4(%rbx), %eax
movl %eax, 0x1c(%rsp)
movl 0x8(%rbx), %eax
movl %eax, 0x18(%rsp)
movl 0xc(%rbx), %eax
movl %eax, 0x14(%rsp)
movl 0x10(%rbx), %eax
shll $0x8, %eax
movzwl %ax, %r15d
addl 0x14(%rbx), %r15d
leaq 0x20(%rsp), %rdi
movq %r13, %rsi
xorl %edx, %edx
callq 0x73dd
leaq 0x1e957(%rip), %rsi     # 0x2575e
leaq 0xa0(%rsp), %rbx
movq %rbx, %rdi
movl %r12d, %edx
movl 0x1c(%rsp), %ecx
movl 0x18(%rsp), %r8d
movl 0x14(%rsp), %r9d
movl %ebp, %eax
callq 0x3070
leaq 0x1e92a(%rip), %rsi     # 0x2575b
leaq 0xfe(%rsp), %r12
movq %r12, %rdi
movl %r15d, %edx
movq 0x8(%rsp), %r15
movl %ebp, %eax
callq 0x3070
movq %rbx, %rdi
movq %r12, %rsi
callq 0x716a
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
cmpl $-0x1, %eax
je 0x6e8c
movl %eax, %ebx
leaq 0x20(%rsp), %rdi
callq 0x72a2
cmpl $0x96, %eax
je 0x6e7f
movl %ebx, %edi
callq 0x37a0
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movl 0x24(%rsp), %edi
callq 0x37a0
jmp 0x6e8c
movl %ebp, %ebx
movq 0x80(%rsp), %rdi
callq 0x3390
movq 0x88(%rsp), %rdi
callq 0x3390
movq %r13, %rdi
callq 0x3390
movq 0x28(%rsp), %rdi
callq 0x3390
movl %ebx, %ecx
movq %r14, %rax
movl %ecx, 0x4(%rax)
testq %rax, %rax
je 0x6ecb
movl 0x4(%rax), %ecx
movl %ecx, (%r15)
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
movl %eax, %ecx
movl %eax, %edx
movq %rcx, %rax
movl %edx, %ecx
movl %ecx, %ebp
testb %bpl, %bpl
jne 0x6ec0
xorl %eax, %eax
jmp 0x6ecb
movl 0xa0(%rsp), %edi
callq 0x37a0
movl 0xa4(%rsp), %edi
movl $0x1, %esi
callq 0x32b0
movl 0xa4(%rsp), %edi
callq 0x37a0
testb $0x1, %bl
je 0x6f61
movq %r14, %rdi
callq 0x6f8d
movq %rax, %rbx
movq (%rax), %rdi
movq %rax, %rsi
callq 0x3220
movq (%rbx), %rdi
callq 0x3390
movq %rbx, %rdi
callq 0x3390
jmp 0x6f83
leaq 0x1e738(%rip), %rdi     # 0x256a0
leaq 0x1e736(%rip), %rsi     # 0x256a5
leaq 0x1e732(%rip), %rdx     # 0x256a8
xorl %eax, %eax
movl %eax, %r8d
movq %r14, %rcx
callq 0x3400
movl $0x1, %edi
callq 0x33c0