// File: /workspace/github_c_makefile_star2_O2/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 %r13
pushq %r12
pushq %rbx
pushq %rax
testq %rdi, %rdi
je 0x1946
movq %rsi, %r15
movq %rdi, %r14
movl 0x8(%rdi), %esi
cmpl $0x40, %esi
jae 0x1965
movq %rdx, %rbx
testq %rdx, %rdx
je 0x1984
movl %ecx, %ebp
leaq 0x18(%r14), %rdi
leaq 0x4(%rsp), %r8
movq %r15, %rdx
movl %ebx, %ecx
callq 0x13dd
testl %eax, %eax
je 0x17ac
js 0x17ea
addq $0x58, %r14
movq %r14, %rdi
callq 0x1110
testl %eax, %eax
je 0x1915
leaq 0x2979(%rip), %rdi      # 0x410d
leaq 0x2869(%rip), %rsi      # 0x4004
leaq 0x2984(%rip), %rcx      # 0x4126
movl $0x115, %edx            # imm = 0x115
callq 0x10b0
movslq 0x4(%rsp), %rax
cmpl %eax, 0x8(%r14)
jbe 0x1821
addq $0x58, %r14
movq %r14, %rdi
callq 0x1110
testl %eax, %eax
je 0x1915
leaq 0x293b(%rip), %rdi      # 0x410d
leaq 0x282b(%rip), %rsi      # 0x4004
leaq 0x2946(%rip), %rcx      # 0x4126
movl $0xc9, %edx
callq 0x10b0
movq (%r14), %r12
testq %r12, %r12
je 0x18d2
leaq 0x58(%r12), %rdi
callq 0x11d0
testl %eax, %eax
jne 0x19a3
addq $0x58, %r14
movq %r14, %rdi
callq 0x1110
testl %eax, %eax
jne 0x19c2
movq %r12, %rdi
jmp 0x1861
cmpq %rbx, %rax
jae 0x1871
movq 0x10(%r14), %rdi
testq %rdi, %rdi
je 0x1901
addq $0x58, %rdi
callq 0x11d0
testl %eax, %eax
jne 0x1a1f
leaq 0x58(%r14), %rdi
callq 0x1110
testl %eax, %eax
jne 0x1a3e
movq 0x10(%r14), %rdi
movslq 0x4(%rsp), %rax
subq %rax, %rbx
movq %r15, %rsi
movq %rbx, %rdx
callq 0x1465
jmp 0x1918
incl 0xb0(%r14)
leaq 0x80(%r14), %r12
leaq 0x58(%r14), %r15
movl 0xc(%r14), %r13d
testl %r13d, %r13d
je 0x18a0
movq %r12, %rdi
movq %r15, %rsi
callq 0x1090
testl %eax, %eax
je 0x18ab
jmp 0x1927
movl %ebp, 0xc(%r14)
decl 0xb0(%r14)
testl %r13d, %r13d
jne 0x1883
movslq 0x4(%rsp), %rax
cmpq %rbx, %rax
jne 0x19e1
movq %r15, %rdi
callq 0x1110
testl %eax, %eax
jne 0x1a00
pushq $0x1
jmp 0x1917
addq $0x58, %r14
movq %r14, %rdi
callq 0x1110
testl %eax, %eax
je 0x1915
leaq 0x2824(%rip), %rdi      # 0x410d
leaq 0x2714(%rip), %rsi      # 0x4004
leaq 0x282f(%rip), %rcx      # 0x4126
movl $0x10d, %edx            # imm = 0x10D
callq 0x10b0
addq $0x58, %r14
movq %r14, %rdi
callq 0x1110
testl %eax, %eax
jne 0x1a5d
pushq $-0x1
popq %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x2845(%rip), %rdi      # 0x4173
leaq 0x26cf(%rip), %rsi      # 0x4004
leaq 0x27ea(%rip), %rcx      # 0x4126
movl $0xf1, %edx
callq 0x10b0
leaq 0x275d(%rip), %rdi      # 0x40aa
leaq 0x26b0(%rip), %rsi      # 0x4004
leaq 0x27cb(%rip), %rcx      # 0x4126
movl $0xbc, %edx
callq 0x10b0
leaq 0x2785(%rip), %rdi      # 0x40f1
leaq 0x2691(%rip), %rsi      # 0x4004
leaq 0x27ac(%rip), %rcx      # 0x4126
movl $0xbe, %edx
callq 0x10b0
leaq 0x2778(%rip), %rdi      # 0x4103
leaq 0x2672(%rip), %rsi      # 0x4004
leaq 0x278d(%rip), %rcx      # 0x4126
movl $0xbf, %edx
callq 0x10b0
leaq 0x2763(%rip), %rdi      # 0x410d
leaq 0x2653(%rip), %rsi      # 0x4004
leaq 0x276e(%rip), %rcx      # 0x4126
movl $0x105, %edx            # imm = 0x105
callq 0x10b0
leaq 0x2744(%rip), %rdi      # 0x410d
leaq 0x2634(%rip), %rsi      # 0x4004
leaq 0x274f(%rip), %rcx      # 0x4126
movl $0x107, %edx            # imm = 0x107
callq 0x10b0
leaq 0x272d(%rip), %rdi      # 0x4115
leaq 0x2615(%rip), %rsi      # 0x4004
leaq 0x2730(%rip), %rcx      # 0x4126
movl $0xf4, %edx
callq 0x10b0
leaq 0x2706(%rip), %rdi      # 0x410d
leaq 0x25f6(%rip), %rsi      # 0x4004
leaq 0x2711(%rip), %rcx      # 0x4126
movl $0xf6, %edx
callq 0x10b0
leaq 0x26e7(%rip), %rdi      # 0x410d
leaq 0x25d7(%rip), %rsi      # 0x4004
leaq 0x26f2(%rip), %rcx      # 0x4126
movl $0xd4, %edx
callq 0x10b0
leaq 0x26c8(%rip), %rdi      # 0x410d
leaq 0x25b8(%rip), %rsi      # 0x4004
leaq 0x26d3(%rip), %rcx      # 0x4126
movl $0xd6, %edx
callq 0x10b0
leaq 0x26a9(%rip), %rdi      # 0x410d
leaq 0x2599(%rip), %rsi      # 0x4004
leaq 0x26b4(%rip), %rcx      # 0x4126
movl $0xdd, %edx
callq 0x10b0