// File: /workspace/github_c_makefile_star2_O0/GangZhuo[P]dohclient/src/channel_chndoh.c
// Function: choose_best_nsmsg
static ns_msg_t* choose_best_nsmsg(channel_req_t* rq)
{
	channel_chndoh_t* c = rq->ctx;
	ns_msg_t* best = NULL;

	if (rq->result_num == 0) {
		best = NULL;
	}
	else if (rq->result_num == 1) {
		best = rq->results[0];
	}
	else if (!c->chnr) {
		best = rq->results[0];
	}
	else {
		int i;
		ns_msg_t* msg, *frn_result = NULL;
		int flags, have_ip, chn_doh, chn_ip, black;

		for (i = 0; i < rq->result_num; i++) {
			msg = rq->results[i];
			flags = get_nsmsg_flags(c, msg);

			black = flags & FLG_BLACKLIST;                /* In blacklist? */
			have_ip = flags & (FLG_A | FLG_AAAA);         /* Have IP? */
			chn_doh = msg->id == 0;                       /* From China DoH? */
			chn_ip = flags & (FLG_A_CHN | FLG_AAAA_CHN);  /* Result is China? */

			if (!chn_doh) {
				frn_result = msg;
			}

			if (black) {
				continue;
			}

			if (have_ip && chn_doh && chn_ip) {
				best = msg;
				rq->untrust = FALSE;
				break;
			}
			else if (!chn_doh) {
				best = msg;
			}
		}

		if (!best) {
			if (frn_result) {
				/* The result which from foreign server is preferred */
				best = frn_result;
			}
			else {
				/* The last one result is preferred */
				best = rq->results[rq->result_num - 1];
			}
		}
	}

	if (loglevel > LOG_DEBUG) {
		print_all_answers(rq, best);
	}

	return best;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq 0x60(%rax), %rax
movq %rax, -0x10(%rbp)
movq $0x0, -0x18(%rbp)
movq -0x8(%rbp), %rax
cmpl $0x0, 0x70(%rax)
jne 0x10f47
movq $0x0, -0x18(%rbp)
jmp 0x11093
movq -0x8(%rbp), %rax
cmpl $0x1, 0x70(%rax)
jne 0x10f65
movq -0x8(%rbp), %rax
movq 0x68(%rax), %rax
movq (%rax), %rax
movq %rax, -0x18(%rbp)
jmp 0x11091
movq -0x10(%rbp), %rax
cmpq $0x0, 0x20(%rax)
jne 0x10f84
movq -0x8(%rbp), %rax
movq 0x68(%rax), %rax
movq (%rax), %rax
movq %rax, -0x18(%rbp)
jmp 0x1108f
movq $0x0, -0x30(%rbp)
movl $0x0, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpl 0x70(%rcx), %eax
jge 0x11056
movq -0x8(%rbp), %rax
movq 0x68(%rax), %rax
movslq -0x1c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x11120
movl %eax, -0x34(%rbp)
movl -0x34(%rbp), %eax
andl $0x1000, %eax           # imm = 0x1000
movl %eax, -0x44(%rbp)
movl -0x34(%rbp), %eax
andl $0x12, %eax
movl %eax, -0x38(%rbp)
movq -0x28(%rbp), %rax
movzwl (%rax), %eax
cmpl $0x0, %eax
sete %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x3c(%rbp)
movl -0x34(%rbp), %eax
andl $0x24, %eax
movl %eax, -0x40(%rbp)
cmpl $0x0, -0x3c(%rbp)
jne 0x11007
movq -0x28(%rbp), %rax
movq %rax, -0x30(%rbp)
cmpl $0x0, -0x44(%rbp)
je 0x1100f
jmp 0x11048
cmpl $0x0, -0x38(%rbp)
je 0x11036
cmpl $0x0, -0x3c(%rbp)
je 0x11036
cmpl $0x0, -0x40(%rbp)
je 0x11036
movq -0x28(%rbp), %rax
movq %rax, -0x18(%rbp)
movq -0x8(%rbp), %rax
movl $0x0, 0x78(%rax)
jmp 0x11056
cmpl $0x0, -0x3c(%rbp)
jne 0x11044
movq -0x28(%rbp), %rax
movq %rax, -0x18(%rbp)
jmp 0x11046
jmp 0x11048
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x10f93
cmpq $0x0, -0x18(%rbp)
jne 0x1108d
cmpq $0x0, -0x30(%rbp)
je 0x1106e
movq -0x30(%rbp), %rax
movq %rax, -0x18(%rbp)
jmp 0x1108b
movq -0x8(%rbp), %rax
movq 0x68(%rax), %rax
movq -0x8(%rbp), %rcx
movl 0x70(%rcx), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x18(%rbp)
jmp 0x1108d
jmp 0x1108f
jmp 0x11091
jmp 0x11093
movb $0x0, %al
callq 0x242d0
cmpl $0x7, (%rax)
jle 0x110ac
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x11460
movq -0x18(%rbp), %rax
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)