// File: /workspace/github_c_makefile_star2_O1/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, %rbx
movl $0x0, (%rax)
testq %r15, %r15
sete %al
testq %r14, %r14
sete %cl
orb %al, %cl
movl $0x16, %eax
jne 0x1e9a
movq %r14, 0x18(%rsp)
movq (%r14), %rcx
testq %rcx, %rcx
je 0x1e9a
cmpb $0x0, (%r15)
je 0x1e9c
cmpb $0x0, (%rcx)
je 0x1e9c
movq %r15, %rdi
callq 0x10d0
movq %rax, %r13
movq %r15, %rax
movzbl (%rax), %ecx
testl %ecx, %ecx
je 0x1d66
cmpl $0x2f, %ecx
je 0x1d4d
incq %rax
jmp 0x1d3c
leaq 0x1482(%rip), %rbp      # 0x31d6
movl $0x0, 0xc(%rsp)
movq %r15, 0x10(%rsp)
jmp 0x1e4e
movb $0x0, 0x50(%rsp)
leaq 0x1447(%rip), %rdi      # 0x31b9
callq 0x1030
testq %rax, %rax
je 0x1e9c
movq %rax, %rbp
cmpb $0x0, (%rax)
je 0x1e9c
leaq 0x50(%rsp), %rax
movq %rax, 0x10(%rsp)
movl $0x0, 0xc(%rsp)
xorl %eax, %eax
movzbl (%rbp,%rax), %ecx
testl %ecx, %ecx
je 0x1db3
cmpl $0x3a, %ecx
je 0x1db3
incq %rax
jmp 0x1da0
testq %rax, %rax
leaq 0x1445(%rip), %rsi      # 0x3202
cmovneq %rbp, %rsi
movl $0x1, %r12d
cmovneq %rax, %r12
leaq (%r12,%r13), %r14
incq %r14
addq %rax, %rbp
cmpq $0x1000, %r14           # imm = 0x1000
jb 0x1e24
leaq 0x13d9(%rip), %rcx      # 0x31be
leaq 0x20(%rsp), %rax
movq %rcx, (%rax)
movq $0x7, 0x8(%rax)
movq %rsi, 0x10(%rax)
movq %r12, 0x18(%rax)
leaq 0x13c2(%rip), %rcx      # 0x31c6
movq %rcx, 0x20(%rax)
movq $0x10, 0x28(%rax)
movl $0x2, %edi
movq %rax, %rsi
movl $0x3, %edx
callq 0x1080
jmp 0x1e80
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 0x3193(%rip), %r12      # 0x4fe8
movq (%r12), %rdx
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rsi
callq 0x1130
movl (%rbx), %eax
cmpq $0x28, %rax
ja 0x1e9c
movabsq $0x11000100004, %rcx    # imm = 0x11000100004
btq %rax, %rcx
jae 0x1ebe
cmpb $0x3a, (%rbp)
leaq 0x1(%rbp), %rbp
je 0x1d9e
movl $0xd, %eax
cmpl $0x0, 0xc(%rsp)
je 0x1e9c
movl %eax, (%rbx)
cmpl $0x0, (%rbx)
jne 0x1ea7
movl $0x2, (%rbx)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x1058, %rsp           # imm = 0x1058
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpq $0xd, %rax
jne 0x1ece
movl $0x1, 0xc(%rsp)
jmp 0x1e80
cmpq $0x8, %rax
jne 0x1e9c
leaq 0x12fc(%rip), %rdi      # 0x31d7
callq 0x1030
testq %rax, %rax
je 0x1e9c
movq %rax, %r15
cmpb $0x0, (%rax)
je 0x1e9c
movq %r15, %rdi
callq 0x10d0
cmpq $0xfff, %rax            # imm = 0xFFF
ja 0x1e9c
movl $0x8, %r13d
movq 0x18(%rsp), %rax
cmpq $0x0, -0x8(%rax,%r13)
leaq 0x8(%r13), %r13
jne 0x1f08
movabsq $0x7fffffff8, %rdi      # imm = 0x7FFFFFFF8
andq %r13, %rdi
callq 0x1180
testq %rax, %rax
je 0x1e9c
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 %eax, %eax
cmpl $0x7, (%rbx)
setne %al
addl $0x7, %eax
jmp 0x1e9a