// File: /workspace/github_c_makefile_star2_O3/s3lase[P]v6shell/pexec.c
// Function: pexec
int
pexec(const char *file, char *const *argv)
{
	const char **esh_argv;
	size_t dlen, flen;
	int cnt, eacces = 0;
	const char *d, *esh, *f, *pnp, *upp;
	char pnbuf[PATHMAX];

	/*
	 * Fail if the value of file, argv, or argv[0] is invalid.
	 */
	errno = 0;
	if (file == NULL || argv == NULL || argv[0] == NULL) {
		errno = EINVAL;
		goto fail;
	}
	if (*file == EOS || *argv[0] == EOS)
		goto fail;
	flen = strlen(file);

	/*
	 * If the name of the specified file contains one or more
	 * `/' characters, it is used as the path name to execute.
	 */
	for (f = file; *f != EOS; f++)
		if (*f == SLASH) {
			pnp = file;
			upp = "";
			goto exec_pathname;
		}
	*pnbuf = EOS;
	pnp = pnbuf;

	/*
	 * Get the user's PATH.  Fail if PATH is unset or is
	 * set to the empty string, as no PATH search shall be
	 * performed in such a case.
	 */
	upp = getenv("PATH");
	if (upp == NULL || *upp == EOS)
		goto fail;

	do {
		/* Find the end of this PATH element. */
		for (d = upp; *upp != COLON && *upp != EOS; upp++)
			;	/* nothing */
		/*
		 * Since this is a shell PATH, double, leading, and/or
		 * trailing colons indicate the current directory.
		 */
		if (d == upp) {
			d = ".";
			dlen = 1;
		} else
			dlen = (size_t)(upp - d);

		/*
		 * Complain if this path name for file would be too long.
		 * Otherwise, use this PATH element to build a possible
		 * path name for file.  Then, attempt to execve(2) it.
		 */
		if (dlen + flen + 1 >= sizeof(pnbuf)) {
			struct iovec msg[3];
			msg[0].iov_base = (char *)"pexec: ";
			msg[0].iov_len  = (size_t)7;
			msg[1].iov_base = (char *)d;
			msg[1].iov_len  = dlen;
			msg[2].iov_base = (char *)": path too long\n";
			msg[2].iov_len  = (size_t)16;
			(void)writev(FD2, msg, 3);
			continue;
		}
		(void)memcpy(pnbuf, d, dlen);
		pnbuf[dlen] = SLASH;
		(void)memcpy(pnbuf + dlen + 1, file, flen);
		pnbuf[dlen + flen + 1] = EOS;

exec_pathname:
		(void)execve(pnp, argv, environ);
		switch (errno) {
		case EACCES:
#if defined(__OpenBSD__)
		case EISDIR:	/* Treat it as an EACCES error. */
#endif
			eacces = 1;
			/*FALLTHROUGH*/
		case ELOOP:
		case ENAMETOOLONG:
		case ENOENT:
		case ENOTDIR:
			break;
		case ENOEXEC:
			/*
			 * Get the user's EXECSHELL.
			 * Fail if it is unset or is set to an unusable value.
			 */
			esh = getenv("EXECSHELL");
			if (esh==NULL||*esh==EOS||strlen(esh)>=sizeof(pnbuf))
				goto fail;
			for (cnt = 0; argv[cnt] != NULL; cnt++)
				;	/* nothing */
			esh_argv = malloc((cnt + 2) * sizeof(char *));
			if (esh_argv == NULL)
				goto fail;
			esh_argv[0] = esh;
			esh_argv[1] = pnp;
			(void)memcpy(&esh_argv[2],&argv[1],cnt*sizeof(char *));
			(void)execve(esh, (char *const *)esh_argv, environ);
			free(esh_argv);
			errno = (errno == E2BIG) ? E2BIG : ENOEXEC;
			/*FALLTHROUGH*/
		default:
			goto fail;
		}
	} while (*upp++ == COLON);	/* Otherwise, *upp was NUL. */
	if (eacces != 0)
		errno = EACCES;

fail:
	if (errno == 0)
		errno = ENOENT;
	return -1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1058, %rsp           # imm = 0x1058
movq %rsi, %r14
movq %rdi, %r15
callq 0x1060
movq %rax, 0x10(%rsp)
movl $0x0, (%rax)
testq %r15, %r15
sete %al
testq %r14, %r14
sete %cl
orb %al, %cl
movl $0x16, %r12d
jne 0x1f8e
movq (%r14), %rax
testq %rax, %rax
je 0x1f8e
movb (%r15), %bpl
movl $0x2, %r12d
testb %bpl, %bpl
je 0x1f8e
cmpb $0x0, (%rax)
je 0x1f8e
movq %r15, %rdi
callq 0x10d0
movq %rax, %r13
leaq 0x1(%r15), %rax
testb %bpl, %bpl
je 0x1d5a
movzbl %bpl, %ecx
cmpl $0x2f, %ecx
je 0x1d41
movb (%rax), %bpl
incq %rax
jmp 0x1d2b
leaq 0x148e(%rip), %rbp      # 0x31d6
movl $0x0, 0xc(%rsp)
movq %r15, 0x18(%rsp)
jmp 0x1e41
movb $0x0, 0x50(%rsp)
leaq 0x1453(%rip), %rdi      # 0x31b9
callq 0x1030
testq %rax, %rax
je 0x1f8e
movq %rax, %rbp
cmpb $0x0, (%rax)
je 0x1f8e
leaq 0x50(%rsp), %rax
movq %rax, 0x18(%rsp)
movl $0x0, 0xc(%rsp)
xorl %eax, %eax
movzbl (%rbp,%rax), %ecx
testl %ecx, %ecx
je 0x1da7
cmpl $0x3a, %ecx
je 0x1da7
incq %rax
jmp 0x1d94
testq %rax, %rax
leaq 0x1451(%rip), %rsi      # 0x3202
cmovneq %rbp, %rsi
movl $0x1, %r12d
cmovneq %rax, %r12
leaq (%r12,%r13), %rbx
incq %rbx
addq %rax, %rbp
cmpq $0x1000, %rbx           # imm = 0x1000
jb 0x1e18
leaq 0x13e5(%rip), %rcx      # 0x31be
leaq 0x20(%rsp), %rax
movq %rcx, (%rax)
movq $0x7, 0x8(%rax)
movq %rsi, 0x10(%rax)
movq %r12, 0x18(%rax)
leaq 0x13ce(%rip), %rcx      # 0x31c6
movq %rcx, 0x20(%rax)
movq $0x10, 0x28(%rax)
movl $0x2, %edi
movq %rax, %rsi
movl $0x3, %edx
callq 0x1080
jmp 0x1e76
leaq 0x50(%rsp), %rdi
movq %r12, %rdx
callq 0x1150
leaq (%rsp,%r12), %rdi
addq $0x51, %rdi
movb $0x2f, -0x1(%rdi)
movq %r15, %rsi
movq %r13, %rdx
callq 0x1150
movb $0x0, 0x50(%rsp,%rbx)
movq 0x31a0(%rip), %r12      # 0x4fe8
movq (%r12), %rdx
movq 0x18(%rsp), %rdi
movq %r14, %rsi
callq 0x1130
movq 0x10(%rsp), %rax
movl (%rax), %eax
cmpq $0x28, %rax
ja 0x1e9c
movabsq $0x11000100004, %rcx    # imm = 0x11000100004
btq %rax, %rcx
jae 0x1eaf
cmpb $0x3a, (%rbp)
leaq 0x1(%rbp), %rbp
je 0x1d92
movl $0xd, %r12d
cmpl $0x0, 0xc(%rsp)
jne 0x1f8e
movq 0x10(%rsp), %rax
movl (%rax), %eax
movl $0x2, %r12d
testl %eax, %eax
jne 0x1f96
jmp 0x1f8e
cmpq $0xd, %rax
jne 0x1ecf
cmpb $0x3a, (%rbp)
jne 0x1f88
incq %rbp
movl $0x1, 0xc(%rsp)
jmp 0x1d92
cmpq $0x8, %rax
jne 0x1e9c
leaq 0x12fb(%rip), %rdi      # 0x31d7
callq 0x1030
testq %rax, %rax
je 0x1f96
movq %rax, %r15
cmpb $0x0, (%rax)
je 0x1f96
movq %r15, %rdi
callq 0x10d0
cmpq $0xfff, %rax            # imm = 0xFFF
ja 0x1f96
movl $0x8, %r13d
cmpq $0x0, -0x8(%r14,%r13)
leaq 0x8(%r13), %r13
jne 0x1f10
movabsq $0x7fffffff8, %rdi      # imm = 0x7FFFFFFF8
andq %r13, %rdi
callq 0x1180
testq %rax, %rax
je 0x1f96
movq %rax, %rbp
movq %r15, (%rax)
movq 0x18(%rsp), %rax
movq %rax, 0x8(%rbp)
movq %rbp, %rdi
addq $0x10, %rdi
addq $0x8, %r14
addq $-0x10, %r13
movq %r14, %rsi
movq %r13, %rdx
callq 0x1150
movq (%r12), %rdx
movq %r15, %rdi
movq %rbp, %rsi
callq 0x1130
movq %rbp, %rdi
callq 0x1040
xorl %r12d, %r12d
movq 0x10(%rsp), %rax
cmpl $0x7, (%rax)
setne %r12b
addl $0x7, %r12d
jmp 0x1f8e
movl $0xd, %r12d
movq 0x10(%rsp), %rax
movl %r12d, (%rax)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x1058, %rsp           # imm = 0x1058
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq