// File: /workspace/github_c_makefile_star2_O0/silverneko[P]miku/isolate/isolate.c
// Function: box_keeper
static void
box_keeper(void)
{
  read_errors_from_fd = error_pipes[0];
  close(error_pipes[1]);

  struct sigaction sa;
  bzero(&sa, sizeof(sa));
  sa.sa_handler = signal_int;
  sigaction(SIGINT, &sa, NULL);

  gettimeofday(&start_time, NULL);
  ticks_per_sec = sysconf(_SC_CLK_TCK);
  if (ticks_per_sec <= 0)
    die("Invalid ticks_per_sec!");

  if (timeout || wall_timeout)
    {
      sa.sa_handler = signal_alarm;
      sigaction(SIGALRM, &sa, NULL);
      alarm(1);
    }

  for(;;)
    {
      struct rusage rus;
      int stat;
      pid_t p;
      if (timer_tick)
	{
	  check_timeout();
	  timer_tick = 0;
	}
      p = wait4(box_pid, &stat, 0, &rus);
      if (p < 0)
	{
	  if (errno == EINTR)
	    continue;
	  die("wait4: %m");
	}
      if (p != box_pid)
	die("wait4: unknown pid %d exited!", p);
      box_pid = 0;

      // Check error pipe if there is an internal error passed from inside the box
      char interr[1024];
      int n = read(read_errors_from_fd, interr, sizeof(interr) - 1);
      if (n > 0)
	{
	  interr[n] = 0;
	  die("%s", interr);
	}

      if (WIFEXITED(stat))
	{
	  final_stats(&rus);
	  if (WEXITSTATUS(stat))
	    {
	      meta_printf("exitcode:%d\n", WEXITSTATUS(stat));
	      err("RE: Exited with error status %d", WEXITSTATUS(stat));
	    }
	  if (timeout && total_ms > timeout)
	    err("TO: Time limit exceeded");
	  if (wall_timeout && wall_ms > wall_timeout)
	    err("TO: Time limit exceeded (wall clock)");
	  flush_line();
	  fprintf(stderr, "OK (%d.%03d sec real, %d.%03d sec wall)\n",
	      total_ms/1000, total_ms%1000,
	      wall_ms/1000, wall_ms%1000);
	  box_exit(0);
	}
      else if (WIFSIGNALED(stat))
	{
	  meta_printf("exitsig:%d\n", WTERMSIG(stat));
	  final_stats(&rus);
	  err("SG: Caught fatal signal %d", WTERMSIG(stat));
	}
      else if (WIFSTOPPED(stat))
	{
	  meta_printf("exitsig:%d\n", WSTOPSIG(stat));
	  final_stats(&rus);
	  err("SG: Stopped by signal %d", WSTOPSIG(stat));
	}
      else
	die("wait4: unknown status %x, giving up!", stat);
    }
}

