// File: /workspace/github_c_makefile_star2_O1/stevenmherring[P]DNS/fine-trie.c
// Function: search
int32_t _search (struct trie_node *node, const char *string, size_t strlen) {
    //This function is always entered by one of the _search functions, this is the HEAVY LIFTER of the searchers.
    int keylen, cmp;
    int rc;
    // First things first, check if we are NULL
    // NULL node means something screwed up somewhere.
    assert(node != NULL);
    if (node == NULL) return -1;
    //Strlen must be below 64 characters, this is just a standard of the application
    assert(node->strlen < 64);
    //Similar to the strlen being below 64 it must be greater than 0
    assert(strlen >0);
    //Check for substring, we start by comparing the sizes.
    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)
      {
        //Unlock the lock we previously acquired in a controller search function
        int rc = pthread_mutex_unlock(&(node->lock));
        assert(rc == 0);
        //return fail code
        return -1;
      }
      else if (strlen > keylen)
      {
        if(node->children != NULL)
        {
          // first lock children, then unlock parent to prevent possible
          // hand over hand
          //acquire childs lock, assert gaurantees we got it or terminates
          rc = pthread_mutex_lock(&(node->children->lock));
          assert(rc == 0);
          //unlock the current nodes lock, this is us moving forward on the search
          rc = pthread_mutex_unlock(&(node->lock));
          assert(rc == 0);
          //Recur into the child
          return _search(node->children, string, strlen - keylen);
        }
        else
        {
          //if there are no children release the lock, return fail code
          rc = pthread_mutex_unlock(&(node->lock));
          assert(rc == 0);
          return -1;
        }
      }
      else
      {
        //strlen should be the same length as keylen, assert it, return the IP address of the node
        assert (strlen == keylen);
        rc = pthread_mutex_unlock(&(node->lock));
        assert(rc == 0);
        return node->ip4_address;
      }
    }
    else if (cmp < 0)
    {
      //cmp returns 0 then we are looking next to us for the node
      if(node->next != NULL)
      {
        // No, look right (the node's key is "less" than the search key)
        // hand over hand
        //assign the next node
        struct trie_node *next = node->next;
        //get its lock, release the lock of current node
        rc = pthread_mutex_lock(&(next->lock));
        assert(rc == 0);
        rc = pthread_mutex_unlock(&(node->lock));
        assert(rc == 0);
        //recur the search through the next node
        return _search(next, string, strlen);
      }
      else
      {
        //not found release lock, return fail.
        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 %r15
pushq %r14
pushq %r12
pushq %rbx
pushq %rax
testq %rdi, %rdi
je 0x164d
movq %rsi, %r15
movq %rdi, %rbx
movl 0x8(%rdi), %esi
cmpl $0x40, %esi
jae 0x166c
movq %rdx, %r14
testq %rdx, %rdx
je 0x168b
leaq 0x18(%rbx), %rdi
leaq 0x4(%rsp), %r8
movq %r15, %rdx
movl %r14d, %ecx
callq 0x13fa
testl %eax, %eax
je 0x150c
js 0x1550
addq $0x58, %rbx
movq %rbx, %rdi
callq 0x1110
movl %eax, %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
testl %ecx, %ecx
je 0x1641
leaq 0x2c19(%rip), %rdi      # 0x410d
leaq 0x2b09(%rip), %rsi      # 0x4004
leaq 0x2bb5(%rip), %rcx      # 0x40b7
movl $0xa5, %edx
callq 0x10b0
movslq 0x4(%rsp), %rax
cmpl %eax, 0x8(%rbx)
jbe 0x1587
addq $0x58, %rbx
movq %rbx, %rdi
callq 0x1110
movl %eax, %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
testl %ecx, %ecx
je 0x1641
leaq 0x2bd5(%rip), %rdi      # 0x410d
leaq 0x2ac5(%rip), %rsi      # 0x4004
leaq 0x2b71(%rip), %rcx      # 0x40b7
movl $0x64, %edx
callq 0x10b0
movq (%rbx), %r12
testq %r12, %r12
je 0x15f0
leaq 0x58(%r12), %rdi
callq 0x11d0
testl %eax, %eax
jne 0x16aa
addq $0x58, %rbx
movq %rbx, %rdi
callq 0x1110
testl %eax, %eax
jne 0x16c9
movq %r12, %rdi
jmp 0x15c7
cmpq %r14, %rax
jae 0x15d4
movq 0x10(%rbx), %rdi
testq %rdi, %rdi
je 0x1626
addq $0x58, %rdi
callq 0x11d0
testl %eax, %eax
jne 0x1726
leaq 0x58(%rbx), %rdi
callq 0x1110
testl %eax, %eax
jne 0x1745
movq 0x10(%rbx), %rdi
movslq 0x4(%rsp), %rax
subq %rax, %r14
movq %r15, %rsi
movq %r14, %rdx
callq 0x1489
jmp 0x1641
jne 0x16e8
leaq 0x58(%rbx), %rdi
callq 0x1110
testl %eax, %eax
jne 0x1707
movl 0xc(%rbx), %eax
jmp 0x1641
addq $0x58, %rbx
movq %rbx, %rdi
callq 0x1110
movl %eax, %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
testl %ecx, %ecx
je 0x1641
leaq 0x2aff(%rip), %rdi      # 0x410d
leaq 0x29ef(%rip), %rsi      # 0x4004
leaq 0x2a9b(%rip), %rcx      # 0x40b7
movl $0x9d, %edx
callq 0x10b0
addq $0x58, %rbx
movq %rbx, %rdi
callq 0x1110
movl %eax, %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
testl %ecx, %ecx
jne 0x1764
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq
leaq 0x2a56(%rip), %rdi      # 0x40aa
leaq 0x29a9(%rip), %rsi      # 0x4004
leaq 0x2a55(%rip), %rcx      # 0x40b7
movl $0x54, %edx
callq 0x10b0
leaq 0x2a7e(%rip), %rdi      # 0x40f1
leaq 0x298a(%rip), %rsi      # 0x4004
leaq 0x2a36(%rip), %rcx      # 0x40b7
movl $0x57, %edx
callq 0x10b0
leaq 0x2a71(%rip), %rdi      # 0x4103
leaq 0x296b(%rip), %rsi      # 0x4004
leaq 0x2a17(%rip), %rcx      # 0x40b7
movl $0x59, %edx
callq 0x10b0
leaq 0x2a5c(%rip), %rdi      # 0x410d
leaq 0x294c(%rip), %rsi      # 0x4004
leaq 0x29f8(%rip), %rcx      # 0x40b7
movl $0x93, %edx
callq 0x10b0
leaq 0x2a3d(%rip), %rdi      # 0x410d
leaq 0x292d(%rip), %rsi      # 0x4004
leaq 0x29d9(%rip), %rcx      # 0x40b7
movl $0x95, %edx
callq 0x10b0
leaq 0x2a26(%rip), %rdi      # 0x4115
leaq 0x290e(%rip), %rsi      # 0x4004
leaq 0x29ba(%rip), %rcx      # 0x40b7
movl $0x82, %edx
callq 0x10b0
leaq 0x29ff(%rip), %rdi      # 0x410d
leaq 0x28ef(%rip), %rsi      # 0x4004
leaq 0x299b(%rip), %rcx      # 0x40b7
movl $0x84, %edx
callq 0x10b0
leaq 0x29e0(%rip), %rdi      # 0x410d
leaq 0x28d0(%rip), %rsi      # 0x4004
leaq 0x297c(%rip), %rcx      # 0x40b7
movl $0x70, %edx
callq 0x10b0
leaq 0x29c1(%rip), %rdi      # 0x410d
leaq 0x28b1(%rip), %rsi      # 0x4004
leaq 0x295d(%rip), %rcx      # 0x40b7
movl $0x73, %edx
callq 0x10b0
leaq 0x29a2(%rip), %rdi      # 0x410d
leaq 0x2892(%rip), %rsi      # 0x4004
leaq 0x293e(%rip), %rcx      # 0x40b7
movl $0x7b, %edx
callq 0x10b0