// File: /workspace/github_cpp_makefile_star2_O1/madpilot[P]mDNSResolver/src/Answer.cpp
// Function: mDNSResolver::Answer::resolve(unsigned char*, unsigned int, unsigned int*, mDNSResolver::Cache&)
MDNS_RESULT Answer::resolve(unsigned char *buffer, unsigned int len, unsigned int* offset, Cache& cache) {
    char* assembled = (char *)malloc(sizeof(char) * MDNS_MAX_NAME_LEN);
    if(assembled == NULL) {
      return E_MDNS_OUT_OF_MEMORY;
    }
    int nameLen = Answer::assembleName(buffer, len, offset, &assembled);

    if(nameLen == -1 * E_MDNS_POINTER_OVERFLOW) {
      free(assembled);
      return -1 * nameLen;
    }

    char *name = (char *)malloc(sizeof(char) * nameLen);
    if(name == NULL) {
      free(assembled);
      return E_MDNS_OUT_OF_MEMORY;
    }
    parseName(&name, assembled, strlen(assembled));
    int cacheIndex = cache.search(name);
    free(name);

    unsigned int type = (buffer[(*offset)++] << 8) + buffer[(*offset)++];
    unsigned int aclass = (buffer[(*offset)++] << 8) + buffer[(*offset)++];
    unsigned long ttl = (buffer[(*offset)++] << 24) + (buffer[(*offset)++] << 16) + (buffer[(*offset)++] << 8) + buffer[(*offset)++];
    unsigned int dataLen = (buffer[(*offset)++] << 8) + buffer[(*offset)++];

    if(type == MDNS_A_RECORD && cacheIndex != -1) {
      resolveAName(buffer, len, offset, cache[cacheIndex], ttl, dataLen);
    } else if(type == MDNS_CNAME_RECORD && cacheIndex != -1) {
      cache[cacheIndex].resolved = false;
      cache[cacheIndex].ttl = ttl;

      unsigned int dataOffset = (*offset);
      (*offset) += dataLen;
      dataLen = Answer::assembleName(buffer, len, &dataOffset, &assembled, dataLen);

      if(dataLen == -1 * E_MDNS_POINTER_OVERFLOW) {
        free(assembled);
        return -1 * dataLen;
      }

      Response r;
      char* responseName = &*r.name;
      parseName(&responseName, assembled, dataLen - 1);
      int cnameIndex = cache.search(r);

      if(cnameIndex == -1) {
        cache.insert(r);
        cnameIndex = cache.search(r);
      }

      cache[cacheIndex].cname = &cache[cnameIndex];
    } else {
      // Not an A record or a CNAME. Ignore.
      (*offset) += dataLen;
    }

    free(assembled);
    return E_MDNS_OK;
  }

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x268, %rsp            # imm = 0x268
movq %rcx, %r14
movq %rdx, %r12
movl %esi, %ebx
movq %rdi, %r13
movl $0xff, %edi
callq 0xb4e0
movq %rax, -0x40(%rbp)
testq %rax, %rax
je 0x49eb9
movq %rax, %r15
leaq -0x40(%rbp), %rcx
movq %r13, %rdi
movl %ebx, %esi
movq %r12, %rdx
movl $0xff, %r8d
callq 0x4a33c
cmpl $-0x22, %eax
jne 0x49ec3
movq %r15, %rdi
callq 0xb480
movl $0x22, %ebx
jmp 0x4a10c
movl $0x27, %ebx
jmp 0x4a10c
movl %ebx, -0x44(%rbp)
movslq %eax, %rdi
callq 0xb4e0
movq %rax, -0x50(%rbp)
testq %rax, %rax
je 0x49fcc
movq %rax, %rbx
movq %r15, %rdi
callq 0xb180
leaq -0x50(%rbp), %rdi
movq %r15, %rsi
movl %eax, %edx
callq 0x4a29e
movq %r14, -0x38(%rbp)
movq %r14, %rdi
movq %rbx, %rsi
callq 0x3a652
movl %eax, -0x2c(%rbp)
movq %rbx, %rdi
callq 0xb480
movl (%r12), %ecx
leal 0x1(%rcx), %eax
movl %eax, (%r12)
movzbl (%r13,%rcx), %edx
shll $0x8, %edx
leal 0x2(%rcx), %esi
movl %esi, (%r12)
movzbl (%r13,%rax), %eax
orl %edx, %eax
leal 0x4(%rcx), %edx
leal 0x5(%rcx), %esi
movl %esi, (%r12)
movzbl (%r13,%rdx), %edx
shll $0x18, %edx
leal 0x6(%rcx), %edi
movl %edi, (%r12)
movzbl (%r13,%rsi), %esi
shll $0x10, %esi
movslq %edx, %rdx
orq %rsi, %rdx
leal 0x7(%rcx), %esi
movl %esi, (%r12)
movzbl (%r13,%rdi), %edi
shll $0x8, %edi
orq %rdx, %rdi
leal 0x8(%rcx), %edx
movl %edx, (%r12)
movzbl (%r13,%rsi), %r14d
orq %rdi, %r14
leal 0x9(%rcx), %esi
movl %esi, (%r12)
movzbl (%r13,%rdx), %edx
shll $0x8, %edx
addl $0xa, %ecx
movl %ecx, (%r12)
movzbl (%r13,%rsi), %ebx
orl %edx, %ebx
cmpl $-0x1, -0x2c(%rbp)
je 0x49fd6
cmpl $0x1, %eax
jne 0x49fd6
movq -0x38(%rbp), %rdi
movl -0x2c(%rbp), %esi
callq 0x3a3ae
movq %r13, %rdi
movq %r12, %rdx
movq %rax, %rcx
movq %r14, %r8
movl %ebx, %r9d
callq 0x4a20c
jmp 0x4a0fb
movl $0x27, %ebx
jmp 0x4a104
cmpl $-0x1, -0x2c(%rbp)
je 0x4a03e
cmpl $0x5, %eax
jne 0x4a03e
movq -0x38(%rbp), %rdi
movl -0x2c(%rbp), %esi
callq 0x3a3ae
movb $0x0, 0x108(%rax)
movq -0x38(%rbp), %rdi
movl -0x2c(%rbp), %esi
callq 0x3a3ae
movq %r14, 0x100(%rax)
movl (%r12), %eax
leaq -0x54(%rbp), %rdx
movl %eax, (%rdx)
addl %ebx, %eax
movl %eax, (%r12)
leaq -0x40(%rbp), %rcx
movq %r13, %rdi
movl -0x44(%rbp), %esi
movl %ebx, %r8d
callq 0x4a33c
movl %eax, %r14d
cmpl $-0x22, %eax
jne 0x4a049
movq %r15, %rdi
callq 0xb480
jmp 0x4a0f0
addl %ecx, %ebx
movl %ebx, (%r12)
jmp 0x4a0fb
leaq -0x178(%rbp), %rbx
movq %rbx, %rdi
callq 0x3c2d8
leaq -0x60(%rbp), %rdi
movq %rbx, (%rdi)
leal -0x1(%r14), %edx
movq %r15, %rsi
callq 0x4a29e
movq -0x38(%rbp), %rdi
movq %rbx, %rsi
callq 0x3a4b6
cmpl $-0x1, %eax
jne 0x4a0c1
leaq -0x290(%rbp), %rbx
leaq -0x178(%rbp), %rsi
movl $0x118, %edx            # imm = 0x118
movq %rbx, %rdi
callq 0xb2f0
movq -0x38(%rbp), %r15
movq %r15, %rdi
movq %rbx, %rsi
callq 0x3a3c2
leaq -0x290(%rbp), %rdi
callq 0x3c3ce
leaq -0x178(%rbp), %rsi
movq %r15, %rdi
callq 0x3a4b6
movq -0x38(%rbp), %r15
movq %r15, %rdi
movl %eax, %esi
callq 0x3a3ae
movq %rax, %rbx
movq %r15, %rdi
movl -0x2c(%rbp), %esi
callq 0x3a3ae
movq %rbx, 0x110(%rax)
leaq -0x178(%rbp), %rdi
callq 0x3c3ce
movl $0x22, %ebx
cmpl $-0x22, %r14d
je 0x4a10c
movl $0x20, %ebx
movq -0x40(%rbp), %r15
movq %r15, %rdi
callq 0xb480
movl %ebx, %eax
addq $0x268, %rsp            # imm = 0x268
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq -0x290(%rbp), %rdi
callq 0x3c3ce
jmp 0x4a134
movq %rax, %rbx
leaq -0x178(%rbp), %rdi
callq 0x3c3ce
movq %rbx, %rdi
callq 0xb6d0