// File: /workspace/github_c_makefile_star2_O3/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
movq $0x0, 0x28(%rsp)
movq %rsi, 0x18(%rsp)
movl %edx, 0x28(%rsp)
movl %edx, 0x20(%rsp)
movl $0x0, 0x24(%rsp)
cmpl $0xb, %edx
jg 0x1a13e
xorl %eax, %eax
callq 0x176e5
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpl $0x3, (%rax)
jl 0x1aa05
leaq 0xaaca(%rip), %rsi      # 0x24be4
leaq 0xab23(%rip), %rdx      # 0x24c44
leaq 0xab25(%rip), %r8       # 0x24c4d
movl $0x3, %edi
movl $0x3a4, %ecx            # imm = 0x3A4
xorl %eax, %eax
callq 0x1771e
jmp 0x1aa05
movq %rdi, %rbx
leaq 0x18(%rsp), %r14
movq %r14, %rdi
movl $0x2, %esi
callq 0x1d588
movw %ax, (%rbx)
movq %r14, %rdi
movl $0x2, %esi
callq 0x1d588
movw %ax, 0x2(%rbx)
movq %r14, %rdi
movl $0x2, %esi
callq 0x1d588
movw %ax, 0x4(%rbx)
movq %r14, %rdi
movl $0x2, %esi
callq 0x1d588
movw %ax, 0x6(%rbx)
movq %r14, %rdi
movl $0x2, %esi
callq 0x1d588
movw %ax, 0x8(%rbx)
movq %r14, %rdi
movl $0x2, %esi
callq 0x1d588
movw %ax, 0xa(%rbx)
movzwl 0x4(%rbx), %edi
testq %rdi, %rdi
je 0x1a26c
movl $0x10, %esi
callq 0x3240
movq %rax, 0x10(%rbx)
testq %rax, %rax
je 0x1a81f
movl $0xa, %r12d
xorl %r13d, %r13d
movq %rsp, %r14
leaq 0x18(%rsp), %r15
xorps %xmm0, %xmm0
movaps %xmm0, (%rsp)
movq $0x0, 0x10(%rsp)
movq 0x10(%rbx), %rbp
movq %r14, %rdi
movq %r15, %rsi
callq 0x1aa19
testl %eax, %eax
js 0x1a898
movq (%rsp), %rax
movq %rax, -0xa(%rbp,%r12)
movl 0x20(%rsp), %eax
subl 0x24(%rsp), %eax
cmpl $0x3, %eax
jle 0x1a8cc
movq %r15, %rdi
movl $0x2, %esi
callq 0x1d588
movw %ax, -0x2(%rbp,%r12)
movq %r15, %rdi
movl $0x2, %esi
callq 0x1d588
movw %ax, (%rbp,%r12)
cmpw $0x0, -0x2(%rbp,%r12)
je 0x1a900
incq %r13
movzwl 0x4(%rbx), %eax
addq $0x10, %r12
cmpq %rax, %r13
jb 0x1a1e0
movzwl 0xa(%rbx), %eax
movzwl 0x6(%rbx), %ecx
movzwl 0x8(%rbx), %edx
addl %ecx, %edx
movzwl %ax, %eax
addl %edx, %eax
je 0x1a858
movl %eax, %edi
movl $0x20, %esi
movq %rdi, 0x68(%rsp)
callq 0x3240
movq %rax, 0x18(%rbx)
testq %rax, %rax
je 0x1a85f
leaq 0x18(%rsp), %r15
xorl %r14d, %r14d
xorps %xmm0, %xmm0
movaps %xmm0, 0x70(%rsp)
movq $0x0, 0x80(%rsp)
movq %r14, %r13
shlq $0x5, %r13
addq 0x18(%rbx), %r13
leaq 0x70(%rsp), %rdi
movq %r15, %rsi
callq 0x1aa19
testl %eax, %eax
js 0x1a934
movq 0x70(%rsp), %rax
movq %rax, (%r13)
movl 0x20(%rsp), %eax
subl 0x24(%rsp), %eax
cmpl $0x9, %eax
jle 0x1a968
movq %r15, %rdi
movl $0x2, %esi
callq 0x1d588
movw %ax, 0x8(%r13)
movq %r15, %rdi
movl $0x2, %esi
callq 0x1d588
movw %ax, 0xa(%r13)
movq %r15, %rdi
movl $0x4, %esi
callq 0x1d588
movl %eax, 0xc(%r13)
movq %r15, %rdi
movl $0x2, %esi
callq 0x1d588
movw %ax, 0x10(%r13)
movl 0x20(%rsp), %ecx
movslq 0x24(%rsp), %rdx
subl %edx, %ecx
movzwl %ax, %ebp
cmpl %ebp, %ecx
jl 0x1a999
movzwl 0x8(%r13), %eax
leal -0x2(%rax), %esi
cmpl $0xd, %esi
ja 0x1a3a2
leaq 0xa79e(%rip), %rdx      # 0x24b04
movslq (%rdx,%rsi,4), %rax
addq %rdx, %rax
jmpq *%rax
xorps %xmm0, %xmm0
movaps %xmm0, (%rsp)
movq $0x0, 0x10(%rsp)
movq %rsp, %rdi
movq %r15, %rsi
callq 0x1aa19
testl %eax, %eax
js 0x1a7f6
movq (%rsp), %rax
movq %rax, 0x18(%r13)
xorl %r12d, %r12d
jmp 0x1a7fc
cmpl $0x29, %eax
jne 0x1a437
movq $0x0, 0x10(%rsp)
addq 0x18(%rsp), %rdx
movq %rdx, (%rsp)
movl %ecx, 0x10(%rsp)
movl %ebp, 0x8(%rsp)
movl $0x0, 0xc(%rsp)
testw %bp, %bp
je 0x1a664
xorl %r15d, %r15d
xorl %r12d, %r12d
cmpl $0x4, %ebp
movq %rsp, %rbp
jb 0x1a9ce
movq %rbp, %rdi
movl $0x2, %esi
callq 0x1d588
movq %rbp, %rdi
movl $0x2, %esi
callq 0x1d588
movl 0x8(%rsp), %ecx
subl 0xc(%rsp), %ecx
cmpl %eax, %ecx
jl 0x1a9ce
movq %rbp, %rdi
movl %eax, %esi
movl $0x1, %edx
callq 0x1cde9
incl %r12d
movl 0x8(%rsp), %ebp
addq $0x10, %r15
subl 0xc(%rsp), %ebp
jg 0x1a3dc
jmp 0x1a66a
movl %ebp, %edi
callq 0x34b0
testq %rax, %rax
je 0x1a9ce
movq %rax, %r15
leaq 0x18(%rsp), %rdi
movq %rax, %rsi
movl %ebp, %edx
callq 0x1d0dd
movzwl 0x10(%r13), %ecx
cmpl %ecx, %eax
jne 0x1a9c6
movq %r15, 0x18(%r13)
jmp 0x1a805
xorps %xmm0, %xmm0
movaps %xmm0, (%rsp)
xorl %eax, %eax
movq %rax, 0x10(%rsp)
movq %rax, 0x50(%rsp)
movaps %xmm0, 0x40(%rsp)
movq %rsp, %rdi
movq %r15, %rsi
callq 0x1c694
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
testl %eax, %eax
js 0x1a7fc
leaq 0x40(%rsp), %rdi
movq %r15, %rsi
callq 0x1c694
jmp 0x1a634
xorps %xmm0, %xmm0
movaps %xmm0, (%rsp)
movq $0x0, 0x10(%rsp)
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
cmpl $0x2, %ecx
jb 0x1a7fc
movq %r15, %rdi
movl $0x2, %esi
callq 0x1d588
movl %eax, %ebp
movq %rsp, %rdi
movq %r15, %rsi
callq 0x1aa19
testl %eax, %eax
js 0x1a7fc
movl $0x10, %edi
callq 0x34b0
testq %rax, %rax
je 0x1a7fc
movq (%rsp), %rcx
movq %rcx, 0x8(%rax)
movw %bp, (%rax)
jmp 0x1a396
xorps %xmm0, %xmm0
movaps %xmm0, (%rsp)
xorl %eax, %eax
movq %rax, 0x10(%rsp)
movq %rax, 0x50(%rsp)
movaps %xmm0, 0x40(%rsp)
movq %rsp, %rdi
movq %r15, %rsi
callq 0x1aa19
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
testl %eax, %eax
js 0x1a7fc
leaq 0x40(%rsp), %rdi
movq %r15, %rsi
callq 0x1aa19
testl %eax, %eax
js 0x1a7fc
movl 0x20(%rsp), %eax
subl 0x24(%rsp), %eax
cmpl $0x14, %eax
jl 0x1a7fc
movl $0x28, %edi
callq 0x34b0
testq %rax, %rax
je 0x1a7fc
movq %rax, %r15
movq (%rsp), %rax
movq %rax, (%r15)
movq 0x40(%rsp), %rax
movq %rax, 0x8(%r15)
leaq 0x18(%rsp), %r12
movq %r12, %rdi
movl $0x4, %esi
callq 0x1d588
movl %eax, 0x10(%r15)
movq %r12, %rdi
movl $0x4, %esi
callq 0x1d588
movl %eax, 0x14(%r15)
movq %r12, %rdi
movl $0x4, %esi
callq 0x1d588
movl %eax, 0x18(%r15)
movq %r12, %rdi
movl $0x4, %esi
callq 0x1d588
movl %eax, 0x1c(%r15)
movq %r12, %rdi
movl $0x4, %esi
callq 0x1d588
movl %eax, 0x20(%r15)
movq %r15, 0x18(%r13)
jmp 0x1a39a
xorps %xmm0, %xmm0
movaps %xmm0, (%rsp)
xorl %eax, %eax
movq %rax, 0x10(%rsp)
movq %rax, 0x50(%rsp)
movaps %xmm0, 0x40(%rsp)
movq %rsp, %rdi
movq %r15, %rsi
callq 0x1aa19
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
testl %eax, %eax
js 0x1a7fc
leaq 0x40(%rsp), %rdi
movq %r15, %rsi
callq 0x1aa19
testl %eax, %eax
js 0x1a7fc
movl $0x10, %edi
callq 0x34b0
testq %rax, %rax
je 0x1a7fc
movq (%rsp), %rcx
movq %rcx, (%rax)
movq 0x40(%rsp), %rcx
movq %rcx, 0x8(%rax)
jmp 0x1a396
xorl %r12d, %r12d
xorl %r15d, %r15d
movl $0x10, %edi
callq 0x34b0
testq %rax, %rax
je 0x1a9ce
movq 0x28(%rsp), %rcx
movq %rcx, 0x10(%rsp)
movups 0x18(%rsp), %xmm0
movaps %xmm0, (%rsp)
movzwl 0x10(%r13), %ebp
addl 0x24(%rsp), %ebp
movl %ebp, 0x8(%rsp)
movl %r12d, 0x8(%rax)
movq %rax, 0x30(%rsp)
movq %r15, %rdi
callq 0x34b0
movq 0x30(%rsp), %rdi
movq %rax, (%rdi)
testq %rax, %rax
je 0x1a7f1
movl %r12d, %edx
shlq $0x4, %rdx
movq %rax, 0x38(%rsp)
movq %rax, %rdi
xorl %esi, %esi
movq %rdx, 0x60(%rsp)
callq 0x31e0
subl 0xc(%rsp), %ebp
jle 0x1a76f
movq 0x38(%rsp), %r15
cmpl $0x3, %ebp
jbe 0x1a779
movq %rsp, %rbp
movq %rbp, %rdi
movl $0x2, %esi
callq 0x1d588
movw %ax, (%r15)
movq %rbp, %rdi
movl $0x2, %esi
callq 0x1d588
movw %ax, 0x2(%r15)
movzwl %ax, %ebp
movq %rbp, %rdi
callq 0x34b0
movq %rax, 0x8(%r15)
testq %rax, %rax
je 0x1a7a0
movl 0x8(%rsp), %ecx
movslq 0xc(%rsp), %rsi
subl %esi, %ecx
cmpl %ebp, %ecx
jl 0x1a7c7
addq (%rsp), %rsi
movq %rax, %rdi
movq %rbp, %rdx
callq 0x32b0
movq %rsp, %rdi
movl %ebp, %esi
movl $0x1, %edx
callq 0x1cde9
movl 0x8(%rsp), %ebp
addq $0x10, %r15
subl 0xc(%rsp), %ebp
jg 0x1a6e9
movq 0x30(%rsp), %rax
jmp 0x1a396
testl %r12d, %r12d
movq 0x38(%rsp), %r12
movq 0x60(%rsp), %r13
je 0x1a7ec
xorl %r15d, %r15d
movq 0x8(%r12,%r15), %rdi
callq 0x3450
addq $0x10, %r15
cmpq %r15, %r13
jne 0x1a78b
jmp 0x1a7ec
testl %r12d, %r12d
movq 0x38(%rsp), %r12
movq 0x60(%rsp), %r13
je 0x1a7ec
xorl %r15d, %r15d
movq 0x8(%r12,%r15), %rdi
callq 0x3450
addq $0x10, %r15
cmpq %r15, %r13
jne 0x1a7b2
jmp 0x1a7ec
testl %r12d, %r12d
movq 0x38(%rsp), %r12
movq 0x60(%rsp), %r13
je 0x1a7ec
xorl %r15d, %r15d
movq 0x8(%r12,%r15), %rdi
callq 0x3450
addq $0x10, %r15
cmpq %r15, %r13
jne 0x1a7d9
movq 0x30(%rsp), %rdi
callq 0x3450
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
testl %r12d, %r12d
js 0x1a9ce
incq %r14
xorl %ebp, %ebp
cmpq 0x68(%rsp), %r14
leaq 0x18(%rsp), %r15
jne 0x1a2a7
jmp 0x1aa05
xorl %eax, %eax
callq 0x176e5
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpl $0x3, (%rax)
jl 0x1aa05
leaq 0xa3a9(%rip), %rsi      # 0x24be4
leaq 0xa402(%rip), %rdx      # 0x24c44
leaq 0x906a(%rip), %r8       # 0x238b3
movl $0x3, %edi
movl $0x3b0, %ecx            # imm = 0x3B0
jmp 0x1a132
xorl %ebp, %ebp
jmp 0x1aa05
xorl %eax, %eax
callq 0x176e5
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpl $0x3, (%rax)
jl 0x1aa05
leaq 0xa369(%rip), %rsi      # 0x24be4
leaq 0xa3c2(%rip), %rdx      # 0x24c44
leaq 0x902a(%rip), %r8       # 0x238b3
movl $0x3, %edi
movl $0x3cb, %ecx            # imm = 0x3CB
jmp 0x1a132
xorl %eax, %eax
callq 0x176e5
cmpl $0x3, (%rax)
jl 0x1aa00
leaq 0xa335(%rip), %rsi      # 0x24be4
leaq 0xa38e(%rip), %rdx      # 0x24c44
leaq 0xa3a2(%rip), %r8       # 0x24c5f
movl $0x3, %edi
movl $0x3b8, %ecx            # imm = 0x3B8
jmp 0x1a9f9
xorl %eax, %eax
callq 0x176e5
cmpl $0x3, (%rax)
jl 0x1aa00
leaq 0xa301(%rip), %rsi      # 0x24be4
leaq 0xa35a(%rip), %rdx      # 0x24c44
leaq 0xa35c(%rip), %r8       # 0x24c4d
movl $0x3, %edi
movl $0x3bc, %ecx            # imm = 0x3BC
jmp 0x1a9f9
xorl %eax, %eax
callq 0x176e5
cmpl $0x3, (%rax)
jl 0x1aa00
leaq 0xa2cd(%rip), %rsi      # 0x24be4
leaq 0xa326(%rip), %rdx      # 0x24c44
leaq 0xa34c(%rip), %r8       # 0x24c71
movl $0x3, %edi
movl $0x3c0, %ecx            # imm = 0x3C0
jmp 0x1a9f9
xorl %eax, %eax
callq 0x176e5
cmpl $0x3, (%rax)
jl 0x1aa00
leaq 0xa299(%rip), %rsi      # 0x24be4
leaq 0xa2f2(%rip), %rdx      # 0x24c44
leaq 0xa306(%rip), %r8       # 0x24c5f
movl $0x3, %edi
movl $0x3d3, %ecx            # imm = 0x3D3
jmp 0x1a9f9
xorl %eax, %eax
callq 0x176e5
cmpl $0x3, (%rax)
jl 0x1aa00
leaq 0xa265(%rip), %rsi      # 0x24be4
leaq 0xa2be(%rip), %rdx      # 0x24c44
leaq 0xa2c0(%rip), %r8       # 0x24c4d
movl $0x3, %edi
movl $0x3d7, %ecx            # imm = 0x3D7
jmp 0x1a9f9
xorl %eax, %eax
callq 0x176e5
cmpl $0x3, (%rax)
jl 0x1aa00
leaq 0xa238(%rip), %rsi      # 0x24be4
leaq 0xa291(%rip), %rdx      # 0x24c44
leaq 0xa293(%rip), %r8       # 0x24c4d
movl $0x3, %edi
movl $0x3dd, %ecx            # imm = 0x3DD
jmp 0x1a9f9
movq %r15, %rdi
callq 0x3450
xorl %eax, %eax
callq 0x176e5
cmpl $0x3, (%rax)
jl 0x1aa00
leaq 0xa203(%rip), %rsi      # 0x24be4
leaq 0xa25c(%rip), %rdx      # 0x24c44
leaq 0xa2a7(%rip), %r8       # 0x24c96
movl $0x3, %edi
movl $0x3df, %ecx            # imm = 0x3DF
xorl %eax, %eax
callq 0x1771e
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movl %ebp, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq