// File: /workspace/github_c_makefile_star2_O3/stevenmherring[P]DNS/fine-trie.c
// Function: search_and_squat
int32_t _search_and_squat(struct trie_node *node, const char *string, size_t strlen, int32_t ip4_address) {

    int keylen, cmp;
    int rc;
    // First things first, check if we are NULL
    // this should never happen
    assert(node != NULL);
    if (node == NULL) return -1;
    assert(node->strlen < 64);
    assert(strlen >0);
    // See if this key is a substring of the string passed in
    cmp = compare_keys(node->key, node->strlen, string, strlen, &keylen);
    if (cmp == 0)
    {
      // Yes, either quit, or recur on the children
      // If this key is longer than our search string, the key isn't here
      if (node->strlen > keylen)
      {
        rc = pthread_mutex_unlock(&(node->lock));
        assert(rc == 0);

        return -1;
      }
      else if (strlen > keylen)
      {
        if(node->children != NULL)
        {
          // first lock children, then unlock parent to prevent possible
          // situation where the thread has no locks and children is deleted
          rc = pthread_mutex_lock(&(node->children->lock));
          assert(rc == 0);
          rc = pthread_mutex_unlock(&(node->lock));
          assert(rc == 0);
          // Recur on children list
          return _search(node->children, string, strlen - keylen);
        }
        else
        {
          rc = pthread_mutex_unlock(&(node->lock));
          assert(rc == 0);
          return -1;
        }
      }
      else
      {
        //here is where squatting block happens, loop through until rv = 1 due to having a max # of squats
        int rv = 0;
        node->waiting++;
        while(!rv)
        {
          if(node->ip4_address == 0)
          {
            node->ip4_address = ip4_address;
            node->waiting--;
            rv = 1;
          }
          else
          {
            int waitSuccess = pthread_cond_wait(&(node->c), &(node->lock));
            assert(waitSuccess == 0);
          }
        }
        assert (strlen == keylen);
        rc = pthread_mutex_unlock(&(node->lock));
        assert(rc == 0);
        return 1;
      }
    }
    else if (cmp < 0)
    {
      if(node->next != NULL)
      {
        // No, look right (the node's key is "less" than the search key)
        // first lock next, then unlock parent to prevent possible
        // situation where the thread has no locks and next is deleted
        //assign next node, get its lock, return lock of current node
        //recur through the next
        struct trie_node *next = node->next;
        rc = pthread_mutex_lock(&(next->lock));
        assert(rc == 0);
        rc = pthread_mutex_unlock(&(node->lock));
        assert(rc == 0);
        return _search(next, string, strlen);
      }
      else
      {
        rc = pthread_mutex_unlock(&(node->lock));
        assert(rc == 0);
        return -1;
      }
    }
    else
    {
      // Quit early
      rc = pthread_mutex_unlock(&(node->lock));
      assert(rc == 0);
      return -1;
    }
  }

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x10, %rsp
testq %rdi, %rdi
je 0x19c5
movq %rsi, %r15
movq %rdi, %r14
movl 0x8(%rdi), %esi
cmpl $0x40, %esi
jae 0x19e4
movq %rdx, %rbx
testq %rdx, %rdx
je 0x1a03
movl %ecx, %ebp
leaq 0x18(%r14), %rdi
leaq 0xc(%rsp), %r8
movq %r15, %rdx
movl %ebx, %ecx
callq 0x13f8
testl %eax, %eax
je 0x1809
js 0x184e
addq $0x58, %r14
movq %r14, %rdi
callq 0x1110
movl %eax, %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
testl %ecx, %ecx
je 0x19b8
leaq 0x291c(%rip), %rdi      # 0x410d
leaq 0x280c(%rip), %rsi      # 0x4004
leaq 0x2927(%rip), %rcx      # 0x4126
movl $0x115, %edx            # imm = 0x115
callq 0x10b0
movslq 0xc(%rsp), %rax
cmpl %eax, 0x8(%r14)
jbe 0x1885
addq $0x58, %r14
movq %r14, %rdi
callq 0x1110
movl %eax, %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
testl %ecx, %ecx
je 0x19b8
leaq 0x28d7(%rip), %rdi      # 0x410d
leaq 0x27c7(%rip), %rsi      # 0x4004
leaq 0x28e2(%rip), %rcx      # 0x4126
movl $0xc9, %edx
callq 0x10b0
movq (%r14), %r12
testq %r12, %r12
je 0x1967
leaq 0x58(%r12), %rdi
callq 0x11d0
testl %eax, %eax
jne 0x1a22
addq $0x58, %r14
movq %r14, %rdi
callq 0x1110
testl %eax, %eax
jne 0x1a41
movq %r12, %rdi
jmp 0x18c5
cmpq %rbx, %rax
jae 0x18d5
movq 0x10(%r14), %rdi
testq %rdi, %rdi
je 0x199d
addq $0x58, %rdi
callq 0x11d0
testl %eax, %eax
jne 0x1a7f
leaq 0x58(%r14), %rdi
callq 0x1110
testl %eax, %eax
jne 0x1a9e
movq 0x10(%r14), %rdi
movslq 0xc(%rsp), %rax
subq %rax, %rbx
movq %r15, %rsi
movq %rbx, %rdx
callq 0x1487
jmp 0x19b8
incl 0xb0(%r14)
leaq 0x80(%r14), %r12
leaq 0x58(%r14), %r15
cmpl $0x0, 0xc(%r14)
je 0x191c
movq %r12, %rdi
movq %r15, %rsi
callq 0x1090
testl %eax, %eax
je 0x18e7
leaq 0x286f(%rip), %rdi      # 0x4173
leaq 0x26f9(%rip), %rsi      # 0x4004
leaq 0x2814(%rip), %rcx      # 0x4126
movl $0xf1, %edx
callq 0x10b0
movl %ebp, 0xc(%r14)
decl 0xb0(%r14)
movslq 0xc(%rsp), %rax
cmpq %rbx, %rax
jne 0x1a60
movq %r15, %rdi
callq 0x1110
movl %eax, %ecx
movl $0x1, %eax
testl %ecx, %ecx
je 0x19b8
leaq 0x27be(%rip), %rdi      # 0x410d
leaq 0x26ae(%rip), %rsi      # 0x4004
leaq 0x27c9(%rip), %rcx      # 0x4126
movl $0xf6, %edx
callq 0x10b0
addq $0x58, %r14
movq %r14, %rdi
callq 0x1110
movl %eax, %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
testl %ecx, %ecx
je 0x19b8
leaq 0x2788(%rip), %rdi      # 0x410d
leaq 0x2678(%rip), %rsi      # 0x4004
leaq 0x2793(%rip), %rcx      # 0x4126
movl $0x10d, %edx            # imm = 0x10D
callq 0x10b0
addq $0x58, %r14
movq %r14, %rdi
callq 0x1110
movl %eax, %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
testl %ecx, %ecx
jne 0x1abd
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
leaq 0x26de(%rip), %rdi      # 0x40aa
leaq 0x2631(%rip), %rsi      # 0x4004
leaq 0x274c(%rip), %rcx      # 0x4126
movl $0xbc, %edx
callq 0x10b0
leaq 0x2706(%rip), %rdi      # 0x40f1
leaq 0x2612(%rip), %rsi      # 0x4004
leaq 0x272d(%rip), %rcx      # 0x4126
movl $0xbe, %edx
callq 0x10b0
leaq 0x26f9(%rip), %rdi      # 0x4103
leaq 0x25f3(%rip), %rsi      # 0x4004
leaq 0x270e(%rip), %rcx      # 0x4126
movl $0xbf, %edx
callq 0x10b0
leaq 0x26e4(%rip), %rdi      # 0x410d
leaq 0x25d4(%rip), %rsi      # 0x4004
leaq 0x26ef(%rip), %rcx      # 0x4126
movl $0x105, %edx            # imm = 0x105
callq 0x10b0
leaq 0x26c5(%rip), %rdi      # 0x410d
leaq 0x25b5(%rip), %rsi      # 0x4004
leaq 0x26d0(%rip), %rcx      # 0x4126
movl $0x107, %edx            # imm = 0x107
callq 0x10b0
leaq 0x26ae(%rip), %rdi      # 0x4115
leaq 0x2596(%rip), %rsi      # 0x4004
leaq 0x26b1(%rip), %rcx      # 0x4126
movl $0xf4, %edx
callq 0x10b0
leaq 0x2687(%rip), %rdi      # 0x410d
leaq 0x2577(%rip), %rsi      # 0x4004
leaq 0x2692(%rip), %rcx      # 0x4126
movl $0xd4, %edx
callq 0x10b0
leaq 0x2668(%rip), %rdi      # 0x410d
leaq 0x2558(%rip), %rsi      # 0x4004
leaq 0x2673(%rip), %rcx      # 0x4126
movl $0xd6, %edx
callq 0x10b0
leaq 0x2649(%rip), %rdi      # 0x410d
leaq 0x2539(%rip), %rsi      # 0x4004
leaq 0x2654(%rip), %rcx      # 0x4126
movl $0xdd, %edx
callq 0x10b0