// File: Ahhgust[P]STRaitRazor/trie.cpp
// Function: Trie::addWordRC(char const*, unsigned int, unsigned int, unsigned char)
void
Trie::addWordRC(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=wordLen; i > 0; --i) {
    c = w[i-1];

    if (c == 'A') {
      if (parent->t == NULL) {
        parent->t = ++mem;
      }
      child = parent->t;

    } else if (c == 'C') {
      if (parent->g == NULL) {
        parent->g = ++mem;
      }
      child = parent->g;
    } else if (c == 'G') {
      if (parent->c == NULL) {
        parent->c = ++mem;
      }
      child = parent->c;
    } else {
      if (parent->a == NULL) {
        parent->a = ++mem;
      }
      child = parent->a;
    }
    
    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 -0x14(%rbp), %eax
movl %eax, -0x20(%rbp)
cmpl $0x0, -0x20(%rbp)
jbe 0x11ba4
movq -0x10(%rbp), %rax
movl -0x20(%rbp), %ecx
subl $0x1, %ecx
movl %ecx, %ecx
movb (%rax,%rcx), %al
movb %al, -0x21(%rbp)
movsbl -0x21(%rbp), %eax
cmpl $0x41, %eax
jne 0x11aea
movq -0x30(%rbp), %rax
cmpq $0x0, 0x18(%rax)
jne 0x11ad9
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 0x11b8e
movsbl -0x21(%rbp), %eax
cmpl $0x43, %eax
jne 0x11b24
movq -0x30(%rbp), %rax
cmpq $0x0, 0x10(%rax)
jne 0x11b16
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 0x11b8c
movsbl -0x21(%rbp), %eax
cmpl $0x47, %eax
jne 0x11b5e
movq -0x30(%rbp), %rax
cmpq $0x0, 0x8(%rax)
jne 0x11b50
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 0x11b8a
movq -0x30(%rbp), %rax
cmpq $0x0, (%rax)
jne 0x11b7f
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 0x11b8c
jmp 0x11b8e
movq -0x38(%rbp), %rax
movq %rax, -0x30(%rbp)
movl -0x20(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x11a91
movq -0x30(%rbp), %rax
cmpq $0x0, 0x20(%rax)
jne 0x11c1a
movl $0x18, %edi
callq 0x32b0
movq %rax, %rdi
movq %rdi, -0x50(%rbp)
callq 0x13210
movq -0x50(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x20(%rax)
movl $0x18, %edi
callq 0x32b0
movq %rax, %rdi
movq %rdi, -0x48(%rbp)
callq 0x13230
movq -0x48(%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 0x11ca8
movl $0x0, -0x20(%rbp)
movl -0x20(%rbp), %eax
movq %rax, -0x58(%rbp)
movq -0x30(%rbp), %rax
movq 0x20(%rax), %rdi
callq 0x8ea0
movq %rax, %rcx
movq -0x58(%rbp), %rax
cmpq %rcx, %rax
jae 0x11c86
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 0x11c79
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 0x11c79
jmp 0x11ca8
jmp 0x11c7b
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x11c21
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