// File: /workspace/github_cpp_makefile_star2_O3/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, %ebp
movq %rsi, %rbx
movl %edi, %r14d
movl %edx, 0xc(%rsp)
movb 0x7827(%rip), %al       # 0xd460
testb %al, %al
je 0x5fc3
movb 0x7839(%rip), %al       # 0xd480
testb %al, %al
je 0x5fcd
leaq 0x77f2(%rip), %rdi      # 0xd448
movq %rbx, %rsi
callq 0x3db6
leaq 0x24(%rsp), %r15
movq %rbx, %rdi
movl %ebp, %esi
movq %r15, %rdx
callq 0x396c
movl %eax, %ebx
movq 0x77d7(%rip), %rsi      # 0xd450
movq 0x77d8(%rip), %rax      # 0xd458
cmpl (%r15), %ebx
jle 0x5cd8
movq 0x754c(%rip), %rcx      # 0xd1d8
leaq 0x77b5(%rip), %r15      # 0xd448
leaq 0x20(%rsp), %r12
movl %ecx, 0x20(%rsp)
cmpq %rax, %rsi
je 0x5cb0
movl %ecx, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x77a2(%rip)      # 0xd450
jmp 0x5cd0
movq %r15, %rdi
movq %r12, %rdx
callq 0x400e
movq 0x7516(%rip), %rcx      # 0xd1d8
movq 0x7787(%rip), %rsi      # 0xd450
movq 0x7788(%rip), %rax      # 0xd458
decl %ebx
cmpl 0x24(%rsp), %ebx
jg 0x5c98
cmpq %rax, %rsi
je 0x5cf0
movl 0xc(%rsp), %eax
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x7762(%rip)      # 0xd450
jmp 0x5d01
leaq 0x7751(%rip), %rdi      # 0xd448
leaq 0xc(%rsp), %rdx
callq 0x3e9a
cmpl $0x0, 0x76e8(%rip)      # 0xd3f0
je 0x5f92
movl %ebp, 0x18(%rsp)
cmpl $0x3, %r14d
jl 0x5f7c
leal -0x3(%r14), %r15d
decl %r14d
movq %r14, 0x28(%rsp)
movq 0x7721(%rip), %r10      # 0xd450
movq 0x7712(%rip), %rbp      # 0xd448
xorl %r9d, %r9d
movq %r10, %r11
movq %rbp, 0x10(%rsp)
movq %r10, %r12
movq %r12, %rax
subq %rbp, %rax
sarq $0x2, %rax
cmpq %rbp, %r12
je 0x5d87
movq 0x747e(%rip), %rdx      # 0xd1d8
cmpq $0x1, %rax
movq %rax, %rsi
adcq $0x0, %rsi
xorl %ecx, %ecx
xorl %edi, %edi
movslq (%rbp,%rcx,4), %r8
cmpq %r8, %rdx
je 0x5d7a
cmpl %r15d, %edi
je 0x5d8b
incl %edi
incq %rcx
cmpq %rcx, %rsi
jne 0x5d69
movq %rsi, %rcx
jmp 0x5d8f
xorl %ecx, %ecx
jmp 0x5d8f
movl %ecx, 0x1c(%rsp)
incl %ecx
cmpq %rcx, %rax
jbe 0x5dd1
movq 0x743b(%rip), %rsi      # 0xd1d8
incq %rcx
xorl %edx, %edx
movq %rcx, %rdi
movslq -0x4(%rbp,%rcx,4), %rcx
xorl %r8d, %r8d
cmpq %rcx, %rsi
setne %r8b
leal (%rdx,%r8,2), %edx
decl %edx
leaq 0x1(%rdi), %rcx
cmpq %rdi, %rax
jbe 0x5dc7
testl %edx, %edx
jns 0x5da2
addq $-0x2, %rcx
testl %edx, %edx
cmovsq %rcx, %rax
movq 0x7690(%rip), %rcx      # 0xd468
cmpq %rcx, 0x7691(%rip)      # 0xd470
je 0x5de8
movq %rcx, 0x7688(%rip)      # 0xd470
cmpq %rbp, %r12
je 0x5ef3
movq %r15, 0x30(%rsp)
movl %eax, %edi
movl 0x1c(%rsp), %r8d
negq %r8
negq %rdi
xorl %r13d, %r13d
movl $0x1, %r14d
movq 0x10(%rsp), %rcx
movq %r11, %rdx
xorl %r12d, %r12d
xorl %ebx, %ebx
leaq (%r8,%r14), %rsi
xorl %eax, %eax
cmpq $0x1, %rsi
je 0x5eb9
leaq (%rdi,%r14), %rsi
cmpq $0x1, %rsi
je 0x5eb9
addq %r13, %rbp
movq 0x762f(%rip), %rsi      # 0xd470
cmpq 0x7630(%rip), %rsi      # 0xd478
je 0x5e5c
movl (%rbp), %eax
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x7616(%rip)      # 0xd470
jmp 0x5e92
movl %ebx, %r15d
movq %rdi, %rbx
leaq 0x75ff(%rip), %rdi      # 0xd468
movq %rbp, %rdx
movq %r8, %rbp
callq 0x3e9a
movq %rbp, %r8
movq %rbx, %rdi
movl %r15d, %ebx
xorl %r9d, %r9d
movq 0x75c1(%rip), %rcx      # 0xd448
movl (%rcx,%r13), %eax
movq 0x75be(%rip), %r10      # 0xd450
movslq %eax, %rsi
xorl %eax, %eax
cmpq %rsi, 0x733a(%rip)      # 0xd1d8
setne %al
cmovel %r9d, %esi
addl %ebx, %esi
leal -0x1(,%rax,2), %eax
addl %eax, %r12d
setle %al
movq %r10, %rdx
movl %esi, %ebx
movq %rdx, %rsi
subq %rcx, %rsi
sarq $0x2, %rsi
cmpq %r14, %rsi
jbe 0x5eda
addq $0x4, %r13
incq %r14
movq %rcx, %rbp
testb %al, %al
je 0x5e19
movq %rdx, %r11
movq %rcx, 0x10(%rsp)
movq %rcx, %rbp
movq %rdx, %r12
testb %al, %al
movq 0x30(%rsp), %r15
je 0x5efb
jmp 0x5f64
xorl %ebx, %ebx
movq %rbp, %rdx
movq %rbp, %rcx
movq %rcx, %rbp
movq %rdx, %r12
leaq 0x7560(%rip), %rdi      # 0xd468
leaq 0x7539(%rip), %rsi      # 0xd448
movq %r10, %r14
movq %r11, %r13
callq 0x5ab9
movq %r14, %r10
xorl %r9d, %r9d
movq 0x10(%rsp), %rcx
movq %r13, %rdx
testb %al, %al
je 0x5f64
leaq 0x74c5(%rip), %rdi      # 0xd3f8
movq 0x28(%rsp), %rsi
leaq 0x7529(%rip), %rdx      # 0xd468
movl %ebx, %ecx
callq 0x7e8e
testb %al, %al
je 0x5fbf
movq 0x74ff(%rip), %r10      # 0xd450
movq 0x74f0(%rip), %rcx      # 0xd448
movq %r10, %rdx
movq %rcx, %rbp
movq %r10, %r12
xorl %r9d, %r9d
leal -0x1(%r15), %eax
movq %rcx, 0x10(%rsp)
movq %rdx, %r11
testl %r15d, %r15d
movl %eax, %r15d
jg 0x5d44
movl $0x28, %edi
callq 0x21d0
movl 0xc(%rsp), %ecx
movl %ecx, (%rax)
movl 0x18(%rsp), %ebp
jmp 0x5fa2
movl $0x28, %edi
callq 0x21d0
movl 0xc(%rsp), %ecx
movl %ecx, (%rax)
movl %ebp, 0x4(%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rax)
movups %xmm0, 0x18(%rax)
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %eax, %eax
jmp 0x5fb0
callq 0x23f7
jmp 0x5c41
callq 0x2443
jmp 0x5c4f