// File: /workspace/github_c_makefile_star2_O0/sfeakes[P]unifi-motion/mongoose.c
// Function: mg_dns_encode_record
int mg_dns_encode_record(struct mbuf *io, struct mg_dns_resource_record *rr,
                         const char *name, size_t nlen, const void *rdata,
                         size_t rlen) {
  size_t pos = io->len;
  uint16_t u16;
  uint32_t u32;

  if (rr->kind == MG_DNS_INVALID_RECORD) {
    return -1; /* LCOV_EXCL_LINE */
  }

  if (mg_dns_encode_name(io, name, nlen) == -1) {
    return -1;
  }

  u16 = htons(rr->rtype);
  mbuf_append(io, &u16, 2);
  u16 = htons(rr->rclass);
  mbuf_append(io, &u16, 2);

  if (rr->kind == MG_DNS_ANSWER) {
    u32 = htonl(rr->ttl);
    mbuf_append(io, &u32, 4);

    if (rr->rtype == MG_DNS_CNAME_RECORD) {
      int clen;
      /* fill size after encoding */
      size_t off = io->len;
      mbuf_append(io, &u16, 2);
      if ((clen = mg_dns_encode_name(io, (const char *) rdata, rlen)) == -1) {
        return -1;
      }
      u16 = clen;
      io->buf[off] = u16 >> 8;
      io->buf[off + 1] = u16 & 0xff;
    } else {
      u16 = htons((uint16_t) rlen);
      mbuf_append(io, &u16, 2);
      mbuf_append(io, rdata, rlen);
    }
  }

  return io->len - pos;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movq %r9, -0x38(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x40(%rbp)
movq -0x18(%rbp), %rax
cmpl $0x0, 0x1c(%rax)
jne 0x184c2
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x18625
movq -0x10(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x28(%rbp), %rdx
callq 0x18370
cmpl $-0x1, %eax
jne 0x184e4
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x18625
movq -0x18(%rbp), %rax
movl 0x10(%rax), %eax
movzwl %ax, %edi
callq 0x31a0
movw %ax, -0x42(%rbp)
movq -0x10(%rbp), %rdi
leaq -0x42(%rbp), %rsi
movl $0x2, %edx
callq 0x89b0
movq -0x18(%rbp), %rax
movl 0x14(%rax), %eax
movzwl %ax, %edi
callq 0x31a0
movw %ax, -0x42(%rbp)
movq -0x10(%rbp), %rdi
leaq -0x42(%rbp), %rsi
movl $0x2, %edx
callq 0x89b0
movq -0x18(%rbp), %rax
cmpl $0x2, 0x1c(%rax)
jne 0x18616
movq -0x18(%rbp), %rax
movl 0x18(%rax), %edi
callq 0x3250
movl %eax, -0x48(%rbp)
movq -0x10(%rbp), %rdi
leaq -0x48(%rbp), %rsi
movl $0x4, %edx
callq 0x89b0
movq -0x18(%rbp), %rax
cmpl $0x5, 0x10(%rax)
jne 0x185e1
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x58(%rbp)
movq -0x10(%rbp), %rdi
leaq -0x42(%rbp), %rsi
movl $0x2, %edx
callq 0x89b0
movq -0x10(%rbp), %rdi
movq -0x30(%rbp), %rsi
movq -0x38(%rbp), %rdx
callq 0x18370
movl %eax, -0x4c(%rbp)
cmpl $-0x1, %eax
jne 0x185a7
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x18625
movl -0x4c(%rbp), %eax
movw %ax, -0x42(%rbp)
movzwl -0x42(%rbp), %eax
sarl $0x8, %eax
movb %al, %dl
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq -0x58(%rbp), %rcx
movb %dl, (%rax,%rcx)
movzwl -0x42(%rbp), %eax
andl $0xff, %eax
movb %al, %dl
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq -0x58(%rbp), %rcx
movb %dl, 0x1(%rax,%rcx)
jmp 0x18614
movq -0x38(%rbp), %rax
movzwl %ax, %edi
callq 0x31a0
movw %ax, -0x42(%rbp)
movq -0x10(%rbp), %rdi
leaq -0x42(%rbp), %rsi
movl $0x2, %edx
callq 0x89b0
movq -0x10(%rbp), %rdi
movq -0x30(%rbp), %rsi
movq -0x38(%rbp), %rdx
callq 0x89b0
jmp 0x18616
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
subq -0x40(%rbp), %rax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nop