// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
leaq -0x58(%rbp), %rdi
xorl %esi, %esi
movl $0x38, %edx
callq 0x3200
movq -0x10(%rbp), %rax
movl 0xc(%rax), %eax
movl %eax, -0x6c(%rbp)
leaq -0x58(%rbp), %rdi
callq 0x2a310
cmpl $0x0, %eax
je 0x29f0a
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x2a2fa
movl -0x1c(%rbp), %eax
movl %eax, -0x58(%rbp)
movl -0x6c(%rbp), %eax
movl %eax, -0x54(%rbp)
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rax
movzwl (%rax), %esi
movl $0x2, %edx
callq 0x2dc90
cmpl $0x2, %eax
je 0x29f45
leaq -0x58(%rbp), %rdi
callq 0x2a340
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x2a2fa
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rax
movzwl 0x2(%rax), %esi
movl $0x2, %edx
callq 0x2dc90
cmpl $0x2, %eax
je 0x29f75
leaq -0x58(%rbp), %rdi
callq 0x2a340
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x2a2fa
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rax
movzwl 0x4(%rax), %esi
movl $0x2, %edx
callq 0x2dc90
cmpl $0x2, %eax
je 0x29fa5
leaq -0x58(%rbp), %rdi
callq 0x2a340
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x2a2fa
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rax
movzwl 0x6(%rax), %esi
movl $0x2, %edx
callq 0x2dc90
cmpl $0x2, %eax
je 0x29fd5
leaq -0x58(%rbp), %rdi
callq 0x2a340
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x2a2fa
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rax
movzwl 0x8(%rax), %esi
movl $0x2, %edx
callq 0x2dc90
cmpl $0x2, %eax
je 0x2a005
leaq -0x58(%rbp), %rdi
callq 0x2a340
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x2a2fa
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rax
movzwl 0xa(%rax), %esi
movl $0x2, %edx
callq 0x2dc90
cmpl $0x2, %eax
je 0x2a035
leaq -0x58(%rbp), %rdi
callq 0x2a340
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x2a2fa
cmpl $0x0, -0x58(%rbp)
je 0x2a07e
movq -0x10(%rbp), %rax
movl 0xc(%rax), %eax
subl $0x1, %eax
movl %eax, -0x5c(%rbp)
movl -0x5c(%rbp), %eax
cmpl -0x6c(%rbp), %eax
jl 0x2a07c
movq -0x10(%rbp), %rax
movq (%rax), %rax
movslq -0x5c(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x0, %eax
jne 0x2a06f
movl -0x5c(%rbp), %eax
subl -0x6c(%rbp), %eax
movl %eax, -0x40(%rbp)
jmp 0x2a07c
jmp 0x2a071
movl -0x5c(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0x2a048
jmp 0x2a07e
movl $0x0, -0x5c(%rbp)
movl -0x5c(%rbp), %eax
movq -0x18(%rbp), %rcx
movzwl 0x4(%rcx), %ecx
cmpl %ecx, %eax
jge 0x2a14d
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x5c(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq %rax, -0x78(%rbp)
movq -0x10(%rbp), %rsi
movq -0x78(%rbp), %rax
movq (%rax), %rdx
leaq -0x58(%rbp), %rdi
callq 0x2a360
cmpl $0x0, %eax
je 0x2a0dd
leaq -0x58(%rbp), %rdi
callq 0x2a340
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x2a2fa
movq -0x10(%rbp), %rdi
movq -0x78(%rbp), %rax
movzwl 0x8(%rax), %esi
movl $0x2, %edx
callq 0x2dc90
cmpl $0x2, %eax
je 0x2a10d
leaq -0x58(%rbp), %rdi
callq 0x2a340
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x2a2fa
movq -0x10(%rbp), %rdi
movq -0x78(%rbp), %rax
movzwl 0xa(%rax), %esi
movl $0x2, %edx
callq 0x2dc90
cmpl $0x2, %eax
je 0x2a13d
leaq -0x58(%rbp), %rdi
callq 0x2a340
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x2a2fa
jmp 0x2a13f
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0x2a085
movl $0x0, -0x5c(%rbp)
movq -0x18(%rbp), %rax
movzwl 0x6(%rax), %eax
movq -0x18(%rbp), %rcx
movzwl 0x8(%rcx), %ecx
addl %ecx, %eax
movq -0x18(%rbp), %rcx
movzwl 0xa(%rcx), %ecx
addl %ecx, %eax
movl %eax, -0x60(%rbp)
movl -0x5c(%rbp), %eax
cmpl -0x60(%rbp), %eax
jge 0x2a2e4
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x5c(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq %rax, -0x80(%rbp)
movq -0x10(%rbp), %rsi
movq -0x80(%rbp), %rax
movq (%rax), %rdx
leaq -0x58(%rbp), %rdi
callq 0x2a360
cmpl $0x0, %eax
je 0x2a1c4
leaq -0x58(%rbp), %rdi
callq 0x2a340
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x2a2fa
movq -0x10(%rbp), %rdi
movq -0x80(%rbp), %rax
movzwl 0x8(%rax), %esi
movl $0x2, %edx
callq 0x2dc90
cmpl $0x2, %eax
je 0x2a1f4
leaq -0x58(%rbp), %rdi
callq 0x2a340
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x2a2fa
movq -0x10(%rbp), %rdi
movq -0x80(%rbp), %rax
movzwl 0xa(%rax), %esi
movl $0x2, %edx
callq 0x2dc90
cmpl $0x2, %eax
je 0x2a224
leaq -0x58(%rbp), %rdi
callq 0x2a340
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x2a2fa
movq -0x10(%rbp), %rdi
movq -0x80(%rbp), %rax
movl 0xc(%rax), %esi
movl $0x4, %edx
callq 0x2dc90
cmpl $0x4, %eax
je 0x2a253
leaq -0x58(%rbp), %rdi
callq 0x2a340
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x2a2fa
movq -0x10(%rbp), %rax
movl 0xc(%rax), %eax
movl %eax, -0x64(%rbp)
movq -0x10(%rbp), %rdi
movq -0x80(%rbp), %rax
movzwl 0x10(%rax), %esi
movl $0x2, %edx
callq 0x2dc90
cmpl $0x2, %eax
je 0x2a28a
leaq -0x58(%rbp), %rdi
callq 0x2a340
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x2a2fa
movq -0x10(%rbp), %rsi
movq -0x80(%rbp), %rdx
leaq -0x58(%rbp), %rdi
callq 0x2a400
cmpl $0x0, %eax
je 0x2a2b2
leaq -0x58(%rbp), %rdi
callq 0x2a340
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x2a2fa
movq -0x10(%rbp), %rax
movl 0xc(%rax), %eax
subl -0x64(%rbp), %eax
subl $0x2, %eax
movl %eax, -0x68(%rbp)
movq -0x10(%rbp), %rdi
movl -0x64(%rbp), %esi
movl -0x68(%rbp), %edx
movl $0x2, %ecx
callq 0x2de60
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0x2a173
leaq -0x58(%rbp), %rdi
callq 0x2a340
movq -0x10(%rbp), %rax
movl 0xc(%rax), %eax
subl -0x6c(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x80, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)