// File: Ahhgust[P]STRaitRazor/trie.cpp
// Function: Trie::findPrefixMatch(char const*, unsigned int, unsigned int*, unsigned char*)
unsigned
Trie::findPrefixMatch(const char *w, unsigned wordLen, unsigned *outId, unsigned char *outType) {
  unsigned i, numHits=0;
  
  char c;
  TrieNode *parent, *child;
  parent = root;
  
  // traverse the tree, 
  for (i=0; i < wordLen && *w; ++i, ++w) {

    if (parent->id != NULL) {
      int size = parent->id->size();
      for ( int i = 0; i < size; ++i) {
	++numHits;
	*outType++ = parent->type->at(i);
	*outId++ = parent->id->at(i);
      }
    }


    c = *w;
    if (c == 'A') {
      if (parent->a == NULL) {
	return numHits;
      }
      child = parent->a;
    } else if (c == 'C') {
      if (parent->c == NULL) {
	return numHits;
      }
      child = parent->c;
    } else if (c == 'G') {
      if (parent->g == NULL) {
	return numHits;
      }
      child = parent->g;
    } else {
      if (parent->t == NULL) {
	return numHits;
      }
      child = parent->t;
    }
    
    parent = child;
  }

  // this is a corner case....
  // the entire binaryword matches the entire anchor sequence

  if (parent->id != NULL) {
    int size = parent->id->size();
    for ( int i = 0; i < size; ++i) {
      ++numHits;
      *outType++ = parent->type->at(i);
      *outId++ = parent->id->at(i);
    }
  }

  return numHits;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movq -0x10(%rbp), %rax
movl $0x0, -0x38(%rbp)
movq (%rax), %rax
movq %rax, -0x48(%rbp)
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %ecx
xorl %eax, %eax
cmpl -0x1c(%rbp), %ecx
movb %al, -0x61(%rbp)
jae 0x112de
movq -0x18(%rbp), %rax
cmpb $0x0, (%rax)
setne %al
movb %al, -0x61(%rbp)
movb -0x61(%rbp), %al
testb $0x1, %al
jne 0x112ea
jmp 0x1144b
movq -0x48(%rbp), %rax
cmpq $0x0, 0x20(%rax)
je 0x11372
movq -0x48(%rbp), %rax
movq 0x20(%rax), %rdi
callq 0x8ea0
movl %eax, -0x54(%rbp)
movl $0x0, -0x58(%rbp)
movl -0x58(%rbp), %eax
cmpl -0x54(%rbp), %eax
jge 0x11370
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
movq -0x48(%rbp), %rax
movq 0x28(%rax), %rdi
movslq -0x58(%rbp), %rsi
callq 0x13190
movb (%rax), %cl
movq -0x30(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x30(%rbp)
movb %cl, (%rax)
movq -0x48(%rbp), %rax
movq 0x20(%rax), %rdi
movslq -0x58(%rbp), %rsi
callq 0x131d0
movl (%rax), %ecx
movq -0x28(%rbp), %rax
movq %rax, %rdx
addq $0x4, %rdx
movq %rdx, -0x28(%rbp)
movl %ecx, (%rax)
movl -0x58(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x58(%rbp)
jmp 0x1130c
jmp 0x11372
movq -0x18(%rbp), %rax
movb (%rax), %al
movb %al, -0x39(%rbp)
movsbl -0x39(%rbp), %eax
cmpl $0x41, %eax
jne 0x113a9
movq -0x48(%rbp), %rax
cmpq $0x0, (%rax)
jne 0x11399
movl -0x38(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x114d9
movq -0x48(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x50(%rbp)
jmp 0x11429
movsbl -0x39(%rbp), %eax
cmpl $0x43, %eax
jne 0x113d6
movq -0x48(%rbp), %rax
cmpq $0x0, 0x8(%rax)
jne 0x113c8
movl -0x38(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x114d9
movq -0x48(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x50(%rbp)
jmp 0x11427
movsbl -0x39(%rbp), %eax
cmpl $0x47, %eax
jne 0x11403
movq -0x48(%rbp), %rax
cmpq $0x0, 0x10(%rax)
jne 0x113f5
movl -0x38(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x114d9
movq -0x48(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x50(%rbp)
jmp 0x11425
movq -0x48(%rbp), %rax
cmpq $0x0, 0x18(%rax)
jne 0x11419
movl -0x38(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x114d9
movq -0x48(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x50(%rbp)
jmp 0x11427
jmp 0x11429
movq -0x50(%rbp), %rax
movq %rax, -0x48(%rbp)
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
movq -0x18(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x18(%rbp)
jmp 0x112c4
movq -0x48(%rbp), %rax
cmpq $0x0, 0x20(%rax)
je 0x114d3
movq -0x48(%rbp), %rax
movq 0x20(%rax), %rdi
callq 0x8ea0
movl %eax, -0x5c(%rbp)
movl $0x0, -0x60(%rbp)
movl -0x60(%rbp), %eax
cmpl -0x5c(%rbp), %eax
jge 0x114d1
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
movq -0x48(%rbp), %rax
movq 0x28(%rax), %rdi
movslq -0x60(%rbp), %rsi
callq 0x13190
movb (%rax), %cl
movq -0x30(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x30(%rbp)
movb %cl, (%rax)
movq -0x48(%rbp), %rax
movq 0x20(%rax), %rdi
movslq -0x60(%rbp), %rsi
callq 0x131d0
movl (%rax), %ecx
movq -0x28(%rbp), %rax
movq %rax, %rdx
addq $0x4, %rdx
movq %rdx, -0x28(%rbp)
movl %ecx, (%rax)
movl -0x60(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x60(%rbp)
jmp 0x1146d
jmp 0x114d3
movl -0x38(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x70, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)