// File: madpilot[P]mDNSResolver/src/Answer.cpp
// Function: mDNSResolver::Answer::assembleName(unsigned char*, unsigned int, unsigned int*, char**, unsigned int)
int Answer::assembleName(unsigned char *buffer, unsigned int len, unsigned int *offset, char **name, unsigned int maxlen) {
    unsigned int index = 0;
    unsigned int nameLength = 0;

    while(index < maxlen) {
      if((buffer[*offset] & 0xc0) == 0xc0) {
        unsigned int pointerOffset = ((buffer[(*offset)++] & 0x3f) << 8) + buffer[(*offset)++];
        if(pointerOffset > len) {
          // Points to somewhere beyond the packet
          return -1 * E_MDNS_POINTER_OVERFLOW;
        }

        char *namePointer = *name + nameLength;
        int pointerLen = assembleName(buffer, len, &pointerOffset, &namePointer, MDNS_MAX_NAME_LEN - nameLength);

        if(pointerLen < 0) {
          return pointerLen;
        }

        nameLength += pointerLen;

        break;
      } else if(buffer[*offset] == '\0') {
        (*name)[nameLength++] = buffer[(*offset)++];
        break;
      } else {
        (*name)[nameLength++] = buffer[(*offset)++];
      }
      index++;
    }

    return nameLength;
  }

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movl %r8d, -0x2c(%rbp)
movl $0x0, -0x30(%rbp)
movl $0x0, -0x34(%rbp)
movl -0x30(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jae 0x7f9cd
movq -0x10(%rbp), %rax
movq -0x20(%rbp), %rcx
movl (%rcx), %ecx
movzbl (%rax,%rcx), %eax
andl $0xc0, %eax
cmpl $0xc0, %eax
jne 0x7f94e
movq -0x10(%rbp), %rax
movq -0x20(%rbp), %rdx
movl (%rdx), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, (%rdx)
movl %ecx, %ecx
movzbl (%rax,%rcx), %eax
andl $0x3f, %eax
shll $0x8, %eax
movq -0x10(%rbp), %rcx
movq -0x20(%rbp), %rsi
movl (%rsi), %edx
movl %edx, %edi
addl $0x1, %edi
movl %edi, (%rsi)
movl %edx, %edx
movzbl (%rcx,%rdx), %ecx
addl %ecx, %eax
movl %eax, -0x38(%rbp)
movl -0x38(%rbp), %eax
cmpl -0x14(%rbp), %eax
jbe 0x7f900
movl $0xffffffde, -0x4(%rbp) # imm = 0xFFFFFFDE
jmp 0x7f9d3
movq -0x28(%rbp), %rax
movq (%rax), %rax
movl -0x34(%rbp), %ecx
addq %rcx, %rax
movq %rax, -0x40(%rbp)
movq -0x10(%rbp), %rdi
movl -0x14(%rbp), %esi
movl $0xff, %r8d
subl -0x34(%rbp), %r8d
leaq -0x38(%rbp), %rdx
leaq -0x40(%rbp), %rcx
callq 0x7f860
movl %eax, -0x44(%rbp)
cmpl $0x0, -0x44(%rbp)
jge 0x7f943
movl -0x44(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x7f9d3
movl -0x44(%rbp), %eax
addl -0x34(%rbp), %eax
movl %eax, -0x34(%rbp)
jmp 0x7f9cd
movq -0x10(%rbp), %rax
movq -0x20(%rbp), %rcx
movl (%rcx), %ecx
movzbl (%rax,%rcx), %eax
cmpl $0x0, %eax
jne 0x7f990
movq -0x10(%rbp), %rax
movq -0x20(%rbp), %rdx
movl (%rdx), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, (%rdx)
movl %ecx, %ecx
movb (%rax,%rcx), %dl
movq -0x28(%rbp), %rax
movq (%rax), %rax
movl -0x34(%rbp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, -0x34(%rbp)
movl %ecx, %ecx
movb %dl, (%rax,%rcx)
jmp 0x7f9cd
movq -0x10(%rbp), %rax
movq -0x20(%rbp), %rdx
movl (%rdx), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, (%rdx)
movl %ecx, %ecx
movb (%rax,%rcx), %dl
movq -0x28(%rbp), %rax
movq (%rax), %rax
movl -0x34(%rbp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, -0x34(%rbp)
movl %ecx, %ecx
movb %dl, (%rax,%rcx)
jmp 0x7f9bf
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x7f889
movl -0x34(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nopl (%rax)