// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %rbx
subq $0x68, %rsp
movl %esi, %eax
movq %rdi, %r14
movb $0x1, 0xe8(%rdi)
leaq -0x3c(%rbp), %rsi
pushq $0x2
popq %rdx
movl %eax, %edi
callq 0x51a0
testl %eax, %eax
je 0xb45f
cmpl $-0x1, %eax
je 0xb47f
leaq 0x32ee(%rip), %rsi      # 0xe5a7
leaq -0x38(%rbp), %rdi
leaq -0x60(%rbp), %rdx
callq 0x50d0
leaq -0x38(%rbp), %rsi
movq %r14, %rdi
callq 0xa478
leaq -0x38(%rbp), %rdi
callq 0x5100
movl -0x3c(%rbp), %eax
movl %eax, %ebx
andl $0x7f, %ebx
je 0xb372
movl %ebx, %ecx
shll $0x18, %ecx
addl $0x1000000, %ecx        # imm = 0x1000000
cmpl $0x2000000, %ecx        # imm = 0x2000000
jl 0xb3b6
movl %ebx, %edi
callq 0x53b0
leaq -0x38(%rbp), %rdi
leaq -0x60(%rbp), %rdx
movq %rax, %rsi
callq 0x50d0
leaq -0x38(%rbp), %rsi
movq %r14, %rdi
callq 0xa478
leaq -0x38(%rbp), %rdi
callq 0x5100
leaq -0x80(%rbp), %r15
movq %r15, %rdi
movl %ebx, %esi
callq 0x60e6
leaq 0x3293(%rip), %rsi      # 0xe5d4
leaq -0x60(%rbp), %rdi
movq %r15, %rdx
callq 0x60b8
leaq 0x3102(%rip), %rdx      # 0xe456
leaq -0x38(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0x5f7f
leaq -0x38(%rbp), %rsi
movq %r14, %rdi
callq 0xa478
jmp 0xb42e
movzbl %ah, %esi
leaq -0x80(%rbp), %rbx
movq %rbx, %rdi
callq 0x60e6
leaq 0x3229(%rip), %rsi      # 0xe5b1
leaq -0x60(%rbp), %rdi
movq %rbx, %rdx
callq 0x60b8
leaq 0x314e(%rip), %rdx      # 0xe4e9
leaq -0x38(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0x5f7f
leaq -0x38(%rbp), %rsi
movq %r14, %rdi
callq 0xa478
jmp 0xb42e
cmpb $0x7f, %al
jne 0xb4d0
movzbl %ah, %ebx
movl %ebx, %edi
callq 0x53b0
leaq -0x38(%rbp), %rdi
leaq -0x60(%rbp), %rdx
movq %rax, %rsi
callq 0x50d0
leaq -0x38(%rbp), %rsi
movq %r14, %rdi
callq 0xa478
leaq -0x38(%rbp), %rdi
callq 0x5100
leaq -0x80(%rbp), %r15
movq %r15, %rdi
movl %ebx, %esi
callq 0x60e6
leaq 0x31ea(%rip), %rsi      # 0xe5ec
leaq -0x60(%rbp), %rdi
movq %r15, %rdx
callq 0x60b8
leaq 0x3041(%rip), %rdx      # 0xe456
leaq -0x38(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0x5f7f
leaq -0x38(%rbp), %rsi
movq %r14, %rdi
callq 0xa478
leaq -0x38(%rbp), %rdi
callq 0x5100
leaq -0x60(%rbp), %rdi
callq 0x5100
leaq -0x80(%rbp), %rdi
callq 0x5100
movb $0x0, 0xe8(%r14)
movl -0x3c(%rbp), %eax
addq $0x68, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
leaq 0x3125(%rip), %rsi      # 0xe58b
leaq -0x38(%rbp), %rdi
leaq -0x60(%rbp), %rdx
callq 0x50d0
leaq -0x38(%rbp), %rsi
movq %r14, %rdi
callq 0xa418
callq 0x5050
movl (%rax), %edi
callq 0x50b0
leaq -0x38(%rbp), %rdi
leaq -0x60(%rbp), %rdx
movq %rax, %rsi
callq 0x50d0
leaq -0x38(%rbp), %rsi
movq %r14, %rdi
callq 0xb030
leaq -0x38(%rbp), %rdi
callq 0x5100
leaq 0x30ab(%rip), %rsi      # 0xe562
leaq -0x38(%rbp), %rdi
leaq -0x60(%rbp), %rdx
callq 0x50d0
leaq -0x38(%rbp), %rsi
movq %r14, %rdi
callq 0xa418
leaq 0x312e(%rip), %rsi      # 0xe605
leaq -0x38(%rbp), %rdi
leaq -0x60(%rbp), %rdx
callq 0x50d0
leaq -0x38(%rbp), %rsi
movq %r14, %rdi
callq 0xa418
jmp 0xb539
jmp 0xb547
jmp 0xb539
jmp 0xb547
jmp 0xb539
jmp 0xb50e
jmp 0xb51c
jmp 0xb50e
jmp 0xb52a
jmp 0xb51c
jmp 0xb539
jmp 0xb52a
jmp 0xb547
jmp 0xb539
jmp 0xb547
movq %rax, %rbx
leaq -0x38(%rbp), %rdi
callq 0x5100
jmp 0xb51f
movq %rax, %rbx
leaq -0x60(%rbp), %rdi
callq 0x5100
jmp 0xb52d
movq %rax, %rbx
leaq -0x80(%rbp), %rdi
jmp 0xb540
jmp 0xb547
jmp 0xb539
jmp 0xb547
movq %rax, %rbx
leaq -0x38(%rbp), %rdi
callq 0x5100
jmp 0xb54a
movq %rax, %rbx
movq %rbx, %rdi
callq 0x53a0