// File: Ahhgust[P]STRaitRazor/trie.cpp
// Function: Trie::addWord(char const*, unsigned int, unsigned int, unsigned char)
void
Trie::addWord(const char *w, unsigned wordLen, unsigned id, unsigned char type) {
  
  unsigned i;
  char c;
  TrieNode *parent, *child;
  parent = root;

  //  cout << w << "\t" << mem - root << "\t";

  // traverse the tree, allocing as needed
  for (i=0; i < wordLen && *w; ++i, ++w) {
    c = *w;
    if (c == 'A') {
      if (parent->a == NULL) {
	parent->a = ++mem;
      }
      child = parent->a;
    } else if (c == 'C') {
      if (parent->c == NULL) {
	parent->c = ++mem;
      }
      child = parent->c;
    } else if (c == 'G') {
      if (parent->g == NULL) {
	parent->g = ++mem;
      }
      child = parent->g;
    } else {
      if (parent->t == NULL) {
	parent->t = ++mem;
      }
      child = parent->t;
    }
    
    parent = child;
  }

  if (parent->id == NULL) {
    parent->id = new vector<unsigned>;
    parent->type = new vector<unsigned char>;
    parent->id->push_back(id);
    parent->type->push_back(type);
  } else {
    // ensure that each type/id pair is only added once    
    for (i=0; i < parent->id->size(); ++i) {
      if (parent->id->at(i) == id &&
          parent->type->at(i) == type)
        return;
    }

    parent->id->push_back(id);
    parent->type->push_back(type);    


  }


  
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movb %r8b, %al
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movl %ecx, -0x18(%rbp)
movb %al, -0x19(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x40(%rbp)
movq (%rax), %rax
movq %rax, -0x30(%rbp)
movl $0x0, -0x20(%rbp)
movl -0x20(%rbp), %ecx
xorl %eax, %eax
cmpl -0x14(%rbp), %ecx
movb %al, -0x41(%rbp)
jae 0x1183c
movq -0x10(%rbp), %rax
cmpb $0x0, (%rax)
setne %al
movb %al, -0x41(%rbp)
movb -0x41(%rbp), %al
testb $0x1, %al
jne 0x11848
jmp 0x11954
movq -0x10(%rbp), %rax
movb (%rax), %al
movb %al, -0x21(%rbp)
movsbl -0x21(%rbp), %eax
cmpl $0x41, %eax
jne 0x1188b
movq -0x30(%rbp), %rax
cmpq $0x0, (%rax)
jne 0x1187b
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rcx
addq $0x30, %rcx
movq %rcx, 0x8(%rax)
movq -0x30(%rbp), %rax
movq %rcx, (%rax)
movq -0x30(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x38(%rbp)
jmp 0x11932
movsbl -0x21(%rbp), %eax
cmpl $0x43, %eax
jne 0x118c5
movq -0x30(%rbp), %rax
cmpq $0x0, 0x8(%rax)
jne 0x118b7
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rcx
addq $0x30, %rcx
movq %rcx, 0x8(%rax)
movq -0x30(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x38(%rbp)
jmp 0x11930
movsbl -0x21(%rbp), %eax
cmpl $0x47, %eax
jne 0x118ff
movq -0x30(%rbp), %rax
cmpq $0x0, 0x10(%rax)
jne 0x118f1
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rcx
addq $0x30, %rcx
movq %rcx, 0x8(%rax)
movq -0x30(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x38(%rbp)
jmp 0x1192e
movq -0x30(%rbp), %rax
cmpq $0x0, 0x18(%rax)
jne 0x11922
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rcx
addq $0x30, %rcx
movq %rcx, 0x8(%rax)
movq -0x30(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x30(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x38(%rbp)
jmp 0x11930
jmp 0x11932
movq -0x38(%rbp), %rax
movq %rax, -0x30(%rbp)
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
movq -0x10(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x10(%rbp)
jmp 0x11822
movq -0x30(%rbp), %rax
cmpq $0x0, 0x20(%rax)
jne 0x119ca
movl $0x18, %edi
callq 0x32b0
movq %rax, %rdi
movq %rdi, -0x58(%rbp)
callq 0x13210
movq -0x58(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x20(%rax)
movl $0x18, %edi
callq 0x32b0
movq %rax, %rdi
movq %rdi, -0x50(%rbp)
callq 0x13230
movq -0x50(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x30(%rbp), %rax
movq 0x20(%rax), %rdi
leaq -0x18(%rbp), %rsi
callq 0x8f70
movq -0x30(%rbp), %rax
movq 0x28(%rax), %rdi
leaq -0x19(%rbp), %rsi
callq 0x13250
jmp 0x11a58
movl $0x0, -0x20(%rbp)
movl -0x20(%rbp), %eax
movq %rax, -0x60(%rbp)
movq -0x30(%rbp), %rax
movq 0x20(%rax), %rdi
callq 0x8ea0
movq %rax, %rcx
movq -0x60(%rbp), %rax
cmpq %rcx, %rax
jae 0x11a36
movq -0x30(%rbp), %rax
movq 0x20(%rax), %rdi
movl -0x20(%rbp), %eax
movl %eax, %esi
callq 0x131d0
movl (%rax), %eax
cmpl -0x18(%rbp), %eax
jne 0x11a29
movq -0x30(%rbp), %rax
movq 0x28(%rax), %rdi
movl -0x20(%rbp), %eax
movl %eax, %esi
callq 0x13190
movzbl (%rax), %eax
movzbl -0x19(%rbp), %ecx
cmpl %ecx, %eax
jne 0x11a29
jmp 0x11a58
jmp 0x11a2b
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x119d1
movq -0x30(%rbp), %rax
movq 0x20(%rax), %rdi
leaq -0x18(%rbp), %rsi
callq 0x8f70
movq -0x30(%rbp), %rax
movq 0x28(%rax), %rdi
leaq -0x19(%rbp), %rsi
callq 0x13250
addq $0x60, %rsp
popq %rbp
retq
nop