// File: /workspace/github_c_makefile_star2_O2/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, 0x8(%rsp)
andl $0x0, (%rax)
testq %r15, %r15
sete %al
testq %r14, %r14
sete %cl
orb %al, %cl
pushq $0x16
popq %rbx
jne 0x1f0c
movq %r14, 0x18(%rsp)
movq (%r14), %rax
testq %rax, %rax
je 0x1f0c
movb (%r15), %bpl
pushq $0x2
popq %rbx
testb %bpl, %bpl
je 0x1f0c
cmpb $0x0, (%rax)
je 0x1f0c
movq %r15, %rdi
callq 0x10d0
movq %rax, %r13
leaq 0x1(%r15), %rax
pushq $0xd
popq %rbx
testb %bpl, %bpl
je 0x1ce9
movzbl %bpl, %ecx
cmpl $0x2f, %ecx
je 0x1cd0
movb (%rax), %bpl
incq %rax
jmp 0x1cba
leaq 0x14ff(%rip), %rbp      # 0x31d6
movl $0x0, 0x4(%rsp)
movq %r15, 0x10(%rsp)
jmp 0x1dcd
movb $0x0, 0x50(%rsp)
leaq 0x14c4(%rip), %rdi      # 0x31b9
callq 0x1030
testq %rax, %rax
je 0x1d1b
movq %rax, %rbp
cmpb $0x0, (%rax)
je 0x1d1b
leaq 0x50(%rsp), %rax
movq %rax, 0x10(%rsp)
movl $0x0, 0x4(%rsp)
jmp 0x1d23
pushq $0x2
popq %rbx
jmp 0x1f0c
xorl %eax, %eax
movzbl (%rbp,%rax), %ecx
testl %ecx, %ecx
je 0x1d38
cmpl $0x3a, %ecx
je 0x1d38
incq %rax
jmp 0x1d25
testq %rax, %rax
leaq 0x14c0(%rip), %rsi      # 0x3202
cmovneq %rbp, %rsi
pushq $0x1
popq %r12
cmovneq %rax, %r12
leaq (%r12,%r13), %r14
incq %r14
addq %rax, %rbp
cmpq $0x1000, %r14           # imm = 0x1000
jb 0x1da3
leaq 0x1456(%rip), %rcx      # 0x31be
leaq 0x20(%rsp), %rax
movq %rcx, (%rax)
movq $0x7, 0x8(%rax)
movq %rsi, 0x10(%rax)
movq %r12, 0x18(%rax)
leaq 0x143f(%rip), %rcx      # 0x31c6
movq %rcx, 0x20(%rax)
movq $0x10, 0x28(%rax)
pushq $0x2
popq %rdi
pushq $0x3
popq %rdx
movq %rax, %rsi
callq 0x1080
jmp 0x1e04
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,%r14)
movq 0x3214(%rip), %r12      # 0x4fe8
movq (%r12), %rdx
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rsi
callq 0x1130
movq 0x8(%rsp), %rax
movl (%rax), %eax
cmpq $0x28, %rax
ja 0x1e24
movabsq $0x11000100004, %rcx    # imm = 0x11000100004
btq %rax, %rcx
jae 0x1e34
cmpb $0x3a, (%rbp)
leaq 0x1(%rbp), %rbp
je 0x1d23
cmpl $0x0, 0x4(%rsp)
jne 0x1f0c
movq 0x8(%rsp), %rax
movl (%rax), %eax
pushq $0x2
popq %rbx
testl %eax, %eax
jne 0x1f13
jmp 0x1f0c
cmpq $0xd, %rax
jne 0x1e53
cmpb $0x3a, (%rbp)
jne 0x1f0c
incq %rbp
pushq $0x1
popq %rax
movl %eax, 0x4(%rsp)
jmp 0x1d23
cmpq $0x8, %rax
jne 0x1e24
leaq 0x1377(%rip), %rdi      # 0x31d7
callq 0x1030
testq %rax, %rax
je 0x1f13
movq %rax, %r15
cmpb $0x0, (%rax)
je 0x1f13
movq %r15, %rdi
callq 0x10d0
cmpq $0xfff, %rax            # imm = 0xFFF
ja 0x1f13
pushq $0x8
popq %r13
movq 0x18(%rsp), %rax
cmpq $0x0, -0x8(%rax,%r13)
leaq 0x8(%r13), %r13
jne 0x1e97
movabsq $0x7fffffff8, %rdi      # imm = 0x7FFFFFFF8
andq %r13, %rdi
callq 0x1180
testq %rax, %rax
je 0x1f13
movq %rax, %rbp
movq %r15, (%rax)
movq 0x10(%rsp), %rax
movq %rax, 0x8(%rbp)
movq %rbp, %rdi
addq $0x10, %rdi
movq 0x18(%rsp), %rsi
addq $0x8, %rsi
addq $-0x10, %r13
movq %r13, %rdx
callq 0x1150
movq (%r12), %rdx
movq %r15, %rdi
movq %rbp, %rsi
callq 0x1130
movq %rbp, %rdi
callq 0x1040
xorl %ebx, %ebx
movq 0x8(%rsp), %rax
cmpl $0x7, (%rax)
setne %bl
addl $0x7, %ebx
movq 0x8(%rsp), %rax
movl %ebx, (%rax)
pushq $-0x1
popq %rax
addq $0x1058, %rsp           # imm = 0x1058
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq