// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movl %ecx, -0x24(%rbp)
cmpq $0x0, -0x10(%rbp)
je 0x1950
jmp 0x196f
leaq 0x3793(%rip), %rdi      # 0x50ea
leaq 0x36e6(%rip), %rsi      # 0x5044
movl $0xbc, %edx
leaq 0x37fc(%rip), %rcx      # 0x5166
callq 0x10a0
cmpq $0x0, -0x10(%rbp)
jne 0x1982
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x1d84
movq -0x10(%rbp), %rax
cmpl $0x40, 0x8(%rax)
jae 0x198e
jmp 0x19ad
leaq 0x379c(%rip), %rdi      # 0x5131
leaq 0x36a8(%rip), %rsi      # 0x5044
movl $0xbe, %edx
leaq 0x37be(%rip), %rcx      # 0x5166
callq 0x10a0
cmpq $0x0, -0x20(%rbp)
jbe 0x19b6
jmp 0x19d5
leaq 0x3786(%rip), %rdi      # 0x5143
leaq 0x3680(%rip), %rsi      # 0x5044
movl $0xbf, %edx
leaq 0x3796(%rip), %rcx      # 0x5166
callq 0x10a0
movq -0x10(%rbp), %rdi
addq $0x18, %rdi
movq -0x10(%rbp), %rax
movl 0x8(%rax), %esi
movq -0x18(%rbp), %rdx
movq -0x20(%rbp), %rax
movl %eax, %ecx
leaq -0x28(%rbp), %r8
callq 0x1460
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
jne 0x1c5f
movq -0x10(%rbp), %rax
movl 0x8(%rax), %eax
cmpl -0x28(%rbp), %eax
jbe 0x1a53
movq -0x10(%rbp), %rdi
addq $0x58, %rdi
callq 0x1110
movl %eax, -0x30(%rbp)
cmpl $0x0, -0x30(%rbp)
jne 0x1a28
jmp 0x1a47
leaq 0x371e(%rip), %rdi      # 0x514d
leaq 0x360e(%rip), %rsi      # 0x5044
movl $0xc9, %edx
leaq 0x3724(%rip), %rcx      # 0x5166
callq 0x10a0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x1d84
movq -0x20(%rbp), %rax
movslq -0x28(%rbp), %rcx
cmpq %rcx, %rax
jbe 0x1b4c
movq -0x10(%rbp), %rax
cmpq $0x0, 0x10(%rax)
je 0x1b09
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
addq $0x58, %rdi
callq 0x11d0
movl %eax, -0x30(%rbp)
cmpl $0x0, -0x30(%rbp)
jne 0x1a8f
jmp 0x1aae
leaq 0x36b7(%rip), %rdi      # 0x514d
leaq 0x35a7(%rip), %rsi      # 0x5044
movl $0xd4, %edx
leaq 0x36bd(%rip), %rcx      # 0x5166
callq 0x10a0
movq -0x10(%rbp), %rdi
addq $0x58, %rdi
callq 0x1110
movl %eax, -0x30(%rbp)
cmpl $0x0, -0x30(%rbp)
jne 0x1ac6
jmp 0x1ae5
leaq 0x3680(%rip), %rdi      # 0x514d
leaq 0x3570(%rip), %rsi      # 0x5044
movl $0xd6, %edx
leaq 0x3686(%rip), %rcx      # 0x5166
callq 0x10a0
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
movslq -0x28(%rbp), %rax
subq %rax, %rdx
callq 0x1570
movl %eax, -0x4(%rbp)
jmp 0x1d84
movq -0x10(%rbp), %rdi
addq $0x58, %rdi
callq 0x1110
movl %eax, -0x30(%rbp)
cmpl $0x0, -0x30(%rbp)
jne 0x1b21
jmp 0x1b40
leaq 0x3625(%rip), %rdi      # 0x514d
leaq 0x3515(%rip), %rsi      # 0x5044
movl $0xdd, %edx
leaq 0x362b(%rip), %rcx      # 0x5166
callq 0x10a0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x1d84
movl $0x0, -0x34(%rbp)
movq -0x10(%rbp), %rax
movl 0xb0(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0xb0(%rax)
cmpl $0x0, -0x34(%rbp)
setne %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x1b75
jmp 0x1bee
movq -0x10(%rbp), %rax
cmpl $0x0, 0xc(%rax)
jne 0x1ba5
movl -0x24(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0xc(%rax)
movq -0x10(%rbp), %rax
movl 0xb0(%rax), %ecx
addl $-0x1, %ecx
movl %ecx, 0xb0(%rax)
movl $0x1, -0x34(%rbp)
jmp 0x1be9
movq -0x10(%rbp), %rdi
addq $0x80, %rdi
movq -0x10(%rbp), %rsi
addq $0x58, %rsi
callq 0x1080
movl %eax, -0x38(%rbp)
cmpl $0x0, -0x38(%rbp)
jne 0x1bc8
jmp 0x1be7
leaq 0x35e4(%rip), %rdi      # 0x51b3
leaq 0x346e(%rip), %rsi      # 0x5044
movl $0xf1, %edx
leaq 0x3584(%rip), %rcx      # 0x5166
callq 0x10a0
jmp 0x1be9
jmp 0x1b66
movq -0x20(%rbp), %rax
movslq -0x28(%rbp), %rcx
cmpq %rcx, %rax
jne 0x1bfd
jmp 0x1c1c
leaq 0x3551(%rip), %rdi      # 0x5155
leaq 0x3439(%rip), %rsi      # 0x5044
movl $0xf4, %edx
leaq 0x354f(%rip), %rcx      # 0x5166
callq 0x10a0
movq -0x10(%rbp), %rdi
addq $0x58, %rdi
callq 0x1110
movl %eax, -0x30(%rbp)
cmpl $0x0, -0x30(%rbp)
jne 0x1c34
jmp 0x1c53
leaq 0x3512(%rip), %rdi      # 0x514d
leaq 0x3402(%rip), %rsi      # 0x5044
movl $0xf6, %edx
leaq 0x3518(%rip), %rcx      # 0x5166
callq 0x10a0
movl $0x1, -0x4(%rbp)
jmp 0x1d84
cmpl $0x0, -0x2c(%rbp)
jge 0x1d46
movq -0x10(%rbp), %rax
cmpq $0x0, (%rax)
je 0x1d06
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rdi
addq $0x58, %rdi
callq 0x11d0
movl %eax, -0x30(%rbp)
cmpl $0x0, -0x30(%rbp)
jne 0x1c9a
jmp 0x1cb9
leaq 0x34ac(%rip), %rdi      # 0x514d
leaq 0x339c(%rip), %rsi      # 0x5044
movl $0x105, %edx            # imm = 0x105
leaq 0x34b2(%rip), %rcx      # 0x5166
callq 0x10a0
movq -0x10(%rbp), %rdi
addq $0x58, %rdi
callq 0x1110
movl %eax, -0x30(%rbp)
cmpl $0x0, -0x30(%rbp)
jne 0x1cd1
jmp 0x1cf0
leaq 0x3475(%rip), %rdi      # 0x514d
leaq 0x3365(%rip), %rsi      # 0x5044
movl $0x107, %edx            # imm = 0x107
leaq 0x347b(%rip), %rcx      # 0x5166
callq 0x10a0
movq -0x40(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
callq 0x1570
movl %eax, -0x4(%rbp)
jmp 0x1d84
movq -0x10(%rbp), %rdi
addq $0x58, %rdi
callq 0x1110
movl %eax, -0x30(%rbp)
cmpl $0x0, -0x30(%rbp)
jne 0x1d1e
jmp 0x1d3d
leaq 0x3428(%rip), %rdi      # 0x514d
leaq 0x3318(%rip), %rsi      # 0x5044
movl $0x10d, %edx            # imm = 0x10D
leaq 0x342e(%rip), %rcx      # 0x5166
callq 0x10a0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x1d84
movq -0x10(%rbp), %rdi
addq $0x58, %rdi
callq 0x1110
movl %eax, -0x30(%rbp)
cmpl $0x0, -0x30(%rbp)
jne 0x1d5e
jmp 0x1d7d
leaq 0x33e8(%rip), %rdi      # 0x514d
leaq 0x32d8(%rip), %rsi      # 0x5044
movl $0x115, %edx            # imm = 0x115
leaq 0x33ee(%rip), %rcx      # 0x5166
callq 0x10a0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl -0x4(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nopl (%rax)