// File: /workspace/github_c_makefile_star2_O1/GangZhuo[P]dohclient/src/ns_msg.c
// Function: ns_parse
int ns_parse(ns_msg_t *msg, const uint8_t *bytes, int nbytes)
{
	int i, rrcount;
	stream_t s = STREAM_INIT();
	ns_qr_t *qr;
	ns_rr_t *rr;

	s.array = (char *)bytes;
	s.cap = nbytes;
	s.size = nbytes;
	s.pos = 0;

#define check_len(n) \
	if (stream_rsize(&s) < (n)) { \
		loge("truncated stream\n"); \
		return -1; \
	}

	check_len(12);

	msg->id = (uint16_t)stream_readi16(&s);
	msg->flags = (uint16_t)stream_readi16(&s);
	msg->qdcount = (uint16_t)stream_readi16(&s);
	msg->ancount = (uint16_t)stream_readi16(&s);
	msg->nscount = (uint16_t)stream_readi16(&s);
	msg->arcount = (uint16_t)stream_readi16(&s);

	if (msg->qdcount > 0) {
		msg->qrs = calloc(msg->qdcount, sizeof(ns_qr_t));
		if (msg->qrs == NULL) {
			loge("alloc\n");
			return -1;
		}

		for (i = 0; i < msg->qdcount; i++) {
			stream_t name = STREAM_INIT();
			qr = msg->qrs + i;
			if (ns_read_domainname(&name, &s) < 0) {
				loge("read domain name\n");
				return -1;
			}
			qr->qname = name.array;
			check_len(4);
			qr->qtype = (uint16_t)stream_readi16(&s);
			qr->qclass = (uint16_t)stream_readi16(&s);
            if (qr->qtype == 0) {
                loge("wrong qtype, maybe pollution result\n");
                return -1;
            }
		}

	}

	rrcount = ns_rrcount(msg);
	if (rrcount > 0) {
		msg->rrs = calloc(rrcount, sizeof(ns_rr_t));
		if (msg->rrs == NULL) {
			loge("alloc\n");
			return -1;
		}

		for (i = 0; i < rrcount; i++) {
			stream_t name = STREAM_INIT();
			rr = msg->rrs + i;
			if (ns_read_domainname(&name, &s) < 0) {
				loge("read domain name\n");
				return -1;
			}
			rr->name = name.array;
			check_len(10);
			rr->type = (uint16_t)stream_readi16(&s);
			rr->cls = (uint16_t)stream_readi16(&s);
			rr->ttl = (uint32_t)stream_readi32(&s);
			rr->rdlength = (uint16_t)stream_readi16(&s);

			check_len(rr->rdlength);
			if (ns_read_rdata(rr, &s) < 0) {
				loge("read rdata\n");
				return -1;
			}
		}

	}

#undef check_len

	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsp)
