// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x10a0, %rsp           # imm = 0x10A0
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0x0, -0x30(%rbp)
callq 0x1060
movl $0x0, (%rax)
cmpq $0x0, -0x8(%rbp)
je 0x22cd
cmpq $0x0, -0x10(%rbp)
je 0x22cd
movq -0x10(%rbp), %rax
cmpq $0x0, (%rax)
jne 0x22dd
callq 0x1060
movl $0x16, (%rax)
jmp 0x2673
movq -0x8(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x0, %eax
je 0x22f8
movq -0x10(%rbp), %rax
movq (%rax), %rax
movsbl (%rax), %eax
cmpl $0x0, %eax
jne 0x22fd
jmp 0x2673
movq -0x8(%rbp), %rdi
callq 0x10d0
movq %rax, -0x28(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x0, %eax
je 0x2352
movq -0x48(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x2f, %eax
jne 0x2342
movq -0x8(%rbp), %rax
movq %rax, -0x50(%rbp)
leaq 0xe9d(%rip), %rax       # 0x31d6
movq %rax, -0x58(%rbp)
jmp 0x24ca
jmp 0x2344
movq -0x48(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x48(%rbp)
jmp 0x2312
movb $0x0, -0x1060(%rbp)
leaq -0x1060(%rbp), %rax
movq %rax, -0x50(%rbp)
leaq 0xe4e(%rip), %rdi       # 0x31b9
callq 0x1030
movq %rax, -0x58(%rbp)
cmpq $0x0, -0x58(%rbp)
je 0x2387
movq -0x58(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x0, %eax
jne 0x238c
jmp 0x2673
jmp 0x238e
movq -0x58(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x58(%rbp), %rax
movsbl (%rax), %ecx
xorl %eax, %eax
cmpl $0x3a, %ecx
movb %al, -0x1091(%rbp)
je 0x23bd
movq -0x58(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x0, %eax
setne %al
movb %al, -0x1091(%rbp)
movb -0x1091(%rbp), %al
testb $0x1, %al
jne 0x23c9
jmp 0x23d9
jmp 0x23cb
movq -0x58(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x58(%rbp)
jmp 0x2396
movq -0x38(%rbp), %rax
cmpq -0x58(%rbp), %rax
jne 0x23f8
leaq 0xe18(%rip), %rax       # 0x3202
movq %rax, -0x38(%rbp)
movq $0x1, -0x20(%rbp)
jmp 0x2407
movq -0x58(%rbp), %rax
movq -0x38(%rbp), %rcx
subq %rcx, %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
addq -0x28(%rbp), %rax
addq $0x1, %rax
cmpq $0x1000, %rax           # imm = 0x1000
jb 0x247e
leaq 0xd9c(%rip), %rax       # 0x31be
movq %rax, -0x1090(%rbp)
movq $0x7, -0x1088(%rbp)
movq -0x38(%rbp), %rax
movq %rax, -0x1080(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x1078(%rbp)
leaq 0xd75(%rip), %rax       # 0x31c6
movq %rax, -0x1070(%rbp)
movq $0x10, -0x1068(%rbp)
leaq -0x1090(%rbp), %rsi
movl $0x2, %edi
movl $0x3, %edx
callq 0x1080
jmp 0x2645
leaq -0x1060(%rbp), %rdi
movq -0x38(%rbp), %rsi
movq -0x20(%rbp), %rdx
callq 0x1150
movq -0x20(%rbp), %rax
movb $0x2f, -0x1060(%rbp,%rax)
leaq -0x1060(%rbp), %rdi
addq -0x20(%rbp), %rdi
addq $0x1, %rdi
movq -0x8(%rbp), %rsi
movq -0x28(%rbp), %rdx
callq 0x1150
movq -0x20(%rbp), %rax
addq -0x28(%rbp), %rax
movb $0x0, -0x105f(%rbp,%rax)
movq -0x50(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq 0x2b0f(%rip), %rax      # 0x4fe8
movq (%rax), %rdx
callq 0x1130
callq 0x1060
movl (%rax), %eax
movl %eax, -0x1098(%rbp)
subl $0x2, %eax
je 0x2540
jmp 0x24f5
movl -0x1098(%rbp), %eax
subl $0x8, %eax
je 0x2545
jmp 0x2502
movl -0x1098(%rbp), %eax
subl $0xd, %eax
je 0x2539
jmp 0x250f
movl -0x1098(%rbp), %eax
subl $0x14, %eax
je 0x2540
jmp 0x251c
movl -0x1098(%rbp), %eax
subl $0x24, %eax
je 0x2540
jmp 0x2529
movl -0x1098(%rbp), %eax
subl $0x28, %eax
je 0x2540
jmp 0x2641
movl $0x1, -0x30(%rbp)
jmp 0x2643
leaq 0xc8b(%rip), %rdi       # 0x31d7
callq 0x1030
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
je 0x2579
movq -0x40(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x0, %eax
je 0x2579
movq -0x40(%rbp), %rdi
callq 0x10d0
cmpq $0x1000, %rax           # imm = 0x1000
jb 0x257e
jmp 0x2673
movl $0x0, -0x2c(%rbp)
movq -0x10(%rbp), %rax
movslq -0x2c(%rbp), %rcx
cmpq $0x0, (%rax,%rcx,8)
je 0x25a1
jmp 0x2596
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x2585
movl -0x2c(%rbp), %eax
addl $0x2, %eax
movslq %eax, %rdi
shlq $0x3, %rdi
callq 0x1180
movq %rax, -0x18(%rbp)
cmpq $0x0, -0x18(%rbp)
jne 0x25c3
jmp 0x2673
movq -0x40(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, (%rax)
movq -0x50(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x18(%rbp), %rdi
addq $0x10, %rdi
movq -0x10(%rbp), %rsi
addq $0x8, %rsi
movslq -0x2c(%rbp), %rdx
shlq $0x3, %rdx
callq 0x1150
movq -0x40(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq 0x29e2(%rip), %rax      # 0x4fe8
movq (%rax), %rdx
callq 0x1130
movq -0x18(%rbp), %rdi
callq 0x1040
callq 0x1060
movl (%rax), %edx
movl $0x8, %eax
movl $0x7, %ecx
cmpl $0x7, %edx
cmovel %ecx, %eax
movl %eax, -0x109c(%rbp)
callq 0x1060
movl -0x109c(%rbp), %ecx
movl %ecx, (%rax)
jmp 0x2673
jmp 0x2645
movq -0x58(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x58(%rbp)
movsbl (%rax), %eax
cmpl $0x3a, %eax
je 0x238e
cmpl $0x0, -0x30(%rbp)
je 0x2671
callq 0x1060
movl $0xd, (%rax)
jmp 0x2673
callq 0x1060
cmpl $0x0, (%rax)
jne 0x2688
callq 0x1060
movl $0x2, (%rax)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x10a0, %rsp           # imm = 0x10A0
popq %rbp
retq
nopw %cs:(%rax,%rax)