// File: zakimjz[P]SLEUTH/treeminer.cpp
// Function: check_canonical(std::vector<int, std::allocator<int>>&, int)
bool check_canonical(vector<int> &cand, int tpos)
{   
   if (mine_ordered) return true;
   
   //only have to check subtrees on rightmost path, with their
   //immediate siblings to the left, and make sure code is smaller
   // or equal
   bool first;
   int i, parent=0, sibling;
   int scnt = 0;

   //base condition for stopping recursion
   if (tpos == 0){
      return true;
   }

   //find the pos of parent and immediate left sibling of tpos under parent
   first = true;
   sibling = -1; //not found!
   for(i=tpos-1; i >= 0; i--){
      if (cand[i] == BranchIt) scnt++;
      else scnt --;
      if (first && scnt ==0){
         sibling = i;
         first = false;
      }
      
      if (scnt < 0){
         parent = i;
         break;
      }
   }

//    cout << "CC " << tpos << " " << sibling << " " << parent << " -- ";
//    for (i=0; i < cand.size(); ++i)
//       if (cand[i] != -1){
//          cout << " " <<  DCB->FreqIdx[cand[i]];
//       }
//      else cout << " " << cand[i];
//    cout << endl;

   bool res = true;
   if (sibling == -1){
      res = check_canonical(cand, parent);
   }
   else{
      //comapre the two subtrees rooted at tpos and sibling
      int j;
      //int it1, it2;
      for(i=tpos, j=sibling; i < cand.size() && j < tpos; i++, j++){
         //it1 = DCB->FreqIdx[cand[i]];
         //it2 = DCB->FreqIdx[cand[j]];
         //it1=cand[i];
         //it2=cand[j];
         //cout << "XX " << i << " " << j << " " 
         //     << it1 << " " << it2 << endl;
         
         if (cand[i] > cand[j]) break;
         else if (cand[i] < cand[j]){
            res = false;
            break;
         }
      }
      if (res) res = check_canonical(cand, parent);
   }
   //cout << "RES = " << res << endl;
   return res;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
testb $0x1, 0x139ce(%rip)     # 0x1e3d4
je 0xaa11
movb $0x1, -0x1(%rbp)
jmp 0xab9f
movl $0x0, -0x20(%rbp)
movl $0x0, -0x28(%rbp)
cmpl $0x0, -0x14(%rbp)
jne 0xaa2e
movb $0x1, -0x1(%rbp)
jmp 0xab9f
movb $0x1, -0x15(%rbp)
movl $0xffffffff, -0x24(%rbp) # imm = 0xFFFFFFFF
movl -0x14(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x1c(%rbp)
cmpl $0x0, -0x1c(%rbp)
jl 0xaaa6
movq -0x10(%rbp), %rdi
movslq -0x1c(%rbp), %rsi
callq 0x6520
movslq (%rax), %rax
cmpq 0x13761(%rip), %rax     # 0x1e1c0
jne 0xaa6c
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0xaa75
movl -0x28(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x28(%rbp)
testb $0x1, -0x15(%rbp)
je 0xaa8b
cmpl $0x0, -0x28(%rbp)
jne 0xaa8b
movl -0x1c(%rbp), %eax
movl %eax, -0x24(%rbp)
movb $0x0, -0x15(%rbp)
cmpl $0x0, -0x28(%rbp)
jge 0xaa99
movl -0x1c(%rbp), %eax
movl %eax, -0x20(%rbp)
jmp 0xaaa6
jmp 0xaa9b
movl -0x1c(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0xaa42
movb $0x1, -0x29(%rbp)
cmpl $-0x1, -0x24(%rbp)
jne 0xaac6
movq -0x10(%rbp), %rdi
movl -0x20(%rbp), %esi
callq 0xa9f0
andb $0x1, %al
movb %al, -0x29(%rbp)
jmp 0xab97
movl -0x14(%rbp), %eax
movl %eax, -0x1c(%rbp)
movl -0x24(%rbp), %eax
movl %eax, -0x30(%rbp)
movslq -0x1c(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x10(%rbp), %rdi
callq 0x4b80
movq -0x40(%rbp), %rcx
movq %rax, %rdx
xorl %eax, %eax
cmpq %rdx, %rcx
movb %al, -0x31(%rbp)
jae 0xab00
movl -0x30(%rbp), %eax
cmpl -0x14(%rbp), %eax
setl %al
movb %al, -0x31(%rbp)
movb -0x31(%rbp), %al
testb $0x1, %al
jne 0xab09
jmp 0xab7e
movq -0x10(%rbp), %rdi
movslq -0x1c(%rbp), %rsi
callq 0x6520
movl (%rax), %eax
movl %eax, -0x44(%rbp)
movq -0x10(%rbp), %rdi
movslq -0x30(%rbp), %rsi
callq 0x6520
movq %rax, %rcx
movl -0x44(%rbp), %eax
cmpl (%rcx), %eax
jle 0xab34
jmp 0xab7e
movq -0x10(%rbp), %rdi
movslq -0x1c(%rbp), %rsi
callq 0x6520
movl (%rax), %eax
movl %eax, -0x48(%rbp)
movq -0x10(%rbp), %rdi
movslq -0x30(%rbp), %rsi
callq 0x6520
movq %rax, %rcx
movl -0x48(%rbp), %eax
cmpl (%rcx), %eax
jge 0xab63
movb $0x0, -0x29(%rbp)
jmp 0xab7e
jmp 0xab65
jmp 0xab67
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0xaad2
testb $0x1, -0x29(%rbp)
je 0xab95
movq -0x10(%rbp), %rdi
movl -0x20(%rbp), %esi
callq 0xa9f0
andb $0x1, %al
movb %al, -0x29(%rbp)
jmp 0xab97
movb -0x29(%rbp), %al
andb $0x1, %al
movb %al, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x50, %rsp
popq %rbp
retq
nopw (%rax,%rax)