// File: 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
movq %rsp, %rbp
subq $0xa0, %rsp
movl %edi, -0xc(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movl %ecx, -0x20(%rbp)
movq $0x0, -0x28(%rbp)
cmpb $0x0, 0x13889(%rip)     # 0x1e460
jne 0xac1c
leaq 0x13880(%rip), %rdi     # 0x1e460
callq 0x2300
cmpl $0x0, %eax
je 0xac1c
leaq 0x13857(%rip), %rdi     # 0x1e448
callq 0x3960
leaq -0x71cd(%rip), %rdi     # 0x3a30
leaq 0x13844(%rip), %rsi     # 0x1e448
leaq 0x1359d(%rip), %rdx     # 0x1e1a8
callq 0x2150
leaq 0x13849(%rip), %rdi     # 0x1e460
callq 0x2130
cmpb $0x0, 0x1385d(%rip)     # 0x1e480
jne 0xac68
leaq 0x13854(%rip), %rdi     # 0x1e480
callq 0x2300
cmpl $0x0, %eax
je 0xac68
leaq 0x1382b(%rip), %rdi     # 0x1e468
callq 0x3960
leaq -0x7219(%rip), %rdi     # 0x3a30
leaq 0x13818(%rip), %rsi     # 0x1e468
leaq 0x13551(%rip), %rdx     # 0x1e1a8
callq 0x2150
leaq 0x1381d(%rip), %rdi     # 0x1e480
callq 0x2130
movq -0x18(%rbp), %rdi
callq 0xc150
movq %rax, %rsi
leaq 0x137cd(%rip), %rdi     # 0x1e448
callq 0x6540
movq -0x18(%rbp), %rdi
movl -0x20(%rbp), %esi
leaq -0x30(%rbp), %rdx
callq 0x5dc0
movl %eax, -0x34(%rbp)
movl -0x34(%rbp), %eax
cmpl -0x30(%rbp), %eax
jle 0xacc0
movq 0x1351e(%rip), %rax     # 0x1e1c0
movl %eax, -0x38(%rbp)
leaq 0x1379c(%rip), %rdi     # 0x1e448
leaq -0x38(%rbp), %rsi
callq 0x6790
movl -0x34(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0xac93
leaq 0x13781(%rip), %rdi     # 0x1e448
leaq -0x1c(%rbp), %rsi
callq 0x67c0
movb $0x1, -0x39(%rbp)
movb $0x1, -0x39(%rbp)
testb $0x1, -0x39(%rbp)
jne 0xaceb
movq -0x28(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0xb051
cmpl $0x0, 0x136ee(%rip)     # 0x1e3e0
jne 0xad4f
movl $0x28, %edi
callq 0x2170
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x70(%rbp)
movl -0x1c(%rbp), %esi
movl -0x20(%rbp), %edx
xorl %r8d, %r8d
movl %r8d, %ecx
callq 0x6490
jmp 0xad1b
movq -0x70(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0xb051
movq -0x70(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
movl $0x28, %esi
callq 0x2180
jmp 0xb05e
movb $0x1, -0x39(%rbp)
movl -0xc(%rbp), %eax
subl $0x3, %eax
movl %eax, -0x58(%rbp)
cmpl $0x0, -0x58(%rbp)
jl 0xafe5
movl $0x0, -0x5c(%rbp)
movl $0x0, -0x60(%rbp)
movslq -0x5c(%rbp), %rax
movq %rax, -0x78(%rbp)
leaq 0x136c5(%rip), %rdi     # 0x1e448
callq 0x4b80
movq %rax, %rcx
movq -0x78(%rbp), %rax
cmpq %rcx, %rax
jae 0xadd8
movslq -0x5c(%rbp), %rsi
leaq 0x136a9(%rip), %rdi     # 0x1e448
callq 0x6520
movslq (%rax), %rax
cmpq 0x13412(%rip), %rax     # 0x1e1c0
je 0xadcb
movl -0x60(%rbp), %eax
cmpl -0x58(%rbp), %eax
jne 0xadc0
movl -0x5c(%rbp), %eax
movl %eax, -0x50(%rbp)
jmp 0xadd8
movl -0x60(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x60(%rbp)
jmp 0xadcb
jmp 0xadcd
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0xad74
movl $0x0, -0x34(%rbp)
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
movslq -0x5c(%rbp), %rax
movq %rax, -0x88(%rbp)
leaq 0x1364e(%rip), %rdi     # 0x1e448
callq 0x4b80
movq -0x88(%rbp), %rcx
movq %rax, %rdx
xorl %eax, %eax
cmpq %rdx, %rcx
movb %al, -0x79(%rbp)
jae 0xae1d
cmpl $0x0, -0x34(%rbp)
setge %al
movb %al, -0x79(%rbp)
movb -0x79(%rbp), %al
testb $0x1, %al
jne 0xae26
jmp 0xae63
movslq -0x5c(%rbp), %rsi
leaq 0x13617(%rip), %rdi     # 0x1e448
callq 0x6520
movslq (%rax), %rax
cmpq 0x13380(%rip), %rax     # 0x1e1c0
jne 0xae4d
movl -0x34(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0xae56
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0xae58
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0xade8
cmpl $0x0, -0x34(%rbp)
jl 0xae7a
leaq 0x135d8(%rip), %rdi     # 0x1e448
callq 0x4b80
movl %eax, -0x54(%rbp)
jmp 0xae83
movl -0x5c(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x54(%rbp)
movl $0x0, -0x2c(%rbp)
leaq 0x135d7(%rip), %rdi     # 0x1e468
callq 0xc370
movb $0x0, -0x61(%rbp)
movl $0x0, -0x34(%rbp)
movl $0x0, -0x60(%rbp)
movslq -0x60(%rbp), %rax
movq %rax, -0x98(%rbp)
leaq 0x1358e(%rip), %rdi     # 0x1e448
callq 0x4b80
movq -0x98(%rbp), %rcx
movq %rax, %rdx
xorl %eax, %eax
cmpq %rdx, %rcx
movb %al, -0x89(%rbp)
jae 0xaee1
movb -0x61(%rbp), %al
xorb $-0x1, %al
movb %al, -0x89(%rbp)
movb -0x89(%rbp), %al
testb $0x1, %al
jne 0xaef0
jmp 0xaf83
movl -0x60(%rbp), %eax
cmpl -0x50(%rbp), %eax
je 0xaf73
movl -0x60(%rbp), %eax
cmpl -0x54(%rbp), %eax
je 0xaf73
movslq -0x60(%rbp), %rsi
leaq 0x1353d(%rip), %rdi     # 0x1e448
callq 0x6520
movq %rax, %rsi
leaq 0x1354e(%rip), %rdi     # 0x1e468
callq 0x67c0
movslq -0x60(%rbp), %rsi
leaq 0x1351e(%rip), %rdi     # 0x1e448
callq 0x6520
movslq (%rax), %rax
cmpq 0x13287(%rip), %rax     # 0x1e1c0
je 0xaf5e
movslq -0x60(%rbp), %rsi
leaq 0x13502(%rip), %rdi     # 0x1e448
callq 0x6520
movl (%rax), %eax
addl -0x2c(%rbp), %eax
movl %eax, -0x2c(%rbp)
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0xaf67
movl -0x34(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x34(%rbp)
cmpl $0x0, -0x34(%rbp)
jg 0xaf71
movb $0x1, -0x61(%rbp)
jmp 0xaf73
jmp 0xaf75
movl -0x60(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x60(%rbp)
jmp 0xaea8
testb $0x1, -0x61(%rbp)
jne 0xafd5
leaq 0x134d8(%rip), %rdi     # 0x1e468
leaq 0x134b1(%rip), %rsi     # 0x1e448
callq 0xa7a0
testb $0x1, %al
jne 0xafa2
jmp 0xafd5
movl -0xc(%rbp), %esi
subl $0x1, %esi
movl -0x2c(%rbp), %ecx
leaq 0x13436(%rip), %rdi     # 0x1e3e8
leaq 0x134af(%rip), %rdx     # 0x1e468
callq 0x10e10
andb $0x1, %al
movb %al, -0x39(%rbp)
testb $0x1, -0x39(%rbp)
jne 0xafd3
movq $0x0, -0x8(%rbp)
jmp 0xb051
jmp 0xafd5
jmp 0xafd7
movl -0x58(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x58(%rbp)
jmp 0xad5c
testb $0x1, -0x39(%rbp)
je 0xb041
movl $0x28, %edi
callq 0x2170
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0xa0(%rbp)
movl -0x1c(%rbp), %esi
movl -0x20(%rbp), %edx
xorl %r8d, %r8d
movl %r8d, %ecx
callq 0x6490
jmp 0xb015
movq -0xa0(%rbp), %rax
movq %rax, -0x28(%rbp)
jmp 0xb049
movq -0xa0(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
movl $0x28, %esi
callq 0x2180
jmp 0xb05e
movq $0x0, -0x28(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0xa0, %rsp
popq %rbp
retq
movq -0x48(%rbp), %rdi
callq 0x22d0
nopw (%rax,%rax)