// File: /workspace/github_c_makefile_star2_O2/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 $0x68, %rsp
movl %edx, %ebp
movq %rsi, %r14
movq %rdi, %rbx
movslq 0xc(%rdi), %r15
xorps %xmm0, %xmm0
movups %xmm0, 0x30(%rsp)
movups %xmm0, 0x20(%rsp)
movups %xmm0, 0x10(%rsp)
movl %edx, 0x8(%rsp)
movl %r15d, 0xc(%rsp)
movzwl (%rsi), %esi
pushq $0x2
popq %rdx
callq 0x1bbcd
cmpl $0x2, %eax
jne 0x1a421
movzwl 0x2(%r14), %esi
pushq $0x2
popq %rdx
movq %rbx, %rdi
callq 0x1bbcd
cmpl $0x2, %eax
jne 0x1a421
movzwl 0x4(%r14), %esi
pushq $0x2
popq %rdx
movq %rbx, %rdi
callq 0x1bbcd
cmpl $0x2, %eax
jne 0x1a421
movzwl 0x6(%r14), %esi
pushq $0x2
popq %rdx
movq %rbx, %rdi
callq 0x1bbcd
cmpl $0x2, %eax
jne 0x1a421
movzwl 0x8(%r14), %esi
pushq $0x2
popq %rdx
movq %rbx, %rdi
callq 0x1bbcd
cmpl $0x2, %eax
jne 0x1a421
movzwl 0xa(%r14), %esi
pushq $0x2
popq %rdx
movq %rbx, %rdi
callq 0x1bbcd
cmpl $0x2, %eax
jne 0x1a421
testl %ebp, %ebp
je 0x1a3b9
movslq 0xc(%rbx), %rcx
movl %ecx, %eax
subl %r15d, %eax
cmpq %r15, %rcx
jle 0x1a3b9
movq (%rbx), %rdx
decl %eax
cmpb $0x0, -0x1(%rdx,%rcx)
leaq -0x1(%rcx), %rcx
jne 0x1a3a0
movl %eax, 0x20(%rsp)
movq %r15, 0x50(%rsp)
pushq $0xa
popq %r13
pushq $-0x1
popq %r15
pushq $0x2
popq %r12
movzwl 0x4(%r14), %eax
incq %r15
cmpq %rax, %r15
jae 0x1a43d
movq 0x10(%r14), %rbp
movq -0xa(%rbp,%r13), %rdx
leaq 0x8(%rsp), %rdi
movq %rbx, %rsi
callq 0x1a760
testl %eax, %eax
jne 0x1a421
movzwl -0x2(%rbp,%r13), %esi
movq %rbx, %rdi
movl %r12d, %edx
callq 0x1bbcd
cmpl $0x2, %eax
jne 0x1a421
movzwl (%rbp,%r13), %esi
addq $0x10, %r13
movq %rbx, %rdi
movl %r12d, %edx
callq 0x1bbcd
cmpl $0x2, %eax
je 0x1a3ca
leaq 0x10(%rsp), %rdi
callq 0x1b42b
pushq $-0x1
popq %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movzwl 0x6(%r14), %eax
movzwl 0x8(%r14), %ecx
addq %rax, %rcx
movzwl 0xa(%r14), %eax
addq %rcx, %rax
movq %rax, 0x58(%rsp)
xorl %r13d, %r13d
cmpq 0x58(%rsp), %r13
je 0x1a74a
movq 0x18(%r14), %rbp
movq %r13, %r15
shlq $0x5, %r15
movq (%rbp,%r15), %rdx
leaq 0x8(%rsp), %rdi
movq %rbx, %rsi
callq 0x1a760
testl %eax, %eax
jne 0x1a421
addq %r15, %rbp
movzwl 0x8(%rbp), %esi
movq %rbx, %rdi
pushq $0x2
popq %r15
movl %r15d, %edx
callq 0x1bbcd
cmpl $0x2, %eax
jne 0x1a421
movzwl 0xa(%rbp), %esi
movq %rbx, %rdi
movl %r15d, %edx
callq 0x1bbcd
cmpl $0x2, %eax
jne 0x1a421
movl 0xc(%rbp), %esi
movq %rbx, %rdi
pushq $0x4
popq %rdx
callq 0x1bbcd
cmpl $0x4, %eax
jne 0x1a421
movl 0xc(%rbx), %eax
movl %eax, 0x44(%rsp)
movzwl 0x10(%rbp), %esi
movq %rbx, %rdi
movl %r15d, %edx
callq 0x1bbcd
cmpl $0x2, %eax
jne 0x1a421
movq %r13, 0x60(%rsp)
movzwl 0x8(%rbp), %eax
leal -0x2(%rax), %ecx
cmpl $0xd, %ecx
ja 0x1a54d
leaq 0x8655(%rip), %rdx      # 0x22b5c
movslq (%rdx,%rcx,4), %rax
addq %rdx, %rax
jmpq *%rax
movq 0x18(%rbp), %rdx
leaq 0x8(%rsp), %rdi
movq %rbx, %rsi
callq 0x1a760
testl %eax, %eax
jne 0x1a421
movl 0xc(%rbx), %edx
movl 0x44(%rsp), %esi
subl %esi, %edx
addl $-0x2, %edx
movq %rbx, %rdi
pushq $0x2
popq %rcx
callq 0x1bcd8
movq 0x60(%rsp), %r13
incq %r13
jmp 0x1a45a
cmpl $0x29, %eax
jne 0x1a5e0
movq 0x18(%rbp), %rax
movq %rax, 0x48(%rsp)
testq %rax, %rax
je 0x1a529
pushq $-0x1
popq %rbp
xorl %r15d, %r15d
movq 0x48(%rsp), %rax
movslq 0x8(%rax), %rax
incq %rbp
cmpq %rax, %rbp
jge 0x1a529
movq 0x48(%rsp), %rax
movq (%rax), %r13
movzwl (%r13,%r15), %esi
movq %rbx, %rdi
pushq $0x2
popq %rdx
callq 0x1bbcd
cmpl $0x2, %eax
jne 0x1a421
movzwl 0x2(%r13,%r15), %esi
movq %rbx, %rdi
pushq $0x2
popq %rdx
callq 0x1bbcd
cmpl $0x2, %eax
jne 0x1a421
leaq 0x10(%r15), %r12
movq 0x8(%r13,%r15), %rsi
movzwl 0x2(%r13,%r15), %edx
movq %rbx, %rdi
callq 0x1ba37
movzwl 0x2(%r13,%r15), %ecx
movq %r12, %r15
cmpl %ecx, %eax
je 0x1a56a
jmp 0x1a421
movq 0x18(%rbp), %rsi
testq %rsi, %rsi
je 0x1a529
movzwl 0x10(%rbp), %edx
movq %rbx, %rdi
callq 0x1ba37
movzwl 0x10(%rbp), %ecx
cmpl %ecx, %eax
je 0x1a529
jmp 0x1a421
movq 0x18(%rbp), %r15
testq %r15, %r15
je 0x1a529
movq (%r15), %rsi
movq %rbx, %rdi
callq 0x1b477
testl %eax, %eax
jne 0x1a421
movq 0x8(%r15), %rsi
movq %rbx, %rdi
callq 0x1b477
jmp 0x1a521
movq 0x18(%rbp), %r15
testq %r15, %r15
je 0x1a529
movzwl (%r15), %esi
movq %rbx, %rdi
pushq $0x2
popq %rdx
callq 0x1bbcd
cmpl $0x2, %eax
je 0x1a741
jmp 0x1a421
movq 0x18(%rbp), %rbp
testq %rbp, %rbp
je 0x1a529
movq (%rbp), %rdx
leaq 0x8(%rsp), %rdi
movq %rbx, %rsi
callq 0x1a760
testl %eax, %eax
jne 0x1a421
movq 0x8(%rbp), %rdx
leaq 0x8(%rsp), %rdi
movq %rbx, %rsi
callq 0x1a760
testl %eax, %eax
jne 0x1a421
movl 0x10(%rbp), %esi
movq %rbx, %rdi
pushq $0x4
popq %rdx
callq 0x1bbcd
cmpl $0x4, %eax
jne 0x1a421
movl 0x14(%rbp), %esi
movq %rbx, %rdi
pushq $0x4
popq %rdx
callq 0x1bbcd
cmpl $0x4, %eax
jne 0x1a421
movl 0x18(%rbp), %esi
movq %rbx, %rdi
pushq $0x4
popq %rdx
callq 0x1bbcd
cmpl $0x4, %eax
jne 0x1a421
movl 0x1c(%rbp), %esi
movq %rbx, %rdi
pushq $0x4
popq %rdx
callq 0x1bbcd
cmpl $0x4, %eax
jne 0x1a421
movl 0x20(%rbp), %esi
movq %rbx, %rdi
pushq $0x4
popq %rdx
callq 0x1bbcd
cmpl $0x4, %eax
je 0x1a529
jmp 0x1a421
movq 0x18(%rbp), %r15
testq %r15, %r15
je 0x1a529
movq (%r15), %rdx
leaq 0x8(%rsp), %rdi
movq %rbx, %rsi
callq 0x1a760
testl %eax, %eax
jne 0x1a421
movq 0x8(%r15), %rdx
jmp 0x1a514
leaq 0x10(%rsp), %rdi
callq 0x1b42b
movl 0xc(%rbx), %eax
subl 0x50(%rsp), %eax
jmp 0x1a42e