// File: /workspace/github_cpp_makefile_star2_O3/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, %r15
movq %rdx, %r12
movl %esi, %r14d
movq %rdi, %r13
movl $0xff, %edi
callq 0xa4e0
movq %rax, -0x48(%rbp)
testq %rax, %rax
je 0x48ccc
movq %rax, %rbx
leaq -0x48(%rbp), %rcx
movq %r13, %rdi
movl %r14d, %esi
movq %r12, %rdx
movl $0xff, %r8d
callq 0x49024
cmpl $-0x22, %eax
jne 0x48bb9
movq %rbx, %rdi
callq 0xa480
movl $0x22, %eax
jmp 0x48dfb
movl %r14d, -0x3c(%rbp)
movslq %eax, %rdi
callq 0xa4e0
movq %rax, -0x50(%rbp)
testq %rax, %rax
je 0x48cc4
movq %rax, %r14
movq %rbx, %rdi
callq 0xa180
leaq -0x50(%rbp), %rdi
movq %rbx, %rsi
movl %eax, %edx
callq 0x48f8c
movq %r15, -0x38(%rbp)
movq %r15, %rdi
movq %r14, %rsi
callq 0x393ba
movl %eax, -0x2c(%rbp)
movq %r14, %rdi
callq 0xa480
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), %r15d
orq %rdi, %r15
leal 0x9(%rcx), %esi
movl %esi, (%r12)
movzbl (%r13,%rdx), %edx
shll $0x8, %edx
addl $0xa, %ecx
movl %ecx, (%r12)
movzbl (%r13,%rsi), %r14d
orl %edx, %r14d
cmpl $-0x1, -0x2c(%rbp)
je 0x48cd6
cmpl $0x1, %eax
jne 0x48cd6
movq -0x38(%rbp), %rdi
movl -0x2c(%rbp), %esi
callq 0x39132
movq %r13, %rdi
movq %r12, %rdx
movq %rax, %rcx
movq %r15, %r8
movl %r14d, %r9d
callq 0x48efa
jmp 0x48dee
movq %rbx, %rdi
callq 0xa480
movl $0x27, %eax
jmp 0x48dfb
cmpl $-0x1, -0x2c(%rbp)
je 0x48de7
cmpl $0x5, %eax
jne 0x48de7
movq -0x38(%rbp), %rdi
movl -0x2c(%rbp), %esi
callq 0x39132
movb $0x0, 0x108(%rax)
movq -0x38(%rbp), %rdi
movl -0x2c(%rbp), %esi
callq 0x39132
movq %r15, 0x100(%rax)
movl (%r12), %eax
leaq -0x54(%rbp), %rdx
movl %eax, (%rdx)
addl %r14d, %eax
movl %eax, (%r12)
leaq -0x48(%rbp), %rcx
movq %r13, %rdi
movl -0x3c(%rbp), %esi
movl %r14d, %r8d
callq 0x49024
cmpl $-0x22, %eax
je 0x48ba7
movl %eax, %r14d
leaq -0x178(%rbp), %r15
movq %r15, %rdi
callq 0x3b03c
leaq -0x60(%rbp), %rdi
movq %r15, (%rdi)
decl %r14d
movq %rbx, %rsi
movl %r14d, %edx
callq 0x48f8c
movq -0x38(%rbp), %r14
movq %r14, %rdi
movq %r15, %rsi
callq 0x39230
movq %r14, %r15
cmpl $-0x1, %eax
jne 0x48dba
leaq -0x290(%rbp), %r14
leaq -0x178(%rbp), %rsi
movl $0x118, %edx            # imm = 0x118
movq %r14, %rdi
callq 0xa2f0
movq %r15, %rdi
movq %r14, %rsi
callq 0x39146
leaq -0x290(%rbp), %rdi
callq 0x3b132
leaq -0x178(%rbp), %rsi
movq %r15, %rdi
callq 0x39230
movq %r15, %rdi
movl %eax, %esi
callq 0x39132
movq %rax, %r14
movq %r15, %rdi
movl -0x2c(%rbp), %esi
callq 0x39132
movq %r14, 0x110(%rax)
leaq -0x178(%rbp), %rdi
callq 0x3b132
jmp 0x48dee
addl %ecx, %r14d
movl %r14d, (%r12)
movq %rbx, %rdi
callq 0xa480
movl $0x20, %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 0x3b132
jmp 0x48e21
movq %rax, %rbx
leaq -0x178(%rbp), %rdi
callq 0x3b132
movq %rbx, %rdi
callq 0xa6d0
nop