// File: /workspace/github_cpp_makefile_star2_O3/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 %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movl %esi, %eax
movq %rdi, %r14
movb $0x1, 0xe8(%rdi)
leaq -0x4c(%rbp), %rsi
movl %eax, %edi
movl $0x2, %edx
callq 0x51e0
testl %eax, %eax
je 0xb508
cmpl $-0x1, %eax
je 0xb533
leaq -0x38(%rbp), %r13
movq %r13, -0x10(%r13)
leaq 0x34d6(%rip), %rsi      # 0xe5c7
leaq 0x34d8(%rip), %rdx      # 0xe5d0
leaq -0x48(%rbp), %rdi
callq 0x7140
leaq -0x48(%rbp), %rsi
movq %r14, %rdi
callq 0x9b7c
movq -0x48(%rbp), %rdi
cmpq %r13, %rdi
je 0xb11b
callq 0x5230
movl -0x4c(%rbp), %eax
movl %eax, %ebx
andl $0x7f, %ebx
je 0xb1fa
movl %ebx, %ecx
shll $0x18, %ecx
addl $0x1000000, %ecx        # imm = 0x1000000
cmpl $0x2000000, %ecx        # imm = 0x2000000
jl 0xb286
movl %ebx, %edi
callq 0x5410
leaq -0x48(%rbp), %rdi
leaq -0x70(%rbp), %rdx
movq %rax, %rsi
callq 0x50f0
leaq -0x48(%rbp), %rsi
movq %r14, %rdi
callq 0x9b7c
movq -0x48(%rbp), %rdi
cmpq %r13, %rdi
je 0xb171
callq 0x5230
cmpl $0x64, %ebx
movl $0x3, %eax
sbbl $0x0, %eax
cmpl $0xa, %ebx
movl $0x1, %r15d
cmovael %eax, %r15d
leaq -0x80(%rbp), %rax
movq %rax, -0x10(%rax)
leaq -0x90(%rbp), %r12
movq %r12, %rdi
movq %r15, %rsi
movl $0x2d, %edx
callq 0x52b0
movq (%r12), %rdi
movl %r15d, %esi
movl %ebx, %edx
callq 0x6a3d
leaq 0x3437(%rip), %rcx      # 0xe5f4
movl $0x17, %r8d
movq %r12, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x5290
leaq -0x60(%rbp), %r15
movq %r15, -0x10(%r15)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0xb3c9
movq %rdx, -0x70(%rbp)
movq (%rcx), %rdx
movq %rdx, -0x60(%rbp)
jmp 0xb3d0
movzbl %ah, %ebx
cmpl $0x64, %ebx
movl $0x3, %eax
sbbl $0x0, %eax
cmpl $0xa, %ebx
movl $0x1, %r15d
cmovael %eax, %r15d
leaq -0x80(%rbp), %rax
movq %rax, -0x10(%rax)
leaq -0x90(%rbp), %r12
movq %r12, %rdi
movq %r15, %rsi
movl $0x2d, %edx
callq 0x52b0
movq (%r12), %rdi
movl %r15d, %esi
movl %ebx, %edx
callq 0x6a3d
leaq 0x3388(%rip), %rcx      # 0xe5d1
movl $0x22, %r8d
movq %r12, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x5290
leaq -0x60(%rbp), %r15
movq %r15, -0x10(%r15)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0xb34b
movq %rdx, -0x70(%rbp)
movq (%rcx), %rdx
movq %rdx, -0x60(%rbp)
jmp 0xb352
cmpb $0x7f, %al
jne 0xb593
movzbl %ah, %ebx
movl %ebx, %edi
callq 0x5410
leaq -0x48(%rbp), %rdi
leaq -0x70(%rbp), %rdx
movq %rax, %rsi
callq 0x50f0
leaq -0x48(%rbp), %rsi
movq %r14, %rdi
callq 0x9b7c
movq -0x48(%rbp), %rdi
cmpq %r13, %rdi
je 0xb2c2
callq 0x5230
cmpl $0x64, %ebx
movl $0x3, %eax
sbbl $0x0, %eax
cmpl $0xa, %ebx
movl $0x1, %r15d
cmovael %eax, %r15d
leaq -0x80(%rbp), %rax
movq %rax, -0x10(%rax)
leaq -0x90(%rbp), %r12
movq %r12, %rdi
movq %r15, %rsi
movl $0x2d, %edx
callq 0x52b0
movq (%r12), %rdi
movl %r15d, %esi
movl %ebx, %edx
callq 0x6a3d
leaq 0x32fe(%rip), %rcx      # 0xe60c
movl $0x18, %r8d
movq %r12, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x5290
leaq -0x60(%rbp), %r15
movq %r15, -0x10(%r15)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0xb417
movq %rdx, -0x70(%rbp)
movq (%rcx), %rdx
movq %rdx, -0x60(%rbp)
jmp 0xb41e
movups (%rcx), %xmm0
movups %xmm0, (%r15)
movq 0x8(%rax), %rdx
leaq -0x70(%rbp), %rdi
movq %rdx, 0x8(%rdi)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
leaq 0x3195(%rip), %rsi      # 0xe509
callq 0x5450
movq %r13, -0x48(%rbp)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0xb399
movq %rdx, -0x48(%rbp)
movq (%rcx), %rdx
movq %rdx, -0x38(%rbp)
jmp 0xb3a1
movups (%rcx), %xmm0
movups %xmm0, (%r13)
movq 0x8(%rax), %rdx
leaq -0x48(%rbp), %rsi
movq %rdx, 0x8(%rsi)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
movq %r14, %rdi
callq 0x9b7c
jmp 0xb4bd
movups (%rcx), %xmm0
movups %xmm0, (%r15)
movq 0x8(%rax), %rdx
leaq -0x70(%rbp), %rdi
movq %rdx, 0x8(%rdi)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
leaq 0x3084(%rip), %rsi      # 0xe476
callq 0x5450
movq %r13, -0x48(%rbp)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0xb465
movq %rdx, -0x48(%rbp)
movq (%rcx), %rdx
movq %rdx, -0x38(%rbp)
jmp 0xb46d
movups (%rcx), %xmm0
movups %xmm0, (%r15)
movq 0x8(%rax), %rdx
leaq -0x70(%rbp), %rdi
movq %rdx, 0x8(%rdi)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
leaq 0x3036(%rip), %rsi      # 0xe476
callq 0x5450
movq %r13, -0x48(%rbp)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0xb492
movq %rdx, -0x48(%rbp)
movq (%rcx), %rdx
movq %rdx, -0x38(%rbp)
jmp 0xb49a
movups (%rcx), %xmm0
movups %xmm0, (%r13)
movq 0x8(%rax), %rdx
leaq -0x48(%rbp), %rsi
movq %rdx, 0x8(%rsi)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
movq %r14, %rdi
callq 0x9b7c
jmp 0xb4bd
movups (%rcx), %xmm0
movups %xmm0, (%r13)
movq 0x8(%rax), %rdx
leaq -0x48(%rbp), %rsi
movq %rdx, 0x8(%rsi)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
movq %r14, %rdi
callq 0x9b7c
movq -0x48(%rbp), %rdi
cmpq %r13, %rdi
je 0xb4cb
callq 0x5230
movq -0x70(%rbp), %rdi
cmpq %r15, %rdi
je 0xb4d9
callq 0x5230
movq -0x90(%rbp), %rdi
leaq -0x80(%rbp), %rax
cmpq %rax, %rdi
je 0xb4ee
callq 0x5230
movb $0x0, 0xe8(%r14)
movl -0x4c(%rbp), %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq -0x38(%rbp), %r15
movq %r15, -0x10(%r15)
leaq 0x3094(%rip), %rsi      # 0xe5ab
leaq 0x30a8(%rip), %rdx      # 0xe5c6
leaq -0x48(%rbp), %rdi
callq 0x7140
leaq -0x48(%rbp), %rsi
movq %r14, %rdi
callq 0x9b10
callq 0x5060
movl (%rax), %edi
callq 0x50d0
leaq -0x48(%rbp), %rdi
leaq -0x70(%rbp), %rdx
movq %rax, %rsi
callq 0x50f0
leaq -0x48(%rbp), %rsi
movq %r14, %rdi
callq 0xacea
leaq -0x38(%rbp), %r15
movq -0x10(%r15), %rdi
cmpq %r15, %rdi
je 0xb56d
callq 0x5230
leaq -0x48(%rbp), %rdi
movq %r15, (%rdi)
leaq 0x3007(%rip), %rsi      # 0xe582
leaq 0x3028(%rip), %rdx      # 0xe5aa
callq 0x7140
leaq -0x48(%rbp), %rsi
movq %r14, %rdi
callq 0x9b10
leaq -0x48(%rbp), %rdi
movq %r13, (%rdi)
leaq 0x3084(%rip), %rsi      # 0xe625
leaq 0x30a9(%rip), %rdx      # 0xe651
callq 0x7140
leaq -0x48(%rbp), %rsi
movq %r14, %rdi
callq 0x9b10
jmp 0xb625
jmp 0xb648
jmp 0xb635
jmp 0xb648
movq %rax, %rbx
leaq -0x38(%rbp), %rax
movq -0x10(%rax), %rdi
jmp 0xb61c
jmp 0xb5e8
jmp 0xb5e8
jmp 0xb5fb
jmp 0xb5fb
jmp 0xb60e
jmp 0xb635
jmp 0xb60e
jmp 0xb648
jmp 0xb635
jmp 0xb648
movq %rax, %rbx
movq -0x48(%rbp), %rdi
cmpq %r13, %rdi
je 0xb5fe
callq 0x5230
jmp 0xb5fe
movq %rax, %rbx
movq -0x70(%rbp), %rdi
cmpq %r15, %rdi
je 0xb611
callq 0x5230
jmp 0xb611
movq %rax, %rbx
movq -0x90(%rbp), %rdi
leaq -0x80(%rbp), %rax
cmpq %rax, %rdi
jne 0xb641
jmp 0xb64b
jmp 0xb648
movq %rax, %rbx
movq -0x48(%rbp), %rdi
cmpq %r15, %rdi
jne 0xb641
jmp 0xb64b
jmp 0xb648
movq %rax, %rbx
movq -0x48(%rbp), %rdi
cmpq %r13, %rdi
je 0xb64b
callq 0x5230
jmp 0xb64b
movq %rax, %rbx
movq %rbx, %rdi
callq 0x5400
nop