// File: /workspace/github_cpp_makefile_star2_O1/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 $0x348, %rsp            # imm = 0x348
movq %rdx, 0x48(%rsp)
movq %rsi, 0x40(%rsp)
movq %rdi, %rbx
movq 0x10(%rdi), %rax
leaq 0x80(%rsp), %r12
movq %r12, -0x10(%r12)
movq 0xc0(%rax), %rsi
movq 0xc8(%rax), %rdx
addq %rsi, %rdx
leaq 0x70(%rsp), %r14
movq %r14, %rdi
callq 0x7456
leaq 0x7133(%rip), %rsi      # 0x14611
movq %r14, %rdi
callq 0x65d0
movq 0xb0(%rbx), %rsi
movq 0xb8(%rbx), %rdx
leaq 0x70(%rsp), %rdi
callq 0x6100
leaq 0x60(%rsp), %rsi
movq %rsi, -0x10(%rsi)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
jne 0xd51e
movups (%rcx), %xmm0
movups %xmm0, (%rsi)
jmp 0xd52b
movq %rdx, 0x50(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x60(%rsp)
movq 0x8(%rax), %rdx
movq %rdx, 0x58(%rsp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
movq 0x70(%rsp), %rdi
cmpq %r12, %rdi
je 0xd55d
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x6330
leaq 0x10(%rsp), %rax
movq %rax, -0x10(%rax)
movq $0x0, -0x8(%rax)
movb $0x0, (%rax)
movq 0xea60(%rip), %r14      # 0x1bfd8
movq %rsp, %r13
leaq 0x70(%rsp), %rbp
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x6300
movsbl %al, %edx
movq %r14, %rdi
movq %r13, %rsi
callq 0x65c0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0xd805
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsp)
movq $0x0, 0x30(%rsp)
movq %r13, %rdi
movl $0x20, %esi
callq 0x6290
cmpq $0x0, 0x8(%rsp)
je 0xd633
xorl %eax, %eax
xorl %r15d, %r15d
movq (%rsp), %rcx
cmpb $0x20, (%rcx,%r15)
jne 0xd629
movslq %eax, %rdx
movq %r15, %rcx
subq %rdx, %rcx
movq %rbp, %rdi
movq %r13, %rsi
callq 0x6350
leaq 0x20(%rsp), %rdi
movq %rbp, %rsi
callq 0xc154
movq 0x70(%rsp), %rdi
cmpq %r12, %rdi
je 0xd625
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x6330
leal 0x1(%r15), %eax
incq %r15
cmpq 0x8(%rsp), %r15
jb 0xd5df
callq 0x60c0
testl %eax, %eax
js 0xd7b5
movq %rbx, %r13
je 0xd67c
movl %eax, %edi
movq %rbp, %rsi
xorl %edx, %edx
callq 0x6250
testl %eax, %eax
jle 0xd7bf
movl 0x70(%rsp), %ecx
movl %ecx, %eax
andl $0x7f, %eax
movq %r13, %rbx
je 0xd792
cmpl $0xd, %eax
movq %rsp, %r13
je 0xd7a6
jmp 0xd7f8
movq 0x20(%rsp), %r12
movq 0x28(%rsp), %r14
movq %r14, %rbx
subq %r12, %rbx
sarq $0x5, %rbx
leaq 0x2(%rbx), %rax
shrq $0x3d, %rax
leaq 0x10(,%rbx,8), %rcx
negq %rax
movl $0x0, %edi
sbbq %rdi, %rdi
orq %rcx, %rdi
callq 0x6030
movq %rax, %r15
movq 0x50(%rsp), %rax
movq %rax, (%r15)
cmpq %r12, %r14
je 0xd6e5
cmpq $0x1, %rbx
movq %rbx, %rax
adcq $0x0, %rax
xorl %ecx, %ecx
movq (%r12), %rdx
movq %rdx, 0x8(%r15,%rcx,8)
incq %rcx
addq $0x20, %r12
cmpq %rcx, %rax
jne 0xd6d0
movq $0x0, 0x8(%r15,%rbx,8)
movq (%r15), %rsi
movq %r13, %rdi
callq 0xeeee
movq (%r15), %rsi
movq %rbp, %rdi
movl $0x8, %edx
callq 0x65b0
leaq 0x278(%rsp), %r14
movq %r14, %rdi
movq %rbp, %rsi
callq 0xefd4
movq %r13, %rbx
movq 0x10(%r13), %rdi
movq %r15, %rsi
callq 0xa8e8
movq 0x10(%rbx), %rdx
leaq 0x2c0(%rsp), %r15
movq %r15, %rdi
movq %r14, %rsi
xorl %ecx, %ecx
callq 0x128ac
movb $0x1, 0x345(%rsp)
movq %r15, %rdi
callq 0x12920
movq %r15, %rdi
movq 0x40(%rsp), %rsi
movq 0x48(%rsp), %rdx
callq 0x12dce
movq %r15, %rdi
callq 0x128c8
movq %r14, %rdi
callq 0xf0b6
movq %rbp, %rdi
callq 0x60b0
leaq 0x80(%rsp), %r12
movq 0xe84b(%rip), %r14      # 0x1bfd8
movq %rsp, %r13
jmp 0xd7a6
shrl $0x8, %ecx
movzbl %cl, %edi
movl %edi, 0x80(%rbx)
testb %dil, %dil
movq %rsp, %r13
jne 0xd7fd
leaq 0x20(%rsp), %rdi
callq 0xbcd0
jmp 0xd580
movl $0x4, %edi
callq 0x63c0
leaq 0x60(%r13), %rdi
leaq 0x6d5e(%rip), %rsi      # 0x14528
callq 0x6410
movl $0x2, 0x80(%r13)
movl $0x8, %edi
callq 0x6190
movq %r13, (%rax)
leaq 0xdf1a(%rip), %rsi      # 0x1b708
movq %rax, %rdi
xorl %edx, %edx
callq 0x6550
cmpl $0xa, %eax
jmp 0xd805
cmpl $0x80, %edi
jne 0xd807
xorl %edi, %edi
callq 0x63c0
jmp 0xd80e
movq %rax, %r14
jmp 0xd82e
movq %rax, %r14
jmp 0xd83b
jmp 0xd850
jmp 0xd850
jmp 0xd850
movq %rax, %r14
leaq 0x2c0(%rsp), %rdi
callq 0x128c8
leaq 0x278(%rsp), %rdi
callq 0xf0b6
leaq 0x70(%rsp), %rdi
callq 0x60b0
jmp 0xd872
jmp 0xd850
jmp 0xd850
movq %rax, %r14
jmp 0xd87c
movq %rax, %r14
jmp 0xd872
movq %rax, %r14
movq 0x70(%rsp), %rdi
cmpq %r12, %rdi
je 0xd872
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x6330
leaq 0x20(%rsp), %rdi
callq 0xbcd0
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0xd897
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x6330
movq 0x50(%rsp), %rdi
leaq 0x60(%rsp), %rax
cmpq %rax, %rdi
je 0xd8cc
movq 0x60(%rsp), %rsi
jmp 0xd8c4
jmp 0xd8af
movq %rax, %r14
movq 0x70(%rsp), %rdi
cmpq %r12, %rdi
je 0xd8cc
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x6330
movq %r14, %rdi
callq 0x6580