// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
leaq -0x40(%rbp), %rdi
xorl %esi, %esi
movl $0x18, %edx
callq 0x3200
movq -0x18(%rbp), %rax
movq %rax, -0x40(%rbp)
movl -0x1c(%rbp), %eax
movl %eax, -0x30(%rbp)
movl -0x1c(%rbp), %eax
movl %eax, -0x38(%rbp)
movl $0x0, -0x34(%rbp)
movl -0x38(%rbp), %eax
subl -0x34(%rbp), %eax
cmpl $0xc, %eax
jge 0x2853e
jmp 0x284fe
movb $0x0, %al
callq 0x242d0
cmpl $0x3, (%rax)
jl 0x28530
movl $0x3, %edi
leaq 0xfefe(%rip), %rsi      # 0x38414
leaq 0xff57(%rip), %rdx      # 0x38474
movl $0x3a4, %ecx            # imm = 0x3A4
leaq 0xff54(%rip), %r8       # 0x3847d
movb $0x0, %al
callq 0x243b0
jmp 0x28532
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x28a62
leaq -0x40(%rbp), %rdi
movl $0x2, %esi
callq 0x2e340
movw %ax, %cx
movq -0x10(%rbp), %rax
movw %cx, (%rax)
leaq -0x40(%rbp), %rdi
movl $0x2, %esi
callq 0x2e340
movw %ax, %cx
movq -0x10(%rbp), %rax
movw %cx, 0x2(%rax)
leaq -0x40(%rbp), %rdi
movl $0x2, %esi
callq 0x2e340
movw %ax, %cx
movq -0x10(%rbp), %rax
movw %cx, 0x4(%rax)
leaq -0x40(%rbp), %rdi
movl $0x2, %esi
callq 0x2e340
movw %ax, %cx
movq -0x10(%rbp), %rax
movw %cx, 0x6(%rax)
leaq -0x40(%rbp), %rdi
movl $0x2, %esi
callq 0x2e340
movw %ax, %cx
movq -0x10(%rbp), %rax
movw %cx, 0x8(%rax)
leaq -0x40(%rbp), %rdi
movl $0x2, %esi
callq 0x2e340
movw %ax, %cx
movq -0x10(%rbp), %rax
movw %cx, 0xa(%rax)
movq -0x10(%rbp), %rax
movzwl 0x4(%rax), %eax
cmpl $0x0, %eax
jle 0x287d0
movq -0x10(%rbp), %rax
movzwl 0x4(%rax), %eax
movl %eax, %edi
movl $0x10, %esi
callq 0x3260
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x10(%rbp), %rax
cmpq $0x0, 0x10(%rax)
jne 0x28650
jmp 0x28610
movb $0x0, %al
callq 0x242d0
cmpl $0x3, (%rax)
jl 0x28642
movl $0x3, %edi
leaq 0xfdec(%rip), %rsi      # 0x38414
leaq 0xfe45(%rip), %rdx      # 0x38474
movl $0x3b0, %ecx            # imm = 0x3B0
leaq 0xe85c(%rip), %r8       # 0x36e97
movb $0x0, %al
callq 0x243b0
jmp 0x28644
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x28a62
movl $0x0, -0x20(%rbp)
movl -0x20(%rbp), %eax
movq -0x10(%rbp), %rcx
movzwl 0x4(%rcx), %ecx
cmpl %ecx, %eax
jge 0x287ce
leaq -0x68(%rbp), %rdi
xorl %esi, %esi
movl $0x18, %edx
callq 0x3200
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x20(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq %rax, -0x48(%rbp)
leaq -0x68(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0x28a70
cmpl $0x0, %eax
jge 0x286e5
jmp 0x286a5
movb $0x0, %al
callq 0x242d0
cmpl $0x3, (%rax)
jl 0x286d7
movl $0x3, %edi
leaq 0xfd57(%rip), %rsi      # 0x38414
leaq 0xfdb0(%rip), %rdx      # 0x38474
movl $0x3b8, %ecx            # imm = 0x3B8
leaq 0xfdbf(%rip), %r8       # 0x3848f
movb $0x0, %al
callq 0x243b0
jmp 0x286d9
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x28a62
movq -0x68(%rbp), %rcx
movq -0x48(%rbp), %rax
movq %rcx, (%rax)
movl -0x38(%rbp), %eax
subl -0x34(%rbp), %eax
cmpl $0x4, %eax
jge 0x2873d
jmp 0x286fd
movb $0x0, %al
callq 0x242d0
cmpl $0x3, (%rax)
jl 0x2872f
movl $0x3, %edi
leaq 0xfcff(%rip), %rsi      # 0x38414
leaq 0xfd58(%rip), %rdx      # 0x38474
movl $0x3bc, %ecx            # imm = 0x3BC
leaq 0xfd55(%rip), %r8       # 0x3847d
movb $0x0, %al
callq 0x243b0
jmp 0x28731
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x28a62
leaq -0x40(%rbp), %rdi
movl $0x2, %esi
callq 0x2e340
movw %ax, %cx
movq -0x48(%rbp), %rax
movw %cx, 0x8(%rax)
leaq -0x40(%rbp), %rdi
movl $0x2, %esi
callq 0x2e340
movw %ax, %cx
movq -0x48(%rbp), %rax
movw %cx, 0xa(%rax)
movq -0x48(%rbp), %rax
movzwl 0x8(%rax), %eax
cmpl $0x0, %eax
jne 0x287be
jmp 0x2877e
movb $0x0, %al
callq 0x242d0
cmpl $0x3, (%rax)
jl 0x287b0
movl $0x3, %edi
leaq 0xfc7e(%rip), %rsi      # 0x38414
leaq 0xfcd7(%rip), %rdx      # 0x38474
movl $0x3c0, %ecx            # imm = 0x3C0
leaq 0xfcf8(%rip), %r8       # 0x384a1
movb $0x0, %al
callq 0x243b0
jmp 0x287b2
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x28a62
jmp 0x287c0
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x28657
jmp 0x287d0
movq -0x10(%rbp), %rax
movzwl 0x6(%rax), %eax
movq -0x10(%rbp), %rcx
movzwl 0x8(%rcx), %ecx
addl %ecx, %eax
movq -0x10(%rbp), %rcx
movzwl 0xa(%rcx), %ecx
addl %ecx, %eax
movl %eax, -0x24(%rbp)
cmpl $0x0, -0x24(%rbp)
jle 0x28a5b
movslq -0x24(%rbp), %rdi
movl $0x20, %esi
callq 0x3260
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x10(%rbp), %rax
cmpq $0x0, 0x18(%rax)
jne 0x2885f
jmp 0x2881f
movb $0x0, %al
callq 0x242d0
cmpl $0x3, (%rax)
jl 0x28851
movl $0x3, %edi
leaq 0xfbdd(%rip), %rsi      # 0x38414
leaq 0xfc36(%rip), %rdx      # 0x38474
movl $0x3cb, %ecx            # imm = 0x3CB
leaq 0xe64d(%rip), %r8       # 0x36e97
movb $0x0, %al
callq 0x243b0
jmp 0x28853
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x28a62
movl $0x0, -0x20(%rbp)
movl -0x20(%rbp), %eax
cmpl -0x24(%rbp), %eax
jge 0x28a59
leaq -0x80(%rbp), %rdi
xorl %esi, %esi
movl $0x18, %edx
callq 0x3200
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x20(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq %rax, -0x50(%rbp)
leaq -0x80(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0x28a70
cmpl $0x0, %eax
jge 0x288ed
jmp 0x288ad
movb $0x0, %al
callq 0x242d0
cmpl $0x3, (%rax)
jl 0x288df
movl $0x3, %edi
leaq 0xfb4f(%rip), %rsi      # 0x38414
leaq 0xfba8(%rip), %rdx      # 0x38474
movl $0x3d3, %ecx            # imm = 0x3D3
leaq 0xfbb7(%rip), %r8       # 0x3848f
movb $0x0, %al
callq 0x243b0
jmp 0x288e1
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x28a62
movq -0x80(%rbp), %rcx
movq -0x50(%rbp), %rax
movq %rcx, (%rax)
movl -0x38(%rbp), %eax
subl -0x34(%rbp), %eax
cmpl $0xa, %eax
jge 0x28945
jmp 0x28905
movb $0x0, %al
callq 0x242d0
cmpl $0x3, (%rax)
jl 0x28937
movl $0x3, %edi
leaq 0xfaf7(%rip), %rsi      # 0x38414
leaq 0xfb50(%rip), %rdx      # 0x38474
movl $0x3d7, %ecx            # imm = 0x3D7
leaq 0xfb4d(%rip), %r8       # 0x3847d
movb $0x0, %al
callq 0x243b0
jmp 0x28939
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x28a62
leaq -0x40(%rbp), %rdi
movl $0x2, %esi
callq 0x2e340
movw %ax, %cx
movq -0x50(%rbp), %rax
movw %cx, 0x8(%rax)
leaq -0x40(%rbp), %rdi
movl $0x2, %esi
callq 0x2e340
movw %ax, %cx
movq -0x50(%rbp), %rax
movw %cx, 0xa(%rax)
leaq -0x40(%rbp), %rdi
movl $0x4, %esi
callq 0x2e340
movl %eax, %ecx
movq -0x50(%rbp), %rax
movl %ecx, 0xc(%rax)
leaq -0x40(%rbp), %rdi
movl $0x2, %esi
callq 0x2e340
movw %ax, %cx
movq -0x50(%rbp), %rax
movw %cx, 0x10(%rax)
movl -0x38(%rbp), %eax
subl -0x34(%rbp), %eax
movq -0x50(%rbp), %rcx
movzwl 0x10(%rcx), %ecx
cmpl %ecx, %eax
jge 0x289f8
jmp 0x289bb
movb $0x0, %al
callq 0x242d0
cmpl $0x3, (%rax)
jl 0x289ed
movl $0x3, %edi
leaq 0xfa41(%rip), %rsi      # 0x38414
leaq 0xfa9a(%rip), %rdx      # 0x38474
movl $0x3dd, %ecx            # imm = 0x3DD
leaq 0xfa97(%rip), %r8       # 0x3847d
movb $0x0, %al
callq 0x243b0
jmp 0x289ef
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x28a62
movq -0x50(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0x28f40
cmpl $0x0, %eax
jge 0x28a49
jmp 0x28a0c
movb $0x0, %al
callq 0x242d0
cmpl $0x3, (%rax)
jl 0x28a3e
movl $0x3, %edi
leaq 0xf9f0(%rip), %rsi      # 0x38414
leaq 0xfa49(%rip), %rdx      # 0x38474
movl $0x3df, %ecx            # imm = 0x3DF
leaq 0xfa8f(%rip), %r8       # 0x384c6
movb $0x0, %al
callq 0x243b0
jmp 0x28a40
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x28a62
jmp 0x28a4b
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x28866
jmp 0x28a5b
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x80, %rsp
popq %rbp
retq
nop