// File: /workspace/github_c_makefile_star2_O3/arachsys[P]containers/console.c
// Function: supervise
int supervise(pid_t child, int console) {
  char buffer[PIPE_BUF];
  int signals, slave, status;
  sigset_t mask;
  ssize_t count, length, offset;
  struct pollfd fds[3];

  if (console < 0) {
    if (waitpid(child, &status, 0) < 0)
      err(EXIT_FAILURE, "waitpid");
    return WIFEXITED(status) ? WEXITSTATUS(status) : EXIT_FAILURE;
  }

  sigemptyset(&mask);
  sigaddset(&mask, SIGCHLD);
  sigprocmask(SIG_BLOCK, &mask, NULL);
  if ((signals = signalfd(-1, &mask, 0)) < 0)
    err(EXIT_FAILURE, "signalfd");

  if (waitpid(child, &status, WNOHANG) > 0)
    if (WIFEXITED(status) || WIFSIGNALED(status))
      raise(SIGCHLD);

  savemode();
  atexit(restoremode);
  rawmode();

  slave = open(ptsname(console), O_RDWR);

  fds[0].fd = console;
  fds[0].events = POLLIN;
  fds[1].fd = STDIN_FILENO;
  fds[1].events = POLLIN;
  fds[2].fd = signals;
  fds[2].events = POLLIN;

  while (1) {
    if (poll(fds, 3, -1) < 0)
        if (errno != EAGAIN && errno != EINTR)
          err(EXIT_FAILURE, "poll");

    if (fds[0].revents & POLLIN) {
      if ((length = read(console, buffer, sizeof(buffer))) < 0)
        if (errno != EAGAIN && errno != EINTR)
          err(EXIT_FAILURE, "read");
      for (offset = 0; length > 0; offset += count, length -= count)
        while ((count = write(STDOUT_FILENO, buffer + offset, length)) < 0)
          if (errno != EAGAIN && errno != EINTR)
            err(EXIT_FAILURE, "write");
    }

    if (fds[1].revents & (POLLHUP | POLLIN)) {
      if ((length = read(STDIN_FILENO, buffer, sizeof(buffer))) == 0)
        fds[1].events = 0;
      else if (length < 0 && errno != EAGAIN && errno != EINTR)
        err(EXIT_FAILURE, "read");
      for (offset = 0; length > 0; offset += count, length -= count)
        while ((count = write(console, buffer + offset, length)) < 0)
          if (errno != EAGAIN && errno != EINTR)
            err(EXIT_FAILURE, "write");
    }

    if (fds[2].revents & POLLIN) {
      if (read(signals, buffer, sizeof(buffer)) < 0)
        if (errno != EAGAIN && errno != EINTR)
          err(EXIT_FAILURE, "read");
      if (waitpid(child, &status, WNOHANG) > 0)
        if (WIFEXITED(status) || WIFSIGNALED(status))
          break;
    }
  }

  close(signals);
  close(slave);

  while ((length = read(console, buffer, sizeof(buffer)))) {
    if (length < 0 && errno != EAGAIN && errno != EINTR)
      break;
    for (offset = 0; length > 0; offset += count, length -= count)
      while ((count = write(STDOUT_FILENO, buffer + offset, length)) < 0)
        if (errno != EAGAIN && errno != EINTR)
          err(EXIT_FAILURE, "write");
  }

  return WIFEXITED(status) ? WEXITSTATUS(status) : EXIT_FAILURE;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x10b8, %rsp           # imm = 0x10B8
movl %edi, %ebp
testl %esi, %esi
js 0x2fba
movl %esi, %ebx
leaq 0x30(%rsp), %r14
movq %r14, %rdi
callq 0x22b0
movq %r14, %rdi
movl $0x11, %esi
callq 0x24a0
xorl %edi, %edi
movq %r14, %rsi
xorl %edx, %edx
callq 0x2050
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
movq %r14, %rsi
xorl %edx, %edx
callq 0x21f0
movl %eax, 0x4(%rsp)
testl %eax, %eax
js 0x301a
movq %rsp, %rsi
movl %ebp, %edi
movl $0x1, %edx
callq 0x23b0
testl %eax, %eax
jle 0x2c7e
movl (%rsp), %eax
andl $0x7f, %eax
je 0x2c74
shll $0x18, %eax
addl $0x1000000, %eax        # imm = 0x1000000
cmpl $0x2000000, %eax        # imm = 0x2000000
jl 0x2c7e
movl $0x11, %edi
callq 0x2060
xorl %edi, %edi
callq 0x20c0
testl %eax, %eax
je 0x2c9f
leaq 0x55ec(%rip), %rsi      # 0x827c
xorl %edi, %edi
callq 0x23c0
testl %eax, %eax
js 0x3011
leaq 0x386(%rip), %rdi       # 0x302c
callq 0x4260
xorl %edi, %edi
callq 0x20c0
testl %eax, %eax
je 0x2ce9
leaq 0xb0(%rsp), %rsi
xorl %edi, %edi
callq 0x23c0
testl %eax, %eax
js 0x3011
leaq 0xb0(%rsp), %r14
movq %r14, %rdi
callq 0x2360
xorl %edi, %edi
xorl %esi, %esi
movq %r14, %rdx
callq 0x23d0
movl %ebp, 0xc(%rsp)
movl %ebx, %edi
callq 0x2040
movq %rax, %rdi
movl $0x2, %esi
xorl %eax, %eax
callq 0x23e0
movl %eax, 0x8(%rsp)
leaq 0x10(%rsp), %rdx
movl %ebx, (%rdx)
movw $0x1, %ax
movw %ax, 0x4(%rdx)
movl $0x0, 0x8(%rdx)
movw %ax, 0xc(%rdx)
movl 0x4(%rsp), %ecx
movl %ecx, 0x10(%rdx)
movw %ax, 0x14(%rdx)
leaq 0xb0(%rsp), %r13
movl $0x3, %esi
leaq 0x10(%rsp), %rdi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x2390
testl %eax, %eax
jns 0x2d61
callq 0x2080
movl (%rax), %eax
cmpl $0x4, %eax
je 0x2d61
cmpl $0xb, %eax
jne 0x3008
testb $0x1, 0x16(%rsp)
je 0x2df5
movl $0x1000, %edx           # imm = 0x1000
movl %ebx, %edi
movq %r13, %rsi
callq 0x2240
testq %rax, %rax
js 0x2de0
je 0x2df5
movq %rax, %r15
xorl %r12d, %r12d
leaq (%rsp,%r12), %rbp
addq $0xb0, %rbp
movl $0x1, %edi
movq %rbp, %rsi
movq %r15, %rdx
callq 0x20f0
testq %rax, %rax
jns 0x2dd6
callq 0x2080
movq %rax, %r14
movl (%r14), %eax
cmpl $0x4, %eax
je 0x2dc1
cmpl $0xb, %eax
jne 0x2fec
movl $0x1, %edi
movq %rbp, %rsi
movq %r15, %rdx
callq 0x20f0
testq %rax, %rax
js 0x2db0
addq %rax, %r12
subq %rax, %r15
jg 0x2d88
jmp 0x2df5
callq 0x2080
movl (%rax), %eax
cmpl $0x4, %eax
je 0x2df5
cmpl $0xb, %eax
jne 0x2fff
testb $0x11, 0x1e(%rsp)
je 0x2e8c
movl $0x1000, %edx           # imm = 0x1000
xorl %edi, %edi
movq %r13, %rsi
callq 0x2240
testq %rax, %rax
je 0x2e6e
js 0x2e77
movq %rax, %r15
xorl %r12d, %r12d
leaq (%rsp,%r12), %rbp
addq $0xb0, %rbp
movl %ebx, %edi
movq %rbp, %rsi
movq %r15, %rdx
callq 0x20f0
testq %rax, %rax
jns 0x2e64
callq 0x2080
movq %rax, %r14
movl (%r14), %eax
cmpl $0x4, %eax
je 0x2e52
cmpl $0xb, %eax
jne 0x2fec
movl %ebx, %edi
movq %rbp, %rsi
movq %r15, %rdx
callq 0x20f0
testq %rax, %rax
js 0x2e41
addq %rax, %r12
subq %rax, %r15
jg 0x2e1c
jmp 0x2e8c
movw $0x0, 0x1c(%rsp)
jmp 0x2e8c
callq 0x2080
movl (%rax), %eax
cmpl $0x4, %eax
je 0x2e8c
cmpl $0xb, %eax
jne 0x2fff
testb $0x1, 0x26(%rsp)
je 0x2d34
movl $0x1000, %edx           # imm = 0x1000
movl 0x4(%rsp), %edi
movq %r13, %rsi
callq 0x2240
testq %rax, %rax
jns 0x2ec2
callq 0x2080
movl (%rax), %eax
cmpl $0x4, %eax
je 0x2ec2
cmpl $0xb, %eax
jne 0x2fff
movl 0xc(%rsp), %edi
movq %rsp, %rsi
movl $0x1, %edx
callq 0x23b0
testl %eax, %eax
jle 0x2d34
movl (%rsp), %eax
andl $0x7f, %eax
je 0x2ef6
shll $0x18, %eax
addl $0x1000000, %eax        # imm = 0x1000000
cmpl $0x2000000, %eax        # imm = 0x2000000
jl 0x2d34
movl 0x4(%rsp), %edi
callq 0x2210
movl 0x8(%rsp), %edi
callq 0x2210
leaq 0xb0(%rsp), %rsi
movl $0x1000, %edx           # imm = 0x1000
movl %ebx, %edi
callq 0x2240
testq %rax, %rax
je 0x2fca
movq %rax, %r15
leaq 0xb0(%rsp), %r14
testq %r15, %r15
js 0x2f8c
xorl %ebp, %ebp
leaq (%rsp,%rbp), %r12
addq $0xb0, %r12
movl $0x1, %edi
movq %r12, %rsi
movq %r15, %rdx
callq 0x20f0
testq %rax, %rax
jns 0x2f82
callq 0x2080
movq %rax, %r13
movl (%r13), %eax
cmpl $0x4, %eax
je 0x2f6d
cmpl $0xb, %eax
jne 0x2fec
movl $0x1, %edi
movq %r12, %rsi
movq %r15, %rdx
callq 0x20f0
testq %rax, %rax
js 0x2f5f
addq %rax, %rbp
subq %rax, %r15
jg 0x2f37
jmp 0x2f9d
callq 0x2080
movl (%rax), %eax
cmpl $0x4, %eax
je 0x2f9d
cmpl $0xb, %eax
jne 0x2fca
movl $0x1000, %edx           # imm = 0x1000
movl %ebx, %edi
movq %r14, %rsi
callq 0x2240
movq %rax, %r15
testq %rax, %rax
jne 0x2f30
jmp 0x2fca
movq %rsp, %rsi
movl %ebp, %edi
xorl %edx, %edx
callq 0x23b0
testl %eax, %eax
js 0x3023
movl (%rsp), %eax
testb $0x7f, %al
movzbl %ah, %ecx
movl $0x1, %eax
cmovel %ecx, %eax
addq $0x10b8, %rsp           # imm = 0x10B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x23f5(%rip), %rsi      # 0x53e8
movl $0x1, %edi
xorl %eax, %eax
callq 0x2310
leaq 0x23dd(%rip), %rsi      # 0x53e3
jmp 0x2ff3
leaq 0x23cf(%rip), %rsi      # 0x53de
jmp 0x2ff3
leaq 0x23ab(%rip), %rsi      # 0x53c3
jmp 0x2ff3
leaq 0x23b4(%rip), %rsi      # 0x53d5
jmp 0x2ff3
leaq 0x23a3(%rip), %rsi      # 0x53cd
jmp 0x2ff3