// File: /workspace/github_c_makefile_star2_O1/silverneko[P]miku/./src/testsuite.cpp
// Function: testsuite(submission&)
int testsuite(submission &sub)
{
   system("rm -f ./testzone/*");
   const int testBoxid = BOXOFFSET + 0, spBoxid = BOXOFFSET + 1;
   sandboxInit(testBoxid);
   sandboxInit(spBoxid);
   int status = compile(sub, testBoxid, spBoxid);
   if(status != OK) return status;
   
   //anyway, only have batch judge right now
   map<pid_t, int> proc;
   int procnum = 0;
   int problem_id = sub.problem_id;
   int* time_limit = sub.time_limit;
   int* mem_limit = sub.mem_limit;

   for(int i = 0; i < sub.testdata_count; ++i){
      while(procnum >= MAXPARNUM){
         int status;
         pid_t cid = waitpid(-1, &status, 0);
         if(cid == -1){
            perror("[ERROR] in testsuite,  `waitpid()` failed :");
            return ER;
         }
         int td = proc[cid];
         eval(sub, td, BOXOFFSET + 10 + td, spBoxid);
         if(AGGUPDATE){
            sendResult(sub, OK, false);
         }
         //cerr << "td" << td << " : " << sub.verdict[td] << endl;
         sandboxDele(BOXOFFSET + 10 + td);
         --procnum;
      }

      if(procnum < MAXPARNUM){
         //batch judge
         ostringstream command;
         command << "batchjudge " << problem_id;
         command << " " << i;
         command << " " << BOXOFFSET + 10 + i;
         command << " " << time_limit[i];
         if(sub.lang == "haskell")
           command << " " << mem_limit[i]*5 + 24*1024;
         else
           command << " " << mem_limit[i];
         command << " " << testBoxid;
         pid_t pid = fork();
         if(pid == -1){
            perror("[ERROR] in testsuite, `fork()` failed :");
            return ER;
         }
         if(pid == 0){
            //child proc
            execl("/bin/sh", "sh", "-c", command.str().c_str(), NULL);
            perror("[ERROR] in testsuite, `execl()` failed :");
            exit(0);
         }
         proc[pid] = i;
         ++procnum;
      }
   }
   while(procnum >= 1){
      int status;
      pid_t cid = waitpid(-1, &status, 0);
      if(cid == -1){
         perror("[ERROR] in testsuite,  `waitpid()` failed :");
         return ER;
      }
      const int td = proc[cid];
      //sub.verdict[td] = eval(problem_id, td);
      eval(sub, td, BOXOFFSET + 10 + td, spBoxid);
      if(AGGUPDATE){
         sendResult(sub, OK, false);
      }
      //cerr << "td" << td << " : " << sub.verdict[td] << endl;
      sandboxDele(BOXOFFSET + 10 + td);
      --procnum;
   }
   //clear box-10
   sandboxDele(testBoxid);
   sandboxDele(spBoxid);
   return OK;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1f8, %rsp            # imm = 0x1F8
movq %rdi, %r14
leaq 0x4b41(%rip), %rdi      # 0x93b8
callq 0x3240
movl 0x8a39(%rip), %r15d     # 0xd2bc
leal 0x1(%r15), %ebx
movl %r15d, %edi
callq 0x457a
movl %ebx, %edi
callq 0x457a
movq %r14, %rdi
movl %r15d, %esi
movl %ebx, %edx
callq 0x4c92
movl %eax, %ebp
testl %eax, %eax
jne 0x4c19
movq %r15, 0x20(%rsp)
leaq 0x58(%rsp), %rax
movl $0x0, (%rax)
xorl %r12d, %r12d
movq %r12, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
movq %r12, 0x20(%rax)
cmpl $0x0, 0xf0(%r14)
setg 0xf(%rsp)
jle 0x4b53
movl (%r14), %eax
movl %eax, 0x18(%rsp)
leaq 0x68(%r14), %rax
movq %rax, 0x28(%rsp)
movq $0x0, 0x10(%rsp)
leaq 0x80(%rsp), %rbp
xorl %r12d, %r12d
cmpl 0x89aa(%rip), %r12d     # 0xd2b8
jl 0x49a2
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
movq %rbp, %rsi
xorl %edx, %edx
callq 0x3200
movl %eax, %r15d
movl %eax, 0x30(%rsp)
cmpl $-0x1, %eax
je 0x4994
leaq 0x50(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x6ebc
movl (%rax), %r13d
movl 0x8975(%rip), %eax      # 0xd2bc
leal (%rax,%r13), %edx
addl $0xa, %edx
movq %r14, %rdi
movl %r13d, %esi
movl %ebx, %ecx
callq 0x56bd
cmpb $0x1, 0x8962(%rip)      # 0xd2c4
jne 0x4970
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x8c41
movl 0x8946(%rip), %eax      # 0xd2bc
leal (%rax,%r13), %edi
addl $0xa, %edi
callq 0x46ea
decl %r12d
cmpl $-0x1, %r15d
jne 0x4907
jmp 0x4b53
leaq 0x4a30(%rip), %rdi      # 0x93cb
callq 0x3220
jmp 0x4985
movq %rbp, %rdi
callq 0x3350
movl $0xb, %edx
movq %rbp, %rdi
leaq 0x4a3e(%rip), %rsi      # 0x93f7
callq 0x32f0
movq %rbp, %rdi
movl 0x18(%rsp), %esi
callq 0x34d0
movl $0x1, %edx
movq %rbp, %rdi
leaq 0x495d(%rip), %rsi      # 0x9336
callq 0x32f0
movq %rbp, %rdi
movq 0x10(%rsp), %rsi
callq 0x34d0
movl $0x1, %edx
movq %rbp, %rdi
leaq 0x493c(%rip), %rsi      # 0x9336
callq 0x32f0
movl 0x88b7(%rip), %eax      # 0xd2bc
movq 0x10(%rsp), %rcx
leal (%rcx,%rax), %esi
addl $0xa, %esi
movq %rbp, %rdi
callq 0x34d0
movl $0x1, %edx
movq %rbp, %rdi
leaq 0x490f(%rip), %rsi      # 0x9336
callq 0x32f0
movq 0x10(%rsp), %rax
movl 0x414(%r14,%rax,4), %esi
movq %rbp, %rdi
callq 0x34d0
movq 0x28(%rsp), %rdi
leaq 0x49b6(%rip), %rsi      # 0x9403
callq 0x30d0
testl %eax, %eax
je 0x4a79
movl $0x1, %edx
movq %rbp, %rdi
leaq 0x48d1(%rip), %rsi      # 0x9336
callq 0x32f0
movq 0x10(%rsp), %rax
movl 0xf4(%r14,%rax,4), %esi
jmp 0x4aa3
movl $0x1, %edx
movq %rbp, %rdi
leaq 0x48ae(%rip), %rsi      # 0x9336
callq 0x32f0
movq 0x10(%rsp), %rax
movl 0xf4(%r14,%rax,4), %eax
leal (%rax,%rax,4), %esi
addl $0x6000, %esi           # imm = 0x6000
movq %rbp, %rdi
callq 0x34d0
movl $0x1, %edx
movq %rbp, %rdi
leaq 0x487c(%rip), %rsi      # 0x9336
callq 0x32f0
movq %rbp, %rdi
movq 0x20(%rsp), %rsi
callq 0x34d0
callq 0x30b0
movl %eax, %r15d
movl %eax, 0x1c(%rsp)
cmpl $-0x1, %eax
je 0x4b45
testl %r15d, %r15d
je 0x4c2d
leaq 0x50(%rsp), %rdi
leaq 0x1c(%rsp), %rsi
callq 0x6ebc
movq 0x10(%rsp), %rcx
movl %ecx, (%rax)
incl %r12d
movq %rbp, %rdi
movq 0x84af(%rip), %rsi      # 0xcfb8
callq 0x3170
leaq 0xf0(%rsp), %rdi
callq 0x3100
cmpl $-0x1, %r15d
je 0x4b53
movq 0x10(%rsp), %rdx
incq %rdx
movslq 0xf0(%r14), %rax
movq %rdx, 0x10(%rsp)
cmpq %rax, %rdx
setl 0xf(%rsp)
jl 0x4907
jmp 0x4b53
leaq 0x48bf(%rip), %rdi      # 0x940b
callq 0x3220
jmp 0x4aff
movl $0x13, %ebp
testb $0x1, 0xf(%rsp)
jne 0x4c0f
testl %r12d, %r12d
jle 0x4bfc
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
leaq 0x80(%rsp), %rsi
xorl %edx, %edx
callq 0x3200
movl %eax, %r13d
movl %eax, 0x30(%rsp)
cmpl $-0x1, %eax
je 0x4bee
leaq 0x50(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x6ebc
movl (%rax), %r15d
movl 0x8718(%rip), %eax      # 0xd2bc
leal (%r15,%rax), %edx
addl $0xa, %edx
movq %r14, %rdi
movl %r15d, %esi
movl %ebx, %ecx
callq 0x56bd
cmpb $0x1, 0x8705(%rip)      # 0xd2c4
jne 0x4bcd
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x8c41
movl 0x86e9(%rip), %eax      # 0xd2bc
leal (%r15,%rax), %edi
addl $0xa, %edi
callq 0x46ea
decl %r12d
cmpl $-0x1, %r13d
jne 0x4b63
jmp 0x4c0f
leaq 0x47d6(%rip), %rdi      # 0x93cb
callq 0x3220
jmp 0x4be2
movq 0x20(%rsp), %rdi
callq 0x46ea
xorl %ebp, %ebp
movl %ebx, %edi
callq 0x46ea
leaq 0x50(%rsp), %rdi
callq 0x717e
movl %ebp, %eax
addq $0x1f8, %rsp            # imm = 0x1F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x30(%rsp), %rdi
leaq 0x80(%rsp), %rsi
callq 0x32a0
leaq 0x30(%rsp), %rdi
callq 0x3550
jmp 0x4c51
jmp 0x4c7d
jmp 0x4c51
jmp 0x4c7d
movq %rax, %rbx
movq 0x835d(%rip), %rsi      # 0xcfb8
leaq 0x80(%rsp), %rdi
callq 0x3170
leaq 0xf0(%rsp), %rdi
callq 0x3100
jmp 0x4c80
jmp 0x4c7d
jmp 0x4c7d
jmp 0x4c7d
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0x717e
movq %rbx, %rdi
callq 0x34e0