// File: ryuichiueda[P]GlueLang/src/ExecWhile.cc
// Function: ExecWhile::execChild(DefFile*, DefStr*)
void ExecWhile::execChild(DefFile *f,  DefStr *s)
{
	auto argv = new char* [2];
	string tmpdir = m_env->m_tmpdir + "/" + m_name;
	argv[0] = (char *)tmpdir.c_str();
	argv[1] = NULL;

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

		if(pid == 0){//child process (procedures in the loop)
			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 != 1){
					exit(m_exit_status);
				}else{
					exit(0);
				}
			}
		}
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x370, %rsp            # imm = 0x370
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x368(%rbp)
movl $0x10, %edi
callq 0x6040
movq %rax, %rcx
movq -0x368(%rbp), %rax
movq %rcx, -0x20(%rbp)
movq 0x10(%rax), %rsi
addq $0xc0, %rsi
leaq 0x821e(%rip), %rdx      # 0x1d5ba
leaq -0x60(%rbp), %rdi
movq %rdi, -0x360(%rbp)
callq 0x79f0
movq -0x368(%rbp), %rdx
movq -0x360(%rbp), %rsi
addq $0xb0, %rdx
leaq -0x40(%rbp), %rdi
callq 0x8720
jmp 0x153cc
leaq -0x60(%rbp), %rdi
callq 0x6280
leaq -0x40(%rbp), %rdi
callq 0x6190
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq %rcx, (%rax)
movq -0x20(%rbp), %rax
movq $0x0, 0x8(%rax)
jmp 0x153f6
callq 0x6110
movl %eax, -0x70(%rbp)
cmpl $0x0, -0x70(%rbp)
jge 0x15428
movl $0x4, %edi
callq 0x64f0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
leaq -0x60(%rbp), %rdi
callq 0x6280
jmp 0x156c9
cmpl $0x0, -0x70(%rbp)
jne 0x1556d
movq -0x368(%rbp), %rdi
movq -0x20(%rbp), %rax
movq (%rax), %rsi
callq 0x15f40
jmp 0x15447
movq -0x20(%rbp), %rax
movq (%rax), %rsi
leaq -0x278(%rbp), %rdi
movl $0x8, %edx
callq 0x6740
jmp 0x15461
leaq -0x2c0(%rbp), %rdi
leaq -0x278(%rbp), %rsi
callq 0x15fe0
jmp 0x15476
movq -0x368(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x20(%rbp), %rsi
callq 0xe120
jmp 0x1548c
movq -0x368(%rbp), %rax
movq 0x10(%rax), %rdx
xorl %eax, %eax
movl %eax, %ecx
leaq -0x348(%rbp), %rdi
leaq -0x2c0(%rbp), %rsi
callq 0x1ad80
jmp 0x154b0
leaq -0x348(%rbp), %rdi
callq 0x143f0
jmp 0x154be
leaq -0x348(%rbp), %rdi
callq 0x1aeb0
jmp 0x154cc
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
leaq -0x348(%rbp), %rdi
callq 0x1b4c0
jmp 0x154e2
leaq -0x348(%rbp), %rdi
callq 0x1add0
leaq -0x2c0(%rbp), %rdi
callq 0x160f0
leaq -0x278(%rbp), %rdi
callq 0x6100
jmp 0x156bb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
jmp 0x156c0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
jmp 0x1555c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
jmp 0x15550
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
leaq -0x348(%rbp), %rdi
callq 0x1add0
leaq -0x2c0(%rbp), %rdi
callq 0x160f0
leaq -0x278(%rbp), %rdi
callq 0x6100
jmp 0x156c0
movl $0x0, -0x34c(%rbp)
movl -0x70(%rbp), %edi
movl -0x34c(%rbp), %edx
leaq -0x350(%rbp), %rsi
callq 0x6330
movl %eax, -0x36c(%rbp)
jmp 0x15594
movl -0x36c(%rbp), %eax
movl %eax, -0x354(%rbp)
cmpl $0x1, -0x354(%rbp)
jge 0x155ff
movq -0x368(%rbp), %rdi
addq $0x60, %rdi
leaq 0x7f16(%rip), %rsi      # 0x1d4d1
callq 0x6580
jmp 0x155c2
movq -0x368(%rbp), %rax
movl $0x2, 0x80(%rax)
movl $0x8, %edi
callq 0x6200
movq %rax, %rdi
movq -0x368(%rbp), %rax
movq %rax, (%rdi)
leaq 0x13307(%rip), %rsi     # 0x288f8
xorl %eax, %eax
movl %eax, %edx
callq 0x66d0
jmp 0x156d2
movl -0x350(%rbp), %eax
andl $0x7f, %eax
cmpl $0x0, %eax
je 0x15668
movl -0x350(%rbp), %eax
andl $0x7f, %eax
addl $0x1, %eax
movsbl %al, %eax
sarl %eax
cmpl $0x0, %eax
jle 0x15636
movl -0x350(%rbp), %eax
andl $0x7f, %eax
cmpl $0xd, %eax
jne 0x15636
jmp 0x153f4
movl -0x350(%rbp), %eax
andl $0x7f, %eax
addl $0x1, %eax
movsbl %al, %eax
sarl %eax
cmpl $0x0, %eax
jle 0x15661
movl -0x350(%rbp), %eax
andl $0x7f, %eax
cmpl $0xa, %eax
jne 0x15661
xorl %edi, %edi
callq 0x64f0
xorl %edi, %edi
callq 0x64f0
movq -0x368(%rbp), %rax
movl -0x350(%rbp), %ecx
andl $0xff00, %ecx           # imm = 0xFF00
sarl $0x8, %ecx
movl %ecx, 0x80(%rax)
cmpl $0x0, 0x80(%rax)
jne 0x15692
jmp 0x153f4
movq -0x368(%rbp), %rax
cmpl $0x1, 0x80(%rax)
je 0x156b4
movq -0x368(%rbp), %rax
movl 0x80(%rax), %edi
callq 0x64f0
xorl %edi, %edi
callq 0x64f0
jmp 0x153f4
leaq -0x40(%rbp), %rdi
callq 0x6280
movq -0x68(%rbp), %rdi
callq 0x6700
nopw %cs:(%rax,%rax)
nopl (%rax)