// File: /workspace/github_c_makefile_star2_O2/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 0x18296
testl %eax, %eax
je 0x7e0f
xorl %eax, %eax
callq 0x165f5
pushq $-0x1
popq %rbp
cmpl $0x3, (%rax)
jl 0x7f62
leaq 0x1879d(%rip), %rsi     # 0x2058c
leaq 0x187a4(%rip), %rdx     # 0x2059a
leaq 0x187af(%rip), %r8      # 0x205ac
pushq $0x3
popq %rdi
pushq $0x66
popq %rcx
xorl %eax, %eax
callq 0x1662e
jmp 0x7f62
movw %bp, (%rbx)
movq %rbx, %rdi
movq %r13, %rsi
callq 0x18245
xorl %ebp, %ebp
testq %r12, %r12
je 0x7f62
pushq $0x1
popq %rsi
movq %r12, %rdi
callq 0x182e8
movq %rax, 0x10(%rbx)
testq %rax, %rax
je 0x7ecc
movw $0x1, 0x4(%rbx)
testq %r14, %r14
je 0x7f62
pushq $0x20
popq %rdi
callq 0x34c0
movq %rax, 0x18(%rbx)
testq %rax, %rax
je 0x7efe
movq %rax, %r13
movw $0x1, 0x6(%rbx)
movq (%r12), %rdi
callq 0x3620
movq %rax, (%r13)
movl 0x8(%r12), %eax
movl %eax, 0x8(%r13)
movl $0x258, 0xc(%r13)       # imm = 0x258
cmpl $0x2, %r15d
pushq $0x4
popq %rax
pushq $0x10
popq %rcx
cmovel %eax, %ecx
movw %cx, 0x10(%r13)
pushq $0x4
popq %rax
pushq $0x10
popq %r15
cmoveq %rax, %r15
movq %r15, %rdi
callq 0x34c0
movq %rax, 0x18(%r13)
testq %rax, %rax
je 0x7f27
movq %rax, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x32c0
jmp 0x7f62
xorl %eax, %eax
callq 0x165f5
pushq $-0x1
popq %rbp
cmpl $0x3, (%rax)
jl 0x7f62
leaq 0x186a6(%rip), %rsi     # 0x2058c
leaq 0x186ad(%rip), %rdx     # 0x2059a
leaq 0x186cd(%rip), %r8      # 0x205c1
pushq $0x3
popq %rdi
pushq $0x70
jmp 0x7e02
xorl %eax, %eax
callq 0x165f5
cmpl $0x3, (%rax)
jl 0x7f57
leaq 0x1867b(%rip), %rsi     # 0x2058c
leaq 0x18682(%rip), %rdx     # 0x2059a
leaq 0x186b7(%rip), %r8      # 0x205d6
pushq $0x3
popq %rdi
pushq $0x79
popq %rcx
jmp 0x7f50
xorl %eax, %eax
callq 0x165f5
cmpl $0x3, (%rax)
jl 0x7f57
leaq 0x18652(%rip), %rsi     # 0x2058c
leaq 0x18659(%rip), %rdx     # 0x2059a
leaq 0x1868e(%rip), %r8      # 0x205d6
pushq $0x3
popq %rdi
movl $0x85, %ecx
xorl %eax, %eax
callq 0x1662e
movq %rbx, %rdi
callq 0x188ce
pushq $-0x1
popq %rbp
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop