// File: /workspace/github_c_makefile_star2_O3/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 0x164b
movq %rsi, %r15
movq %rdi, %rbx
movl 0x8(%rdi), %esi
cmpl $0x40, %esi
jae 0x166a
movq %rdx, %r14
testq %rdx, %rdx
je 0x1689
leaq 0x18(%rbx), %rdi
leaq 0x4(%rsp), %r8
movq %r15, %rdx
movl %r14d, %ecx
callq 0x13f8
testl %eax, %eax
je 0x150a
js 0x154e
addq $0x58, %rbx
movq %rbx, %rdi
callq 0x1110
movl %eax, %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
testl %ecx, %ecx
je 0x163f
leaq 0x2c1b(%rip), %rdi      # 0x410d
leaq 0x2b0b(%rip), %rsi      # 0x4004
leaq 0x2bb7(%rip), %rcx      # 0x40b7
movl $0xa5, %edx
callq 0x10b0
movslq 0x4(%rsp), %rax
cmpl %eax, 0x8(%rbx)
jbe 0x1585
addq $0x58, %rbx
movq %rbx, %rdi
callq 0x1110
movl %eax, %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
testl %ecx, %ecx
je 0x163f
leaq 0x2bd7(%rip), %rdi      # 0x410d
leaq 0x2ac7(%rip), %rsi      # 0x4004
leaq 0x2b73(%rip), %rcx      # 0x40b7
movl $0x64, %edx
callq 0x10b0
movq (%rbx), %r12
testq %r12, %r12
je 0x15ee
leaq 0x58(%r12), %rdi
callq 0x11d0
testl %eax, %eax
jne 0x16a8
addq $0x58, %rbx
movq %rbx, %rdi
callq 0x1110
testl %eax, %eax
jne 0x16c7
movq %r12, %rdi
jmp 0x15c5
cmpq %r14, %rax
jae 0x15d2
movq 0x10(%rbx), %rdi
testq %rdi, %rdi
je 0x1624
addq $0x58, %rdi
callq 0x11d0
testl %eax, %eax
jne 0x1724
leaq 0x58(%rbx), %rdi
callq 0x1110
testl %eax, %eax
jne 0x1743
movq 0x10(%rbx), %rdi
movslq 0x4(%rsp), %rax
subq %rax, %r14
movq %r15, %rsi
movq %r14, %rdx
callq 0x1487
jmp 0x163f
jne 0x16e6
leaq 0x58(%rbx), %rdi
callq 0x1110
testl %eax, %eax
jne 0x1705
movl 0xc(%rbx), %eax
jmp 0x163f
addq $0x58, %rbx
movq %rbx, %rdi
callq 0x1110
movl %eax, %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
testl %ecx, %ecx
je 0x163f
leaq 0x2b01(%rip), %rdi      # 0x410d
leaq 0x29f1(%rip), %rsi      # 0x4004
leaq 0x2a9d(%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 0x1762
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq
leaq 0x2a58(%rip), %rdi      # 0x40aa
leaq 0x29ab(%rip), %rsi      # 0x4004
leaq 0x2a57(%rip), %rcx      # 0x40b7
movl $0x54, %edx
callq 0x10b0
leaq 0x2a80(%rip), %rdi      # 0x40f1
leaq 0x298c(%rip), %rsi      # 0x4004
leaq 0x2a38(%rip), %rcx      # 0x40b7
movl $0x57, %edx
callq 0x10b0
leaq 0x2a73(%rip), %rdi      # 0x4103
leaq 0x296d(%rip), %rsi      # 0x4004
leaq 0x2a19(%rip), %rcx      # 0x40b7
movl $0x59, %edx
callq 0x10b0
leaq 0x2a5e(%rip), %rdi      # 0x410d
leaq 0x294e(%rip), %rsi      # 0x4004
leaq 0x29fa(%rip), %rcx      # 0x40b7
movl $0x93, %edx
callq 0x10b0
leaq 0x2a3f(%rip), %rdi      # 0x410d
leaq 0x292f(%rip), %rsi      # 0x4004
leaq 0x29db(%rip), %rcx      # 0x40b7
movl $0x95, %edx
callq 0x10b0
leaq 0x2a28(%rip), %rdi      # 0x4115
leaq 0x2910(%rip), %rsi      # 0x4004
leaq 0x29bc(%rip), %rcx      # 0x40b7
movl $0x82, %edx
callq 0x10b0
leaq 0x2a01(%rip), %rdi      # 0x410d
leaq 0x28f1(%rip), %rsi      # 0x4004
leaq 0x299d(%rip), %rcx      # 0x40b7
movl $0x84, %edx
callq 0x10b0
leaq 0x29e2(%rip), %rdi      # 0x410d
leaq 0x28d2(%rip), %rsi      # 0x4004
leaq 0x297e(%rip), %rcx      # 0x40b7
movl $0x70, %edx
callq 0x10b0
leaq 0x29c3(%rip), %rdi      # 0x410d
leaq 0x28b3(%rip), %rsi      # 0x4004
leaq 0x295f(%rip), %rcx      # 0x40b7
movl $0x73, %edx
callq 0x10b0
leaq 0x29a4(%rip), %rdi      # 0x410d
leaq 0x2894(%rip), %rsi      # 0x4004
leaq 0x2940(%rip), %rcx      # 0x40b7
movl $0x7b, %edx
callq 0x10b0