// File: AndreiBarsan[P]uShell/shell.cc
// Function: microshell::core::Shell::wait_child(int)
int Shell::wait_child(int child_pid) {
  int child_status;
  int child_exit_code = -1;
  int waitpid_options = WUNTRACED;

  this->waiting_for_child = true;
  // TODO(andrei) Consider using wait4 and logging rusage data.
  while(true) {
    pid_t ret = waitpid(child_pid, &child_status, waitpid_options);
    if(-1 == ret) {
      this->error(strerror(errno));
      this->fatal("`waitpid' has encountered a fatal error.");
    }
    if(0 == ret) {
      this->fatal("waitpid should not return 0");
    }
    this->info("Woken up!");

    if(WIFEXITED(child_status)) {
      child_exit_code = WEXITSTATUS(child_status);
      this->info(
        "Child exited normally (exit code: " + to_string(child_exit_code) + ")."
      );
      break;
    }
    else if(WIFSIGNALED(child_status)) {
      int child_murdering_signal = WTERMSIG(child_status);
      this->info(strsignal(child_murdering_signal));
      this->info(
        "Child killed by signal " + to_string(child_murdering_signal) + "."
      );
      break;
    }
    else if(WIFSTOPPED(child_status)) {
      int child_stopper = WSTOPSIG(child_status);
      this->info(strsignal(child_stopper));
      this->info("Child stopped by signal " + to_string(child_stopper) + ".");
      break;
    }
    else {
      this->fatal("Unexpected child process state. Terminating.");
    }
  }
  this->waiting_for_child = false;

  // TODO(andrei) This is broken on killed/suspended children.
  return child_status;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x2f0, %rsp            # imm = 0x2F0
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x280(%rbp)
movl $0xffffffff, -0x14(%rbp) # imm = 0xFFFFFFFF
movl $0x2, -0x18(%rbp)
movb $0x1, 0xe8(%rax)
movl -0xc(%rbp), %edi
movl -0x18(%rbp), %edx
leaq -0x10(%rbp), %rsi
callq 0x5200
movl %eax, -0x1c(%rbp)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl -0x1c(%rbp), %eax
jne 0x1ba93
callq 0x5070
movl (%rax), %edi
callq 0x50d0
movq %rax, -0x290(%rbp)
leaq -0x41(%rbp), %rdi
movq %rdi, -0x288(%rbp)
callq 0x5470
movq -0x290(%rbp), %rsi
movq -0x288(%rbp), %rdx
leaq -0x40(%rbp), %rdi
callq 0x5360
jmp 0x1b9be
movq -0x280(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0x1b560
jmp 0x1b9d0
leaq -0x40(%rbp), %rdi
callq 0x5190
leaq -0x41(%rbp), %rdi
callq 0x52c0
leaq -0x79(%rbp), %rdi
movq %rdi, -0x298(%rbp)
callq 0x5470
movq -0x298(%rbp), %rdx
leaq 0x7b49(%rip), %rsi      # 0x23549
leaq -0x78(%rbp), %rdi
callq 0x5360
jmp 0x1ba0b
movq -0x280(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0x19ee0
jmp 0x1ba1d
leaq -0x78(%rbp), %rdi
callq 0x5190
leaq -0x79(%rbp), %rdi
callq 0x52c0
jmp 0x1ba93
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x1ba54
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x40(%rbp), %rdi
callq 0x5190
leaq -0x41(%rbp), %rdi
callq 0x52c0
jmp 0x1c0b6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x1ba85
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x78(%rbp), %rdi
callq 0x5190
leaq -0x79(%rbp), %rdi
callq 0x52c0
jmp 0x1c0b6
xorl %eax, %eax
cmpl -0x1c(%rbp), %eax
jne 0x1bb33
leaq -0xa1(%rbp), %rdi
movq %rdi, -0x2a0(%rbp)
callq 0x5470
movq -0x2a0(%rbp), %rdx
leaq 0x7ab3(%rip), %rsi      # 0x23572
leaq -0xa0(%rbp), %rdi
callq 0x5360
jmp 0x1bacd
movq -0x280(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
callq 0x19ee0
jmp 0x1bae2
leaq -0xa0(%rbp), %rdi
callq 0x5190
leaq -0xa1(%rbp), %rdi
callq 0x52c0
jmp 0x1bb33
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x1bb22
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0xa0(%rbp), %rdi
callq 0x5190
leaq -0xa1(%rbp), %rdi
callq 0x52c0
jmp 0x1c0b6
leaq -0xc9(%rbp), %rdi
movq %rdi, -0x2a8(%rbp)
callq 0x5470
movq -0x2a8(%rbp), %rdx
leaq 0x7a3a(%rip), %rsi      # 0x2358e
leaq -0xc8(%rbp), %rdi
callq 0x5360
jmp 0x1bb62
movq -0x280(%rbp), %rdi
leaq -0xc8(%rbp), %rsi
callq 0x19f70
jmp 0x1bb77
leaq -0xc8(%rbp), %rdi
callq 0x5190
leaq -0xc9(%rbp), %rdi
callq 0x52c0
movl -0x10(%rbp), %eax
andl $0x7f, %eax
cmpl $0x0, %eax
jne 0x1bcb9
movzbl -0xf(%rbp), %eax
movl %eax, -0x14(%rbp)
movl -0x14(%rbp), %esi
leaq -0x130(%rbp), %rdi
movq %rdi, -0x2b0(%rbp)
callq 0x6ca0
movq -0x2b0(%rbp), %rdx
leaq 0x79cf(%rip), %rsi      # 0x23598
leaq -0x110(%rbp), %rdi
callq 0x6c50
jmp 0x1bbd7
leaq 0x78f2(%rip), %rdx      # 0x234d0
leaq -0xf0(%rbp), %rdi
leaq -0x110(%rbp), %rsi
callq 0x69b0
jmp 0x1bbf3
movq -0x280(%rbp), %rdi
leaq -0xf0(%rbp), %rsi
callq 0x19f70
jmp 0x1bc08
leaq -0xf0(%rbp), %rdi
callq 0x5190
leaq -0x110(%rbp), %rdi
callq 0x5190
leaq -0x130(%rbp), %rdi
callq 0x5190
jmp 0x1c09c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x1bc57
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0xc8(%rbp), %rdi
callq 0x5190
leaq -0xc9(%rbp), %rdi
callq 0x52c0
jmp 0x1c0b6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x1bca8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x1bc9c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0xf0(%rbp), %rdi
callq 0x5190
leaq -0x110(%rbp), %rdi
callq 0x5190
leaq -0x130(%rbp), %rdi
callq 0x5190
jmp 0x1c0b6
movl -0x10(%rbp), %eax
andl $0x7f, %eax
addl $0x1, %eax
movsbl %al, %eax
sarl %eax
cmpl $0x0, %eax
jle 0x1be61
movl -0x10(%rbp), %eax
andl $0x7f, %eax
movl %eax, -0x134(%rbp)
movl -0x134(%rbp), %edi
callq 0x5480
movq %rax, -0x2c0(%rbp)
leaq -0x159(%rbp), %rdi
movq %rdi, -0x2b8(%rbp)
callq 0x5470
movq -0x2c0(%rbp), %rsi
movq -0x2b8(%rbp), %rdx
leaq -0x158(%rbp), %rdi
callq 0x5360
jmp 0x1bd1d
movq -0x280(%rbp), %rdi
leaq -0x158(%rbp), %rsi
callq 0x19f70
jmp 0x1bd32
leaq -0x158(%rbp), %rdi
callq 0x5190
leaq -0x159(%rbp), %rdi
callq 0x52c0
movl -0x134(%rbp), %esi
leaq -0x1c0(%rbp), %rdi
movq %rdi, -0x2c8(%rbp)
callq 0x6ca0
movq -0x2c8(%rbp), %rdx
leaq 0x784a(%rip), %rsi      # 0x235bb
leaq -0x1a0(%rbp), %rdi
callq 0x6c50
jmp 0x1bd7f
leaq 0x76b7(%rip), %rdx      # 0x2343d
leaq -0x180(%rbp), %rdi
leaq -0x1a0(%rbp), %rsi
callq 0x69b0
jmp 0x1bd9b
movq -0x280(%rbp), %rdi
leaq -0x180(%rbp), %rsi
callq 0x19f70
jmp 0x1bdb0
leaq -0x180(%rbp), %rdi
callq 0x5190
leaq -0x1a0(%rbp), %rdi
callq 0x5190
leaq -0x1c0(%rbp), %rdi
callq 0x5190
jmp 0x1c09c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x1bdff
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x158(%rbp), %rdi
callq 0x5190
leaq -0x159(%rbp), %rdi
callq 0x52c0
jmp 0x1c0b6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x1be50
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x1be44
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x180(%rbp), %rdi
callq 0x5190
leaq -0x1a0(%rbp), %rdi
callq 0x5190
leaq -0x1c0(%rbp), %rdi
callq 0x5190
jmp 0x1c0b6
movl -0x10(%rbp), %eax
andl $0xff, %eax
cmpl $0x7f, %eax
jne 0x1c001
movzbl -0xf(%rbp), %eax
movl %eax, -0x1c4(%rbp)
movl -0x1c4(%rbp), %edi
callq 0x5480
movq %rax, -0x2d8(%rbp)
leaq -0x1e9(%rbp), %rdi
movq %rdi, -0x2d0(%rbp)
callq 0x5470
movq -0x2d8(%rbp), %rsi
movq -0x2d0(%rbp), %rdx
leaq -0x1e8(%rbp), %rdi
callq 0x5360
jmp 0x1bebd
movq -0x280(%rbp), %rdi
leaq -0x1e8(%rbp), %rsi
callq 0x19f70
jmp 0x1bed2
leaq -0x1e8(%rbp), %rdi
callq 0x5190
leaq -0x1e9(%rbp), %rdi
callq 0x52c0
movl -0x1c4(%rbp), %esi
leaq -0x250(%rbp), %rdi
movq %rdi, -0x2e0(%rbp)
callq 0x6ca0
movq -0x2e0(%rbp), %rdx
leaq 0x76c2(%rip), %rsi      # 0x235d3
leaq -0x230(%rbp), %rdi
callq 0x6c50
jmp 0x1bf1f
leaq 0x7517(%rip), %rdx      # 0x2343d
leaq -0x210(%rbp), %rdi
leaq -0x230(%rbp), %rsi
callq 0x69b0
jmp 0x1bf3b
movq -0x280(%rbp), %rdi
leaq -0x210(%rbp), %rsi
callq 0x19f70
jmp 0x1bf50
leaq -0x210(%rbp), %rdi
callq 0x5190
leaq -0x230(%rbp), %rdi
callq 0x5190
leaq -0x250(%rbp), %rdi
callq 0x5190
jmp 0x1c09c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x1bf9f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x1e8(%rbp), %rdi
callq 0x5190
leaq -0x1e9(%rbp), %rdi
callq 0x52c0
jmp 0x1c0b6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x1bff0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x1bfe4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x210(%rbp), %rdi
callq 0x5190
leaq -0x230(%rbp), %rdi
callq 0x5190
leaq -0x250(%rbp), %rdi
callq 0x5190
jmp 0x1c0b6
leaq -0x271(%rbp), %rdi
movq %rdi, -0x2e8(%rbp)
callq 0x5470
movq -0x2e8(%rbp), %rdx
leaq 0x75ca(%rip), %rsi      # 0x235ec
leaq -0x270(%rbp), %rdi
callq 0x5360
jmp 0x1c030
movq -0x280(%rbp), %rdi
leaq -0x270(%rbp), %rsi
callq 0x19ee0
jmp 0x1c045
leaq -0x270(%rbp), %rdi
callq 0x5190
leaq -0x271(%rbp), %rdi
callq 0x52c0
jmp 0x1c093
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x1c085
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x270(%rbp), %rdi
callq 0x5190
leaq -0x271(%rbp), %rdi
callq 0x52c0
jmp 0x1c0b6
jmp 0x1c095
jmp 0x1c097
jmp 0x1b962
movq -0x280(%rbp), %rax
movb $0x0, 0xe8(%rax)
movl -0x10(%rbp), %eax
addq $0x2f0, %rsp            # imm = 0x2F0
popq %rbp
retq
movq -0x50(%rbp), %rdi
callq 0x5460
nop