// File: /workspace/github_c_makefile_star2_O0/sfeakes[P]unifi-motion/mongoose.c
// Function: mg_dns_uncompress_name
size_t mg_dns_uncompress_name(struct mg_dns_message *msg, struct mg_str *name,
                              char *dst, int dst_len) {
  int chunk_len, num_ptrs = 0;
  char *old_dst = dst;
  const unsigned char *data = (unsigned char *) name->p;
  const unsigned char *end = (unsigned char *) msg->pkt.p + msg->pkt.len;

  if (data >= end) {
    return 0;
  }

  while ((chunk_len = *data++)) {
    int leeway = dst_len - (dst - old_dst);
    if (data >= end) {
      return 0;
    }

    if ((chunk_len & 0xc0) == 0xc0) {
      uint16_t off = (data[-1] & (~0xc0)) << 8 | data[0];
      if (off >= msg->pkt.len) {
        return 0;
      }
      /* Basic circular loop avoidance: allow up to 16 pointer hops. */
      if (++num_ptrs > 15) {
        return 0;
      }
      data = (unsigned char *) msg->pkt.p + off;
      continue;
    }
    if (chunk_len > 63) {
      return 0;
    }
    if (chunk_len > leeway) {
      chunk_len = leeway;
    }

    if (data + chunk_len >= end) {
      return 0;
    }

    memcpy(dst, data, chunk_len);
    data += chunk_len;
    dst += chunk_len;
    leeway -= chunk_len;
    if (leeway == 0) {
      return dst - old_dst;
    }
    *dst++ = '.';
  }

  if (dst != old_dst) {
    *--dst = 0;
  }
  return dst - old_dst;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movl %ecx, -0x24(%rbp)
movl $0x0, -0x2c(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x40(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq -0x10(%rbp), %rcx
addq 0x8(%rcx), %rax
movq %rax, -0x48(%rbp)
movq -0x40(%rbp), %rax
cmpq -0x48(%rbp), %rax
jb 0x1809b
movq $0x0, -0x8(%rbp)
jmp 0x1823a
jmp 0x1809d
movq -0x40(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x40(%rbp)
movzbl (%rax), %eax
movl %eax, -0x28(%rbp)
cmpl $0x0, %eax
je 0x1820e
movslq -0x24(%rbp), %rax
movq -0x20(%rbp), %rcx
movq -0x38(%rbp), %rdx
subq %rdx, %rcx
subq %rcx, %rax
movl %eax, -0x4c(%rbp)
movq -0x40(%rbp), %rax
cmpq -0x48(%rbp), %rax
jb 0x180e7
movq $0x0, -0x8(%rbp)
jmp 0x1823a
movl -0x28(%rbp), %eax
andl $0xc0, %eax
cmpl $0xc0, %eax
jne 0x18163
movq -0x40(%rbp), %rax
movzbl -0x1(%rax), %eax
andl $0xffffff3f, %eax       # imm = 0xFFFFFF3F
shll $0x8, %eax
movq -0x40(%rbp), %rcx
movzbl (%rcx), %ecx
orl %ecx, %eax
movw %ax, -0x4e(%rbp)
movzwl -0x4e(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jb 0x1812e
movq $0x0, -0x8(%rbp)
jmp 0x1823a
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
cmpl $0xf, %eax
jle 0x18149
movq $0x0, -0x8(%rbp)
jmp 0x1823a
movq -0x10(%rbp), %rax
movq (%rax), %rax
movzwl -0x4e(%rbp), %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x40(%rbp)
jmp 0x1809d
cmpl $0x3f, -0x28(%rbp)
jle 0x18176
movq $0x0, -0x8(%rbp)
jmp 0x1823a
movl -0x28(%rbp), %eax
cmpl -0x4c(%rbp), %eax
jle 0x18184
movl -0x4c(%rbp), %eax
movl %eax, -0x28(%rbp)
movq -0x40(%rbp), %rax
movslq -0x28(%rbp), %rcx
addq %rcx, %rax
cmpq -0x48(%rbp), %rax
jb 0x181a2
movq $0x0, -0x8(%rbp)
jmp 0x1823a
movq -0x20(%rbp), %rdi
movq -0x40(%rbp), %rsi
movslq -0x28(%rbp), %rdx
callq 0x33b0
movl -0x28(%rbp), %ecx
movq -0x40(%rbp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x40(%rbp)
movl -0x28(%rbp), %ecx
movq -0x20(%rbp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x20(%rbp)
movl -0x28(%rbp), %ecx
movl -0x4c(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x4c(%rbp)
cmpl $0x0, -0x4c(%rbp)
jne 0x181f7
movq -0x20(%rbp), %rax
movq -0x38(%rbp), %rcx
subq %rcx, %rax
movq %rax, -0x8(%rbp)
jmp 0x1823a
movq -0x20(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x20(%rbp)
movb $0x2e, (%rax)
jmp 0x1809d
movq -0x20(%rbp), %rax
cmpq -0x38(%rbp), %rax
je 0x1822b
movq -0x20(%rbp), %rax
movq %rax, %rcx
addq $-0x1, %rcx
movq %rcx, -0x20(%rbp)
movb $0x0, -0x1(%rax)
movq -0x20(%rbp), %rax
movq -0x38(%rbp), %rcx
subq %rcx, %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)