// File: /workspace/github_c_makefile_star2_O3/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 $0x1e8, %rsp            # imm = 0x1E8
movq %rdi, %r14
leaq 0x4c05(%rip), %rdi      # 0x93c8
callq 0x3240
movl 0x8aed(%rip), %r12d     # 0xd2bc
leal 0x1(%r12), %ebx
movl %r12d, %edi
callq 0x44c6
movl %ebx, %edi
callq 0x44c6
movq %r14, %rdi
movl %r12d, %esi
movl %ebx, %edx
callq 0x4ba4
movl %eax, %r15d
testl %eax, %eax
jne 0x4b22
movq %r12, 0x10(%rsp)
leaq 0x48(%rsp), %rax
movl $0x0, (%rax)
xorl %ebp, %ebp
movq %rbp, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
movq %rbp, 0x20(%rax)
cmpl $0x0, 0xf0(%r14)
jle 0x4b04
movl (%r14), %eax
movl %eax, 0x8(%rsp)
leaq 0x68(%r14), %rax
movq %rax, 0x18(%rsp)
leaq 0x70(%rsp), %r12
leaq 0x4aef(%rip), %r13      # 0x9336
movq $0x0, (%rsp)
cmpl 0x8a63(%rip), %ebp      # 0xd2b8
jge 0x485c
movl %ebp, %r15d
jmp 0x48d3
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
movq %r12, %rsi
xorl %edx, %edx
callq 0x3200
movl %eax, 0x20(%rsp)
cmpl $-0x1, %eax
je 0x4a73
leaq 0x40(%rsp), %rdi
leaq 0x20(%rsp), %rsi
callq 0x6dd8
movl (%rax), %esi
leal 0xa(%rsi), %r15d
movl 0x8a29(%rip), %edx      # 0xd2bc
addl %r15d, %edx
movq %r14, %rdi
movl %ebx, %ecx
callq 0x55c4
cmpb $0x1, 0x8a1d(%rip)      # 0xd2c4
jne 0x48b5
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x8b54
addl 0x8a00(%rip), %r15d     # 0xd2bc
movl %r15d, %edi
callq 0x4636
leal -0x1(%rbp), %r15d
cmpl 0x89ea(%rip), %ebp      # 0xd2b8
movl %r15d, %ebp
jg 0x485c
movq %r12, %rdi
callq 0x3350
movl $0xb, %edx
movq %r12, %rdi
leaq 0x4b1d(%rip), %rsi      # 0x9407
callq 0x32f0
movq %r12, %rdi
movl 0x8(%rsp), %esi
callq 0x34d0
movl $0x1, %edx
movq %r12, %rdi
movq %r13, %rsi
callq 0x32f0
movq %r12, %rdi
movq (%rsp), %rsi
callq 0x34d0
movl $0x1, %edx
movq %r12, %rdi
movq %r13, %rsi
callq 0x32f0
movl 0x898f(%rip), %eax      # 0xd2bc
movq (%rsp), %rcx
leal (%rcx,%rax), %esi
addl $0xa, %esi
movq %r12, %rdi
callq 0x34d0
movl $0x1, %edx
movq %r12, %rdi
movq %r13, %rsi
callq 0x32f0
movq (%rsp), %rax
movl 0x414(%r14,%rax,4), %esi
movq %r12, %rdi
callq 0x34d0
movq 0x18(%rsp), %rdi
leaq 0x4aa4(%rip), %rsi      # 0x9413
callq 0x30d0
testl %eax, %eax
je 0x4996
movl $0x1, %edx
movq %r12, %rdi
movq %r13, %rsi
callq 0x32f0
movq (%rsp), %rax
movl 0xf4(%r14,%rax,4), %esi
jmp 0x49bb
movl $0x1, %edx
movq %r12, %rdi
movq %r13, %rsi
callq 0x32f0
movq (%rsp), %rax
movl 0xf4(%r14,%rax,4), %eax
leal (%rax,%rax,4), %esi
addl $0x6000, %esi           # imm = 0x6000
movq %r12, %rdi
callq 0x34d0
movl $0x1, %edx
movq %r12, %rdi
movq %r13, %rsi
callq 0x32f0
movq %r12, %rdi
movq 0x10(%rsp), %rsi
callq 0x34d0
callq 0x30b0
movl %eax, %r13d
movl %eax, 0xc(%rsp)
cmpl $-0x1, %eax
je 0x4a65
testl %r13d, %r13d
je 0x4b45
leaq 0x40(%rsp), %rdi
leaq 0xc(%rsp), %rsi
callq 0x6dd8
movq (%rsp), %rcx
movl %ecx, (%rax)
incl %r15d
movl %r15d, %ebp
movq %r12, %rdi
movq 0x8599(%rip), %rsi      # 0xcfb8
callq 0x3170
leaq 0xe0(%rsp), %rdi
callq 0x3100
movl $0x13, %r15d
cmpl $-0x1, %r13d
je 0x4b18
movq (%rsp), %rdx
incq %rdx
movslq 0xf0(%r14), %rax
movq %rdx, (%rsp)
cmpq %rax, %rdx
leaq 0x48d9(%rip), %r13      # 0x9336
jl 0x484f
jmp 0x4a8a
leaq 0x49af(%rip), %rdi      # 0x941b
callq 0x3220
jmp 0x4a12
leaq 0x4961(%rip), %rdi      # 0x93db
callq 0x3220
movl $0x13, %r15d
jmp 0x4b18
testl %ebp, %ebp
jle 0x4b04
incl %ebp
leaq 0x20(%rsp), %r13
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
leaq 0x70(%rsp), %rsi
xorl %edx, %edx
callq 0x3200
movl %eax, 0x20(%rsp)
cmpl $-0x1, %eax
je 0x4b37
leaq 0x40(%rsp), %rdi
movq %r13, %rsi
callq 0x6dd8
movl (%rax), %esi
leal 0xa(%rsi), %r12d
movl 0x87f0(%rip), %edx      # 0xd2bc
addl %r12d, %edx
movq %r14, %rdi
movl %ebx, %ecx
callq 0x55c4
cmpb $0x1, 0x87e4(%rip)      # 0xd2c4
jne 0x4aee
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x8b54
addl 0x87c7(%rip), %r12d     # 0xd2bc
movl %r12d, %edi
callq 0x4636
decl %ebp
cmpl $0x1, %ebp
jg 0x4a95
movq 0x10(%rsp), %rdi
callq 0x4636
xorl %r15d, %r15d
movl %ebx, %edi
callq 0x4636
leaq 0x40(%rsp), %rdi
callq 0x709c
movl %r15d, %eax
addq $0x1e8, %rsp            # imm = 0x1E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x489d(%rip), %rdi      # 0x93db
callq 0x3220
jmp 0x4b18
leaq 0x20(%rsp), %rdi
leaq 0x70(%rsp), %rsi
callq 0x32a0
leaq 0x20(%rsp), %rdi
callq 0x3550
jmp 0x4b8f
jmp 0x4b6a
jmp 0x4b8f
jmp 0x4b6a
jmp 0x4b8f
jmp 0x4b8f
movq %rax, %rbx
movq 0x8444(%rip), %rsi      # 0xcfb8
leaq 0x70(%rsp), %rdi
callq 0x3170
leaq 0xe0(%rsp), %rdi
callq 0x3100
jmp 0x4b92
jmp 0x4b8f
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x709c
movq %rbx, %rdi
callq 0x34e0