// File: /workspace/github_c_makefile_star2_O1/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 0x19db
movq %rsi, %r15
movq %rdi, %r14
movl 0x8(%rdi), %esi
cmpl $0x40, %esi
jae 0x19fa
movq %rdx, %rbx
testq %rdx, %rdx
je 0x1a19
movl %ecx, %ebp
leaq 0x18(%r14), %rdi
leaq 0x4(%rsp), %r8
movq %r15, %rdx
movl %ebx, %ecx
callq 0x13fa
testl %eax, %eax
je 0x180a
js 0x184f
addq $0x58, %r14
movq %r14, %rdi
callq 0x1110
movl %eax, %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
testl %ecx, %ecx
je 0x19ad
leaq 0x291b(%rip), %rdi      # 0x410d
leaq 0x280b(%rip), %rsi      # 0x4004
leaq 0x2926(%rip), %rcx      # 0x4126
movl $0x115, %edx            # imm = 0x115
callq 0x10b0
movslq 0x4(%rsp), %rax
cmpl %eax, 0x8(%r14)
jbe 0x1886
addq $0x58, %r14
movq %r14, %rdi
callq 0x1110
movl %eax, %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
testl %ecx, %ecx
je 0x19ad
leaq 0x28d6(%rip), %rdi      # 0x410d
leaq 0x27c6(%rip), %rsi      # 0x4004
leaq 0x28e1(%rip), %rcx      # 0x4126
movl $0xc9, %edx
callq 0x10b0
movq (%r14), %r12
testq %r12, %r12
je 0x195c
leaq 0x58(%r12), %rdi
callq 0x11d0
testl %eax, %eax
jne 0x1a38
addq $0x58, %r14
movq %r14, %rdi
callq 0x1110
testl %eax, %eax
jne 0x1a57
movq %r12, %rdi
jmp 0x18c6
cmpq %rbx, %rax
jae 0x18d6
movq 0x10(%r14), %rdi
testq %rdi, %rdi
je 0x1992
addq $0x58, %rdi
callq 0x11d0
testl %eax, %eax
jne 0x1a95
leaq 0x58(%r14), %rdi
callq 0x1110
testl %eax, %eax
jne 0x1ab4
movq 0x10(%r14), %rdi
movslq 0x4(%rsp), %rax
subq %rax, %rbx
movq %r15, %rsi
movq %rbx, %rdx
callq 0x1489
jmp 0x19ad
incl 0xb0(%r14)
leaq 0x80(%r14), %r12
leaq 0x58(%r14), %r15
xorl %r13d, %r13d
cmpl $0x0, 0xc(%r14)
je 0x1906
movq %r12, %rdi
movq %r15, %rsi
callq 0x1090
testl %eax, %eax
je 0x1917
jmp 0x19bc
movl %ebp, 0xc(%r14)
decl 0xb0(%r14)
movl $0x1, %r13d
testl %r13d, %r13d
je 0x18eb
movslq 0x4(%rsp), %rax
cmpq %rbx, %rax
jne 0x1a76
movq %r15, %rdi
callq 0x1110
movl %eax, %ecx
movl $0x1, %eax
testl %ecx, %ecx
je 0x19ad
leaq 0x27c9(%rip), %rdi      # 0x410d
leaq 0x26b9(%rip), %rsi      # 0x4004
leaq 0x27d4(%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 0x19ad
leaq 0x2793(%rip), %rdi      # 0x410d
leaq 0x2683(%rip), %rsi      # 0x4004
leaq 0x279e(%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 0x1ad3
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x27b0(%rip), %rdi      # 0x4173
leaq 0x263a(%rip), %rsi      # 0x4004
leaq 0x2755(%rip), %rcx      # 0x4126
movl $0xf1, %edx
callq 0x10b0
leaq 0x26c8(%rip), %rdi      # 0x40aa
leaq 0x261b(%rip), %rsi      # 0x4004
leaq 0x2736(%rip), %rcx      # 0x4126
movl $0xbc, %edx
callq 0x10b0
leaq 0x26f0(%rip), %rdi      # 0x40f1
leaq 0x25fc(%rip), %rsi      # 0x4004
leaq 0x2717(%rip), %rcx      # 0x4126
movl $0xbe, %edx
callq 0x10b0
leaq 0x26e3(%rip), %rdi      # 0x4103
leaq 0x25dd(%rip), %rsi      # 0x4004
leaq 0x26f8(%rip), %rcx      # 0x4126
movl $0xbf, %edx
callq 0x10b0
leaq 0x26ce(%rip), %rdi      # 0x410d
leaq 0x25be(%rip), %rsi      # 0x4004
leaq 0x26d9(%rip), %rcx      # 0x4126
movl $0x105, %edx            # imm = 0x105
callq 0x10b0
leaq 0x26af(%rip), %rdi      # 0x410d
leaq 0x259f(%rip), %rsi      # 0x4004
leaq 0x26ba(%rip), %rcx      # 0x4126
movl $0x107, %edx            # imm = 0x107
callq 0x10b0
leaq 0x2698(%rip), %rdi      # 0x4115
leaq 0x2580(%rip), %rsi      # 0x4004
leaq 0x269b(%rip), %rcx      # 0x4126
movl $0xf4, %edx
callq 0x10b0
leaq 0x2671(%rip), %rdi      # 0x410d
leaq 0x2561(%rip), %rsi      # 0x4004
leaq 0x267c(%rip), %rcx      # 0x4126
movl $0xd4, %edx
callq 0x10b0
leaq 0x2652(%rip), %rdi      # 0x410d
leaq 0x2542(%rip), %rsi      # 0x4004
leaq 0x265d(%rip), %rcx      # 0x4126
movl $0xd6, %edx
callq 0x10b0
leaq 0x2633(%rip), %rdi      # 0x410d
leaq 0x2523(%rip), %rsi      # 0x4004
leaq 0x263e(%rip), %rcx      # 0x4126
movl $0xdd, %edx
callq 0x10b0