// File: /workspace/github_c_makefile_star2_O2/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 0x4139(%rip), %rdi      # 0x83c8
callq 0x3260
movl 0x8029(%rip), %r15d     # 0xc2c4
leal 0x1(%r15), %ebx
movl %r15d, %edi
callq 0x40b2
movl %ebx, %edi
callq 0x40b2
movq %r14, %rdi
movl %r15d, %esi
movl %ebx, %edx
callq 0x4643
testl %eax, %eax
jne 0x45a8
movq %r15, 0x18(%rsp)
leaq 0x58(%rsp), %rax
andl $0x0, (%rax)
andq $0x0, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
andq $0x0, 0x20(%rax)
movl (%r14), %eax
movl %eax, 0x10(%rsp)
leaq 0x68(%r14), %rax
movq %rax, 0x28(%rsp)
xorl %eax, %eax
leaq 0x80(%rsp), %r12
xorl %r15d, %r15d
movq %rax, %rcx
movslq 0xf0(%r14), %rdx
pushq $0x13
popq %rax
movl %eax, 0xc(%rsp)
movq %rcx, (%rsp)
cmpq %rdx, %rcx
jge 0x4500
movq %rdx, 0x20(%rsp)
leaq 0x50(%rsp), %rbp
cmpl 0x7f92(%rip), %r15d     # 0xc2c0
jl 0x4399
pushq $-0x1
popq %rdi
movq %r12, %rsi
xorl %edx, %edx
callq 0x3230
movl %eax, 0x30(%rsp)
cmpl $-0x1, %eax
je 0x4573
movq %rbp, %rdi
leaq 0x30(%rsp), %rsi
callq 0x5b26
movl (%rax), %esi
leal 0xa(%rsi), %r13d
movl 0x7f61(%rip), %edx      # 0xc2c4
addl %r13d, %edx
movq %r14, %rdi
movl %ebx, %ecx
callq 0x4cd4
cmpb $0x1, 0x7f55(%rip)      # 0xc2cc
jne 0x4385
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x72d6
addl 0x7f38(%rip), %r13d     # 0xc2c4
movl %r13d, %edi
callq 0x4193
decl %r15d
jmp 0x4327
movq %r12, %rdi
callq 0x3360
movq %r12, %rdi
leaq 0x405c(%rip), %rsi      # 0x8407
callq 0x3290
movq %rax, %rdi
movl 0x10(%rsp), %esi
callq 0x34c0
movq %r12, %rdi
leaq 0x3f70(%rip), %rsi      # 0x8336
callq 0x3290
movq %rax, %rdi
movq (%rsp), %rsi
callq 0x34c0
movq %r12, %rdi
leaq 0x3f55(%rip), %rsi      # 0x8336
callq 0x3290
movl 0x7ed8(%rip), %ecx      # 0xc2c4
movq (%rsp), %rdx
leal (%rdx,%rcx), %esi
addl $0xa, %esi
movq %rax, %rdi
callq 0x34c0
movq %r12, %rdi
leaq 0x3f2e(%rip), %rsi      # 0x8336
callq 0x3290
movq (%rsp), %rcx
movl 0x414(%r14,%rcx,4), %esi
movq %rax, %rdi
callq 0x34c0
movq 0x28(%rsp), %rdi
leaq 0x3fe6(%rip), %rsi      # 0x8413
callq 0x5b74
testb %al, %al
je 0x445c
movq %r12, %rdi
leaq 0x3ef6(%rip), %rsi      # 0x8336
callq 0x3290
movq (%rsp), %rcx
movl 0xf4(%r14,%rcx,4), %ecx
leal (%rcx,%rcx,4), %esi
addl $0x6000, %esi           # imm = 0x6000
jmp 0x4477
movq %r12, %rdi
leaq 0x3ed0(%rip), %rsi      # 0x8336
callq 0x3290
movq (%rsp), %rcx
movl 0xf4(%r14,%rcx,4), %esi
movq %rax, %rdi
callq 0x34c0
movq %r12, %rdi
leaq 0x3ead(%rip), %rsi      # 0x8336
callq 0x3290
movq %rax, %rdi
movq 0x18(%rsp), %rsi
callq 0x34c0
callq 0x30e0
movl %eax, %r13d
movl %eax, 0x14(%rsp)
cmpl $-0x1, %eax
je 0x44e3
testl %r13d, %r13d
je 0x45ba
movq %rbp, %rdi
leaq 0x14(%rsp), %rsi
callq 0x5b26
movq (%rsp), %rbp
movl %ebp, (%rax)
incl %r15d
movq %r12, %rdi
callq 0x30b0
leaq 0x1(%rbp), %rax
cmpl $-0x1, %r13d
jne 0x42ff
jmp 0x44f5
leaq 0x3f31(%rip), %rdi      # 0x841b
callq 0x3250
movq (%rsp), %rbp
jmp 0x44cb
cmpq 0x20(%rsp), %rbp
jl 0x459a
leaq 0x50(%rsp), %r12
leaq 0x30(%rsp), %r13
testl %r15d, %r15d
jle 0x4581
pushq $-0x1
popq %rdi
leaq 0x80(%rsp), %rsi
xorl %edx, %edx
callq 0x3230
movl %eax, 0x30(%rsp)
cmpl $-0x1, %eax
je 0x4573
movq %r12, %rdi
movq %r13, %rsi
callq 0x5b26
movl (%rax), %esi
leal 0xa(%rsi), %ebp
movl 0x7d84(%rip), %edx      # 0xc2c4
addl %ebp, %edx
movq %r14, %rdi
movl %ebx, %ecx
callq 0x4cd4
cmpb $0x1, 0x7d79(%rip)      # 0xc2cc
jne 0x4561
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x72d6
addl 0x7d5d(%rip), %ebp      # 0xc2c4
movl %ebp, %edi
callq 0x4193
decl %r15d
jmp 0x450a
leaq 0x3e61(%rip), %rdi      # 0x83db
callq 0x3250
jmp 0x459a
movq 0x18(%rsp), %rdi
callq 0x4193
movl $0x0, 0xc(%rsp)
movl %ebx, %edi
callq 0x4193
leaq 0x50(%rsp), %rdi
callq 0x5d96
movl 0xc(%rsp), %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 0x32c0
leaq 0x30(%rsp), %rbx
movq (%rbx), %rcx
leaq 0x3e68(%rip), %rdi      # 0x8443
leaq 0x3e66(%rip), %rsi      # 0x8448
leaq 0x3e62(%rip), %rdx      # 0x844b
xorl %r8d, %r8d
xorl %eax, %eax
callq 0x3320
movq %rbx, %rdi
callq 0x3170
leaq 0x3e4c(%rip), %rdi      # 0x844e
callq 0x3250
xorl %edi, %edi
callq 0x3300
jmp 0x462e
jmp 0x461a
jmp 0x461a
jmp 0x462e
jmp 0x462e
jmp 0x462e
movq %rax, %rbx
leaq 0x80(%rsp), %rdi
callq 0x30b0
jmp 0x4631
jmp 0x462e
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0x5d96
movq %rbx, %rdi
callq 0x34d0