// File: /workspace/github_c_makefile_star2_O0/ebiggers[P]mysh/mysh_main.c
// Function: execute_pipeline
static int execute_pipeline(struct list_head command_tokens[],
			    unsigned ncommands)
{
	unsigned i;
	int ret;

	bool async;
	struct list_head redir_lists[ncommands];
	struct list_head cmd_arg_lists[ncommands];
	struct list_head var_assignment_lists[ncommands];
	unsigned cmd_nargs[ncommands];
	unsigned num_redirs[ncommands];

	int pipe_fds[2];
	int prev_read_end;
	pid_t child_pids[ncommands];

	mysh_assert(ncommands > 0);
	async = false;
	i = 0;
	do {
		INIT_LIST_HEAD(&cmd_arg_lists[i]);
		INIT_LIST_HEAD(&redir_lists[i]);
		INIT_LIST_HEAD(&var_assignment_lists[i]);
	} while (++i != ncommands);
	i = 0;
	do {
		ret = parse_tok_list(&command_tokens[i],
				     (i == ncommands - 1) ? &async : NULL,
				     &cmd_arg_lists[i],
				     &var_assignment_lists[i],
				     &redir_lists[i],
				     &cmd_nargs[i],
				     &num_redirs[i]);
		if (ret)
			goto out_free_lists;
	} while (++i != ncommands);

	if (mysh_noexecute)
		goto out_free_lists;

	/* If the pipeline only has one command and is not being executed
	 * asynchronously, try interpreting the command as a sequence of
	 * variable assignments or as a shell builtin.  Note: this means that
	 * with the current code, builtins cannot be used as parts of a
	 * multi-component pipeline. */
	if (ncommands == 1 && !async) {
		if (cmd_nargs[0] == 0) {
			/* No arguments: just process leading variable
			 * assignments. */
			ret = process_var_assignments(&var_assignment_lists[0]);
			goto out_free_lists;
		} else if (maybe_execute_builtin(&cmd_arg_lists[0],
						 &redir_lists[0],
						 cmd_nargs[0], &ret))
			goto out_free_lists;
		/* Not a builtin */
	}

	/* Verify that there is a command to execute for each component of the
	 * pipeline (e.g. there isn't a component that is only redirections) */
	i = 0;
	do {
		if (cmd_nargs[i] == 0) {
			mysh_error("Expected command name");
			ret = -1;
			goto out_free_lists;
		}
	} while (++i != ncommands);

	/* Execute the commands in the pipeline.  This essentially is done by
	 * fork() followed by execvp() for each command.  Redirections are
	 * handled by the forked child processes before calling execvp().  Pipes
	 * are created with pipe() before the fork.  Note that each component of
	 * the pipeline other than the first and last is involved with two
	 * different pipes: one to read from, and one to write to.  The
	 * @prev_read_end variable is used to save the read end of a pipe for
	 * the next pipeline component. */
	prev_read_end = pipe_fds[0] = pipe_fds[1] = -1;
	i = 0;
	do {
		/* Unless this is the last command, create a new pair of pipes */
		if (i != ncommands - 1) {
			if (pipe(pipe_fds)) {
				mysh_error_with_errno("can't create pipes");
				goto out_close_pipes;
			}
		}

		/* Fork the process */
		ret = fork();
		if (ret < 0) {
			/* fork() error */
			mysh_error_with_errno("can't fork child process");
			goto out_close_pipes;
		} else if (ret == 0) {
			if (async) {
				/* Background processes are executed with a new
				 * session ID and with standard input redirected
				 * from /dev/null. */
				int fd;

				errno = 0;
				setsid();
				fd = open("/dev/null", O_RDONLY);
				dup2(fd, STDIN_FILENO);
				close(fd);
				if (errno) {
					mysh_error_with_errno("problem daemonizing "
							      "background process");
				}
			}
			/* Child: set up redirections and execute new process */
			start_child(&cmd_arg_lists[i],
				    &redir_lists[i],
				    &var_assignment_lists[i],
				    cmd_nargs[i], i, ncommands,
				    pipe_fds, prev_read_end);
			/* Not reached */
			mysh_assert(0);
		}

		/* Parent: save child pid in an array */
		child_pids[i] = ret;

		/* Close any pipes we created that are no longer needed; also
		 * save the read end of the pipe (if any) in the prev_read_end
		 * variable.  */
		if (prev_read_end >= 0)
			close(prev_read_end);
		prev_read_end = pipe_fds[0];
		pipe_fds[0] = -1;
		if (pipe_fds[1] >= 0) {
			close(pipe_fds[1]);
			pipe_fds[1] = -1;
		}
	} while (++i != ncommands);
	ret = 0;
out_close_pipes:
	if (pipe_fds[0] >= 0)
		close(pipe_fds[0]);
	if (pipe_fds[1] >= 0)
		close(pipe_fds[1]);
	if (prev_read_end >= 0)
		close(prev_read_end);
	/* If no error has occurred and the pipeline is not being executed
	 * asynchronously, wait for the commands to terminate.  'ret' is set to
	 * the exit status of the last command in the pipeline.
	 *
	 * If the pipeline is to be asynchronous, don't wait for any children;
	 * just add an entry to the list of background pipelines. */
	if (ret == 0) {
		if (async)
			ret = add_background_pipeline(child_pids, ncommands);
		else
			ret = wait_for_children(child_pids, ncommands);
	}
out_free_lists:
	i = 0;
	do {
		free_string_list(&cmd_arg_lists[i]);
		free_string_list(&var_assignment_lists[i]);
		free_redir_list(&redir_lists[i]);
	} while (++i != ncommands);
	return ret;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xb0, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movl -0xc(%rbp), %eax
movq %rsp, %rcx
movq %rcx, -0x20(%rbp)
movq %rax, %rdx
shlq $0x4, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x98(%rbp)
movq %rcx, %rsp
movq %rax, -0x28(%rbp)
movl -0xc(%rbp), %eax
movq %rax, %rdx
shlq $0x4, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x90(%rbp)
movq %rcx, %rsp
movq %rax, -0x30(%rbp)
movl -0xc(%rbp), %eax
movq %rax, %rdx
shlq $0x4, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x88(%rbp)
movq %rcx, %rsp
movq %rax, -0x38(%rbp)
movl -0xc(%rbp), %eax
leaq 0xf(,%rax,4), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x80(%rbp)
movq %rcx, %rsp
movq %rax, -0x40(%rbp)
movl -0xc(%rbp), %eax
leaq 0xf(,%rax,4), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x78(%rbp)
movq %rcx, %rsp
movq %rax, -0x48(%rbp)
movl -0xc(%rbp), %eax
leaq 0xf(,%rax,4), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x70(%rbp)
movq %rcx, %rsp
movq %rax, -0x60(%rbp)
cmpl $0x0, -0xc(%rbp)
jbe 0x493b
jmp 0x495a
leaq 0x5d5c(%rip), %rdi      # 0xa69e
leaq 0x5d06(%rip), %rsi      # 0xa64f
movl $0x14c, %edx            # imm = 0x14C
leaq 0x5d57(%rip), %rcx      # 0xa6ac
callq 0x2160
movb $0x0, -0x15(%rbp)
movl $0x0, -0x10(%rbp)
movq -0x90(%rbp), %rdi
movl -0x10(%rbp), %eax
shlq $0x4, %rax
addq %rax, %rdi
callq 0x40e0
movq -0x98(%rbp), %rdi
movl -0x10(%rbp), %eax
shlq $0x4, %rax
addq %rax, %rdi
callq 0x40e0
movq -0x88(%rbp), %rdi
movl -0x10(%rbp), %eax
shlq $0x4, %rax
addq %rax, %rdi
callq 0x40e0
movl -0x10(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x10(%rbp)
cmpl -0xc(%rbp), %eax
jne 0x4965
movl $0x0, -0x10(%rbp)
movq -0x8(%rbp), %rax
movl -0x10(%rbp), %ecx
shlq $0x4, %rcx
addq %rcx, %rax
movq %rax, -0xa0(%rbp)
movl -0x10(%rbp), %eax
movl -0xc(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jne 0x49eb
leaq -0x15(%rbp), %rax
movq %rax, -0xa8(%rbp)
jmp 0x49f6
xorl %eax, %eax
movq %rax, -0xa8(%rbp)
jmp 0x49f6
movq -0xa0(%rbp), %rdi
movq -0x78(%rbp), %rax
movq -0x80(%rbp), %r9
movq -0x98(%rbp), %r8
movq -0x88(%rbp), %rcx
movq -0x90(%rbp), %rdx
movq -0xa8(%rbp), %rsi
movl -0x10(%rbp), %r10d
shlq $0x4, %r10
addq %r10, %rdx
movl -0x10(%rbp), %r10d
shlq $0x4, %r10
addq %r10, %rcx
movl -0x10(%rbp), %r10d
shlq $0x4, %r10
addq %r10, %r8
movl -0x10(%rbp), %r10d
shlq $0x2, %r10
addq %r10, %r9
movl -0x10(%rbp), %r10d
shlq $0x2, %r10
addq %r10, %rax
subq $0x10, %rsp
movq %rax, (%rsp)
callq 0x6fd0
addq $0x10, %rsp
movl %eax, -0x14(%rbp)
cmpl $0x0, -0x14(%rbp)
je 0x4a77
jmp 0x4d09
jmp 0x4a79
movl -0x10(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x10(%rbp)
cmpl -0xc(%rbp), %eax
jne 0x49bc
cmpl $0x0, 0x9ba6(%rip)      # 0xe638
je 0x4a99
jmp 0x4d09
cmpl $0x1, -0xc(%rbp)
jne 0x4aee
testb $0x1, -0x15(%rbp)
jne 0x4aee
movq -0x80(%rbp), %rax
cmpl $0x0, (%rax)
jne 0x4ac2
movq -0x88(%rbp), %rdi
callq 0x4dd0
movl %eax, -0x14(%rbp)
jmp 0x4d09
movq -0x98(%rbp), %rsi
movq -0x90(%rbp), %rdi
movq -0x80(%rbp), %rax
movl (%rax), %edx
leaq -0x14(%rbp), %rcx
callq 0x24e0
testb $0x1, %al
jne 0x4ae5
jmp 0x4aea
jmp 0x4d09
jmp 0x4aec
jmp 0x4aee
movl $0x0, -0x10(%rbp)
movq -0x80(%rbp), %rax
movl -0x10(%rbp), %ecx
cmpl $0x0, (%rax,%rcx,4)
jne 0x4b1c
leaq 0x5bda(%rip), %rdi      # 0xa6e3
movb $0x0, %al
callq 0x8c20
movl $0xffffffff, -0x14(%rbp) # imm = 0xFFFFFFFF
jmp 0x4d09
jmp 0x4b1e
movl -0x10(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x10(%rbp)
cmpl -0xc(%rbp), %eax
jne 0x4af5
movl $0xffffffff, -0x4c(%rbp) # imm = 0xFFFFFFFF
movl $0xffffffff, -0x50(%rbp) # imm = 0xFFFFFFFF
movl $0xffffffff, -0x54(%rbp) # imm = 0xFFFFFFFF
movl $0x0, -0x10(%rbp)
movl -0x10(%rbp), %eax
movl -0xc(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
je 0x4b78
leaq -0x50(%rbp), %rdi
callq 0x21d0
cmpl $0x0, %eax
je 0x4b76
leaq 0x5b8f(%rip), %rdi      # 0xa6f9
movb $0x0, %al
callq 0x8cf0
jmp 0x4caf
jmp 0x4b78
callq 0x23c0
movl %eax, -0x14(%rbp)
cmpl $0x0, -0x14(%rbp)
jge 0x4b99
leaq 0x5b7f(%rip), %rdi      # 0xa70c
movb $0x0, %al
callq 0x8cf0
jmp 0x4caf
cmpl $0x0, -0x14(%rbp)
jne 0x4c55
testb $0x1, -0x15(%rbp)
je 0x4bf8
callq 0x2060
movl $0x0, (%rax)
callq 0x21e0
leaq 0x5b65(%rip), %rdi      # 0xa725
xorl %esi, %esi
movb $0x0, %al
callq 0x22c0
movl %eax, -0x64(%rbp)
movl -0x64(%rbp), %edi
xorl %esi, %esi
callq 0x2120
movl -0x64(%rbp), %edi
callq 0x21c0
callq 0x2060
cmpl $0x0, (%rax)
je 0x4bf6
leaq 0x5b40(%rip), %rdi      # 0xa72f
movb $0x0, %al
callq 0x8cf0
jmp 0x4bf8
movq -0x80(%rbp), %rax
movq -0x88(%rbp), %rdx
movq -0x98(%rbp), %rsi
movq -0x90(%rbp), %rdi
movl -0x10(%rbp), %ecx
shlq $0x4, %rcx
addq %rcx, %rdi
movl -0x10(%rbp), %ecx
shlq $0x4, %rcx
addq %rcx, %rsi
movl -0x10(%rbp), %ecx
shlq $0x4, %rcx
addq %rcx, %rdx
movl -0x10(%rbp), %ecx
movl (%rax,%rcx,4), %ecx
movl -0x10(%rbp), %r8d
movl -0xc(%rbp), %r9d
leaq -0x50(%rbp), %r10
movl -0x54(%rbp), %eax
subq $0x10, %rsp
movq %r10, (%rsp)
movl %eax, 0x8(%rsp)
callq 0x4e40
jmp 0x4c57
movq -0x70(%rbp), %rax
movl -0x14(%rbp), %edx
movl -0x10(%rbp), %ecx
movl %edx, (%rax,%rcx,4)
cmpl $0x0, -0x54(%rbp)
jl 0x4c72
movl -0x54(%rbp), %edi
callq 0x21c0
movl -0x50(%rbp), %eax
movl %eax, -0x54(%rbp)
movl $0xffffffff, -0x50(%rbp) # imm = 0xFFFFFFFF
cmpl $0x0, -0x4c(%rbp)
jl 0x4c94
movl -0x4c(%rbp), %edi
callq 0x21c0
movl $0xffffffff, -0x4c(%rbp) # imm = 0xFFFFFFFF
jmp 0x4c96
movl -0x10(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x10(%rbp)
cmpl -0xc(%rbp), %eax
jne 0x4b48
movl $0x0, -0x14(%rbp)
cmpl $0x0, -0x50(%rbp)
jl 0x4cbd
movl -0x50(%rbp), %edi
callq 0x21c0
cmpl $0x0, -0x4c(%rbp)
jl 0x4ccb
movl -0x4c(%rbp), %edi
callq 0x21c0
cmpl $0x0, -0x54(%rbp)
jl 0x4cd9
movl -0x54(%rbp), %edi
callq 0x21c0
cmpl $0x0, -0x14(%rbp)
jne 0x4d07
testb $0x1, -0x15(%rbp)
je 0x4cf6
movq -0x70(%rbp), %rdi
movl -0xc(%rbp), %esi
callq 0x50b0
movl %eax, -0x14(%rbp)
jmp 0x4d05
movq -0x70(%rbp), %rdi
movl -0xc(%rbp), %esi
callq 0x5180
movl %eax, -0x14(%rbp)
jmp 0x4d07
jmp 0x4d09
movl $0x0, -0x10(%rbp)
movq -0x90(%rbp), %rdi
movl -0x10(%rbp), %eax
shlq $0x4, %rax
addq %rax, %rdi
callq 0x9050
movq -0x88(%rbp), %rdi
movl -0x10(%rbp), %eax
shlq $0x4, %rax
addq %rax, %rdi
callq 0x9050
movq -0x98(%rbp), %rdi
movl -0x10(%rbp), %eax
shlq $0x4, %rax
addq %rax, %rdi
callq 0x5260
movl -0x10(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x10(%rbp)
cmpl -0xc(%rbp), %eax
jne 0x4d10
movl -0x14(%rbp), %eax
movq -0x20(%rbp), %rcx
movq %rcx, %rsp
movq %rbp, %rsp
popq %rbp
retq
nop