// File: /workspace/github_c_makefile_star2_O0/GangZhuo[P]dohclient/src/channel_tcp.c
// Function: tcp_create_nsmsg
static int tcp_create_nsmsg(stream_t *s, tcpreq_t *rq, subnet_t *subnet)
{
	int r = 0;
	ns_msg_t msg;
	int len;

	init_ns_msg(&msg);

	msg.id = rq->id;
	msg.flags = rq->flags;
	msg.qdcount = 1;
	msg.qrs = ns_qr_clone(&rq->qr, 1);

	if (subnet) {
		ns_rr_t* rr;
		rr = ns_find_opt_rr(&msg);
		if (rr == NULL) {
			rr = ns_add_optrr(&msg);
			if (rr == NULL) {
				loge("Can't add option record to ns_msg_t\n");
				ns_msg_free(&msg);
				return -1;
			}
		}

		rr->cls = NS_PAYLOAD_SIZE; /* reset edns payload size */

		if (ns_optrr_set_ecs(rr, (struct sockaddr*)&subnet->addr, subnet->mask, 0) != 0) {
			loge("Can't add ecs option\n");
			ns_msg_free(&msg);
			return -1;
		}
	}

	stream_reset(s);

	if (s->cap < NS_PAYLOAD_SIZE) {
		if (stream_set_cap(s, NS_PAYLOAD_SIZE)) {
			loge("stream_set_cap() error\n");
			ns_msg_free(&msg);
			return -1;
		}
	}

	// write length
	stream_writei16(s, 0);

	if ((len = ns_serialize(s, &msg, 0)) <= 0) {
		loge("ns_serialize() error\n");
		stream_free(s);
		ns_msg_free(&msg);
		return -1;
	}

	ns_msg_free(&msg);

	s->pos = 0;

	stream_seti16(s, 0, len);

	return len;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movl $0x0, -0x24(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x270f0
movq -0x18(%rbp), %rax
movw (%rax), %ax
movw %ax, -0x48(%rbp)
movq -0x18(%rbp), %rax
movw 0x2(%rax), %ax
movw %ax, -0x46(%rbp)
movw $0x1, -0x44(%rbp)
movq -0x18(%rbp), %rdi
addq $0x8, %rdi
movl $0x1, %esi
callq 0x271c0
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x20(%rbp)
je 0x169a5
leaq -0x48(%rbp), %rdi
movl $0x29, %esi
callq 0x29010
movq %rax, -0x58(%rbp)
cmpq $0x0, -0x58(%rbp)
jne 0x1692c
leaq -0x48(%rbp), %rdi
callq 0x29220
movq %rax, -0x58(%rbp)
cmpq $0x0, -0x58(%rbp)
jne 0x1692a
jmp 0x168e1
movb $0x0, %al
callq 0x242d0
cmpl $0x3, (%rax)
jl 0x16913
movl $0x3, %edi
leaq 0x1ff37(%rip), %rsi     # 0x36830
leaq 0x20029(%rip), %rdx     # 0x36929
movl $0x137, %ecx            # imm = 0x137
leaq 0x1fb20(%rip), %r8      # 0x3642c
movb $0x0, %al
callq 0x243b0
jmp 0x16915
leaq -0x48(%rbp), %rdi
callq 0x27870
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x16acd
jmp 0x1692c
movq -0x58(%rbp), %rax
movw $0x400, 0xa(%rax)       # imm = 0x400
movq -0x58(%rbp), %rdi
movq -0x20(%rbp), %rsi
addq $0x10, %rsi
movq -0x20(%rbp), %rax
movl 0x90(%rax), %edx
xorl %ecx, %ecx
callq 0x29e00
cmpl $0x0, %eax
je 0x169a3
jmp 0x1695a
movb $0x0, %al
callq 0x242d0
cmpl $0x3, (%rax)
jl 0x1698c
movl $0x3, %edi
leaq 0x1febe(%rip), %rsi     # 0x36830
leaq 0x1ffb0(%rip), %rdx     # 0x36929
movl $0x140, %ecx            # imm = 0x140
leaq 0x1facc(%rip), %r8      # 0x36451
movb $0x0, %al
callq 0x243b0
jmp 0x1698e
leaq -0x48(%rbp), %rdi
callq 0x27870
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x16acd
jmp 0x169a5
movq -0x10(%rbp), %rax
movl $0x0, 0xc(%rax)
movq -0x10(%rbp), %rax
movl $0x0, 0x8(%rax)
movq -0x10(%rbp), %rax
cmpl $0x400, 0x10(%rax)      # imm = 0x400
jge 0x16a28
movq -0x10(%rbp), %rdi
movl $0x400, %esi            # imm = 0x400
callq 0x2d900
cmpl $0x0, %eax
je 0x16a26
jmp 0x169dd
movb $0x0, %al
callq 0x242d0
cmpl $0x3, (%rax)
jl 0x16a0f
movl $0x3, %edi
leaq 0x1fe3b(%rip), %rsi     # 0x36830
leaq 0x1ff2d(%rip), %rdx     # 0x36929
movl $0x14a, %ecx            # imm = 0x14A
leaq 0x1f313(%rip), %r8      # 0x35d1b
movb $0x0, %al
callq 0x243b0
jmp 0x16a11
leaq -0x48(%rbp), %rdi
callq 0x27870
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x16acd
jmp 0x16a28
movq -0x10(%rbp), %rdi
xorl %esi, %esi
movl $0x2, %edx
callq 0x2dc90
movq -0x10(%rbp), %rdi
leaq -0x48(%rbp), %rsi
xorl %edx, %edx
callq 0x29ec0
movl %eax, -0x4c(%rbp)
cmpl $0x0, %eax
jg 0x16aa0
jmp 0x16a51
movb $0x0, %al
callq 0x242d0
cmpl $0x3, (%rax)
jl 0x16a83
movl $0x3, %edi
leaq 0x1fdc7(%rip), %rsi     # 0x36830
leaq 0x1feb9(%rip), %rdx     # 0x36929
movl $0x154, %ecx            # imm = 0x154
leaq 0x1f2e6(%rip), %r8      # 0x35d62
movb $0x0, %al
callq 0x243b0
jmp 0x16a85
movq -0x10(%rbp), %rdi
callq 0x2d640
leaq -0x48(%rbp), %rdi
callq 0x27870
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x16acd
leaq -0x48(%rbp), %rdi
callq 0x27870
movq -0x10(%rbp), %rax
movl $0x0, 0xc(%rax)
movq -0x10(%rbp), %rdi
movl -0x4c(%rbp), %edx
xorl %esi, %esi
movl $0x2, %ecx
callq 0x2de60
movl -0x4c(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)