// File: /workspace/github_c_makefile_star2_O1/GangZhuo[P]dohclient/src/channel.c
// Function: channel_build_msg
int channel_build_msg(
	ns_msg_t *msg,
	const uint16_t id,
	const ns_flags_t *flags,
	const ns_qr_t* qr,
	void *ip, int family)
{
	if (init_ns_msg(msg)) {
		loge("init_ns_msg() error\n");
		return -1;
	}

	msg->id = id;
	ns_set_flags(msg, flags);

	if (qr) {
		msg->qrs = ns_qr_clone(qr, 1);
		if (!msg->qrs) {
			loge("ns_qr_clone() error\n");
			return -1;
		}
		msg->qdcount = 1;
	}

	if (qr && ip) {
		msg->rrs = (ns_rr_t*)malloc(sizeof(ns_rr_t));
		if (!msg->rrs) {
			loge("alloc \n");
			ns_msg_free(msg);
			return -1;
		}
		msg->ancount = 1;
		msg->rrs->name = strdup(qr->qname);
		msg->rrs->type = qr->qtype;
		msg->rrs->cls = qr->qclass;
		msg->rrs->ttl = 600;
		msg->rrs->rdlength = family == AF_INET ? 4 : 16;
		msg->rrs->rdata = (char*)malloc(msg->rrs->rdlength);
		if (!msg->rrs->rdata) {
			loge("alloc \n");
			ns_msg_free(msg);
			return -1;
		}

		memcpy(msg->rrs->rdata, ip, msg->rrs->rdlength);
	}

	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl %r9d, %r15d
movq %r8, %r14
movq %rcx, %r12
movq %rdx, %r13
movl %esi, %ebp
movq %rdi, %rbx
callq 0x19b12
testl %eax, %eax
je 0x8462
xorl %eax, %eax
callq 0x17d59
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpl $0x3, (%rax)
jl 0x85e4
leaq 0x1a16e(%rip), %rsi     # 0x225ac
leaq 0x1a175(%rip), %rdx     # 0x225ba
leaq 0x1a180(%rip), %r8      # 0x225cc
movl $0x3, %edi
movl $0x66, %ecx
xorl %eax, %eax
callq 0x17d92
jmp 0x85e4
movw %bp, (%rbx)
movq %rbx, %rdi
movq %r13, %rsi
callq 0x19ac1
testq %r12, %r12
je 0x8495
movq %r12, %rdi
movl $0x1, %esi
callq 0x19b6a
movq %rax, 0x10(%rbx)
testq %rax, %rax
je 0x853f
movw $0x1, 0x4(%rbx)
testq %r12, %r12
sete %al
testq %r14, %r14
sete %cl
xorl %ebp, %ebp
orb %al, %cl
jne 0x85e4
movl $0x20, %edi
callq 0x34b0
movq %rax, 0x18(%rbx)
testq %rax, %rax
je 0x8578
movq %rax, %r13
movw $0x1, 0x6(%rbx)
movq (%r12), %rdi
callq 0x3610
movq %rax, (%r13)
movzwl 0x8(%r12), %eax
movw %ax, 0x8(%r13)
movzwl 0xa(%r12), %eax
movw %ax, 0xa(%r13)
movl $0x258, 0xc(%r13)       # imm = 0x258
cmpl $0x2, %r15d
movl $0x4, %eax
movl $0x10, %ecx
cmovel %eax, %ecx
movw %cx, 0x10(%r13)
movl $0x4, %eax
movl $0x10, %r15d
cmoveq %rax, %r15
movq %r15, %rdi
callq 0x34b0
movq %rax, 0x18(%r13)
testq %rax, %rax
je 0x85a5
movq %rax, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x32b0
jmp 0x85e4
xorl %eax, %eax
callq 0x17d59
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpl $0x3, (%rax)
jl 0x85e4
leaq 0x1a051(%rip), %rsi     # 0x225ac
leaq 0x1a058(%rip), %rdx     # 0x225ba
leaq 0x1a078(%rip), %r8      # 0x225e1
movl $0x3, %edi
movl $0x70, %ecx
jmp 0x8456
xorl %eax, %eax
callq 0x17d59
cmpl $0x3, (%rax)
jl 0x85d7
leaq 0x1a021(%rip), %rsi     # 0x225ac
leaq 0x1a028(%rip), %rdx     # 0x225ba
leaq 0x1a05d(%rip), %r8      # 0x225f6
movl $0x3, %edi
movl $0x79, %ecx
jmp 0x85d0
xorl %eax, %eax
callq 0x17d59
cmpl $0x3, (%rax)
jl 0x85d7
leaq 0x19ff4(%rip), %rsi     # 0x225ac
leaq 0x19ffb(%rip), %rdx     # 0x225ba
leaq 0x1a030(%rip), %r8      # 0x225f6
movl $0x3, %edi
movl $0x85, %ecx
xorl %eax, %eax
callq 0x17d92
movq %rbx, %rdi
callq 0x1a1dd
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax)