// File: 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
movq %rsp, %rbp
subq $0x420, %rsp            # imm = 0x420
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x3e0(%rbp)
movq 0x10(%rax), %rsi
addq $0xc0, %rsi
leaq 0x97e6(%rip), %rdx      # 0x1d5ba
leaq -0x58(%rbp), %rdi
movq %rdi, -0x3d8(%rbp)
callq 0x79f0
movq -0x3e0(%rbp), %rdx
movq -0x3d8(%rbp), %rsi
addq $0xb0, %rdx
leaq -0x38(%rbp), %rdi
callq 0x8720
jmp 0x13e04
leaq -0x58(%rbp), %rdi
callq 0x6280
leaq -0x88(%rbp), %rdi
callq 0x65a0
movq 0x151b0(%rip), %rdi     # 0x28fd0
leaq -0x88(%rbp), %rsi
callq 0x60d0
movq %rax, -0x3e8(%rbp)
jmp 0x13e35
movq -0x3e8(%rbp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
addq %rax, %rdi
callq 0x6550
movb %al, -0x3e9(%rbp)
jmp 0x13e53
movb -0x3e9(%rbp), %al
testb $0x1, %al
jne 0x13e62
jmp 0x143bc
leaq -0xa0(%rbp), %rdi
callq 0xf3e0
leaq -0x88(%rbp), %rdi
movl $0x20, %esi
callq 0x6290
jmp 0x13e81
movl $0x0, -0xa4(%rbp)
movslq -0xa4(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq -0xb0(%rbp), %rax
movq %rax, -0x3f8(%rbp)
leaq -0x88(%rbp), %rdi
callq 0x62c0
movq %rax, %rcx
movq -0x3f8(%rbp), %rax
cmpq %rcx, %rax
jae 0x13fc1
movq -0xb0(%rbp), %rsi
leaq -0x88(%rbp), %rdi
callq 0x6770
movq %rax, -0x400(%rbp)
jmp 0x13ee2
movq -0x400(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x20, %eax
jne 0x13fa8
movslq -0xa4(%rbp), %rdx
movq -0xb0(%rbp), %rcx
subq %rdx, %rcx
leaq -0xd0(%rbp), %rdi
leaq -0x88(%rbp), %rsi
callq 0x6440
jmp 0x13f1b
leaq -0xa0(%rbp), %rdi
leaq -0xd0(%rbp), %rsi
callq 0xf440
jmp 0x13f30
leaq -0xd0(%rbp), %rdi
callq 0x6280
movq -0xb0(%rbp), %rax
addq $0x1, %rax
movl %eax, -0xa4(%rbp)
jmp 0x13fa8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0x58(%rbp), %rdi
callq 0x6280
jmp 0x143d8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
jmp 0x143c3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
jmp 0x143ae
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0xd0(%rbp), %rdi
callq 0x6280
jmp 0x143ae
jmp 0x13faa
movq -0xb0(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xb0(%rbp)
jmp 0x13e99
callq 0x6110
movl %eax, -0xd4(%rbp)
cmpl $0x0, -0xd4(%rbp)
jge 0x13fdf
movl $0x4, %edi
callq 0x64f0
cmpl $0x0, -0xd4(%rbp)
jne 0x1421d
leaq -0xa0(%rbp), %rdi
callq 0xb6a0
addq $0x2, %rax
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
movq $-0x1, %rax
cmovoq %rax, %rdi
callq 0x6040
movq %rax, -0x408(%rbp)
jmp 0x14020
movq -0x408(%rbp), %rax
movq %rax, -0xe0(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x6190
movq %rax, %rcx
movq -0xe0(%rbp), %rax
movq %rcx, (%rax)
movq $0x0, -0xe8(%rbp)
movq -0xe8(%rbp), %rax
movq %rax, -0x410(%rbp)
leaq -0xa0(%rbp), %rdi
callq 0xb6a0
movq %rax, %rcx
movq -0x410(%rbp), %rax
cmpq %rcx, %rax
jae 0x140bd
movq -0xe8(%rbp), %rsi
leaq -0xa0(%rbp), %rdi
callq 0xb680
movq %rax, %rdi
callq 0x6190
movq %rax, %rdx
movq -0xe0(%rbp), %rax
movq -0xe8(%rbp), %rcx
movq %rdx, 0x8(%rax,%rcx,8)
movq -0xe8(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xe8(%rbp)
jmp 0x1404f
movq -0xe0(%rbp), %rax
movq %rax, -0x418(%rbp)
leaq -0xa0(%rbp), %rdi
callq 0xb6a0
movq -0x3e0(%rbp), %rdi
movq %rax, %rcx
movq -0x418(%rbp), %rax
movq $0x0, 0x8(%rax,%rcx,8)
movq -0xe0(%rbp), %rax
movq (%rax), %rsi
callq 0x15f40
jmp 0x14102
movq -0xe0(%rbp), %rax
movq (%rax), %rsi
leaq -0x2f0(%rbp), %rdi
movl $0x8, %edx
callq 0x6740
jmp 0x1411f
leaq -0x338(%rbp), %rdi
leaq -0x2f0(%rbp), %rsi
callq 0x15fe0
jmp 0x14134
movq -0x3e0(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0xe0(%rbp), %rsi
callq 0xe120
jmp 0x1414d
movq -0x3e0(%rbp), %rax
movq 0x10(%rax), %rdx
xorl %eax, %eax
movl %eax, %ecx
leaq -0x3c0(%rbp), %rdi
leaq -0x338(%rbp), %rsi
callq 0x1ad80
jmp 0x14171
leaq -0x3c0(%rbp), %rdi
callq 0x143f0
jmp 0x1417f
leaq -0x3c0(%rbp), %rdi
callq 0x1aeb0
jmp 0x1418d
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
leaq -0x3c0(%rbp), %rdi
callq 0x1b4c0
jmp 0x141a3
leaq -0x3c0(%rbp), %rdi
callq 0x1add0
leaq -0x338(%rbp), %rdi
callq 0x160f0
leaq -0x2f0(%rbp), %rdi
callq 0x6100
jmp 0x14382
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
jmp 0x1420c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
jmp 0x14200
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0x3c0(%rbp), %rdi
callq 0x1add0
leaq -0x338(%rbp), %rdi
callq 0x160f0
leaq -0x2f0(%rbp), %rdi
callq 0x6100
jmp 0x143ae
movl $0x0, -0x3c4(%rbp)
movl -0xd4(%rbp), %edi
movl -0x3c4(%rbp), %edx
leaq -0x3c8(%rbp), %rsi
callq 0x6330
movl %eax, -0x41c(%rbp)
jmp 0x14247
movl -0x41c(%rbp), %eax
movl %eax, -0x3cc(%rbp)
cmpl $0x1, -0x3cc(%rbp)
jge 0x142b2
movq -0x3e0(%rbp), %rdi
addq $0x60, %rdi
leaq 0x9263(%rip), %rsi      # 0x1d4d1
callq 0x6580
jmp 0x14275
movq -0x3e0(%rbp), %rax
movl $0x2, 0x80(%rax)
movl $0x8, %edi
callq 0x6200
movq %rax, %rdi
movq -0x3e0(%rbp), %rax
movq %rax, (%rdi)
leaq 0x14454(%rip), %rsi     # 0x286f8
xorl %eax, %eax
movl %eax, %edx
callq 0x66d0
jmp 0x143e1
movl -0x3c8(%rbp), %eax
andl $0x7f, %eax
cmpl $0x0, %eax
je 0x14325
movl -0x3c8(%rbp), %eax
andl $0x7f, %eax
addl $0x1, %eax
movsbl %al, %eax
sarl %eax
cmpl $0x0, %eax
jle 0x142f3
movl -0x3c8(%rbp), %eax
andl $0x7f, %eax
cmpl $0xd, %eax
jne 0x142f3
movl $0x2, -0x3d0(%rbp)
jmp 0x1438c
movl -0x3c8(%rbp), %eax
andl $0x7f, %eax
addl $0x1, %eax
movsbl %al, %eax
sarl %eax
cmpl $0x0, %eax
jle 0x1431e
movl -0x3c8(%rbp), %eax
andl $0x7f, %eax
cmpl $0xa, %eax
jne 0x1431e
xorl %edi, %edi
callq 0x64f0
xorl %edi, %edi
callq 0x64f0
movq -0x3e0(%rbp), %rax
movl -0x3c8(%rbp), %ecx
andl $0xff00, %ecx           # imm = 0xFF00
sarl $0x8, %ecx
movl %ecx, 0x80(%rax)
cmpl $0x0, 0x80(%rax)
jne 0x14356
movl $0x2, -0x3d0(%rbp)
jmp 0x1438c
movq -0x3e0(%rbp), %rax
cmpl $0x80, 0x80(%rax)
jne 0x14370
xorl %edi, %edi
callq 0x64f0
movq -0x3e0(%rbp), %rax
movl 0x80(%rax), %edi
callq 0x64f0
movl $0x0, -0x3d0(%rbp)
leaq -0xa0(%rbp), %rdi
callq 0xf4b0
movl -0x3d0(%rbp), %eax
testl %eax, %eax
je 0x143a9
jmp 0x143a4
jmp 0x13e19
jmp 0x13e19
leaq -0xa0(%rbp), %rdi
callq 0xf4b0
jmp 0x143c3
xorl %edi, %edi
callq 0x64f0
leaq -0x88(%rbp), %rdi
callq 0x6280
leaq -0x38(%rbp), %rdi
callq 0x6280
movq -0x60(%rbp), %rdi
callq 0x6700
nopw %cs:(%rax,%rax)
nopl (%rax,%rax)