// File: /workspace/github_cpp_makefile_star2_O2/zakimjz[P]SLEUTH/treeminer.cpp
// Function: test_node(int, Eqclass*, int, int)
Eqnode *test_node(int iter, Eqclass *eq, int val, int pos)
{
   Eqnode *eqn = NULL;
   
   //perform pruning

   //prune based on frequent subtree
   static vector<int> cand;
   static vector<int> subtree;
 
   int hval;
   int scope, scnt;

   //form the candidate preifx
   cand = eq->prefix();
   scnt = eq->get_scope(pos, scope); //what is the scope of node.pos

   while(scnt > scope){
      cand.push_back(BranchIt);
      scnt--;
   }
   cand.push_back(val);

   bool res = true;

   //check if this patten is in canonical form
   //res = check_canonical(cand, cand.size()-1);
   res= true;

   //if not canonical, will be pruned
   if (!res) return eqn;
   
   //if noprune, return with a new Eqnode
   if (prune_type == noprune){
      eqn = new Eqnode(val,pos);
      return eqn;
   }
   


   //check subtrees
   int omita, omitb;
   res = true;
   //omit i-th item (omita) and associated BranchIt (omitb)
   int i,j,k;

   for (i=iter-3; i >= 0; i--){
      //find pos for i-th item
      for (j=0,k=0; j < cand.size(); j++){
         if (cand[j] != BranchIt){
            if (k == i){
               omita = j;
               break;
            }
            else k++;
         }
      }
      
      //find pos for associated BranchIt
      scnt = 0;
      for(j++; j < cand.size() && scnt >= 0; j++){
         if (cand[j] == BranchIt) scnt--;
         else scnt++;
      }
      if (scnt >= 0) omitb = cand.size();
      else omitb = j-1;

      //cout << "OMIT " << i << " " << omita << " " << omitb << endl;

      hval = 0;
      subtree.clear();
      bool rootless = false;
      scnt = 0;
      for (k=0; k < cand.size() && !rootless; k++){
         if (k != omita && k != omitb){
            subtree.push_back(cand[k]);
            if (cand[k] != BranchIt){
               hval += cand[k];
               scnt++;
            }
            else scnt--;
            if (scnt <= 0) rootless = true;
            
         }
      }

      //cout << "LEXTEST " << subtree << " vs " << cand;
   
      //skip a rootless subtree
      if (!rootless && lexsmaller(subtree, cand)){
         //cout << " -- SMALLER ";
         res = FK.find(iter-1, subtree, hval);  
         //cout << ((res)? " ** FOUND\n":" ** NOTFOUND\n");
         if (!res) return NULL; //subtree not found!
      }
      //else cout << " -- GREATER " << endl;
   }
   
  
   if (res) eqn = new Eqnode(val,pos);
   else eqn = NULL;

   return eqn;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %ecx, 0xc(%rsp)
movq %rsi, %rbx
movl %edi, %r14d
movl %edx, 0x8(%rsp)
movb 0x7eff(%rip), %al       # 0xd430
testb %al, %al
je 0x5758
movb 0x7f11(%rip), %al       # 0xd450
testb %al, %al
je 0x57a9
leaq 0x7eca(%rip), %r15      # 0xd418
movq %r15, %rdi
movq %rbx, %rsi
callq 0x393a
leaq 0x1c(%rsp), %rdx
movq %rbx, %rdi
movl 0xc(%rsp), %esi
callq 0x35e8
movl %eax, %ebx
leaq 0x18(%rsp), %r12
cmpl 0x1c(%rsp), %ebx
jle 0x5590
movl 0x7c23(%rip), %eax      # 0xd1a0
movl %eax, 0x18(%rsp)
movq %r15, %rdi
movq %r12, %rsi
callq 0x3cec
decl %ebx
jmp 0x5571
leaq 0x7e81(%rip), %rdi      # 0xd418
leaq 0x8(%rsp), %rsi
callq 0x3a04
cmpl $0x0, 0x7e18(%rip)      # 0xd3c0
je 0x571f
leal -0x3(%r14), %r11d
decl %r14d
movq %r14, 0x20(%rsp)
xorl %r15d, %r15d
testl %r11d, %r11d
js 0x571f
movq 0x7e53(%rip), %rcx      # 0xd420
movq 0x7e44(%rip), %rax      # 0xd418
movq %rcx, %rdx
subq %rax, %rdx
sarq $0x2, %rdx
xorl %edi, %edi
movq 0x7bb9(%rip), %rsi      # 0xd1a0
xorl %r8d, %r8d
cmpq %rdi, %rdx
je 0x5605
movslq (%rax,%rdi,4), %r9
cmpq %r9, %rsi
je 0x5600
cmpl %r11d, %r8d
je 0x560a
incl %r8d
incq %rdi
jmp 0x55ea
movq %rdx, %rdi
jmp 0x560e
movl %edi, 0x10(%rsp)
movl %edi, %edi
incq %rdi
xorl %r8d, %r8d
cmpq %rdi, %rdx
jbe 0x563a
testl %r8d, %r8d
js 0x563a
movslq (%rax,%rdi,4), %r9
xorl %r10d, %r10d
cmpq %r9, %rsi
setne %r10b
leal (%r8,%r10,2), %r8d
decl %r8d
incq %rdi
jmp 0x5616
movl %r11d, 0x14(%rsp)
decq %rdi
testl %r8d, %r8d
cmovnsq %rdx, %rdi
movq 0x7de8(%rip), %rdx      # 0xd438
cmpq %rdx, 0x7de9(%rip)      # 0xd440
je 0x5660
movq %rdx, 0x7de0(%rip)      # 0xd440
movl 0x10(%rsp), %r14d
movl %edi, %r13d
xorl %r12d, %r12d
xorl %ebx, %ebx
xorl %ebp, %ebp
xorl %edx, %edx
movq %rcx, %rsi
subq %rax, %rsi
sarq $0x2, %rsi
cmpq %r12, %rsi
jbe 0x56d8
testb $0x1, %dl
jne 0x56d8
xorl %edx, %edx
cmpq %r14, %r12
je 0x56d3
cmpq %r13, %r12
je 0x56d3
leaq (%rax,%r12,4), %rsi
leaq 0x7d9c(%rip), %rdi      # 0xd438
callq 0x3a04
movq 0x7d70(%rip), %rax      # 0xd418
movslq (%rax,%r12,4), %rsi
xorl %ecx, %ecx
cmpq %rsi, 0x7aeb(%rip)      # 0xd1a0
setne %cl
cmovel %r15d, %esi
addl %ebx, %esi
leal -0x1(,%rcx,2), %ecx
addl %ecx, %ebp
setle %dl
movq 0x7d4f(%rip), %rcx      # 0xd420
movl %esi, %ebx
incq %r12
jmp 0x5671
testb $0x1, %dl
jne 0x5712
leaq 0x7d54(%rip), %rdi      # 0xd438
leaq 0x7d2d(%rip), %rsi      # 0xd418
callq 0x53c5
testb %al, %al
je 0x5712
leaq 0x7ccd(%rip), %rdi      # 0xd3c8
movq 0x20(%rsp), %rsi
leaq 0x7d31(%rip), %rdx      # 0xd438
movl %ebx, %ecx
callq 0x73fe
testb %al, %al
je 0x5746
movl 0x14(%rsp), %r11d
decl %r11d
jmp 0x55bd
pushq $0x28
popq %rdi
callq 0x2170
movq %rax, %r15
movl 0x8(%rsp), %eax
movl %eax, (%r15)
movl 0xc(%rsp), %eax
movl %eax, 0x4(%r15)
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%r15)
movups %xmm0, 0x18(%r15)
movq %r15, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x7cd1(%rip), %rdi      # 0xd430
callq 0x2300
testl %eax, %eax
je 0x5539
xorps %xmm0, %xmm0
movups %xmm0, 0x7ca2(%rip)     # 0xd418
andq $0x0, 0x7caa(%rip)      # 0xd428
leaq 0x7c93(%rip), %rsi      # 0xd418
leaq 0x96e(%rip), %rdi       # 0x60fa
leaq 0x79fd(%rip), %rdx      # 0xd190
callq 0x2130
leaq 0x7c91(%rip), %rdi      # 0xd430
callq 0x2110
jmp 0x5539
leaq 0x7ca0(%rip), %rdi      # 0xd450
callq 0x2300
testl %eax, %eax
je 0x5547
xorps %xmm0, %xmm0
movups %xmm0, 0x7c71(%rip)     # 0xd438
andq $0x0, 0x7c79(%rip)      # 0xd448
leaq 0x7c62(%rip), %rsi      # 0xd438
leaq 0x91d(%rip), %rdi       # 0x60fa
leaq 0x79ac(%rip), %rdx      # 0xd190
callq 0x2130
leaq 0x7c60(%rip), %rdi      # 0xd450
callq 0x2110
jmp 0x5547