// File: /workspace/github_c_makefile_star2_O2/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 0x2e80
movl %esi, %ebx
leaq 0x30(%rsp), %r14
movq %r14, %rdi
callq 0x22b0
pushq $0x11
popq %rsi
movq %r14, %rdi
callq 0x24a0
xorl %edi, %edi
movq %r14, %rsi
xorl %edx, %edx
callq 0x2050
pushq $-0x1
popq %rdi
movq %r14, %rsi
xorl %edx, %edx
callq 0x21f0
movl %eax, 0x4(%rsp)
testl %eax, %eax
js 0x2edc
movq %rsp, %rsi
pushq $0x1
popq %rdx
movl %ebp, %edi
callq 0x23b0
testl %eax, %eax
jle 0x2bad
movl (%rsp), %eax
andl $0x7f, %eax
je 0x2ba5
shll $0x18, %eax
addl $0x1000000, %eax        # imm = 0x1000000
cmpl $0x2000000, %eax        # imm = 0x2000000
jl 0x2bad
pushq $0x11
popq %rdi
callq 0x2060
xorl %edi, %edi
callq 0x20c0
testl %eax, %eax
je 0x2bce
leaq 0x46bd(%rip), %rsi      # 0x727c
xorl %edi, %edi
callq 0x23c0
testl %eax, %eax
js 0x2ed3
leaq 0x319(%rip), %rdi       # 0x2eee
callq 0x3fc0
xorl %edi, %edi
callq 0x20c0
testl %eax, %eax
je 0x2c18
leaq 0xb0(%rsp), %rsi
xorl %edi, %edi
callq 0x23c0
testl %eax, %eax
js 0x2ed3
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
pushq $0x2
popq %rsi
movq %rax, %rdi
xorl %eax, %eax
callq 0x23e0
movl %eax, 0x8(%rsp)
leaq 0x10(%rsp), %rdx
movl %ebx, (%rdx)
movw $0x1, %ax
movw %ax, 0x4(%rdx)
andl $0x0, 0x8(%rdx)
movw %ax, 0xc(%rdx)
movl 0x4(%rsp), %ecx
movl %ecx, 0x10(%rdx)
movw %ax, 0x14(%rdx)
leaq 0xb0(%rsp), %rbp
pushq $0x1
popq %r14
leaq 0x10(%rsp), %rdi
pushq $0x3
popq %rsi
pushq $-0x1
popq %rdx
callq 0x2390
testl %eax, %eax
jns 0x2c8b
callq 0x2080
movl (%rax), %eax
cmpl $0x4, %eax
je 0x2c8b
cmpl $0xb, %eax
jne 0x2eca
testb $0x1, 0x16(%rsp)
jne 0x2cd8
testb $0x11, 0x1e(%rsp)
je 0x2d91
movl $0x1000, %edx           # imm = 0x1000
xorl %edi, %edi
movq %rbp, %rsi
callq 0x2240
movq %rax, %r13
testq %rax, %rax
je 0x2d48
jns 0x2d4e
callq 0x2080
movl (%rax), %eax
cmpl $0x4, %eax
je 0x2d4e
cmpl $0xb, %eax
je 0x2d4e
jmp 0x2ec1
movl $0x1000, %edx           # imm = 0x1000
movl %ebx, %edi
movq %rbp, %rsi
callq 0x2240
movq %rax, %r13
testq %rax, %rax
jns 0x2d04
callq 0x2080
movl (%rax), %eax
cmpl $0x4, %eax
je 0x2d04
cmpl $0xb, %eax
jne 0x2ec1
xorl %r12d, %r12d
testq %r13, %r13
jle 0x2c92
leaq (%rsp,%r12), %r15
addq $0xb0, %r15
movl %r14d, %edi
movq %r15, %rsi
movq %r13, %rdx
callq 0x20f0
testq %rax, %rax
jns 0x2d40
callq 0x2080
movl (%rax), %eax
cmpl $0x4, %eax
je 0x2d17
cmpl $0xb, %eax
je 0x2d17
jmp 0x2eb0
addq %rax, %r12
subq %rax, %r13
jmp 0x2d07
andw $0x0, 0x1c(%rsp)
xorl %r12d, %r12d
testq %r13, %r13
jle 0x2d91
leaq (%rsp,%r12), %r15
addq $0xb0, %r15
movl %ebx, %edi
movq %r15, %rsi
movq %r13, %rdx
callq 0x20f0
testq %rax, %rax
jns 0x2d89
callq 0x2080
movl (%rax), %eax
cmpl $0x4, %eax
je 0x2d61
cmpl $0xb, %eax
je 0x2d61
jmp 0x2eb0
addq %rax, %r12
subq %rax, %r13
jmp 0x2d51
testb $0x1, 0x26(%rsp)
je 0x2c62
movl $0x1000, %edx           # imm = 0x1000
movl 0x4(%rsp), %edi
movq %rbp, %rsi
callq 0x2240
testq %rax, %rax
jns 0x2dc7
callq 0x2080
movl (%rax), %eax
cmpl $0x4, %eax
je 0x2dc7
cmpl $0xb, %eax
jne 0x2ec1
movl 0xc(%rsp), %edi
movq %rsp, %rsi
movl %r14d, %edx
callq 0x23b0
testl %eax, %eax
jle 0x2c62
movl (%rsp), %eax
andl $0x7f, %eax
je 0x2df9
shll $0x18, %eax
addl $0x1000000, %eax        # imm = 0x1000000
cmpl $0x2000000, %eax        # imm = 0x2000000
jl 0x2c62
movl 0x4(%rsp), %edi
callq 0x2210
movl 0x8(%rsp), %edi
callq 0x2210
leaq 0xb0(%rsp), %r14
pushq $0x1
popq %rbp
movl $0x1000, %edx           # imm = 0x1000
movl %ebx, %edi
movq %r14, %rsi
callq 0x2240
testq %rax, %rax
je 0x2e90
movq %rax, %r15
jns 0x2e40
callq 0x2080
movl (%rax), %eax
cmpl $0xb, %eax
je 0x2e40
cmpl $0x4, %eax
jne 0x2e90
xorl %r13d, %r13d
testq %r15, %r15
jle 0x2e16
leaq (%rsp,%r13), %r12
addq $0xb0, %r12
movl %ebp, %edi
movq %r12, %rsi
movq %r15, %rdx
callq 0x20f0
testq %rax, %rax
jns 0x2e78
callq 0x2080
movl (%rax), %eax
cmpl $0x4, %eax
je 0x2e53
cmpl $0xb, %eax
je 0x2e53
jmp 0x2eb0
addq %rax, %r13
subq %rax, %r15
jmp 0x2e43
movq %rsp, %rsi
movl %ebp, %edi
xorl %edx, %edx
callq 0x23b0
testl %eax, %eax
js 0x2ee5
pushq $0x1
popq %rcx
movl (%rsp), %eax
testb $0x7f, %al
movzbl %ah, %eax
cmovnel %ecx, %eax
addq $0x10b8, %rsp           # imm = 0x10B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1531(%rip), %rsi      # 0x43e8
pushq $0x1
popq %rdi
xorl %eax, %eax
callq 0x2310
leaq 0x151b(%rip), %rsi      # 0x43e3
jmp 0x2eb7
leaq 0x150d(%rip), %rsi      # 0x43de
jmp 0x2eb7
leaq 0x14e9(%rip), %rsi      # 0x43c3
jmp 0x2eb7
leaq 0x14f2(%rip), %rsi      # 0x43d5
jmp 0x2eb7
leaq 0x14e1(%rip), %rsi      # 0x43cd
jmp 0x2eb7