// File: /workspace/github_c_makefile_star2_O3/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
movl %edx, %ebp
movq %rsi, %r14
movq %rdi, %rbx
movslq 0xc(%rdi), %r13
xorps %xmm0, %xmm0
movups %xmm0, 0x30(%rsp)
movups %xmm0, 0x20(%rsp)
movups %xmm0, 0x10(%rsp)
movl %edx, 0x8(%rsp)
movl %r13d, 0xc(%rsp)
movzwl (%rsi), %esi
movl $0x2, %edx
callq 0x1d115
cmpl $0x2, %eax
jne 0x1b7fc
movzwl 0x2(%r14), %esi
movq %rbx, %rdi
movl $0x2, %edx
callq 0x1d115
cmpl $0x2, %eax
jne 0x1b7fc
movzwl 0x4(%r14), %esi
movq %rbx, %rdi
movl $0x2, %edx
callq 0x1d115
cmpl $0x2, %eax
jne 0x1b7fc
movzwl 0x6(%r14), %esi
movq %rbx, %rdi
movl $0x2, %edx
callq 0x1d115
cmpl $0x2, %eax
jne 0x1b7fc
movzwl 0x8(%r14), %esi
movq %rbx, %rdi
movl $0x2, %edx
callq 0x1d115
cmpl $0x2, %eax
jne 0x1b7fc
movzwl 0xa(%r14), %esi
movq %rbx, %rdi
movl $0x2, %edx
callq 0x1d115
cmpl $0x2, %eax
jne 0x1b7fc
testl %ebp, %ebp
je 0x1b684
movslq 0xc(%rbx), %rcx
movl %ecx, %eax
subl %r13d, %eax
cmpq %r13, %rcx
jle 0x1b684
movq (%rbx), %rdx
decl %eax
cmpb $0x0, -0x1(%rdx,%rcx)
leaq -0x1(%rcx), %rcx
jne 0x1b66b
movl %eax, 0x20(%rsp)
cmpw $0x0, 0x4(%r14)
je 0x1b6fa
movl $0xa, %r12d
xorl %ebp, %ebp
movq 0x10(%r14), %r15
movq -0xa(%r15,%r12), %rdx
leaq 0x8(%rsp), %rdi
movq %rbx, %rsi
callq 0x1b81a
testl %eax, %eax
jne 0x1b7fc
movzwl -0x2(%r15,%r12), %esi
movq %rbx, %rdi
movl $0x2, %edx
callq 0x1d115
cmpl $0x2, %eax
jne 0x1b7fc
movzwl (%r15,%r12), %esi
movq %rbx, %rdi
movl $0x2, %edx
callq 0x1d115
cmpl $0x2, %eax
jne 0x1b7fc
incq %rbp
movzwl 0x4(%r14), %eax
addq $0x10, %r12
cmpq %rax, %rbp
jb 0x1b694
movzwl 0x6(%r14), %ecx
movzwl 0x8(%r14), %eax
movq %rcx, %rsi
leal (%rax,%rcx), %edx
movzwl 0xa(%r14), %ecx
addl %ecx, %edx
je 0x1b7ea
addl %ecx, %eax
addl %eax, %esi
shll $0x5, %esi
movq %rsi, 0x40(%rsp)
xorl %r15d, %r15d
movq 0x18(%r14), %r12
movq (%r12,%r15), %rdx
leaq 0x8(%rsp), %rdi
movq %rbx, %rsi
callq 0x1b81a
testl %eax, %eax
jne 0x1b7fc
movzwl 0x8(%r12,%r15), %esi
movq %rbx, %rdi
movl $0x2, %edx
callq 0x1d115
cmpl $0x2, %eax
jne 0x1b7fc
movzwl 0xa(%r12,%r15), %esi
movq %rbx, %rdi
movl $0x2, %edx
callq 0x1d115
cmpl $0x2, %eax
jne 0x1b7fc
movl 0xc(%r12,%r15), %esi
movq %rbx, %rdi
movl $0x4, %edx
callq 0x1d115
cmpl $0x4, %eax
jne 0x1b7fc
movl 0xc(%rbx), %ebp
movzwl 0x10(%r12,%r15), %esi
movq %rbx, %rdi
movl $0x2, %edx
callq 0x1d115
cmpl $0x2, %eax
jne 0x1b7fc
addq %r15, %r12
leaq 0x8(%rsp), %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x1ba26
testl %eax, %eax
jne 0x1b7fc
movl 0xc(%rbx), %edx
subl %ebp, %edx
addl $-0x2, %edx
movq %rbx, %rdi
movl %ebp, %esi
movl $0x2, %ecx
callq 0x1d224
addq $0x20, %r15
cmpq %r15, 0x40(%rsp)
jne 0x1b726
leaq 0x10(%rsp), %rdi
callq 0x1c7e3
movl 0xc(%rbx), %eax
subl %r13d, %eax
jmp 0x1b80b
leaq 0x10(%rsp), %rdi
callq 0x1c7e3
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq