// File: ryuichiueda[P]GlueLang/src/Pipeline.cc
// Function: Pipeline::exec(DefFile*, DefStr*)
int Pipeline::exec(DefFile *f,  DefStr *s)
{
	// When wait(1) is set in the command line,
	// wait(1) is done in this process.
	// Wait(1) cannot be connected with other commands
	//auto *cl = (Executable *)m_nodes[0];
	if(((Executable *)m_nodes[0])->m_is_wait)
		return execWait();

	int pip[2];
	int prevfd = -1;

	for(auto *n : m_nodes){
		auto *p = (Executable *)n;
		pip[1] = -1;
		if ( p != m_nodes.back() && pipe(pip) < 0) {
			close(prevfd);
			m_error_msg = "Pipe call failed";
			throw this;
		}

		p->setPipe(pip,prevfd);
		//Only the last command is connected to the file/string
		if( n != m_nodes.back() )
			m_pids.push_back( p->exec(NULL,NULL) ); //to pipeline
		else
			m_pids.push_back( p->exec(f,s) ); //to file/string
		prevfd = p->getPrevPipe();
	}

	if(s != NULL)
		s->readFifo();

	for(auto pid : m_pids)
		waitCommands(pid);

	return m_exit_status;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xb0, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq -0x10(%rbp), %rdi
movq %rdi, -0x88(%rbp)
addq $0x18, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x14640
movq (%rax), %rax
testb $0x1, 0x85(%rax)
je 0x1a4df
movq -0x88(%rbp), %rdi
callq 0x1a720
movl %eax, -0x4(%rbp)
jmp 0x1a70f
movq -0x88(%rbp), %rax
movl $0xffffffff, -0x2c(%rbp) # imm = 0xFFFFFFFF
addq $0x18, %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rdi
callq 0xc010
movq %rax, -0x40(%rbp)
movq -0x38(%rbp), %rdi
callq 0xc040
movq %rax, -0x48(%rbp)
leaq -0x40(%rbp), %rdi
leaq -0x48(%rbp), %rsi
callq 0xc070
testb $0x1, %al
jne 0x1a525
jmp 0x1a689
leaq -0x40(%rbp), %rdi
callq 0xc0b0
movq -0x88(%rbp), %rdi
movq (%rax), %rax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
movq %rax, -0x58(%rbp)
movl $0xffffffff, -0x24(%rbp) # imm = 0xFFFFFFFF
movq -0x58(%rbp), %rax
movq %rax, -0x90(%rbp)
addq $0x18, %rdi
callq 0x19ba0
movq %rax, %rcx
movq -0x90(%rbp), %rax
cmpq (%rcx), %rax
je 0x1a5c2
leaq -0x28(%rbp), %rdi
callq 0x6590
cmpl $0x0, %eax
jge 0x1a5c2
movl -0x2c(%rbp), %edi
callq 0x6790
movq -0x88(%rbp), %rdi
addq $0x60, %rdi
leaq 0x314b(%rip), %rsi      # 0x1d6e1
callq 0x6580
movl $0x8, %edi
callq 0x6200
movq %rax, %rdi
movq -0x88(%rbp), %rax
movq %rax, (%rdi)
leaq 0xe5f7(%rip), %rsi      # 0x28bb0
xorl %eax, %eax
movl %eax, %edx
callq 0x66d0
movq -0x58(%rbp), %rdi
leaq -0x28(%rbp), %rsi
movl -0x2c(%rbp), %edx
callq 0x15f00
movq -0x88(%rbp), %rdi
movq -0x50(%rbp), %rax
movq %rax, -0x98(%rbp)
addq $0x18, %rdi
callq 0x19ba0
movq %rax, %rcx
movq -0x98(%rbp), %rax
cmpq (%rcx), %rax
je 0x1a636
movq -0x88(%rbp), %rax
addq $0x90, %rax
movq %rax, -0xa0(%rbp)
movq -0x58(%rbp), %rdi
movq (%rdi), %rax
xorl %ecx, %ecx
movl %ecx, %edx
movq %rdx, %rsi
callq *0x18(%rax)
movq -0xa0(%rbp), %rdi
movl %eax, -0x5c(%rbp)
leaq -0x5c(%rbp), %rsi
callq 0xbb80
jmp 0x1a66f
movq -0x88(%rbp), %rax
addq $0x90, %rax
movq %rax, -0xa8(%rbp)
movq -0x58(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
movq (%rdi), %rax
callq *0x18(%rax)
movq -0xa8(%rbp), %rdi
movl %eax, -0x60(%rbp)
leaq -0x60(%rbp), %rsi
callq 0xbb80
movq -0x58(%rbp), %rdi
callq 0x1ad00
movl %eax, -0x2c(%rbp)
leaq -0x40(%rbp), %rdi
callq 0xc0d0
jmp 0x1a50f
cmpq $0x0, -0x20(%rbp)
je 0x1a699
movq -0x20(%rbp), %rdi
callq 0xb520
movq -0x88(%rbp), %rax
addq $0x90, %rax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rdi
callq 0xc930
movq %rax, -0x70(%rbp)
movq -0x68(%rbp), %rdi
callq 0xa760
movq %rax, -0x78(%rbp)
leaq -0x70(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0x1ad20
testb $0x1, %al
jne 0x1a6d7
jmp 0x1a6ff
leaq -0x70(%rbp), %rdi
callq 0xa7d0
movq -0x88(%rbp), %rdi
movl (%rax), %eax
movl %eax, -0x7c(%rbp)
movl -0x7c(%rbp), %esi
callq 0x1aa50
leaq -0x70(%rbp), %rdi
callq 0x1ad60
jmp 0x1a6c4
movq -0x88(%rbp), %rax
movl 0x80(%rax), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xb0, %rsp
popq %rbp
retq
nopl (%rax,%rax)