// File: /workspace/github_c_makefile_star2_O2/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 $0x88, %rsp
andq $0x0, 0x30(%rsp)
movq %rsi, 0x20(%rsp)
movl %edx, 0x30(%rsp)
movl %edx, 0x28(%rsp)
andl $0x0, 0x2c(%rsp)
pushq $-0x1
popq %rbx
cmpl $0xb, %edx
jg 0x18ef9
xorl %eax, %eax
callq 0x165f5
cmpl $0x3, (%rax)
jl 0x19773
leaq 0x9d2d(%rip), %rsi      # 0x22c04
leaq 0x9d86(%rip), %rdx      # 0x22c64
leaq 0x9d88(%rip), %r8       # 0x22c6d
pushq $0x3
popq %rdi
movl $0x3a4, %ecx            # imm = 0x3A4
xorl %eax, %eax
callq 0x1662e
jmp 0x19773
movq %rdi, %r14
leaq 0x20(%rsp), %r15
pushq $0x2
popq %rbp
movq %r15, %rdi
movl %ebp, %esi
callq 0x1bfb1
movw %ax, (%r14)
movq %r15, %rdi
movl %ebp, %esi
callq 0x1bfb1
movw %ax, 0x2(%r14)
movq %r15, %rdi
movl %ebp, %esi
callq 0x1bfb1
movw %ax, 0x4(%r14)
movq %r15, %rdi
movl %ebp, %esi
callq 0x1bfb1
movw %ax, 0x6(%r14)
movq %r15, %rdi
movl %ebp, %esi
callq 0x1bfb1
movw %ax, 0x8(%r14)
movq %r15, %rdi
movl %ebp, %esi
callq 0x1bfb1
movw %ax, 0xa(%r14)
movzwl 0x4(%r14), %r13d
testq %r13, %r13
je 0x19054
pushq $0x10
popq %rsi
movq %r13, %rdi
callq 0x3250
movq %rax, 0x10(%r14)
testq %rax, %rax
je 0x1901a
pushq $0xa
popq %rbx
xorl %r15d, %r15d
leaq 0x20(%rsp), %r12
pushq $0x2
popq %rbp
movzwl %r13w, %eax
cmpq %rax, %r15
jae 0x1904c
xorps %xmm0, %xmm0
movaps %xmm0, (%rsp)
andq $0x0, 0x10(%rsp)
movq 0x10(%r14), %r13
movq %rsp, %rdi
movq %r12, %rsi
callq 0x19787
testl %eax, %eax
js 0x19616
movq (%rsp), %rax
movq %rax, -0xa(%r13,%rbx)
movl 0x28(%rsp), %eax
subl 0x2c(%rsp), %eax
cmpl $0x3, %eax
jle 0x19648
movq %r12, %rdi
movl %ebp, %esi
callq 0x1bfb1
movw %ax, -0x2(%r13,%rbx)
movq %r12, %rdi
movl %ebp, %esi
callq 0x1bfb1
movw %ax, (%r13,%rbx)
cmpw $0x0, -0x2(%r13,%rbx)
je 0x1967a
incq %r15
movzwl 0x4(%r14), %r13d
addq $0x10, %rbx
jmp 0x18f91
xorl %eax, %eax
callq 0x165f5
cmpl $0x3, (%rax)
jl 0x19773
leaq 0x9bd3(%rip), %rsi      # 0x22c04
leaq 0x9c2c(%rip), %rdx      # 0x22c64
leaq 0x8874(%rip), %r8       # 0x218b3
pushq $0x3
popq %rdi
movl $0x3b0, %ecx            # imm = 0x3B0
jmp 0x18eed
movzwl 0xa(%r14), %eax
pushq $-0x1
popq %rbx
movzwl 0x6(%r14), %ecx
movzwl 0x8(%r14), %edx
addl %ecx, %edx
movzwl %ax, %eax
addl %edx, %eax
je 0x195dd
movl %eax, %edi
pushq $0x20
popq %rsi
movq %rdi, 0x60(%rsp)
callq 0x3250
movq %rax, 0x18(%r14)
testq %rax, %rax
je 0x195e4
xorl %ebx, %ebx
leaq 0x20(%rsp), %r13
xorl %r15d, %r15d
cmpq 0x60(%rsp), %r15
je 0x19773
xorps %xmm0, %xmm0
movaps %xmm0, 0x70(%rsp)
andq $0x0, 0x80(%rsp)
movq %r15, %rbp
shlq $0x5, %rbp
addq 0x18(%r14), %rbp
leaq 0x70(%rsp), %rdi
movq %r13, %rsi
callq 0x19787
testl %eax, %eax
js 0x196ac
movq 0x70(%rsp), %rax
movq %rax, (%rbp)
movl 0x28(%rsp), %eax
subl 0x2c(%rsp), %eax
cmpl $0x9, %eax
jle 0x196de
movq %r15, 0x68(%rsp)
movq %r13, %rdi
pushq $0x2
popq %r15
movl %r15d, %esi
callq 0x1bfb1
movw %ax, 0x8(%rbp)
movq %r13, %rdi
movl %r15d, %esi
callq 0x1bfb1
movw %ax, 0xa(%rbp)
movq %r13, %rdi
pushq $0x4
popq %rsi
callq 0x1bfb1
movl %eax, 0xc(%rbp)
movq %r13, %rdi
movl %r15d, %esi
callq 0x1bfb1
movw %ax, 0x10(%rbp)
movl 0x28(%rsp), %edx
movslq 0x2c(%rsp), %rcx
subl %ecx, %edx
movzwl %ax, %r12d
cmpl %r12d, %edx
jl 0x1970d
movzwl 0x8(%rbp), %eax
leal -0x2(%rax), %esi
cmpl $0xd, %esi
ja 0x191a2
leaq 0x99ce(%rip), %rcx      # 0x22b24
movslq (%rcx,%rsi,4), %rax
addq %rcx, %rax
jmpq *%rax
xorps %xmm0, %xmm0
movaps %xmm0, (%rsp)
andq $0x0, 0x10(%rsp)
movq %rsp, %rdi
movq %r13, %rsi
callq 0x19787
pushq $-0x1
popq %rcx
testl %eax, %eax
js 0x19188
movq (%rsp), %rax
movq %rax, 0x18(%rbp)
xorl %ecx, %ecx
testl %ecx, %ecx
js 0x19740
movq 0x68(%rsp), %r15
incq %r15
leaq 0x20(%rsp), %r13
jmp 0x19091
cmpl $0x29, %eax
jne 0x19235
andq $0x0, 0x10(%rsp)
addq 0x20(%rsp), %rcx
movq %rcx, (%rsp)
movl %edx, 0x10(%rsp)
movzwl 0x10(%rbp), %eax
movl %eax, 0x8(%rsp)
andl $0x0, 0xc(%rsp)
xorl %r13d, %r13d
xorl %r12d, %r12d
xorl %r15d, %r15d
xorl %ecx, %ecx
movq %rsp, %rbx
subl %ecx, %eax
jle 0x19460
cmpl $0x4, %eax
jb 0x19740
movq %rbx, %rdi
pushq $0x2
popq %rbx
movl %ebx, %esi
callq 0x1bfb1
movq %rsp, %rdi
movl %ebx, %esi
movq %rsp, %rbx
callq 0x1bfb1
movl 0x8(%rsp), %ecx
subl 0xc(%rsp), %ecx
cmpl %eax, %ecx
jl 0x19740
movq %rbx, %rdi
movl %eax, %esi
pushq $0x1
popq %rdx
callq 0x1b8c3
movl 0x8(%rsp), %eax
movl 0xc(%rsp), %ecx
decq %r15
addq $0x10, %r12
incq %r13
jmp 0x191d9
movl %r12d, %edi
callq 0x34c0
testq %rax, %rax
je 0x19740
movq %rax, %r15
leaq 0x20(%rsp), %rdi
movq %rax, %rsi
movl %r12d, %edx
callq 0x1bb95
movzwl 0x10(%rbp), %ecx
cmpl %ecx, %eax
jne 0x19738
movq %r15, 0x18(%rbp)
jmp 0x19190
xorps %xmm0, %xmm0
movaps %xmm0, (%rsp)
andq $0x0, 0x10(%rsp)
movaps %xmm0, 0x40(%rsp)
andq $0x0, 0x50(%rsp)
movq %rsp, %rdi
movq %r13, %rsi
callq 0x1b2f1
pushq $-0x1
popq %rcx
testl %eax, %eax
js 0x19188
leaq 0x40(%rsp), %rdi
movq %r13, %rsi
callq 0x1b2f1
jmp 0x1942c
andq $0x0, 0x10(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, (%rsp)
pushq $-0x1
popq %rcx
cmpl $0x2, %edx
jb 0x19188
movq %r13, %rdi
pushq $0x2
popq %rsi
callq 0x1bfb1
movl %eax, %r15d
movq %rsp, %rdi
movq %r13, %rsi
callq 0x19787
pushq $-0x1
popq %rcx
testl %eax, %eax
js 0x19188
pushq $0x10
popq %rdi
callq 0x34c0
pushq $-0x1
popq %rcx
testq %rax, %rax
je 0x19188
movq (%rsp), %rcx
movq %rcx, 0x8(%rax)
movw %r15w, (%rax)
jmp 0x19182
xorps %xmm0, %xmm0
movaps %xmm0, (%rsp)
andq $0x0, 0x10(%rsp)
movaps %xmm0, 0x40(%rsp)
andq $0x0, 0x50(%rsp)
movq %rsp, %rdi
movq %r13, %rsi
callq 0x19787
pushq $-0x1
popq %rcx
testl %eax, %eax
js 0x19188
leaq 0x40(%rsp), %rdi
movq %r13, %rsi
callq 0x19787
pushq $-0x1
popq %rdx
movl %edx, %ecx
testl %eax, %eax
js 0x19188
movl 0x28(%rsp), %eax
subl 0x2c(%rsp), %eax
movl %edx, %ecx
cmpl $0x14, %eax
jl 0x19188
pushq $0x28
popq %rdi
callq 0x34c0
pushq $-0x1
popq %rcx
testq %rax, %rax
je 0x19188
movq %rax, %r12
movq (%rsp), %rax
movq %rax, (%r12)
movq 0x40(%rsp), %rax
movq %rax, 0x8(%r12)
movq %r13, %rdi
pushq $0x4
popq %r15
movl %r15d, %esi
callq 0x1bfb1
movl %eax, 0x10(%r12)
movq %r13, %rdi
movl %r15d, %esi
callq 0x1bfb1
movl %eax, 0x14(%r12)
movq %r13, %rdi
movl %r15d, %esi
callq 0x1bfb1
movl %eax, 0x18(%r12)
movq %r13, %rdi
movl %r15d, %esi
callq 0x1bfb1
movl %eax, 0x1c(%r12)
movq %r13, %rdi
movl %r15d, %esi
callq 0x1bfb1
movl %eax, 0x20(%r12)
movq %r12, 0x18(%rbp)
jmp 0x19186
xorps %xmm0, %xmm0
movaps %xmm0, (%rsp)
andq $0x0, 0x10(%rsp)
movaps %xmm0, 0x40(%rsp)
andq $0x0, 0x50(%rsp)
movq %rsp, %rdi
movq %r13, %rsi
callq 0x19787
pushq $-0x1
popq %rcx
testl %eax, %eax
js 0x19188
leaq 0x40(%rsp), %rdi
movq %r13, %rsi
callq 0x19787
pushq $-0x1
popq %rcx
testl %eax, %eax
js 0x19188
pushq $0x10
popq %rdi
callq 0x34c0
pushq $-0x1
popq %rcx
testq %rax, %rax
je 0x19188
movq (%rsp), %rcx
movq %rcx, (%rax)
movq 0x40(%rsp), %rcx
movq %rcx, 0x8(%rax)
jmp 0x19182
pushq $0x10
popq %rdi
callq 0x34c0
testq %rax, %rax
je 0x19740
movq 0x30(%rsp), %rcx
movq %rcx, 0x10(%rsp)
movups 0x20(%rsp), %xmm0
movaps %xmm0, (%rsp)
movzwl 0x10(%rbp), %ebx
addl 0x2c(%rsp), %ebx
negl %r15d
movl %ebx, 0x8(%rsp)
movl %r15d, 0x8(%rax)
movq %rax, %r15
movabsq $0xffffffff0, %rax      # imm = 0xFFFFFFFF0
andq %rax, %r12
pushq $0x1
popq %rdi
movq %r12, %rsi
callq 0x3250
movq %rax, %r12
movq %r15, 0x38(%rsp)
movq %rax, (%r15)
movq %rax, 0x58(%rsp)
testq %rax, %rax
je 0x1955d
subl 0xc(%rsp), %ebx
jle 0x1956c
cmpl $0x3, %ebx
jbe 0x1957e
movq %rsp, %r15
movq %r15, %rdi
pushq $0x2
popq %rsi
callq 0x1bfb1
movq 0x58(%rsp), %rbx
movw %ax, (%rbx)
movq %r15, %rdi
pushq $0x2
popq %rsi
callq 0x1bfb1
movw %ax, 0x2(%rbx)
movzwl %ax, %r15d
movq %r15, %rdi
callq 0x34c0
movq %rax, 0x8(%rbx)
testq %rax, %rax
movl $0x0, %ebx
je 0x19599
movl 0x8(%rsp), %ecx
movslq 0xc(%rsp), %rsi
subl %esi, %ecx
cmpl %r15d, %ecx
jl 0x195b2
addq (%rsp), %rsi
movq %rax, %rdi
movq %r15, %rdx
callq 0x32c0
movq %rsp, %rdi
movl %r15d, %esi
pushq $0x1
popq %rdx
callq 0x1b8c3
movl 0x8(%rsp), %ebx
addq $0x10, 0x58(%rsp)
jmp 0x194cb
movq 0x38(%rsp), %rdi
callq 0x3460
pushq $-0x1
popq %rcx
jmp 0x19577
movq 0x38(%rsp), %rax
movq %rax, 0x18(%rbp)
xorl %ecx, %ecx
xorl %ebx, %ebx
jmp 0x19188
addq $0x8, %r12
xorl %ebx, %ebx
subq $0x1, %r13
jb 0x195cb
movq (%r12), %rdi
callq 0x3460
addq $0x10, %r12
jmp 0x19584
addq $0x8, %r12
subq $0x1, %r13
jb 0x195cb
movq (%r12), %rdi
callq 0x3460
addq $0x10, %r12
jmp 0x1959d
addq $0x8, %r12
subq $0x1, %r13
jb 0x195cb
movq (%r12), %rdi
callq 0x3460
addq $0x10, %r12
jmp 0x195b6
movq 0x38(%rsp), %rdi
callq 0x3460
pushq $-0x1
popq %rcx
jmp 0x19188
xorl %ebx, %ebx
jmp 0x19773
xorl %eax, %eax
callq 0x165f5
cmpl $0x3, (%rax)
jl 0x19773
leaq 0x9609(%rip), %rsi      # 0x22c04
leaq 0x9662(%rip), %rdx      # 0x22c64
leaq 0x82aa(%rip), %r8       # 0x218b3
pushq $0x3
popq %rdi
movl $0x3cb, %ecx            # imm = 0x3CB
jmp 0x18eed
xorl %eax, %eax
callq 0x165f5
cmpl $0x3, (%rax)
jl 0x19770
leaq 0x95d7(%rip), %rsi      # 0x22c04
leaq 0x9630(%rip), %rdx      # 0x22c64
leaq 0x9644(%rip), %r8       # 0x22c7f
pushq $0x3
popq %rdi
movl $0x3b8, %ecx            # imm = 0x3B8
jmp 0x19769
xorl %eax, %eax
callq 0x165f5
cmpl $0x3, (%rax)
jl 0x19770
leaq 0x95a5(%rip), %rsi      # 0x22c04
leaq 0x95fe(%rip), %rdx      # 0x22c64
leaq 0x9600(%rip), %r8       # 0x22c6d
pushq $0x3
popq %rdi
movl $0x3bc, %ecx            # imm = 0x3BC
jmp 0x19769
xorl %eax, %eax
callq 0x165f5
cmpl $0x3, (%rax)
jl 0x19770
leaq 0x9573(%rip), %rsi      # 0x22c04
leaq 0x95cc(%rip), %rdx      # 0x22c64
leaq 0x95f2(%rip), %r8       # 0x22c91
pushq $0x3
popq %rdi
movl $0x3c0, %ecx            # imm = 0x3C0
jmp 0x19769
xorl %eax, %eax
callq 0x165f5
cmpl $0x3, (%rax)
jl 0x19770
leaq 0x9541(%rip), %rsi      # 0x22c04
leaq 0x959a(%rip), %rdx      # 0x22c64
leaq 0x95ae(%rip), %r8       # 0x22c7f
pushq $0x3
popq %rdi
movl $0x3d3, %ecx            # imm = 0x3D3
jmp 0x19769
xorl %eax, %eax
callq 0x165f5
cmpl $0x3, (%rax)
jl 0x19770
leaq 0x950f(%rip), %rsi      # 0x22c04
leaq 0x9568(%rip), %rdx      # 0x22c64
leaq 0x956a(%rip), %r8       # 0x22c6d
pushq $0x3
popq %rdi
movl $0x3d7, %ecx            # imm = 0x3D7
jmp 0x19769
xorl %eax, %eax
callq 0x165f5
cmpl $0x3, (%rax)
jl 0x19770
leaq 0x94e4(%rip), %rsi      # 0x22c04
leaq 0x953d(%rip), %rdx      # 0x22c64
leaq 0x953f(%rip), %r8       # 0x22c6d
pushq $0x3
popq %rdi
movl $0x3dd, %ecx            # imm = 0x3DD
jmp 0x19769
movq %r15, %rdi
callq 0x3460
xorl %eax, %eax
callq 0x165f5
cmpl $0x3, (%rax)
jl 0x19770
leaq 0x94b1(%rip), %rsi      # 0x22c04
leaq 0x950a(%rip), %rdx      # 0x22c64
leaq 0x9555(%rip), %r8       # 0x22cb6
pushq $0x3
popq %rdi
movl $0x3df, %ecx            # imm = 0x3DF
xorl %eax, %eax
callq 0x1662e
pushq $-0x1
popq %rbx
movl %ebx, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq