// File: /workspace/github_cpp_makefile_star2_O1/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 0xb5de
cmpl $-0x1, %eax
je 0xb609
leaq -0x38(%rbp), %r13
movq %r13, -0x10(%r13)
leaq 0x3400(%rip), %rsi      # 0xe5c7
leaq 0x3402(%rip), %rdx      # 0xe5d0
leaq -0x48(%rbp), %rdi
callq 0x713a
leaq -0x48(%rbp), %rsi
movq %r14, %rdi
callq 0x9c10
movq -0x48(%rbp), %rdi
cmpq %r13, %rdi
je 0xb1f1
callq 0x5230
movl -0x4c(%rbp), %eax
movl %eax, %ebx
andl $0x7f, %ebx
je 0xb2d0
movl %ebx, %ecx
shll $0x18, %ecx
addl $0x1000000, %ecx        # imm = 0x1000000
cmpl $0x2000000, %ecx        # imm = 0x2000000
jl 0xb35c
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 0x9c10
movq -0x48(%rbp), %rdi
cmpq %r13, %rdi
je 0xb247
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 0x6a15
leaq 0x3361(%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 0xb49f
movq %rdx, -0x70(%rbp)
movq (%rcx), %rdx
movq %rdx, -0x60(%rbp)
jmp 0xb4a6
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 0x6a15
leaq 0x32b2(%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 0xb421
movq %rdx, -0x70(%rbp)
movq (%rcx), %rdx
movq %rdx, -0x60(%rbp)
jmp 0xb428
cmpb $0x7f, %al
jne 0xb669
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 0x9c10
movq -0x48(%rbp), %rdi
cmpq %r13, %rdi
je 0xb398
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 0x6a15
leaq 0x3228(%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 0xb4ed
movq %rdx, -0x70(%rbp)
movq (%rcx), %rdx
movq %rdx, -0x60(%rbp)
jmp 0xb4f4
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 0x30bf(%rip), %rsi      # 0xe509
callq 0x5450
movq %r13, -0x48(%rbp)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0xb46f
movq %rdx, -0x48(%rbp)
movq (%rcx), %rdx
movq %rdx, -0x38(%rbp)
jmp 0xb477
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 0x9c10
jmp 0xb593
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 0x2fae(%rip), %rsi      # 0xe476
callq 0x5450
movq %r13, -0x48(%rbp)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0xb53b
movq %rdx, -0x48(%rbp)
movq (%rcx), %rdx
movq %rdx, -0x38(%rbp)
jmp 0xb543
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 0x2f60(%rip), %rsi      # 0xe476
callq 0x5450
movq %r13, -0x48(%rbp)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0xb568
movq %rdx, -0x48(%rbp)
movq (%rcx), %rdx
movq %rdx, -0x38(%rbp)
jmp 0xb570
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 0x9c10
jmp 0xb593
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 0x9c10
movq -0x48(%rbp), %rdi
cmpq %r13, %rdi
je 0xb5a1
callq 0x5230
movq -0x70(%rbp), %rdi
cmpq %r15, %rdi
je 0xb5af
callq 0x5230
movq -0x90(%rbp), %rdi
leaq -0x80(%rbp), %rax
cmpq %rax, %rdi
je 0xb5c4
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 0x2fbe(%rip), %rsi      # 0xe5ab
leaq 0x2fd2(%rip), %rdx      # 0xe5c6
leaq -0x48(%rbp), %rdi
callq 0x713a
leaq -0x48(%rbp), %rsi
movq %r14, %rdi
callq 0x9ba4
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 0xadb0
leaq -0x38(%rbp), %r15
movq -0x10(%r15), %rdi
cmpq %r15, %rdi
je 0xb643
callq 0x5230
leaq -0x48(%rbp), %rdi
movq %r15, (%rdi)
leaq 0x2f31(%rip), %rsi      # 0xe582
leaq 0x2f52(%rip), %rdx      # 0xe5aa
callq 0x713a
leaq -0x48(%rbp), %rsi
movq %r14, %rdi
callq 0x9ba4
leaq -0x48(%rbp), %rdi
movq %r13, (%rdi)
leaq 0x2fae(%rip), %rsi      # 0xe625
leaq 0x2fd3(%rip), %rdx      # 0xe651
callq 0x713a
leaq -0x48(%rbp), %rsi
movq %r14, %rdi
callq 0x9ba4
jmp 0xb6fb
jmp 0xb71e
jmp 0xb70b
jmp 0xb71e
movq %rax, %rbx
leaq -0x38(%rbp), %rax
movq -0x10(%rax), %rdi
jmp 0xb6f2
jmp 0xb6be
jmp 0xb6be
jmp 0xb6d1
jmp 0xb6d1
jmp 0xb6e4
jmp 0xb70b
jmp 0xb6e4
jmp 0xb71e
jmp 0xb70b
jmp 0xb71e
movq %rax, %rbx
movq -0x48(%rbp), %rdi
cmpq %r13, %rdi
je 0xb6d4
callq 0x5230
jmp 0xb6d4
movq %rax, %rbx
movq -0x70(%rbp), %rdi
cmpq %r15, %rdi
je 0xb6e7
callq 0x5230
jmp 0xb6e7
movq %rax, %rbx
movq -0x90(%rbp), %rdi
leaq -0x80(%rbp), %rax
cmpq %rax, %rdi
jne 0xb717
jmp 0xb721
jmp 0xb71e
movq %rax, %rbx
movq -0x48(%rbp), %rdi
cmpq %r15, %rdi
jne 0xb717
jmp 0xb721
jmp 0xb71e
movq %rax, %rbx
movq -0x48(%rbp), %rdi
cmpq %r13, %rdi
je 0xb721
callq 0x5230
jmp 0xb721
movq %rax, %rbx
movq %rbx, %rdi
callq 0x5400
nop