// File: /workspace/github_c_makefile_star2_O0/sfeakes[P]unifi-motion/mongoose.c
// Function: mg_parse_dns
int mg_parse_dns(const char *buf, int len, struct mg_dns_message *msg) {
  struct mg_dns_header *header = (struct mg_dns_header *) buf;
  unsigned char *data = (unsigned char *) buf + sizeof(*header);
  unsigned char *end = (unsigned char *) buf + len;
  int i;

  memset(msg, 0, sizeof(*msg));
  msg->pkt.p = buf;
  msg->pkt.len = len;

  if (len < (int) sizeof(*header)) return -1;

  msg->transaction_id = header->transaction_id;
  msg->flags = ntohs(header->flags);
  msg->num_questions = ntohs(header->num_questions);
  if (msg->num_questions > (int) ARRAY_SIZE(msg->questions)) {
    msg->num_questions = (int) ARRAY_SIZE(msg->questions);
  }
  msg->num_answers = ntohs(header->num_answers);
  if (msg->num_answers > (int) ARRAY_SIZE(msg->answers)) {
    msg->num_answers = (int) ARRAY_SIZE(msg->answers);
  }

  for (i = 0; i < msg->num_questions; i++) {
    data = mg_parse_dns_resource_record(data, end, &msg->questions[i], 0);
    if (data == NULL) return -1;
  }

  for (i = 0; i < msg->num_answers; i++) {
    data = mg_parse_dns_resource_record(data, end, &msg->answers[i], 1);
    if (data == NULL) return -1;
  }

  return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rax
addq $0xc, %rax
movq %rax, -0x30(%rbp)
movq -0x10(%rbp), %rax
movslq -0x14(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x38(%rbp)
movq -0x20(%rbp), %rdi
xorl %esi, %esi
movl $0xc20, %edx            # imm = 0xC20
callq 0x3260
movq -0x10(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, (%rax)
movslq -0x14(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x8(%rax)
cmpl $0xc, -0x14(%rbp)
jge 0x1881f
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x18957
movq -0x28(%rbp), %rax
movw (%rax), %cx
movq -0x20(%rbp), %rax
movw %cx, 0x12(%rax)
movq -0x28(%rbp), %rax
movzwl 0x2(%rax), %edi
callq 0x34e0
movw %ax, %cx
movq -0x20(%rbp), %rax
movw %cx, 0x10(%rax)
movq -0x28(%rbp), %rax
movzwl 0x4(%rax), %edi
callq 0x34e0
movzwl %ax, %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0x14(%rax)
movq -0x20(%rbp), %rax
cmpl $0x20, 0x14(%rax)
jle 0x18872
movq -0x20(%rbp), %rax
movl $0x20, 0x14(%rax)
movq -0x28(%rbp), %rax
movzwl 0x6(%rax), %edi
callq 0x34e0
movzwl %ax, %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0x18(%rax)
movq -0x20(%rbp), %rax
cmpl $0x20, 0x18(%rax)
jle 0x1889e
movq -0x20(%rbp), %rax
movl $0x20, 0x18(%rax)
movl $0x0, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
movq -0x20(%rbp), %rcx
cmpl 0x14(%rcx), %eax
jge 0x188f4
movq -0x30(%rbp), %rdi
movq -0x38(%rbp), %rsi
movq -0x20(%rbp), %rdx
addq $0x20, %rdx
movslq -0x3c(%rbp), %rax
imulq $0x30, %rax, %rax
addq %rax, %rdx
xorl %ecx, %ecx
callq 0x18960
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
jne 0x188e7
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x18957
jmp 0x188e9
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x188a5
movl $0x0, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
movq -0x20(%rbp), %rcx
cmpl 0x18(%rcx), %eax
jge 0x18950
movq -0x30(%rbp), %rdi
movq -0x38(%rbp), %rsi
movq -0x20(%rbp), %rdx
addq $0x620, %rdx            # imm = 0x620
movslq -0x3c(%rbp), %rax
imulq $0x30, %rax, %rax
addq %rax, %rdx
movl $0x1, %ecx
callq 0x18960
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
jne 0x18943
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x18957
jmp 0x18945
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x188fb
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq