// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x10e0, %rsp           # imm = 0x10E0
movl %edi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
cmpl $0x0, -0xc(%rbp)
jge 0x2d5f
movl -0x8(%rbp), %edi
leaq -0x101c(%rbp), %rsi
xorl %edx, %edx
callq 0x23c0
cmpl $0x0, %eax
jge 0x2d20
movl $0x1, %edi
leaq 0x26dc(%rip), %rsi      # 0x53f5
movb $0x0, %al
callq 0x2320
movl -0x101c(%rbp), %eax
andl $0x7f, %eax
cmpl $0x0, %eax
jne 0x2d44
movl -0x101c(%rbp), %eax
andl $0xff00, %eax           # imm = 0xFF00
sarl $0x8, %eax
movl %eax, -0x10d4(%rbp)
jmp 0x2d51
movl $0x1, %eax
movl %eax, -0x10d4(%rbp)
jmp 0x2d51
movl -0x10d4(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x32b3
leaq -0x10a0(%rbp), %rdi
callq 0x22c0
leaq -0x10a0(%rbp), %rdi
movl $0x11, %esi
callq 0x24b0
xorl %edi, %edi
leaq -0x10a0(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x2050
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
leaq -0x10a0(%rbp), %rsi
xorl %edx, %edx
callq 0x2200
movl %eax, -0x1014(%rbp)
cmpl $0x0, %eax
jge 0x2dbf
movl $0x1, %edi
leaq 0x2645(%rip), %rsi      # 0x53fd
movb $0x0, %al
callq 0x2320
movl -0x8(%rbp), %edi
leaq -0x101c(%rbp), %rsi
movl $0x1, %edx
callq 0x23c0
cmpl $0x0, %eax
jle 0x2e08
movl -0x101c(%rbp), %eax
andl $0x7f, %eax
cmpl $0x0, %eax
je 0x2dfc
movl -0x101c(%rbp), %eax
andl $0x7f, %eax
addl $0x1, %eax
movsbl %al, %eax
sarl %eax
cmpl $0x0, %eax
jle 0x2e06
movl $0x11, %edi
callq 0x2060
jmp 0x2e08
callq 0x32c0
leaq 0x4ec(%rip), %rdi       # 0x3300
callq 0x4920
callq 0x3330
movl -0xc(%rbp), %edi
callq 0x2040
movq %rax, %rdi
movl $0x2, %esi
movb $0x0, %al
callq 0x23f0
movl %eax, -0x1018(%rbp)
movl -0xc(%rbp), %eax
movl %eax, -0x10d0(%rbp)
movw $0x1, -0x10cc(%rbp)
movl $0x0, -0x10c8(%rbp)
movw $0x1, -0x10c4(%rbp)
movl -0x1014(%rbp), %eax
movl %eax, -0x10c0(%rbp)
movw $0x1, -0x10bc(%rbp)
leaq -0x10d0(%rbp), %rdi
movl $0x3, %esi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x23a0
cmpl $0x0, %eax
jge 0x2eb9
callq 0x2080
cmpl $0xb, (%rax)
je 0x2eb7
callq 0x2080
cmpl $0x4, (%rax)
je 0x2eb7
movl $0x1, %edi
leaq 0x2556(%rip), %rsi      # 0x5406
movb $0x0, %al
callq 0x2320
jmp 0x2eb9
movswl -0x10ca(%rbp), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x2fbc
movl -0xc(%rbp), %edi
leaq -0x1010(%rbp), %rsi
movl $0x1000, %edx           # imm = 0x1000
callq 0x2250
movq %rax, -0x10b0(%rbp)
cmpq $0x0, %rax
jge 0x2f16
callq 0x2080
cmpl $0xb, (%rax)
je 0x2f14
callq 0x2080
cmpl $0x4, (%rax)
je 0x2f14
movl $0x1, %edi
leaq 0x24fe(%rip), %rsi      # 0x540b
movb $0x0, %al
callq 0x2320
jmp 0x2f16
movq $0x0, -0x10b8(%rbp)
cmpq $0x0, -0x10b0(%rbp)
jle 0x2fba
jmp 0x2f31
leaq -0x1010(%rbp), %rsi
addq -0x10b8(%rbp), %rsi
movq -0x10b0(%rbp), %rdx
movl $0x1, %edi
callq 0x20f0
movq %rax, -0x10a8(%rbp)
cmpq $0x0, %rax
jge 0x2f86
callq 0x2080
cmpl $0xb, (%rax)
je 0x2f84
callq 0x2080
cmpl $0x4, (%rax)
je 0x2f84
movl $0x1, %edi
leaq 0x2493(%rip), %rsi      # 0x5410
movb $0x0, %al
callq 0x2320
jmp 0x2f31
jmp 0x2f88
movq -0x10a8(%rbp), %rax
addq -0x10b8(%rbp), %rax
movq %rax, -0x10b8(%rbp)
movq -0x10a8(%rbp), %rcx
movq -0x10b0(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x10b0(%rbp)
jmp 0x2f21
jmp 0x2fbc
movswl -0x10c2(%rbp), %eax
andl $0x11, %eax
cmpl $0x0, %eax
je 0x30d1
leaq -0x1010(%rbp), %rsi
xorl %edi, %edi
movl $0x1000, %edx           # imm = 0x1000
callq 0x2250
movq %rax, -0x10b0(%rbp)
cmpq $0x0, %rax
jne 0x2ffa
movw $0x0, -0x10c4(%rbp)
jmp 0x302d
cmpq $0x0, -0x10b0(%rbp)
jge 0x302b
callq 0x2080
cmpl $0xb, (%rax)
je 0x302b
callq 0x2080
cmpl $0x4, (%rax)
je 0x302b
movl $0x1, %edi
leaq 0x23e7(%rip), %rsi      # 0x540b
movb $0x0, %al
callq 0x2320
jmp 0x302d
movq $0x0, -0x10b8(%rbp)
cmpq $0x0, -0x10b0(%rbp)
jle 0x30cf
jmp 0x3048
movl -0xc(%rbp), %edi
leaq -0x1010(%rbp), %rsi
addq -0x10b8(%rbp), %rsi
movq -0x10b0(%rbp), %rdx
callq 0x20f0
movq %rax, -0x10a8(%rbp)
cmpq $0x0, %rax
jge 0x309b
callq 0x2080
cmpl $0xb, (%rax)
je 0x3099
callq 0x2080
cmpl $0x4, (%rax)
je 0x3099
movl $0x1, %edi
leaq 0x237e(%rip), %rsi      # 0x5410
movb $0x0, %al
callq 0x2320
jmp 0x3048
jmp 0x309d
movq -0x10a8(%rbp), %rax
addq -0x10b8(%rbp), %rax
movq %rax, -0x10b8(%rbp)
movq -0x10a8(%rbp), %rcx
movq -0x10b0(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x10b0(%rbp)
jmp 0x3038
jmp 0x30d1
movswl -0x10ba(%rbp), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x316d
movl -0x1014(%rbp), %edi
leaq -0x1010(%rbp), %rsi
movl $0x1000, %edx           # imm = 0x1000
callq 0x2250
cmpq $0x0, %rax
jge 0x312a
callq 0x2080
cmpl $0xb, (%rax)
je 0x3128
callq 0x2080
cmpl $0x4, (%rax)
je 0x3128
movl $0x1, %edi
leaq 0x22ea(%rip), %rsi      # 0x540b
movb $0x0, %al
callq 0x2320
jmp 0x312a
movl -0x8(%rbp), %edi
leaq -0x101c(%rbp), %rsi
movl $0x1, %edx
callq 0x23c0
cmpl $0x0, %eax
jle 0x316b
movl -0x101c(%rbp), %eax
andl $0x7f, %eax
cmpl $0x0, %eax
je 0x3167
movl -0x101c(%rbp), %eax
andl $0x7f, %eax
addl $0x1, %eax
movsbl %al, %eax
sarl %eax
cmpl $0x0, %eax
jle 0x3169
jmp 0x3172
jmp 0x316b
jmp 0x316d
jmp 0x2e75
movl -0x1014(%rbp), %edi
callq 0x2220
movl -0x1018(%rbp), %edi
callq 0x2220
movl -0xc(%rbp), %edi
leaq -0x1010(%rbp), %rsi
movl $0x1000, %edx           # imm = 0x1000
callq 0x2250
movq %rax, -0x10b0(%rbp)
cmpq $0x0, %rax
je 0x3279
cmpq $0x0, -0x10b0(%rbp)
jge 0x31d0
callq 0x2080
cmpl $0xb, (%rax)
je 0x31d0
callq 0x2080
cmpl $0x4, (%rax)
je 0x31d0
jmp 0x3279
movq $0x0, -0x10b8(%rbp)
cmpq $0x0, -0x10b0(%rbp)
jle 0x3274
jmp 0x31eb
leaq -0x1010(%rbp), %rsi
addq -0x10b8(%rbp), %rsi
movq -0x10b0(%rbp), %rdx
movl $0x1, %edi
callq 0x20f0
movq %rax, -0x10a8(%rbp)
cmpq $0x0, %rax
jge 0x3240
callq 0x2080
cmpl $0xb, (%rax)
je 0x323e
callq 0x2080
cmpl $0x4, (%rax)
je 0x323e
movl $0x1, %edi
leaq 0x21d9(%rip), %rsi      # 0x5410
movb $0x0, %al
callq 0x2320
jmp 0x31eb
jmp 0x3242
movq -0x10a8(%rbp), %rax
addq -0x10b8(%rbp), %rax
movq %rax, -0x10b8(%rbp)
movq -0x10a8(%rbp), %rcx
movq -0x10b0(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x10b0(%rbp)
jmp 0x31db
jmp 0x3188
movl -0x101c(%rbp), %eax
andl $0x7f, %eax
cmpl $0x0, %eax
jne 0x329d
movl -0x101c(%rbp), %eax
andl $0xff00, %eax           # imm = 0xFF00
sarl $0x8, %eax
movl %eax, -0x10d8(%rbp)
jmp 0x32aa
movl $0x1, %eax
movl %eax, -0x10d8(%rbp)
jmp 0x32aa
movl -0x10d8(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x10e0, %rsp           # imm = 0x10E0
popq %rbp
retq
nop