// File: /workspace/github_cpp_makefile_star2_O2/ryuichiueda[P]GlueLang/src/ExecForEach.cc
// Function: ExecForEach::execChild(DefFile*, DefStr*)
void ExecForEach::execChild(DefFile *f,  DefStr *s)
{
	string tmpdir = m_env->m_tmpdir + "/" + m_name;

	string line;
	while (getline(cin, line)){
		vector<string> ws;
		line += ' ';//add a space at the end of a line
		int from = 0;
		for(size_t i=from;i<line.size();i++){
			if(line.at(i) == ' '){
				ws.push_back(line.substr(from,i-from));
				from = i + 1;
			}
		}

		int pid = fork();
		if(pid < 0)
			exit(4);

		if(pid == 0){//child process (procedures in the loop)
			auto argv = new char* [ws.size()+2];
			argv[0] = (char *)tmpdir.c_str();
			for(size_t i=0;i<ws.size();i++)
				argv[i+1] = (char *)(ws[i].c_str());
			argv[ws.size()+1] = NULL;

			vOptProc(argv[0]);
			ifstream ifs(argv[0]);
			Feeder feeder(&ifs);
		
			m_env->initExecProc((const char**)argv);
			Script scr(&feeder,m_env,NULL);
		
			scr.setSilent();
			scr.parse();
			scr.exec(f,s); // exit in the exec function
		}else{
			int options = 0;
			int status;
			int wpid = waitpid(pid,&status,options);
			if(wpid < 1){
				m_error_msg = "Command wait error";
				m_exit_status = 2;
				throw this;
			}

			if(!WIFEXITED(status)){
				if(WIFSIGNALED(status) && WTERMSIG(status) == 13){//sigpipe
					continue;
				}else if(WIFSIGNALED(status) && WTERMSIG(status) == SIGUSR1){
					exit(0);
				}else{
					exit(0);
				}
			}else{
			       	m_exit_status = WEXITSTATUS(status);
			       	if(m_exit_status == 0){
					continue;
				}else if(m_exit_status == 128){
					exit(0);
				}else{
					exit(m_exit_status);
				}
			}
		}
	}
	exit(0);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x358, %rsp            # imm = 0x358
movq %rdx, 0x38(%rsp)
movq %rsi, 0x30(%rsp)
movq %rdi, %rbx
movl $0xc0, %esi
addq 0x10(%rdi), %rsi
leaq 0x6032(%rip), %rdx      # 0x115ee
leaq 0x150(%rsp), %r14
movq %r14, %rdi
callq 0x70c7
leaq 0xb0(%rbx), %rdx
leaq 0x60(%rsp), %rdi
movq %r14, %rsi
callq 0x75a1
leaq 0x150(%rsp), %r12
movq %r12, %rdi
callq 0x6150
leaq 0x50(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
movq 0xe9c8(%rip), %r14      # 0x19fd0
leaq 0x40(%rsp), %rbp
leaq 0x10(%rsp), %r15
movq %rbx, 0x8(%rsp)
movq %r14, %rdi
movq %rbp, %rsi
callq 0x6090
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0xb851
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
andq $0x0, 0x20(%rsp)
movq %rbp, %rdi
pushq $0x20
popq %rsi
callq 0x6290
xorl %r13d, %r13d
xorl %ebx, %ebx
cmpq 0x48(%rsp), %r13
jae 0xb699
movq %rbp, %rdi
movq %r13, %rsi
callq 0x65c0
cmpb $0x20, (%rax)
jne 0xb694
movslq %ebx, %rdx
movq %r13, %rcx
subq %rdx, %rcx
movq %r12, %rdi
movq %rbp, %rsi
callq 0x6340
movq %r15, %rdi
movq %r12, %rsi
callq 0xa540
movq %r12, %rdi
callq 0x6150
leal 0x1(%r13), %ebx
incq %r13
jmp 0xb652
callq 0x60d0
testl %eax, %eax
js 0xb801
je 0xb6e1
movl %eax, %edi
movq %r12, %rsi
xorl %edx, %edx
callq 0x6260
testl %eax, %eax
jle 0xb80e
movl 0x150(%rsp), %ecx
movl %ecx, %eax
andl $0x7f, %eax
movq 0x8(%rsp), %rbx
je 0xb7e3
cmpl $0xd, %eax
je 0xb7f4
jmp 0xb809
movq 0x10(%rsp), %rbx
movq 0x18(%rsp), %r14
subq %rbx, %r14
sarq $0x5, %r14
leaq 0x2(%r14), %rax
shrq $0x3d, %rax
leaq 0x10(,%r14,8), %rcx
negq %rax
movl $0x0, %edi
sbbq %rdi, %rdi
orq %rcx, %rdi
callq 0x6030
movq %rax, %r13
movq 0x60(%rsp), %rax
movq %rax, (%r13)
xorl %eax, %eax
cmpq %rax, %r14
je 0xb739
movq (%rbx), %rcx
movq %rcx, 0x8(%r13,%rax,8)
incq %rax
addq $0x20, %rbx
jmp 0xb723
andq $0x0, 0x8(%r13,%r14,8)
movq (%r13), %rsi
movq 0x8(%rsp), %rdi
callq 0xc8da
movq (%r13), %rsi
movq %r12, %rdi
pushq $0x8
popq %rdx
callq 0x6590
movq 0xe86d(%rip), %r14      # 0x19fd0
leaq 0x80(%rsp), %rdi
movq %r12, %rsi
callq 0xc95c
movq 0x8(%rsp), %rbx
movq 0x10(%rbx), %rdi
movq %r13, %rsi
callq 0x977a
movq 0x10(%rbx), %rdx
leaq 0xc8(%rsp), %r13
movq %r13, %rdi
leaq 0x80(%rsp), %rsi
xorl %ecx, %ecx
callq 0xf490
movb $0x1, 0x14d(%rsp)
movq %r13, %rdi
callq 0xf504
movq %r13, %rdi
movq 0x30(%rsp), %rsi
movq 0x38(%rsp), %rdx
callq 0xf8b8
movq %r13, %rdi
callq 0xf4ac
leaq 0x80(%rsp), %rdi
callq 0xca02
movq %r12, %rdi
callq 0x60c0
jmp 0xb7f4
shrl $0x8, %ecx
movzbl %cl, %edi
movl %edi, 0x80(%rbx)
testb %dil, %dil
jne 0xb849
movq %r15, %rdi
callq 0xa256
jmp 0xb617
pushq $0x4
popq %rdi
callq 0x63a0
cmpl $0xa, %eax
jmp 0xb851
movq 0x8(%rsp), %rbx
leaq 0x60(%rbx), %rdi
leaq 0x5cea(%rip), %rsi      # 0x11508
callq 0x65b0
movl $0x2, 0x80(%rbx)
pushq $0x8
popq %rdi
callq 0x6190
movq %rbx, (%rax)
leaq 0xdec9(%rip), %rsi      # 0x19708
movq %rax, %rdi
xorl %edx, %edx
callq 0x6540
cmpl $0x80, %edi
jne 0xb853
xorl %edi, %edi
callq 0x63a0
jmp 0xb85a
movq %rax, %rbx
jmp 0xb87a
movq %rax, %rbx
jmp 0xb887
jmp 0xb8b3
jmp 0xb8b3
jmp 0xb8b3
movq %rax, %rbx
leaq 0xc8(%rsp), %rdi
callq 0xf4ac
leaq 0x80(%rsp), %rdi
callq 0xca02
leaq 0x150(%rsp), %rdi
callq 0x60c0
jmp 0xb8b6
jmp 0xb8b3
jmp 0xb8b3
movq %rax, %rbx
jmp 0xb8c0
movq %rax, %rbx
leaq 0x150(%rsp), %rdi
callq 0x6150
jmp 0xb8b6
jmp 0xb8b3
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0xa256
leaq 0x40(%rsp), %rdi
callq 0x6150
leaq 0x60(%rsp), %rdi
jmp 0xb8dc
movq %rax, %rbx
leaq 0x150(%rsp), %rdi
callq 0x6150
movq %rbx, %rdi
callq 0x6560
nopl (%rax)