// File: /workspace/github_c_makefile_star2_O1/GangZhuo[P]dohclient/src/ns_msg.c
// Function: ns_serialize
int ns_serialize(stream_t *s, ns_msg_t *msg, int compression)
{
	serialize_ctx ctx = { 0 };
	int i, rrcount, pos, rdlen, spos = s->pos;
	ns_qr_t *qr;
	ns_rr_t *rr;

#define check(n, c) \
	if ((c) != (n)) { \
		serialize_free(&ctx); \
		return -1; \
	}

	if (serialize_init(&ctx) != 0)
		return -1;

	ctx.compression = compression;
	ctx.startpos = spos;

	check(2, stream_writei16(s, msg->id));
	check(2, stream_writei16(s, msg->flags));
	check(2, stream_writei16(s, msg->qdcount));
	check(2, stream_writei16(s, msg->ancount));
	check(2, stream_writei16(s, msg->nscount));
	check(2, stream_writei16(s, msg->arcount));

	if (ctx.compression) {
		/* find '\0' */
		for (i = s->pos - 1; i >= spos; i--) {
			if (s->array[i] == 0) {
				ctx.rlabel.offset = i - spos;
				break;
			}
		}
	}

	for (i = 0; i < msg->qdcount; i++) {
		qr = msg->qrs + i;
		check(0, ns_write_domainname(&ctx, s, qr->qname));
		check(2, stream_writei16(s, qr->qtype));
		check(2, stream_writei16(s, qr->qclass));
	}

	for (i = 0, rrcount = ns_rrcount(msg);
		i < rrcount; i++) {
		rr = msg->rrs + i;
		check(0, ns_write_domainname(&ctx, s, rr->name));
		check(2, stream_writei16(s, rr->type));
		check(2, stream_writei16(s, rr->cls));
		check(4, stream_writei32(s, rr->ttl));
        pos = s->pos;
		check(2, stream_writei16(s, rr->rdlength));
        check(0, ns_write_rdata(&ctx, s, rr));
        rdlen = s->pos - pos - 2;
        stream_seti16(s, pos, rdlen);
	}

	serialize_free(&ctx);

#undef check

	return (s->pos - spos);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rsi, %r14
movq %rdi, %rbx
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsp)
movaps %xmm0, 0x10(%rsp)
movaps %xmm0, (%rsp)
xorl %eax, %eax
movq %rax, 0x30(%rsp)
movslq 0xc(%rdi), %r13
movaps %xmm0, (%rsp)
movq %rax, 0x30(%rsp)
movaps %xmm0, 0x20(%rsp)
movaps %xmm0, 0x10(%rsp)
movl %edx, (%rsp)
movl %r13d, 0x4(%rsp)
movzwl (%rsi), %esi
movl $0x2, %edx
callq 0x1d916
cmpl $0x2, %eax
jne 0x1c039
movzwl 0x2(%r14), %esi
movq %rbx, %rdi
movl $0x2, %edx
callq 0x1d916
cmpl $0x2, %eax
jne 0x1c039
movzwl 0x4(%r14), %esi
movq %rbx, %rdi
movl $0x2, %edx
callq 0x1d916
cmpl $0x2, %eax
jne 0x1c039
movzwl 0x6(%r14), %esi
movq %rbx, %rdi
movl $0x2, %edx
callq 0x1d916
cmpl $0x2, %eax
jne 0x1c039
movzwl 0x8(%r14), %esi
movq %rbx, %rdi
movl $0x2, %edx
callq 0x1d916
cmpl $0x2, %eax
jne 0x1c039
movzwl 0xa(%r14), %esi
movq %rbx, %rdi
movl $0x2, %edx
callq 0x1d916
cmpl $0x2, %eax
jne 0x1c039
cmpl $0x0, (%rsp)
je 0x1becb
movslq 0xc(%rbx), %rcx
movl %ecx, %eax
subl %r13d, %eax
cmpq %r13, %rcx
jle 0x1becb
movq (%rbx), %rdx
decl %eax
cmpb $0x0, -0x1(%rdx,%rcx)
leaq -0x1(%rcx), %rcx
jne 0x1beb2
movl %eax, 0x18(%rsp)
cmpw $0x0, 0x4(%r14)
je 0x1bf3f
movl $0xa, %r12d
xorl %ebp, %ebp
movq 0x10(%r14), %r15
movq -0xa(%r15,%r12), %rdx
movq %rsp, %rdi
movq %rbx, %rsi
callq 0x1c057
testl %eax, %eax
jne 0x1c039
movzwl -0x2(%r15,%r12), %esi
movq %rbx, %rdi
movl $0x2, %edx
callq 0x1d916
cmpl $0x2, %eax
jne 0x1c039
movzwl (%r15,%r12), %esi
movq %rbx, %rdi
movl $0x2, %edx
callq 0x1d916
cmpl $0x2, %eax
jne 0x1c039
incq %rbp
movzwl 0x4(%r14), %eax
addq $0x10, %r12
cmpq %rax, %rbp
jb 0x1bedb
movzwl 0x6(%r14), %ecx
movzwl 0x8(%r14), %eax
movq %rcx, %rsi
leal (%rax,%rcx), %edx
movzwl 0xa(%r14), %ecx
addl %ecx, %edx
je 0x1c027
addl %ecx, %eax
addl %eax, %esi
shll $0x5, %esi
movq %rsi, 0x40(%rsp)
xorl %r15d, %r15d
movq 0x18(%r14), %r12
movq (%r12,%r15), %rdx
movq %rsp, %rdi
movq %rbx, %rsi
callq 0x1c057
testl %eax, %eax
jne 0x1c039
movzwl 0x8(%r12,%r15), %esi
movq %rbx, %rdi
movl $0x2, %edx
callq 0x1d916
cmpl $0x2, %eax
jne 0x1c039
movzwl 0xa(%r12,%r15), %esi
movq %rbx, %rdi
movl $0x2, %edx
callq 0x1d916
cmpl $0x2, %eax
jne 0x1c039
movl 0xc(%r12,%r15), %esi
movq %rbx, %rdi
movl $0x4, %edx
callq 0x1d916
cmpl $0x4, %eax
jne 0x1c039
movl 0xc(%rbx), %ebp
movzwl 0x10(%r12,%r15), %esi
movq %rbx, %rdi
movl $0x2, %edx
callq 0x1d916
cmpl $0x2, %eax
jne 0x1c039
addq %r15, %r12
movq %rsp, %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x1c2e9
testl %eax, %eax
jne 0x1c039
movl 0xc(%rbx), %edx
subl %ebp, %edx
addl $-0x2, %edx
movq %rbx, %rdi
movl %ebp, %esi
movl $0x2, %ecx
callq 0x1da17
addq $0x20, %r15
cmpq %r15, 0x40(%rsp)
jne 0x1bf6b
leaq 0x8(%rsp), %rdi
callq 0x1d0b0
movl 0xc(%rbx), %eax
subl %r13d, %eax
jmp 0x1c048
leaq 0x8(%rsp), %rdi
callq 0x1d0b0
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq