// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq $0x0, -0x20(%rbp)
movq -0x18(%rbp), %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
movq -0x10(%rbp), %rdi
leaq 0x314ea(%rip), %rsi     # 0x37687
callq 0x3080
cmpq -0x10(%rbp), %rax
jne 0x61ef
movl $0x1, %edi
movl $0xc, %esi
leaq 0x314d6(%rip), %rdx     # 0x3768f
movl $0x10f, %ecx            # imm = 0x10F
leaq 0x314d2(%rip), %r8      # 0x37697
callq 0x31bf0
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
movl $0x4, (%rax)
movq -0x10(%rbp), %rdi
callq 0x6540
movl %eax, %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0x4(%rax)
jmp 0x650d
movq -0x10(%rbp), %rdi
leaq 0x314a3(%rip), %rsi     # 0x3769d
callq 0x3080
cmpq -0x10(%rbp), %rax
jne 0x624c
movl $0x1, %edi
movl $0xc, %esi
leaq 0x31479(%rip), %rdx     # 0x3768f
movl $0x113, %ecx            # imm = 0x113
leaq 0x31475(%rip), %r8      # 0x37697
callq 0x31bf0
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
movl $0x5, (%rax)
movq -0x10(%rbp), %rdi
callq 0x6a90
movl %eax, %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0x4(%rax)
jmp 0x650b
movq -0x10(%rbp), %rdi
leaq 0x344ef(%rip), %rsi     # 0x3a746
callq 0x34b0
cmpl $0x0, %eax
jne 0x62a1
movl $0x1, %edi
movl $0xc, %esi
leaq 0x3141d(%rip), %rdx     # 0x3768f
movl $0x117, %ecx            # imm = 0x117
leaq 0x31419(%rip), %r8      # 0x37697
callq 0x31bf0
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
movl $0x1, (%rax)
movq -0x20(%rbp), %rax
movl $0x0, 0x4(%rax)
jmp 0x6509
movq -0x10(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
cmpb $0x0, (%rax)
je 0x62e4
callq 0x3310
movq (%rax), %rax
movq -0x28(%rbp), %rcx
movsbl (%rcx), %ecx
movslq %ecx, %rcx
movzwl (%rax,%rcx,2), %eax
andl $0x2000, %eax           # imm = 0x2000
cmpl $0x0, %eax
jne 0x62d4
jmp 0x62e4
jmp 0x62d6
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
jmp 0x62a9
movq -0x28(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x3c, %eax
jne 0x64aa
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
cmpb $0x0, (%rax)
je 0x6369
callq 0x3310
movq (%rax), %rax
movq -0x30(%rbp), %rcx
movsbl (%rcx), %ecx
movslq %ecx, %rcx
movzwl (%rax,%rcx,2), %eax
andl $0x4, %eax
cmpl $0x0, %eax
je 0x6359
movq -0x30(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x2e, %eax
je 0x6359
movq -0x30(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x5f, %eax
je 0x6359
movq -0x30(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x2d, %eax
je 0x6359
movq -0x30(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x3a, %eax
je 0x6359
jmp 0x6369
jmp 0x635b
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
jmp 0x6300
movq -0x30(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x0, %eax
setne %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x34(%rbp)
leaq -0x3c(%rbp), %rdi
callq 0x3470
cmpl $0x0, %eax
je 0x6399
movq $0x0, -0x8(%rbp)
jmp 0x6529
callq 0x3490
movl %eax, -0x40(%rbp)
cmpl $-0x1, -0x40(%rbp)
jne 0x63c4
movl -0x3c(%rbp), %edi
callq 0x3780
movl -0x38(%rbp), %edi
callq 0x3780
movq $0x0, -0x8(%rbp)
jmp 0x6529
cmpl $0x0, -0x40(%rbp)
jne 0x645c
movl -0x3c(%rbp), %edi
callq 0x3780
movl -0x38(%rbp), %edi
movl $0x1, %esi
callq 0x32a0
movl -0x38(%rbp), %edi
callq 0x3780
cmpl $0x0, -0x34(%rbp)
jne 0x6429
movq -0x28(%rbp), %rdi
addq $0x1, %rdi
callq 0x6ea0
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rax
movq (%rax), %rdi
movq -0x48(%rbp), %rsi
callq 0x3210
movq -0x48(%rbp), %rax
movq (%rax), %rdi
callq 0x3380
movq -0x48(%rbp), %rdi
callq 0x3380
jmp 0x6452
movq -0x28(%rbp), %rcx
addq $0x1, %rcx
leaq 0x3126c(%rip), %rdi     # 0x376a4
leaq 0x3126a(%rip), %rsi     # 0x376a9
leaq 0x31266(%rip), %rdx     # 0x376ac
xorl %eax, %eax
movl %eax, %r8d
movb $0x0, %al
callq 0x33f0
movl $0x1, %edi
callq 0x33b0
movl -0x38(%rbp), %edi
callq 0x3780
movl $0x1, %edi
movl $0xc, %esi
leaq 0x3121a(%rip), %rdx     # 0x3768f
movl $0x139, %ecx            # imm = 0x139
leaq 0x31216(%rip), %r8      # 0x37697
callq 0x31bf0
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
movl $0x3, (%rax)
movl -0x3c(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0x4(%rax)
movl -0x40(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0x8(%rax)
jmp 0x6507
movq -0x10(%rbp), %rdi
xorl %esi, %esi
movb $0x0, %al
callq 0x31c0
movl %eax, %ecx
movq -0x18(%rbp), %rax
movl %ecx, (%rax)
movq -0x18(%rbp), %rax
cmpl $0x0, (%rax)
jl 0x6505
movl $0x1, %edi
movl $0xc, %esi
leaq 0x311b6(%rip), %rdx     # 0x3768f
movl $0x145, %ecx            # imm = 0x145
leaq 0x311b2(%rip), %r8      # 0x37697
callq 0x31bf0
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
movl $0x2, (%rax)
movq -0x18(%rbp), %rax
movl (%rax), %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0x4(%rax)
jmp 0x6507
jmp 0x6509
jmp 0x650b
jmp 0x650d
cmpq $0x0, -0x20(%rbp)
je 0x6521
movq -0x20(%rbp), %rax
movl 0x4(%rax), %ecx
movq -0x18(%rbp), %rax
movl %ecx, (%rax)
movq -0x20(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)