// File: /workspace/github_cpp_makefile_star2_O2/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 0xa4f0
movq %rax, -0x48(%rbp)
testq %rax, %rax
je 0x3972d
movq %rax, %rbx
leaq -0x48(%rbp), %rcx
movq %r13, %rdi
movl %r14d, %esi
movq %r12, %rdx
callq 0x399c8
cmpl $-0x22, %eax
jne 0x3961a
movq %rbx, %rdi
callq 0xa480
pushq $0x22
jmp 0x39856
movl %r14d, -0x3c(%rbp)
movslq %eax, %rdi
callq 0xa4f0
movq %rax, -0x50(%rbp)
testq %rax, %rax
je 0x39725
movq %rax, %r14
movq %rbx, %rdi
callq 0xa1b0
leaq -0x50(%rbp), %rdi
movq %rbx, %rsi
movl %eax, %edx
callq 0x399d8
movq %r15, -0x38(%rbp)
movq %r15, %rdi
movq %r14, %rsi
callq 0x2ea7a
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 0x39734
cmpl $0x1, %eax
jne 0x39734
movq -0x38(%rbp), %rdi
movl -0x2c(%rbp), %esi
callq 0x2e916
movq %r13, %rdi
movq %r12, %rdx
movq %rax, %rcx
movq %r15, %r8
movl %r14d, %r9d
callq 0x39948
jmp 0x3984c
movq %rbx, %rdi
callq 0xa480
pushq $0x27
jmp 0x39856
cmpl $-0x1, -0x2c(%rbp)
je 0x39845
cmpl $0x5, %eax
jne 0x39845
movq -0x38(%rbp), %rdi
movl -0x2c(%rbp), %esi
callq 0x2e916
movb $0x0, 0x108(%rax)
movq -0x38(%rbp), %rdi
movl -0x2c(%rbp), %esi
callq 0x2e916
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 0x39a78
cmpl $-0x22, %eax
je 0x3960b
movl %eax, %r14d
leaq -0x178(%rbp), %r15
movq %r15, %rdi
callq 0x2fd48
leaq -0x60(%rbp), %rdi
movq %r15, (%rdi)
decl %r14d
movq %rbx, %rsi
movl %r14d, %edx
callq 0x399d8
movq -0x38(%rbp), %r14
movq %r14, %rdi
movq %r15, %rsi
callq 0x2e9a8
movq %r14, %r15
cmpl $-0x1, %eax
jne 0x39818
leaq -0x290(%rbp), %r14
leaq -0x178(%rbp), %rsi
movl $0x118, %edx            # imm = 0x118
movq %r14, %rdi
callq 0xa330
movq %r15, %rdi
movq %r14, %rsi
callq 0x2e92a
leaq -0x290(%rbp), %rdi
callq 0x2fe3c
leaq -0x178(%rbp), %rsi
movq %r15, %rdi
callq 0x2e9a8
movq %r15, %rdi
movl %eax, %esi
callq 0x2e916
movq %rax, %r14
movq %r15, %rdi
movl -0x2c(%rbp), %esi
callq 0x2e916
movq %r14, 0x110(%rax)
leaq -0x178(%rbp), %rdi
callq 0x2fe3c
jmp 0x3984c
addl %ecx, %r14d
movl %r14d, (%r12)
movq %rbx, %rdi
callq 0xa480
pushq $0x20
popq %rax
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 0x2fe3c
jmp 0x3987d
movq %rax, %rbx
leaq -0x178(%rbp), %rdi
callq 0x2fe3c
movq %rbx, %rdi
callq 0xa6c0
nop