movq $0x0, 0x30(%rsp)
movq %rsi, 0x20(%rsp)
movl %edx, 0x30(%rsp)
movl %edx, 0x28(%rsp)
movl $0x0, 0x2c(%rsp)
cmpl $0xb, %edx
jg 0x1a859
xorl %eax, %eax
callq 0x17d59
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpl $0x3, (%rax)
jl 0x1b1ca
leaq 0xa39f(%rip), %rsi      # 0x24bd4
leaq 0xa3f8(%rip), %rdx      # 0x24c34
leaq 0xa3fa(%rip), %r8       # 0x24c3d
movl $0x3, %edi
movl $0x3a4, %ecx            # imm = 0x3A4
xorl %eax, %eax
callq 0x17d92
jmp 0x1b1ca
movq %rdi, %rbx
leaq 0x20(%rsp), %r14
movq %r14, %rdi
movl $0x2, %esi
callq 0x1dd7b
movw %ax, (%rbx)
movq %r14, %rdi
movl $0x2, %esi
callq 0x1dd7b
movw %ax, 0x2(%rbx)
movq %r14, %rdi
movl $0x2, %esi
callq 0x1dd7b
movw %ax, 0x4(%rbx)
movq %r14, %rdi
movl $0x2, %esi
callq 0x1dd7b
movw %ax, 0x6(%rbx)
movq %r14, %rdi
movl $0x2, %esi
callq 0x1dd7b
movw %ax, 0x8(%rbx)
movq %r14, %rdi
movl $0x2, %esi
callq 0x1dd7b
movw %ax, 0xa(%rbx)
movzwl 0x4(%rbx), %edi
testq %rdi, %rdi
je 0x1a98e
movl $0x10, %esi
callq 0x3240
movq %rax, 0x10(%rbx)
testq %rax, %rax
je 0x1afec
cmpw $0x0, 0x4(%rbx)
je 0x1a98e
movl $0xa, %r12d
xorl %r13d, %r13d
movq %rsp, %r14
leaq 0x20(%rsp), %r15
xorps %xmm0, %xmm0
movaps %xmm0, (%rsp)
movq $0x0, 0x10(%rsp)
movq 0x10(%rbx), %rbp
movq %r14, %rdi
movq %r15, %rsi
callq 0x1b1db
testl %eax, %eax
js 0x1b065
movq (%rsp), %rax
movq %rax, -0xa(%rbp,%r12)
movl 0x28(%rsp), %eax
subl 0x2c(%rsp), %eax
cmpl $0x3, %eax
jle 0x1b099
movq %r15, %rdi
movl $0x2, %esi
callq 0x1dd7b
movw %ax, -0x2(%rbp,%r12)
movq %r15, %rdi
movl $0x2, %esi
callq 0x1dd7b
movw %ax, (%rbp,%r12)
cmpw $0x0, -0x2(%rbp,%r12)
je 0x1b0cd
incq %r13
movzwl 0x4(%rbx), %eax
addq $0x10, %r12
cmpq %rax, %r13
jb 0x1a906
movzwl 0x6(%rbx), %eax
movzwl 0x8(%rbx), %ecx
addl %eax, %ecx
movzwl 0xa(%rbx), %eax
addl %ecx, %eax
je 0x1b025
movl %eax, %edi
movl $0x20, %esi
movq %rdi, 0x58(%rsp)
callq 0x3240
movq %rax, 0x18(%rbx)
testq %rax, %rax
je 0x1b02c
xorl %r15d, %r15d
leaq 0xa128(%rip), %r12      # 0x24af4
xorl %r14d, %r14d
xorps %xmm0, %xmm0
movaps %xmm0, 0x60(%rsp)
movq $0x0, 0x70(%rsp)
movq %r14, %r13
shlq $0x5, %r13
addq 0x18(%rbx), %r13
leaq 0x60(%rsp), %rdi
leaq 0x20(%rsp), %rbp
movq %rbp, %rsi
callq 0x1b1db
testl %eax, %eax
js 0x1b101
movq 0x60(%rsp), %rax
movq %rax, (%r13)
movl 0x28(%rsp), %eax
subl 0x2c(%rsp), %eax
cmpl $0x9, %eax
jle 0x1b135
movq %rbp, %rdi
movl $0x2, %esi
callq 0x1dd7b
movw %ax, 0x8(%r13)
movq %rbp, %rdi
movl $0x2, %esi
callq 0x1dd7b
movw %ax, 0xa(%r13)
movq %rbp, %rdi
movl $0x4, %esi
callq 0x1dd7b
movl %eax, 0xc(%r13)
movq %rbp, %rdi
movl $0x2, %esi
callq 0x1dd7b
movw %ax, 0x10(%r13)
movl 0x28(%rsp), %ecx
movslq 0x2c(%rsp), %rdx
subl %edx, %ecx
movzwl %ax, %esi
cmpl %esi, %ecx
jl 0x1b166
movzwl 0x8(%r13), %esi
leal -0x2(%rsi), %edi
cmpl $0xd, %edi
ja 0x1aac4
movslq (%r12,%rdi,4), %rdx
addq %r12, %rdx
jmpq *%rdx
xorps %xmm0, %xmm0
movaps %xmm0, (%rsp)
movq $0x0, 0x10(%rsp)
movq %rsp, %rdi
movq %rbp, %rsi
callq 0x1b1db
testl %eax, %eax
js 0x1afca
movq (%rsp), %rax
movq %rax, 0x18(%r13)
xorl %edx, %edx
jmp 0x1afcf
cmpl $0x29, %esi
jne 0x1ab71
xorps %xmm0, %xmm0
movaps %xmm0, (%rsp)
movq $0x0, 0x10(%rsp)
addq 0x20(%rsp), %rdx
movq %rdx, (%rsp)
movl %ecx, 0x10(%rsp)
movzwl 0x10(%r13), %eax
movl %eax, 0x8(%rsp)
movl $0x0, 0xc(%rsp)
xorl %r15d, %r15d
movl 0x8(%rsp), %eax
subl 0xc(%rsp), %eax
jle 0x1adb7
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpl $0x4, %eax
jge 0x1ab1a
xorl %eax, %eax
jmp 0x1ab65
movq %rsp, %r12
movq %r12, %rdi
movl $0x2, %esi
callq 0x1dd7b
movq %r12, %rdi
movl $0x2, %esi
callq 0x1dd7b
movl 0x8(%rsp), %ecx
subl 0xc(%rsp), %ecx
cmpl %eax, %ecx
jge 0x1ab47
xorl %eax, %eax
jmp 0x1ab5e
movq %r12, %rdi
movl %eax, %esi
movl $0x1, %edx
callq 0x1d619
incl %r15d
movb $0x1, %al
movl %r15d, %ebp
leaq 0x9f8f(%rip), %r12      # 0x24af4
movl %ebp, %r15d
testb %al, %al
jne 0x1aafe
jmp 0x1adba
movzwl %ax, %ebp
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
cmpl %ebp, %ecx
jl 0x1afcf
movzwl %ax, %edi
callq 0x34b0
testq %rax, %rax
je 0x1af0a
movq %rax, %r15
leaq 0x20(%rsp), %rdi
movq %rax, %rsi
movl %ebp, %edx
callq 0x1d8de
movzwl 0x10(%r13), %ecx
cmpl %ecx, %eax
jne 0x1af02
movq %r15, 0x18(%r13)
xorl %edx, %edx
jmp 0x1aefa
xorps %xmm0, %xmm0
movaps %xmm0, (%rsp)
movq %r15, 0x10(%rsp)
movq %r15, 0x50(%rsp)
movaps %xmm0, 0x40(%rsp)
movq %rsp, %rdi
movq %rbp, %rsi
callq 0x1cf61
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
testl %eax, %eax
js 0x1afcf
leaq 0x40(%rsp), %rdi
movq %rbp, %rsi
callq 0x1cf61
jmp 0x1ad82
xorps %xmm0, %xmm0
movaps %xmm0, (%rsp)
movq $0x0, 0x10(%rsp)
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
cmpl $0x2, %ecx
jl 0x1afcf
movq %rbp, %rdi
movl $0x2, %esi
callq 0x1dd7b
movq %rbp, %rsi
movl %eax, %ebp
movq %rsp, %rdi
callq 0x1b1db
testl %eax, %eax
js 0x1afca
movl $0x10, %edi
callq 0x34b0
testq %rax, %rax
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
je 0x1afcf
movq (%rsp), %rcx
movq %rcx, 0x8(%rax)
movw %bp, (%rax)
jmp 0x1aab9
xorps %xmm0, %xmm0
movaps %xmm0, (%rsp)
movq %r15, 0x10(%rsp)
movq %r15, 0x50(%rsp)
movaps %xmm0, 0x40(%rsp)
movq %rsp, %rdi
movq %rbp, %rsi
callq 0x1b1db
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
testl %eax, %eax
js 0x1afcf
leaq 0x40(%rsp), %rdi
movq %rbp, %rsi
callq 0x1b1db
testl %eax, %eax
js 0x1afca
movl 0x28(%rsp), %eax
subl 0x2c(%rsp), %eax
cmpl $0x14, %eax
jl 0x1afca
movl $0x28, %edi
callq 0x34b0
testq %rax, %rax
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
je 0x1afcf
movq %rax, %rbp
movq (%rsp), %rax
movq %rax, (%rbp)
movq 0x40(%rsp), %rax
movq %rax, 0x8(%rbp)
leaq 0x20(%rsp), %rdi
movl $0x4, %esi
callq 0x1dd7b
movl %eax, 0x10(%rbp)
leaq 0x20(%rsp), %rdi
movl $0x4, %esi
callq 0x1dd7b
movl %eax, 0x14(%rbp)
leaq 0x20(%rsp), %rdi
movl $0x4, %esi
callq 0x1dd7b
movl %eax, 0x18(%rbp)
leaq 0x20(%rsp), %rdi
movl $0x4, %esi
callq 0x1dd7b
movl %eax, 0x1c(%rbp)
leaq 0x20(%rsp), %rdi
movl $0x4, %esi
callq 0x1dd7b
movl %eax, 0x20(%rbp)
movq %rbp, 0x18(%r13)
jmp 0x1aabd
xorps %xmm0, %xmm0
movaps %xmm0, (%rsp)
movq %r15, 0x10(%rsp)
movq %r15, 0x50(%rsp)
movaps %xmm0, 0x40(%rsp)
movq %rsp, %rdi
movq %rbp, %rsi
callq 0x1b1db
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
testl %eax, %eax
js 0x1afcf
leaq 0x40(%rsp), %rdi
movq %rbp, %rsi
callq 0x1b1db
testl %eax, %eax
js 0x1afca
movl $0x10, %edi
callq 0x34b0
testq %rax, %rax
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
je 0x1afcf
movq (%rsp), %rcx
movq %rcx, (%rax)
movq 0x40(%rsp), %rcx
movq %rcx, 0x8(%rax)
jmp 0x1aab9
movl %r15d, %ebp
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
testl %ebp, %ebp
js 0x1aefa
movl $0x10, %edi
callq 0x34b0
testq %rax, %rax
je 0x1af0a
movq %rax, %r15
movq 0x30(%rsp), %rax
movq %rax, 0x10(%rsp)
movaps 0x20(%rsp), %xmm0
movaps %xmm0, (%rsp)
movzwl 0x10(%r13), %r12d
addl 0x2c(%rsp), %r12d
movl %ebp, %eax
movl %r12d, 0x8(%rsp)
movl %ebp, 0x8(%r15)
movq %rax, %rbp
shlq $0x4, %rbp
movq %rbp, %rdi
callq 0x34b0
movq %rax, (%r15)
testq %rax, %rax
je 0x1af12
movq %r15, 0x38(%rsp)
movq %rax, %rdi
xorl %esi, %esi
movq %rbp, %rdx
callq 0x31e0
subl 0xc(%rsp), %r12d
movq %rsp, %r15
jle 0x1aee0
xorl %ebp, %ebp
cmpl $0x3, %r12d
jbe 0x1af1a
movq 0x38(%rsp), %rax
movq (%rax), %r12
movq %r15, %rdi
movl $0x2, %esi
callq 0x1dd7b
movw %ax, (%r12,%rbp)
movq %r15, %rdi
movl $0x2, %esi
callq 0x1dd7b
movw %ax, 0x2(%r12,%rbp)
movzwl %ax, %r15d
movq %r15, %rdi
callq 0x34b0
movq %rax, 0x8(%r12,%rbp)
testq %rax, %rax
je 0x1af52
movl 0x8(%rsp), %ecx
movslq 0xc(%rsp), %rsi
subl %esi, %ecx
cmpl %r15d, %ecx
jl 0x1af86
addq (%rsp), %rsi
movq %rax, %rdi
movq %r15, %rdx
callq 0x32b0
movzwl 0x2(%r12,%rbp), %esi
movq %rsp, %r15
movq %r15, %rdi
movl $0x1, %edx
callq 0x1d619
movl 0x8(%rsp), %r12d
addq $0x10, %rbp
subl 0xc(%rsp), %r12d
jg 0x1ae42
movq 0x38(%rsp), %rax
movq %rax, 0x18(%r13)
xorl %edx, %edx
xorl %r15d, %r15d
leaq 0x9bff(%rip), %r12      # 0x24af4
jmp 0x1afcf
xorl %r15d, %r15d
jmp 0x1afcf
movq %r15, %rdi
callq 0x3450
xorl %r15d, %r15d
jmp 0x1afca
movq %r15, %rdi
jmp 0x1afbb
movq 0x38(%rsp), %r13
cmpl $0x0, 0x8(%r13)
jle 0x1afb8
movl $0x8, %r15d
xorl %r12d, %r12d
movq (%r13), %rax
movq (%rax,%r15), %rdi
callq 0x3450
incq %r12
movslq 0x8(%r13), %rax
addq $0x10, %r15
cmpq %rax, %r12
jl 0x1af33
jmp 0x1afb8
movq 0x38(%rsp), %r13
cmpl $0x0, 0x8(%r13)
jle 0x1afb8
movl $0x8, %r15d
xorl %r12d, %r12d
movq (%r13), %rax
movq (%rax,%r15), %rdi
callq 0x3450
incq %r12
movslq 0x8(%r13), %rax
addq $0x10, %r15
cmpq %rax, %r12
jl 0x1af67
jmp 0x1afb8
movq 0x38(%rsp), %r13
cmpl $0x0, 0x8(%r13)
jle 0x1afb8
movl $0x8, %r15d
xorl %r12d, %r12d
movq (%r13), %rax
movq (%rax,%r15), %rdi
callq 0x3450
incq %r12
movslq 0x8(%r13), %rax
addq $0x10, %r15
cmpq %rax, %r12
jl 0x1af9b
movq %r13, %rdi
callq 0x3450
xorl %r15d, %r15d
leaq 0x9b2a(%rip), %r12      # 0x24af4
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
testl %edx, %edx
js 0x1b193
incq %r14
xorl %ebp, %ebp
cmpq 0x58(%rsp), %r14
jne 0x1a9cf
jmp 0x1b1ca
xorl %eax, %eax
callq 0x17d59
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpl $0x3, (%rax)
jl 0x1b1ca
leaq 0x9bcc(%rip), %rsi      # 0x24bd4
leaq 0x9c25(%rip), %rdx      # 0x24c34
leaq 0x888d(%rip), %r8       # 0x238a3
movl $0x3, %edi
movl $0x3b0, %ecx            # imm = 0x3B0
jmp 0x1a84d
xorl %ebp, %ebp
jmp 0x1b1ca
xorl %eax, %eax
callq 0x17d59
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpl $0x3, (%rax)
jl 0x1b1ca
leaq 0x9b8c(%rip), %rsi      # 0x24bd4
leaq 0x9be5(%rip), %rdx      # 0x24c34
leaq 0x884d(%rip), %r8       # 0x238a3
movl $0x3, %edi
movl $0x3cb, %ecx            # imm = 0x3CB
jmp 0x1a84d
xorl %eax, %eax
callq 0x17d59
cmpl $0x3, (%rax)
jl 0x1b1c5
leaq 0x9b58(%rip), %rsi      # 0x24bd4
leaq 0x9bb1(%rip), %rdx      # 0x24c34
leaq 0x9bc5(%rip), %r8       # 0x24c4f
movl $0x3, %edi
movl $0x3b8, %ecx            # imm = 0x3B8
jmp 0x1b1be
xorl %eax, %eax
callq 0x17d59
cmpl $0x3, (%rax)
jl 0x1b1c5
leaq 0x9b24(%rip), %rsi      # 0x24bd4
leaq 0x9b7d(%rip), %rdx      # 0x24c34
leaq 0x9b7f(%rip), %r8       # 0x24c3d
movl $0x3, %edi
movl $0x3bc, %ecx            # imm = 0x3BC
jmp 0x1b1be
xorl %eax, %eax
callq 0x17d59
cmpl $0x3, (%rax)
jl 0x1b1c5
leaq 0x9af0(%rip), %rsi      # 0x24bd4
leaq 0x9b49(%rip), %rdx      # 0x24c34
leaq 0x9b6f(%rip), %r8       # 0x24c61
movl $0x3, %edi
movl $0x3c0, %ecx            # imm = 0x3C0
jmp 0x1b1be
xorl %eax, %eax
callq 0x17d59
cmpl $0x3, (%rax)
jl 0x1b1c5
leaq 0x9abc(%rip), %rsi      # 0x24bd4
leaq 0x9b15(%rip), %rdx      # 0x24c34
leaq 0x9b29(%rip), %r8       # 0x24c4f
movl $0x3, %edi
movl $0x3d3, %ecx            # imm = 0x3D3
jmp 0x1b1be
xorl %eax, %eax
callq 0x17d59
cmpl $0x3, (%rax)
jl 0x1b1c5
leaq 0x9a88(%rip), %rsi      # 0x24bd4
leaq 0x9ae1(%rip), %rdx      # 0x24c34
leaq 0x9ae3(%rip), %r8       # 0x24c3d
movl $0x3, %edi
movl $0x3d7, %ecx            # imm = 0x3D7
jmp 0x1b1be
xorl %eax, %eax
callq 0x17d59
cmpl $0x3, (%rax)
jl 0x1b1c5
leaq 0x9a5b(%rip), %rsi      # 0x24bd4
leaq 0x9ab4(%rip), %rdx      # 0x24c34
leaq 0x9ab6(%rip), %r8       # 0x24c3d
movl $0x3, %edi
movl $0x3dd, %ecx            # imm = 0x3DD
jmp 0x1b1be
xorl %eax, %eax
callq 0x17d59
cmpl $0x3, (%rax)
jl 0x1b1c5
leaq 0x9a2e(%rip), %rsi      # 0x24bd4
leaq 0x9a87(%rip), %rdx      # 0x24c34
leaq 0x9ad2(%rip), %r8       # 0x24c86
movl $0x3, %edi
movl $0x3df, %ecx            # imm = 0x3DF
xorl %eax, %eax
callq 0x17d92
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movl %ebp, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq