// File: /workspace/github_c_makefile_star2_O1/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 0x2fd4
movl %esi, %ebx
leaq 0x30(%rsp), %r14
movq %r14, %rdi
callq 0x22c0
movq %r14, %rdi
movl $0x11, %esi
callq 0x24b0
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 0x3034
movq %rsp, %rsi
movl %ebp, %edi
movl $0x1, %edx
callq 0x23c0
testl %eax, %eax
jle 0x2c8e
movl (%rsp), %eax
andl $0x7f, %eax
je 0x2c84
shll $0x18, %eax
addl $0x1000000, %eax        # imm = 0x1000000
cmpl $0x2000000, %eax        # imm = 0x2000000
jl 0x2c8e
movl $0x11, %edi
callq 0x2060
xorl %edi, %edi
callq 0x20c0
testl %eax, %eax
je 0x2caf
leaq 0x55e4(%rip), %rsi      # 0x8284
xorl %edi, %edi
callq 0x23d0
testl %eax, %eax
js 0x302b
leaq 0x390(%rip), %rdi       # 0x3046
callq 0x42d0
xorl %edi, %edi
callq 0x20c0
testl %eax, %eax
je 0x2cf9
leaq 0xb0(%rsp), %rsi
xorl %edi, %edi
callq 0x23d0
testl %eax, %eax
js 0x302b
leaq 0xb0(%rsp), %r14
movq %r14, %rdi
callq 0x2370
xorl %edi, %edi
xorl %esi, %esi
movq %r14, %rdx
callq 0x23e0
movl %ebp, 0xc(%rsp)
movl %ebx, %edi
callq 0x2040
movq %rax, %rdi
movl $0x2, %esi
xorl %eax, %eax
callq 0x23f0
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 0x23a0
testl %eax, %eax
jns 0x2d71
callq 0x2080
movl (%rax), %eax
cmpl $0x4, %eax
je 0x2d71
cmpl $0xb, %eax
jne 0x3022
testb $0x1, 0x16(%rsp)
je 0x2e06
movl $0x1000, %edx           # imm = 0x1000
movl %ebx, %edi
movq %r13, %rsi
callq 0x2250
movq %rax, %r15
testq %rax, %rax
jns 0x2da8
callq 0x2080
movl (%rax), %eax
cmpl $0x4, %eax
je 0x2da8
cmpl $0xb, %eax
jne 0x3019
testq %r15, %r15
jle 0x2e06
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 0x2dfe
callq 0x2080
movq %rax, %r14
movl (%r14), %eax
cmpl $0x4, %eax
je 0x2de9
cmpl $0xb, %eax
jne 0x3006
movl $0x1, %edi
movq %rbp, %rsi
movq %r15, %rdx
callq 0x20f0
testq %rax, %rax
js 0x2dd8
addq %rax, %r12
subq %rax, %r15
jg 0x2db0
testb $0x11, 0x1e(%rsp)
je 0x2e9f
movl $0x1000, %edx           # imm = 0x1000
xorl %edi, %edi
movq %r13, %rsi
callq 0x2250
movq %rax, %r15
testq %rax, %rax
je 0x2e40
jns 0x2e47
callq 0x2080
movl (%rax), %eax
cmpl $0x4, %eax
je 0x2e47
cmpl $0xb, %eax
je 0x2e47
jmp 0x3019
movw $0x0, 0x1c(%rsp)
testq %r15, %r15
jle 0x2e9f
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 0x2e97
callq 0x2080
movq %rax, %r14
movl (%r14), %eax
cmpl $0x4, %eax
je 0x2e85
cmpl $0xb, %eax
jne 0x3006
movl %ebx, %edi
movq %rbp, %rsi
movq %r15, %rdx
callq 0x20f0
testq %rax, %rax
js 0x2e74
addq %rax, %r12
subq %rax, %r15
jg 0x2e4f
testb $0x1, 0x26(%rsp)
je 0x2d44
movl $0x1000, %edx           # imm = 0x1000
movl 0x4(%rsp), %edi
movq %r13, %rsi
callq 0x2250
testq %rax, %rax
jns 0x2ed5
callq 0x2080
movl (%rax), %eax
cmpl $0x4, %eax
je 0x2ed5
cmpl $0xb, %eax
jne 0x3019
movl 0xc(%rsp), %edi
movq %rsp, %rsi
movl $0x1, %edx
callq 0x23c0
testl %eax, %eax
jle 0x2d44
movl (%rsp), %eax
andl $0x7f, %eax
je 0x2f09
shll $0x18, %eax
addl $0x1000000, %eax        # imm = 0x1000000
cmpl $0x2000000, %eax        # imm = 0x2000000
jl 0x2d44
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 0x2250
testq %rax, %rax
je 0x2fe4
movq %rax, %r15
leaq 0xb0(%rsp), %r14
testq %r15, %r15
jns 0x2f5d
callq 0x2080
movl (%rax), %eax
cmpl $0xb, %eax
je 0x2f5d
cmpl $0x4, %eax
jne 0x2fe4
testq %r15, %r15
jle 0x2fb7
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 0x2faf
callq 0x2080
movq %rax, %r13
movl (%r13), %eax
cmpl $0x4, %eax
je 0x2f9a
cmpl $0xb, %eax
jne 0x3006
movl $0x1, %edi
movq %r12, %rsi
movq %r15, %rdx
callq 0x20f0
testq %rax, %rax
js 0x2f8c
addq %rax, %rbp
subq %rax, %r15
jg 0x2f64
movl $0x1000, %edx           # imm = 0x1000
movl %ebx, %edi
movq %r14, %rsi
callq 0x2250
movq %rax, %r15
testq %rax, %rax
jne 0x2f43
jmp 0x2fe4
movq %rsp, %rsi
movl %ebp, %edi
xorl %edx, %edx
callq 0x23c0
testl %eax, %eax
js 0x303d
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 0x23db(%rip), %rsi      # 0x53e8
movl $0x1, %edi
xorl %eax, %eax
callq 0x2320
leaq 0x23c3(%rip), %rsi      # 0x53e3
jmp 0x300d
leaq 0x23b5(%rip), %rsi      # 0x53de
jmp 0x300d
leaq 0x2391(%rip), %rsi      # 0x53c3
jmp 0x300d
leaq 0x239a(%rip), %rsi      # 0x53d5
jmp 0x300d
leaq 0x2389(%rip), %rsi      # 0x53cd
jmp 0x300d