// Assembly:
subq $0x548, %rsp            # imm = 0x548
movl 0x5c93(%rip), %eax      # 0xa880
movl %eax, 0x5c95(%rip)      # 0xa888
movl 0x5c8b(%rip), %edi      # 0xa884
callq 0x2280
leaq 0x4b0(%rsp), %rdi
movl $0x98, %esi
callq 0x2420
leaq 0xf09(%rip), %rax       # 0x5b20
movq %rax, 0x4b0(%rsp)
movl $0x2, %edi
leaq 0x4b0(%rsp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x20d0
leaq 0x5824(%rip), %rdi      # 0xa460
xorl %eax, %eax
movl %eax, %esi
callq 0x2230
movl $0x2, %edi
callq 0x2430
movl %eax, 0x56ff(%rip)      # 0xa354
cmpl $0x0, 0x56f8(%rip)      # 0xa354
jg 0x4c6c
leaq 0x2bea(%rip), %rdi      # 0x784f
movb $0x0, %al
callq 0x2ed0
cmpl $0x0, 0x5685(%rip)      # 0xa2f8
jne 0x4c7e
cmpl $0x0, 0x5684(%rip)      # 0xa300
je 0x4cad
leaq 0xecb(%rip), %rax       # 0x5b50
movq %rax, 0x4b0(%rsp)
movl $0xe, %edi
leaq 0x4b0(%rsp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x20d0
movl $0x1, %edi
callq 0x2270
jmp 0x4caf
movl 0x5bd7(%rip), %eax      # 0xa88c
cmpl $0x0, %eax
je 0x4cc9
callq 0x5b70
movl $0x0, 0x5bc3(%rip)      # 0xa88c
movl 0x5671(%rip), %edi      # 0xa340
leaq 0x41c(%rsp), %rsi
xorl %edx, %edx
leaq 0x420(%rsp), %rcx
callq 0x23f0
movl %eax, 0x418(%rsp)
cmpl $0x0, 0x418(%rsp)
jge 0x4d11
callq 0x2060
cmpl $0x4, (%rax)
jne 0x4d03
jmp 0x4caf
leaq 0x2b5c(%rip), %rdi      # 0x7866
movb $0x0, %al
callq 0x2ed0
movl 0x418(%rsp), %eax
cmpl 0x5622(%rip), %eax      # 0xa340
je 0x4d35
movl 0x418(%rsp), %esi
leaq 0x2b42(%rip), %rdi      # 0x7870
movb $0x0, %al
callq 0x2ed0
movl $0x0, 0x5601(%rip)      # 0xa340
movl 0x5b43(%rip), %edi      # 0xa888
leaq 0x10(%rsp), %rsi
movl $0x3ff, %edx            # imm = 0x3FF
callq 0x22c0
movl %eax, 0xc(%rsp)
cmpl $0x0, 0xc(%rsp)
jle 0x4d7c
movslq 0xc(%rsp), %rax
movb $0x0, 0x10(%rsp,%rax)
leaq 0x10(%rsp), %rsi
leaq 0x2629(%rip), %rdi      # 0x739e
movb $0x0, %al
callq 0x2ed0
movl 0x41c(%rsp), %eax
andl $0x7f, %eax
cmpl $0x0, %eax
jne 0x4ea2
leaq 0x420(%rsp), %rdi
callq 0x37e0
movl 0x41c(%rsp), %eax
andl $0xff00, %eax           # imm = 0xFF00
sarl $0x8, %eax
cmpl $0x0, %eax
je 0x4dec
movl 0x41c(%rsp), %esi
andl $0xff00, %esi           # imm = 0xFF00
sarl $0x8, %esi
leaq 0x2ac7(%rip), %rdi      # 0x788e
movb $0x0, %al
callq 0x3620
movl 0x41c(%rsp), %esi
andl $0xff00, %esi           # imm = 0xFF00
sarl $0x8, %esi
leaq 0x2ab6(%rip), %rdi      # 0x789b
movb $0x0, %al
callq 0x5c30
cmpl $0x0, 0x5505(%rip)      # 0xa2f8
je 0x4e11
movl 0x554d(%rip), %eax      # 0xa348
cmpl 0x54f7(%rip), %eax      # 0xa2f8
jle 0x4e11
leaq 0x2ab1(%rip), %rdi      # 0x78bb
movb $0x0, %al
callq 0x5c30
cmpl $0x0, 0x54e8(%rip)      # 0xa300
je 0x4e36
movl 0x552c(%rip), %eax      # 0xa34c
cmpl 0x54da(%rip), %eax      # 0xa300
jle 0x4e36
leaq 0x2aa4(%rip), %rdi      # 0x78d3
movb $0x0, %al
callq 0x5c30
callq 0x35f0
movq 0x51b6(%rip), %rax      # 0x9ff8
movq (%rax), %rdi
movl 0x54fd(%rip), %eax      # 0xa348
movl $0x3e8, %ecx            # imm = 0x3E8
cltd
idivl %ecx
movl %eax, 0x8(%rsp)
movl 0x54eb(%rip), %eax      # 0xa348
movl $0x3e8, %ecx            # imm = 0x3E8
cltd
idivl %ecx
movl %edx, %ecx
movl 0x54df(%rip), %eax      # 0xa34c
movl $0x3e8, %esi            # imm = 0x3E8
cltd
idivl %esi
movl %eax, %r8d
movl 0x54ce(%rip), %eax      # 0xa34c
movl $0x3e8, %esi            # imm = 0x3E8
cltd
idivl %esi
movl %edx, %r9d
movl 0x8(%rsp), %edx
leaq 0x2a64(%rip), %rsi      # 0x78f8
movb $0x0, %al
callq 0x2300
xorl %edi, %edi
callq 0x36e0
movl 0x41c(%rsp), %eax
andl $0x7f, %eax
addl $0x1, %eax
movsbl %al, %eax
sarl %eax
cmpl $0x0, %eax
jle 0x4ef6
movl 0x41c(%rsp), %esi
andl $0x7f, %esi
leaq 0x2a57(%rip), %rdi      # 0x7921
movb $0x0, %al
callq 0x3620
leaq 0x420(%rsp), %rdi
callq 0x37e0
movl 0x41c(%rsp), %esi
andl $0x7f, %esi
leaq 0x2a3e(%rip), %rdi      # 0x792d
movb $0x0, %al
callq 0x5c30
movl 0x41c(%rsp), %eax
andl $0xff, %eax
cmpl $0x7f, %eax
jne 0x4f50
movl 0x41c(%rsp), %esi
andl $0xff00, %esi           # imm = 0xFF00
sarl $0x8, %esi
leaq 0x2a03(%rip), %rdi      # 0x7921
movb $0x0, %al
callq 0x3620
leaq 0x420(%rsp), %rdi
callq 0x37e0
movl 0x41c(%rsp), %esi
andl $0xff00, %esi           # imm = 0xFF00
sarl $0x8, %esi
leaq 0x29ff(%rip), %rdi      # 0x7948
movb $0x0, %al
callq 0x5c30
movl 0x41c(%rsp), %esi
leaq 0x2a03(%rip), %rdi      # 0x7961
movb $0x0, %al
callq 0x2ed0
nopw %cs:(%rax,%rax)