// File: /workspace/github_c_makefile_star2_O0/GangZhuo[P]dohclient/src/channel_chndoh.c
// Function: query_doh_addr_cb
static int query_doh_addr_cb(channel_t* ctx,
	int status,
	ns_msg_t* result,
	int fromcache,
	int trust,
	void* state)
{
	channel_chndoh_t* c = (channel_chndoh_t*)ctx;
	doh_server_t* doh = (doh_server_t*)state;
	const char* key;
	int i, rrcount;
	ns_rr_t* rr;
	uint16_t port;

	/* ignore failed result */
	if (status || !result || result->qdcount < 1) {
		if (doh->addr_expire < time(NULL) + 1 * 60 * 60) {
			doh->addr_expire += 60;
		}
		return 0;
	}

	key = msg_key(result);

	logi("new DoH server's IP: %s - %s\n",
		key, msg_answers(result));
	
	doh->addr_expire = time(NULL) + ns_get_ttl(result);

	if (doh->addr->addr.ss_family == AF_INET) {
		port = ((struct sockaddr_in*)&doh->addr->addr)->sin_port;
	}
	else {
		port = ((struct sockaddr_in6*)&doh->addr->addr)->sin6_port;
	}

	rrcount = result->ancount + result->nscount;
	for (i = 0; i < rrcount; i++) {
		rr = result->rrs + i;
		if (rr->type == NS_QTYPE_A) {
			struct in_addr* in = (struct in_addr*)rr->rdata;
			struct sockaddr_in* addr = (struct sockaddr_in*)&doh->addr->addr;
			memset(&doh->addr, 0, sizeof(sockaddr_t));
			doh->addr->addrlen = sizeof(struct sockaddr_in);
			addr->sin_family = AF_INET;
			addr->sin_port = port;
			memcpy(&addr->sin_addr, in, sizeof(struct in_addr));
			break;
		}
		else if (rr->type == NS_QTYPE_AAAA) {
			struct in6_addr* in6 = (struct in6_addr*)rr->rdata;
			struct sockaddr_in6* addr = (struct sockaddr_in6*)&doh->addr->addr;
			memset(&doh->addr, 0, sizeof(sockaddr_t));
			doh->addr->addrlen = sizeof(struct sockaddr_in6);
			addr->sin6_family = AF_INET;
			addr->sin6_port = port;
			memcpy(&addr->sin6_addr, in6, sizeof(struct in6_addr));
			break;
		}
	}

	if (result && !fromcache) {
		ns_msg_free(result);
		free(result);
	}
	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xa0, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
movl %ecx, -0x24(%rbp)
movl %r8d, -0x28(%rbp)
movq %r9, -0x30(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0x40(%rbp)
cmpl $0x0, -0x14(%rbp)
jne 0x1003b
cmpq $0x0, -0x20(%rbp)
je 0x1003b
movq -0x20(%rbp), %rax
movzwl 0x4(%rax), %eax
cmpl $0x1, %eax
jge 0x10082
movq -0x40(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x88(%rbp)
xorl %eax, %eax
movl %eax, %edi
callq 0x3330
movq %rax, %rcx
movq -0x88(%rbp), %rax
addq $0xe10, %rcx            # imm = 0xE10
cmpq %rcx, %rax
jge 0x10076
movq -0x40(%rbp), %rax
movq (%rax), %rcx
addq $0x3c, %rcx
movq %rcx, (%rax)
movl $0x0, -0x4(%rbp)
jmp 0x1028f
movq -0x20(%rbp), %rdi
callq 0x10500
movq %rax, -0x48(%rbp)
movb $0x0, %al
callq 0x242d0
cmpl $0x6, (%rax)
jl 0x100e0
movq -0x48(%rbp), %rax
movq %rax, -0x90(%rbp)
movq -0x20(%rbp), %rdi
callq 0x27de0
movq -0x90(%rbp), %r9
movl $0x6, %edi
leaq 0x25ebc(%rip), %rsi     # 0x35f7e
leaq 0x261b4(%rip), %rdx     # 0x3627d
movl $0xe4, %ecx
leaq 0x261ba(%rip), %r8      # 0x3628f
movq %rax, (%rsp)
movb $0x0, %al
callq 0x243b0
jmp 0x100e2
xorl %eax, %eax
movl %eax, %edi
callq 0x3330
movq %rax, -0x98(%rbp)
movq -0x20(%rbp), %rdi
callq 0x27120
movq -0x98(%rbp), %rcx
movl %eax, %eax
addq %rax, %rcx
movq -0x40(%rbp), %rax
movq %rcx, (%rax)
movq -0x40(%rbp), %rax
movzwl 0x8(%rax), %eax
cmpl $0x2, %eax
jne 0x10129
movq -0x40(%rbp), %rax
movw 0xa(%rax), %ax
movw %ax, -0x5a(%rbp)
jmp 0x10135
movq -0x40(%rbp), %rax
movw 0xa(%rax), %ax
movw %ax, -0x5a(%rbp)
movq -0x20(%rbp), %rax
movzwl 0x6(%rax), %eax
movq -0x20(%rbp), %rcx
movzwl 0x8(%rcx), %ecx
addl %ecx, %eax
movl %eax, -0x50(%rbp)
movl $0x0, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
cmpl -0x50(%rbp), %eax
jge 0x10269
movq -0x20(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x4c(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rax
movzwl 0x8(%rax), %eax
cmpl $0x1, %eax
jne 0x101e2
movq -0x58(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x68(%rbp)
movq -0x40(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x70(%rbp)
movq -0x40(%rbp), %rdi
addq $0x8, %rdi
xorl %esi, %esi
movl $0x88, %edx
callq 0x3200
movq -0x40(%rbp), %rax
movl $0x10, 0x88(%rax)
movq -0x70(%rbp), %rax
movw $0x2, (%rax)
movw -0x5a(%rbp), %cx
movq -0x70(%rbp), %rax
movw %cx, 0x2(%rax)
movq -0x70(%rbp), %rax
movq -0x68(%rbp), %rcx
movl (%rcx), %ecx
movl %ecx, 0x4(%rax)
jmp 0x10269
movq -0x58(%rbp), %rax
movzwl 0x8(%rax), %eax
cmpl $0x1c, %eax
jne 0x10257
movq -0x58(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x78(%rbp)
movq -0x40(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x80(%rbp)
movq -0x40(%rbp), %rdi
addq $0x8, %rdi
xorl %esi, %esi
movl $0x88, %edx
callq 0x3200
movq -0x40(%rbp), %rax
movl $0x1c, 0x88(%rax)
movq -0x80(%rbp), %rax
movw $0x2, (%rax)
movw -0x5a(%rbp), %cx
movq -0x80(%rbp), %rax
movw %cx, 0x2(%rax)
movq -0x80(%rbp), %rax
movq -0x78(%rbp), %rcx
movq (%rcx), %rdx
movq %rdx, 0x8(%rax)
movq 0x8(%rcx), %rcx
movq %rcx, 0x10(%rax)
jmp 0x10269
jmp 0x10259
jmp 0x1025b
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x10151
cmpq $0x0, -0x20(%rbp)
je 0x10288
cmpl $0x0, -0x24(%rbp)
jne 0x10288
movq -0x20(%rbp), %rdi
callq 0x27870
movq -0x20(%rbp), %rdi
callq 0x3460
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xa0, %rsp
popq %rbp
retq
nopl (%rax,%rax)