// File: /workspace/github_c_makefile_star2_O0/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 %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
cmpq $0x0, -0x10(%rbp)
je 0x158d
jmp 0x15ac
leaq 0x3b56(%rip), %rdi      # 0x50ea
leaq 0x3aa9(%rip), %rsi      # 0x5044
movl $0x54, %edx
leaq 0x3b50(%rip), %rcx      # 0x50f7
callq 0x10a0
cmpq $0x0, -0x10(%rbp)
jne 0x15bf
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x1922
movq -0x10(%rbp), %rax
cmpl $0x40, 0x8(%rax)
jae 0x15cb
jmp 0x15ea
leaq 0x3b5f(%rip), %rdi      # 0x5131
leaq 0x3a6b(%rip), %rsi      # 0x5044
movl $0x57, %edx
leaq 0x3b12(%rip), %rcx      # 0x50f7
callq 0x10a0
cmpq $0x0, -0x20(%rbp)
jbe 0x15f3
jmp 0x1612
leaq 0x3b49(%rip), %rdi      # 0x5143
leaq 0x3a43(%rip), %rsi      # 0x5044
movl $0x59, %edx
leaq 0x3aea(%rip), %rcx      # 0x50f7
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 -0x24(%rbp), %r8
callq 0x1460
movl %eax, -0x28(%rbp)
cmpl $0x0, -0x28(%rbp)
jne 0x17fd
movq -0x10(%rbp), %rax
movl 0x8(%rax), %eax
cmpl -0x24(%rbp), %eax
jbe 0x1690
movq -0x10(%rbp), %rdi
addq $0x58, %rdi
callq 0x1110
movl %eax, -0x30(%rbp)
cmpl $0x0, -0x30(%rbp)
jne 0x1665
jmp 0x1684
leaq 0x3ae1(%rip), %rdi      # 0x514d
leaq 0x39d1(%rip), %rsi      # 0x5044
movl $0x64, %edx
leaq 0x3a78(%rip), %rcx      # 0x50f7
callq 0x10a0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x1922
movq -0x20(%rbp), %rax
movslq -0x24(%rbp), %rcx
cmpq %rcx, %rax
jbe 0x1789
movq -0x10(%rbp), %rax
cmpq $0x0, 0x10(%rax)
je 0x1746
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
addq $0x58, %rdi
callq 0x11d0
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
jne 0x16cc
jmp 0x16eb
leaq 0x3a7a(%rip), %rdi      # 0x514d
leaq 0x396a(%rip), %rsi      # 0x5044
movl $0x70, %edx
leaq 0x3a11(%rip), %rcx      # 0x50f7
callq 0x10a0
movq -0x10(%rbp), %rdi
addq $0x58, %rdi
callq 0x1110
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
jne 0x1703
jmp 0x1722
leaq 0x3a43(%rip), %rdi      # 0x514d
leaq 0x3933(%rip), %rsi      # 0x5044
movl $0x73, %edx
leaq 0x39da(%rip), %rcx      # 0x50f7
callq 0x10a0
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
movslq -0x24(%rbp), %rax
subq %rax, %rdx
callq 0x1570
movl %eax, -0x4(%rbp)
jmp 0x1922
movq -0x10(%rbp), %rdi
addq $0x58, %rdi
callq 0x1110
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
jne 0x175e
jmp 0x177d
leaq 0x39e8(%rip), %rdi      # 0x514d
leaq 0x38d8(%rip), %rsi      # 0x5044
movl $0x7b, %edx
leaq 0x397f(%rip), %rcx      # 0x50f7
callq 0x10a0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x1922
movq -0x20(%rbp), %rax
movslq -0x24(%rbp), %rcx
cmpq %rcx, %rax
jne 0x1798
jmp 0x17b7
leaq 0x39b6(%rip), %rdi      # 0x5155
leaq 0x389e(%rip), %rsi      # 0x5044
movl $0x82, %edx
leaq 0x3945(%rip), %rcx      # 0x50f7
callq 0x10a0
movq -0x10(%rbp), %rdi
addq $0x58, %rdi
callq 0x1110
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
jne 0x17cf
jmp 0x17ee
leaq 0x3977(%rip), %rdi      # 0x514d
leaq 0x3867(%rip), %rsi      # 0x5044
movl $0x84, %edx
leaq 0x390e(%rip), %rcx      # 0x50f7
callq 0x10a0
movq -0x10(%rbp), %rax
movl 0xc(%rax), %eax
movl %eax, -0x4(%rbp)
jmp 0x1922
cmpl $0x0, -0x28(%rbp)
jge 0x18e4
movq -0x10(%rbp), %rax
cmpq $0x0, (%rax)
je 0x18a4
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rdi
addq $0x58, %rdi
callq 0x11d0
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
jne 0x1838
jmp 0x1857
leaq 0x390e(%rip), %rdi      # 0x514d
leaq 0x37fe(%rip), %rsi      # 0x5044
movl $0x93, %edx
leaq 0x38a5(%rip), %rcx      # 0x50f7
callq 0x10a0
movq -0x10(%rbp), %rdi
addq $0x58, %rdi
callq 0x1110
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
jne 0x186f
jmp 0x188e
leaq 0x38d7(%rip), %rdi      # 0x514d
leaq 0x37c7(%rip), %rsi      # 0x5044
movl $0x95, %edx
leaq 0x386e(%rip), %rcx      # 0x50f7
callq 0x10a0
movq -0x38(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
callq 0x1570
movl %eax, -0x4(%rbp)
jmp 0x1922
movq -0x10(%rbp), %rdi
addq $0x58, %rdi
callq 0x1110
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
jne 0x18bc
jmp 0x18db
leaq 0x388a(%rip), %rdi      # 0x514d
leaq 0x377a(%rip), %rsi      # 0x5044
movl $0x9d, %edx
leaq 0x3821(%rip), %rcx      # 0x50f7
callq 0x10a0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x1922
movq -0x10(%rbp), %rdi
addq $0x58, %rdi
callq 0x1110
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
jne 0x18fc
jmp 0x191b
leaq 0x384a(%rip), %rdi      # 0x514d
leaq 0x373a(%rip), %rsi      # 0x5044
movl $0xa5, %edx
leaq 0x37e1(%rip), %rcx      # 0x50f7
callq 0x10a0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl -0x4(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nopl (%rax,%rax)