// File: /workspace/github_c_makefile_star2_O2/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 0x160d
movq %rsi, %r15
movq %rdi, %rbx
movl 0x8(%rdi), %esi
cmpl $0x40, %esi
jae 0x1626
movq %rdx, %r14
testq %rdx, %rdx
je 0x163f
leaq 0x18(%rbx), %rdi
leaq 0x4(%rsp), %r8
movq %r15, %rdx
movl %r14d, %ecx
callq 0x13dd
testl %eax, %eax
je 0x14e1
js 0x151b
addq $0x58, %rbx
movq %rbx, %rdi
callq 0x1110
testl %eax, %eax
je 0x15fe
leaq 0x2c44(%rip), %rdi      # 0x410d
leaq 0x2b34(%rip), %rsi      # 0x4004
leaq 0x2be0(%rip), %rcx      # 0x40b7
movl $0xa5, %edx
callq 0x10b0
movslq 0x4(%rsp), %rax
cmpl %eax, 0x8(%rbx)
jbe 0x1552
addq $0x58, %rbx
movq %rbx, %rdi
callq 0x1110
testl %eax, %eax
je 0x15fe
leaq 0x2c07(%rip), %rdi      # 0x410d
leaq 0x2af7(%rip), %rsi      # 0x4004
leaq 0x2ba3(%rip), %rcx      # 0x40b7
pushq $0x64
jmp 0x1656
movq (%rbx), %r12
testq %r12, %r12
je 0x15bb
leaq 0x58(%r12), %rdi
callq 0x11d0
testl %eax, %eax
jne 0x165c
addq $0x58, %rbx
movq %rbx, %rdi
callq 0x1110
testl %eax, %eax
jne 0x167b
movq %r12, %rdi
jmp 0x1592
cmpq %r14, %rax
jae 0x159f
movq 0x10(%rbx), %rdi
testq %rdi, %rdi
je 0x15ea
addq $0x58, %rdi
callq 0x11d0
testl %eax, %eax
jne 0x16d8
leaq 0x58(%rbx), %rdi
callq 0x1110
testl %eax, %eax
jne 0x16f4
movq 0x10(%rbx), %rdi
movslq 0x4(%rsp), %rax
subq %rax, %r14
movq %r15, %rsi
movq %r14, %rdx
callq 0x1465
jmp 0x1601
jne 0x169a
leaq 0x58(%rbx), %rdi
callq 0x1110
testl %eax, %eax
jne 0x16b9
movl 0xc(%rbx), %eax
jmp 0x1601
addq $0x58, %rbx
movq %rbx, %rdi
callq 0x1110
testl %eax, %eax
je 0x15fe
leaq 0x2b3b(%rip), %rdi      # 0x410d
leaq 0x2a2b(%rip), %rsi      # 0x4004
leaq 0x2ad7(%rip), %rcx      # 0x40b7
movl $0x9d, %edx
callq 0x10b0
addq $0x58, %rbx
movq %rbx, %rdi
callq 0x1110
testl %eax, %eax
jne 0x1710
pushq $-0x1
popq %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq
leaq 0x2a96(%rip), %rdi      # 0x40aa
leaq 0x29e9(%rip), %rsi      # 0x4004
leaq 0x2a95(%rip), %rcx      # 0x40b7
pushq $0x54
jmp 0x1656
leaq 0x2ac4(%rip), %rdi      # 0x40f1
leaq 0x29d0(%rip), %rsi      # 0x4004
leaq 0x2a7c(%rip), %rcx      # 0x40b7
pushq $0x57
jmp 0x1656
leaq 0x2abd(%rip), %rdi      # 0x4103
leaq 0x29b7(%rip), %rsi      # 0x4004
leaq 0x2a63(%rip), %rcx      # 0x40b7
pushq $0x59
popq %rdx
callq 0x10b0
leaq 0x2aaa(%rip), %rdi      # 0x410d
leaq 0x299a(%rip), %rsi      # 0x4004
leaq 0x2a46(%rip), %rcx      # 0x40b7
movl $0x93, %edx
callq 0x10b0
leaq 0x2a8b(%rip), %rdi      # 0x410d
leaq 0x297b(%rip), %rsi      # 0x4004
leaq 0x2a27(%rip), %rcx      # 0x40b7
movl $0x95, %edx
callq 0x10b0
leaq 0x2a74(%rip), %rdi      # 0x4115
leaq 0x295c(%rip), %rsi      # 0x4004
leaq 0x2a08(%rip), %rcx      # 0x40b7
movl $0x82, %edx
callq 0x10b0
leaq 0x2a4d(%rip), %rdi      # 0x410d
leaq 0x293d(%rip), %rsi      # 0x4004
leaq 0x29e9(%rip), %rcx      # 0x40b7
movl $0x84, %edx
callq 0x10b0
leaq 0x2a2e(%rip), %rdi      # 0x410d
leaq 0x291e(%rip), %rsi      # 0x4004
leaq 0x29ca(%rip), %rcx      # 0x40b7
pushq $0x70
jmp 0x1656
leaq 0x2a12(%rip), %rdi      # 0x410d
leaq 0x2902(%rip), %rsi      # 0x4004
leaq 0x29ae(%rip), %rcx      # 0x40b7
pushq $0x73
jmp 0x1656
leaq 0x29f6(%rip), %rdi      # 0x410d
leaq 0x28e6(%rip), %rsi      # 0x4004
leaq 0x2992(%rip), %rcx      # 0x40b7
pushq $0x7b
jmp 0x1656