// File: /workspace/github_c_makefile_star2_O3/rakitzis[P]rc/exec.c
// Function: exec
extern void exec(List *s, bool parent) {
	char **av, **ev = NULL;
	int stat;
	pid_t pid;
	builtin_t *b;
	char *path = NULL;
	bool didfork, returning, saw_exec, saw_builtin;
	struct termios t;
	av = list2array(s, dashex);
	saw_builtin = saw_exec = FALSE;
	do {
		if (*av == NULL	|| isabsolute(*av))
			b = NULL;
		else if (!saw_builtin && fnlookup(*av) != NULL)
			b = funcall;
		else
			b = isbuiltin(*av);

		/*
		   a builtin applies only to the immediately following
		   command, e.g., builtin exec echo hi
		*/
		saw_builtin = FALSE;

		if (b == b_exec) {
			av++;
			saw_exec = TRUE;
			parent = FALSE;
		} else if (b == b_builtin) {
			av++;
			saw_builtin = TRUE;
		}
	} while (b == b_exec || b == b_builtin);
	if (*av == NULL && saw_exec) { /* do redirs and return on a null exec */
		doredirs();
		return;
	}
	/* force an exit on exec with any rc_error, but not for null commands as above */
	if (saw_exec)
		rc_pid = -1;
	if (b == NULL) {
		path = which(*av, TRUE);
		if (path == NULL && *av != NULL) { /* perform null commands for redirections */
			set(FALSE);
			redirq = NULL;
			if (parent)
				return;
			rc_exit(1);
		}
		ev = makeenv(); /* environment only needs to be built for execve() */
	}
	/*
	   If parent & the redirq is nonnull, builtin or not it has to fork.
	   If the fifoq is nonnull, then it must be emptied at the end so we
	   must fork no matter what.
	 */
	if ((parent && (b == NULL || redirq != NULL)) || outstanding_cmdarg()) {
		if (interactive)
			tcgetattr(0, &t);
		pid = rc_fork();
		didfork = TRUE;
	} else {
		pid = 0;
		didfork = FALSE;
	}
	returning = (!didfork && parent);
	switch (pid) {
	case -1:
		uerror("fork");
		rc_error(NULL);
		/* NOTREACHED */
	case 0:
		if (!returning)
			setsigdefaults(FALSE);
		pop_cmdarg(FALSE);
		doredirs();

		/* null commands performed for redirections */
		if (*av == NULL || b != NULL) {
			if (b != NULL)
				(*b)(av);
			if (returning)
				return;
			rc_exit(getstatus());
		}
		rc_execve(path, av, ev);

#ifdef DEFAULTINTERP
		if (errno == ENOEXEC) {
			*av = path;
			*--av = DEFAULTINTERP;
			execve(*av, (char * const *) av, (char * const *) ev);
		}
#endif

		uerror(*av);
		rc_exit(1);
		/* NOTREACHED */
	default:
		redirq = NULL;
		rc_wait4(pid, &stat, TRUE);
		if (interactive && WIFSIGNALED(stat))
			tcsetattr(0, TCSANOW, &t);
		setstatus(-1, stat);
		/*
		   There is a very good reason for having this weird
		   nl_on_intr variable: when rc and its child both
		   process a SIGINT, (i.e., the child has a SIGINT
		   catcher installed) then you don't want rc to print
		   a newline when the child finally exits. Here's an
		   example: ed, <type ^C>, <type "q">. rc does not
		   and should not print a newline before the next
		   prompt, even though there's a SIGINT in its signal
		   vector.
		*/
		if (WIFEXITED(stat))
			nl_on_intr = FALSE;
		sigchk();
		nl_on_intr = TRUE;
		pop_cmdarg(TRUE);
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movl %esi, 0x4(%rsp)
leaq 0x153d7(%rip), %rax     # 0x19bf0
movl (%rax), %esi
callq 0x5110
movq %rax, %rbx
xorl %r15d, %r15d
leaq -0x1285(%rip), %r12     # 0x35a8
leaq -0x128d(%rip), %r13     # 0x35a7
xorl %ebp, %ebp
movq (%rbx), %rdi
testq %rdi, %rdi
je 0x4847
callq 0xc7b8
testl %eax, %eax
je 0x4864
xorl %r14d, %r14d
xorl %ebp, %ebp
cmpq %r12, %r14
sete %bpl
leaq (%rbx,%rbp,8), %rbx
xorl %eax, %eax
testb %al, %al
jne 0x4836
cmpq %r12, %r14
je 0x4836
jmp 0x48a9
testl %ebp, %ebp
je 0x4893
movq (%rbx), %rdi
callq 0x349c
movq %rax, %r14
cmpq %r13, %rax
jne 0x484a
addq $0x8, %rbx
movl $0x1, %r15d
movl $0x0, 0x4(%rsp)
movb $0x1, %al
movq %r13, %r14
xorl %ebp, %ebp
jmp 0x4859
movq (%rbx), %rdi
callq 0x4f99
leaq -0x13b4(%rip), %r14     # 0x34ee
testq %rax, %rax
jne 0x484a
jmp 0x4868
movq (%rbx), %rdi
testq %rdi, %rdi
sete %al
testl %r15d, %r15d
setne %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x48d2
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xb7c0
testl %r15d, %r15d
je 0x48e4
leaq 0x152e2(%rip), %rax     # 0x19bc0
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
testq %r14, %r14
movl 0x4(%rsp), %ebp
je 0x48f5
xorl %r12d, %r12d
xorl %r15d, %r15d
jmp 0x4913
movl $0x1, %esi
callq 0xde71
movq %rax, %r15
testq %rax, %rax
je 0x498b
callq 0x79c6
movq %rax, %r12
testl %ebp, %ebp
je 0x493c
testq %r14, %r14
je 0x4945
leaq 0x151a5(%rip), %rax     # 0x19ac8
cmpq $0x0, (%rax)
jne 0x4945
callq 0x4733
testl %eax, %eax
jne 0x4945
movb $0x1, %r13b
xorl %ebp, %ebp
jmp 0x49cd
callq 0x4733
testl %eax, %eax
je 0x49c1
leaq 0x15288(%rip), %rax     # 0x19bd4
cmpl $0x0, (%rax)
je 0x495d
leaq 0xc(%rsp), %rsi
xorl %edi, %edi
callq 0x32c0
callq 0xce78
movl %eax, %ebp
testl %eax, %eax
je 0x49c3
cmpl $-0x1, %ebp
jne 0x4a4b
leaq 0xaa82(%rip), %rdi      # 0xf3fa
callq 0xc6dd
xorl %edi, %edi
callq 0x46c0
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0x49c3
cmpq $0x0, (%rbx)
je 0x490b
xorl %edi, %edi
callq 0xbbec
leaq 0x15125(%rip), %rax     # 0x19ac8
movq $0x0, (%rax)
testl %ebp, %ebp
jne 0x4adb
movl $0x1, %edi
callq 0x4eb5
jmp 0x490b
xorl %ebp, %ebp
xorl %r13d, %r13d
xorl %edi, %edi
callq 0x4dba
xorl %edi, %edi
callq 0x4748
callq 0xb7c0
testq %r14, %r14
jne 0x49e4
cmpq $0x0, (%rbx)
jne 0x4a04
testq %r14, %r14
je 0x49ef
movq %rbx, %rdi
callq *%r14
testb %r13b, %r13b
jne 0x4adb
callq 0xbb95
movl %eax, %edi
callq 0x4eb5
movq %r15, %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x31e0
callq 0x3040
cmpl $0x8, (%rax)
jne 0x4a39
movq %r15, (%rbx)
leaq 0xa9d9(%rip), %rdi      # 0xf3ff
movq %rdi, -0x8(%rbx)
addq $-0x8, %rbx
movq %rbx, %rsi
movq %r12, %rdx
callq 0x31e0
movq (%rbx), %rdi
callq 0xc6dd
movl $0x1, %edi
callq 0x4eb5
leaq 0x15076(%rip), %rax     # 0x19ac8
movq $0x0, (%rax)
leaq 0x8(%rsp), %rbx
movl %ebp, %edi
movq %rbx, %rsi
movl $0x1, %edx
callq 0xcf0f
leaq 0x15160(%rip), %rax     # 0x19bd4
cmpl $0x0, (%rax)
movl (%rbx), %esi
je 0x4aa1
movl %esi, %eax
andl $0x7f, %eax
shll $0x18, %eax
addl $0x1000000, %eax        # imm = 0x1000000
cmpl $0x2000000, %eax        # imm = 0x2000000
jl 0x4aa1
leaq 0xc(%rsp), %rdx
xorl %edi, %edi
xorl %esi, %esi
callq 0x32f0
movl 0x8(%rsp), %esi
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0xbc0e
testb $0x7f, 0x8(%rsp)
jne 0x4abf
leaq 0x14837(%rip), %rax     # 0x192f0
movl $0x0, (%rax)
callq 0xb9f9
leaq 0x14825(%rip), %rax     # 0x192f0
movl $0x1, (%rax)
movl $0x1, %edi
callq 0x4748
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop