// File: /workspace/github_cpp_makefile_star2_O1/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 $0x38, %rsp
movl %ecx, 0x14(%rsp)
movq %rsi, %r15
movl %edi, %ebx
movl %edx, 0x10(%rsp)
movb 0x77ea(%rip), %al       # 0xd460
testb %al, %al
je 0x5fc2
movb 0x77fc(%rip), %al       # 0xd480
testb %al, %al
je 0x5fcc
leaq 0x77b5(%rip), %rdi      # 0xd448
movq %r15, %rsi
callq 0x3e1a
leaq 0x24(%rsp), %r12
movq %r15, %rdi
movl 0x14(%rsp), %esi
movq %r12, %rdx
callq 0x39cc
cmpl (%r12), %eax
jle 0x5cff
movl %eax, %ebp
leaq 0x778a(%rip), %r15      # 0xd448
leaq 0x20(%rsp), %r12
movl 0x750f(%rip), %eax      # 0xd1d8
movl %eax, 0x20(%rsp)
movq 0x777c(%rip), %rsi      # 0xd450
cmpq 0x777d(%rip), %rsi      # 0xd458
je 0x5cec
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x7766(%rip)      # 0xd450
jmp 0x5cf7
movq %r15, %rdi
movq %r12, %rdx
callq 0x405e
decl %ebp
cmpl 0x24(%rsp), %ebp
jg 0x5cc3
movq 0x774a(%rip), %rsi      # 0xd450
cmpq 0x774b(%rip), %rsi      # 0xd458
je 0x5d22
movl 0x10(%rsp), %eax
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x7730(%rip)      # 0xd450
jmp 0x5d33
leaq 0x771f(%rip), %rdi      # 0xd448
leaq 0x10(%rsp), %rdx
callq 0x3ef0
cmpl $0x0, 0x76b6(%rip)      # 0xd3f0
je 0x5f82
cmpl $0x3, %ebx
jl 0x5f82
leal -0x3(%rbx), %r11d
decl %ebx
movq %rbx, 0x28(%rsp)
movb $0x1, 0xf(%rsp)
xorl %r10d, %r10d
movq 0x76ed(%rip), %rdx      # 0xd450
movq 0x76de(%rip), %rcx      # 0xd448
movq %rdx, %rax
subq %rcx, %rax
sarq $0x2, %rax
cmpq %rcx, %rdx
je 0x5dae
movq 0x7458(%rip), %rsi      # 0xd1d8
cmpq $0x1, %rax
movq %rax, %rdi
adcq $0x0, %rdi
xorl %edx, %edx
xorl %r8d, %r8d
movslq (%rcx,%rdx,4), %r9
cmpq %r9, %rsi
je 0x5da1
cmpl %r11d, %r8d
je 0x5db2
incl %r8d
incq %rdx
cmpq %rdx, %rdi
jne 0x5d90
movq %rdi, %rdx
jmp 0x5db6
xorl %edx, %edx
jmp 0x5db6
movl %edx, 0x18(%rsp)
leal 0x1(%rdx), %esi
movb $0x1, %dil
cmpq %rsi, %rax
jbe 0x5dff
movq 0x7410(%rip), %rsi      # 0xd1d8
movl %edx, %edx
xorl %r8d, %r8d
movl %r8d, %edi
movslq 0x4(%rcx,%rdx,4), %r8
xorl %r9d, %r9d
cmpq %r8, %rsi
setne %r9b
leal -0x1(,%r9,2), %r8d
leaq 0x2(%rdx), %r9
addl %edi, %r8d
setns %dil
incq %rdx
cmpq %r9, %rax
jbe 0x5dff
testl %r8d, %r8d
jns 0x5dcd
testb %dil, %dil
cmovel %edx, %eax
movq 0x765c(%rip), %rcx      # 0xd468
cmpq %rcx, 0x765d(%rip)      # 0xd470
je 0x5e1c
movq %rcx, 0x7654(%rip)      # 0xd470
movq %r11, 0x30(%rsp)
movq 0x7620(%rip), %rdx      # 0xd448
cmpq %rdx, 0x7621(%rip)      # 0xd450
je 0x5f18
movl %eax, %r8d
movl 0x18(%rsp), %ebp
negq %rbp
negq %r8
xorl %r15d, %r15d
movl $0x1, %ebx
xorl %r14d, %r14d
xorl %r12d, %r12d
xorl %r13d, %r13d
movl $0x1, %edi
leaq (%rbx,%rbp), %rax
cmpq $0x1, %rax
je 0x5eeb
leaq (%r8,%rbx), %rax
cmpq $0x1, %rax
je 0x5eeb
addq %r15, %rdx
movq 0x75f6(%rip), %rsi      # 0xd470
cmpq 0x75f7(%rip), %rsi      # 0xd478
je 0x5e94
movl (%rdx), %eax
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x75de(%rip)      # 0xd470
jmp 0x5eb8
leaq 0x75cd(%rip), %rdi      # 0xd468
movl %r13d, 0x1c(%rsp)
movq %r8, %r13
callq 0x3ef0
movq %r13, %r8
movl 0x1c(%rsp), %r13d
movl $0x1, %edi
xorl %r10d, %r10d
movq 0x7589(%rip), %rax      # 0xd448
movslq (%rax,%r15), %rax
xorl %ecx, %ecx
cmpq %rax, 0x730c(%rip)      # 0xd1d8
cmovel %r10d, %eax
setne %cl
addl %r13d, %eax
leal -0x1(,%rcx,2), %ecx
addl %ecx, %r12d
movzbl %r14b, %r14d
cmovlel %edi, %r14d
movl %eax, %r13d
movq 0x755e(%rip), %rax      # 0xd450
movq 0x754f(%rip), %rdx      # 0xd448
subq %rdx, %rax
sarq $0x2, %rax
cmpq %rbx, %rax
jbe 0x5f1e
addq $0x4, %r15
incq %rbx
testb $0x1, %r14b
je 0x5e58
jmp 0x5f1e
xorl %r13d, %r13d
xorl %r14d, %r14d
movb $0x1, %bl
testb $0x1, %r14b
jne 0x5f64
leaq 0x753b(%rip), %rdi      # 0xd468
leaq 0x7514(%rip), %rsi      # 0xd448
callq 0x5ad2
xorl %r10d, %r10d
testb %al, %al
je 0x5f64
leaq 0x74b1(%rip), %rdi      # 0xd3f8
movq 0x28(%rsp), %rsi
leaq 0x7515(%rip), %rdx      # 0xd468
movl %r13d, %ecx
callq 0x7b16
xorl %r10d, %r10d
movb %al, 0xf(%rsp)
movl %eax, %ebx
testb %bl, %bl
je 0x5fab
movq 0x30(%rsp), %rcx
leal -0x1(%rcx), %eax
testl %ecx, %ecx
movl %eax, %r11d
jg 0x5d5c
testb $0x1, 0xf(%rsp)
je 0x5fbd
movl $0x28, %edi
callq 0x21d0
movq %rax, %r10
movl 0x10(%rsp), %eax
movl %eax, (%r10)
movl 0x14(%rsp), %eax
movl %eax, 0x4(%r10)
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%r10)
movups %xmm0, 0x18(%r10)
movq %r10, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %r10d, %r10d
jmp 0x5fab
callq 0x23f7
jmp 0x5c7e
callq 0x2443
jmp 0x5c8c