// File: /workspace/github_c_makefile_star2_O3/GangZhuo[P]dohclient/src/channel_doh.c
// Function: http_cb
static void http_cb(
	int status,
	http_request_t* request,
	http_response_t* response,
	void* state)
{
	channel_req_t* rq = (channel_req_t*)state;
	channel_doh_t* c = rq->ctx;
	ns_msg_t* result = NULL;
	char* data;
	int datalen;
	int http_status = 0;
	const char* status_text = NULL;

	if (status == HTTP_OK &&
		(http_status = http_response_get_status_code(response, &status_text)) == 200) {
		result = (ns_msg_t*)malloc(sizeof(ns_msg_t));
		if (!result) {
			loge("alloc\n");
			goto exit;
		}
		
		if (init_ns_msg(result)) {
			loge("init_ns_msg() error\n");
			free(result);
			result = NULL;
			goto exit;
		}

		data = http_response_get_data(response, &datalen);
		if (ns_parse(result, (const uint8_t*)data, datalen)) {
			loge("ns_parse() error\n");
			ns_msg_free(result);
			free(result);
			result = NULL;
			goto exit;
		}

		if (!rq->results) {
			rq->results = (ns_msg_t**)malloc(sizeof(ns_msg_t*) * rq->wait_num);
			if (!rq->results) {
				loge("alloc\n");
				ns_msg_free(result);
				free(result);
				result = NULL;
				goto exit;
			}
			memset(rq->results, 0, sizeof(ns_msg_t*) * rq->wait_num);
		}
		rq->results[rq->result_num++] = result;
	}
	else {
		loge("query %s failed: HTTP %d %s - %s\n",
			rq->qr.qname,
			http_status,
			status_text,
			http_request_get_host(request));
		rq->untrust = TRUE;
	}

exit:
	free(http_request_get_data(request, &datalen));
	http_request_destroy(request);
	http_response_destroy(response);

	rq->wait_num--;

	if (rq->wait_num == 0) {

		dllist_remove(&rq->entry);
		rbtree_remove(&c->reqdic, &rq->rbn);
		c->req_count--;

		if (rq->callback) {
			result = choose_best_nsmsg(rq);
			rq->callback((channel_t*)c, result ? 0 : -1, result, FALSE, !rq->untrust, rq->cb_state);
		}
		else {
			result = NULL;
		}

		/* Detach the result, so can keep the result in memory,
		after myreq_destroy() called. */
		detach_result(rq, result);
		myreq_destroy(rq);
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rcx, %rbp
movq %rdx, %r15
movq %rsi, %r12
movq 0x60(%rcx), %rax
movq %rax, 0x8(%rsp)
movq $0x0, 0x10(%rsp)
xorl %r14d, %r14d
testl %edi, %edi
jne 0xdc61
leaq 0x10(%rsp), %rsi
movq %r15, %rdi
callq 0x147fa
movl %eax, %r14d
cmpl $0xc8, %eax
jne 0xdc61
movl $0x20, %edi
callq 0x34b0
testq %rax, %rax
je 0xdd9e
movq %rax, %r13
movq %rax, %rdi
callq 0x19496
testl %eax, %eax
je 0xddd9
xorl %eax, %eax
callq 0x176e5
cmpl $0x3, (%rax)
jl 0xde35
leaq 0x153cc(%rip), %rsi     # 0x23009
leaq 0x15345(%rip), %rdx     # 0x22f89
leaq 0x14961(%rip), %r8      # 0x225ac
movl $0x3, %edi
movl $0x1eb, %ecx            # imm = 0x1EB
xorl %eax, %eax
callq 0x1771e
jmp 0xde35
xorl %eax, %eax
callq 0x176e5
cmpl $0x3, (%rax)
jl 0xdcb7
movq 0x8(%rbp), %r13
movq 0x10(%rsp), %rbx
movq %r12, %rdi
callq 0x1412e
movq %rax, %r10
subq $0x8, %rsp
leaq 0x1537d(%rip), %rsi     # 0x23009
leaq 0x152f6(%rip), %rdx     # 0x22f89
leaq 0x154c4(%rip), %r8      # 0x2315e
movl $0x3, %edi
movl $0x20c, %ecx            # imm = 0x20C
movq %r13, %r9
xorl %eax, %eax
pushq %r10
pushq %rbx
pushq %r14
callq 0x1771e
addq $0x20, %rsp
movl $0x1, 0x78(%rbp)
leaq 0x4(%rsp), %rsi
movq %r12, %rdi
callq 0x1417e
movq %rax, %rdi
callq 0x3450
movq %r12, %rdi
callq 0x13ee7
movq %r15, %rdi
callq 0x145c8
decl 0x74(%rbp)
jne 0xe0f5
movq 0x28(%rbp), %rax
movq 0x30(%rbp), %rcx
movq %rax, (%rcx)
movq %rcx, 0x8(%rax)
movq 0x8(%rsp), %rbx
leaq 0x68(%rbx), %rdi
leaq 0x38(%rbp), %rsi
callq 0x3a34
decl 0x78(%rbx)
cmpq $0x0, 0x18(%rbp)
je 0xdd87
movl 0x70(%rbp), %eax
testl %eax, %eax
je 0xdd97
cmpl $0x1, %eax
je 0xdd8e
movq 0x60(%rbp), %r14
cmpq $0x0, 0x20(%r14)
je 0xdd8e
testl %eax, %eax
jle 0xde88
xorl %r15d, %r15d
xorl %ebx, %ebx
movq 0x68(%rbp), %rax
movq (%rax,%r15,8), %r12
movq %r14, %rdi
movq %r12, %rsi
callq 0xe138
cmpl $0xfff, %eax            # imm = 0xFFF
ja 0xdd76
btl $0xa, %eax
jae 0xdd6d
movl %eax, %ecx
andl $0x12, %ecx
je 0xdd6d
movl %eax, %ecx
andl $0x24, %ecx
jne 0xde7d
testl $0x400, %eax            # imm = 0x400
cmoveq %r12, %rbx
incq %r15
movslq 0x70(%rbp), %rax
cmpq %rax, %r15
jl 0xdd3b
jmp 0xde80
xorl %ebx, %ebx
jmp 0xe0c8
movq 0x68(%rbp), %rax
jmp 0xde96
xorl %ebx, %ebx
jmp 0xde99
xorl %eax, %eax
callq 0x176e5
cmpl $0x3, (%rax)
jl 0xdcbe
leaq 0x15254(%rip), %rsi     # 0x23009
leaq 0x151cd(%rip), %rdx     # 0x22f89
leaq 0x15af0(%rip), %r8      # 0x238b3
movl $0x3, %edi
movl $0x1e6, %ecx            # imm = 0x1E6
xorl %eax, %eax
callq 0x1771e
jmp 0xdcbe
leaq 0x4(%rsp), %r14
movq %r15, %rdi
movq %r14, %rsi
callq 0x14855
movl (%r14), %edx
movq %r13, %rdi
movq %rax, %rsi
callq 0x1a0ca
testl %eax, %eax
je 0xde42
xorl %eax, %eax
callq 0x176e5
cmpl $0x3, (%rax)
jl 0xde2d
leaq 0x151fb(%rip), %rsi     # 0x23009
leaq 0x15174(%rip), %rdx     # 0x22f89
leaq 0x15175(%rip), %r8      # 0x22f91
movl $0x3, %edi
movl $0x1f3, %ecx            # imm = 0x1F3
xorl %eax, %eax
callq 0x1771e
movq %r13, %rdi
callq 0x19ae0
movq %r13, %rdi
callq 0x3450
jmp 0xdcbe
movq 0x68(%rbp), %rax
testq %rax, %rax
jne 0xde6a
movslq 0x74(%rbp), %rsi
shlq $0x3, %rsi
movl $0x1, %edi
callq 0x3240
movq %rax, 0x68(%rbp)
testq %rax, %rax
je 0xe104
movslq 0x70(%rbp), %rcx
leal 0x1(%rcx), %edx
movl %edx, 0x70(%rbp)
movq %r13, (%rax,%rcx,8)
jmp 0xdcbe
movq %r12, %rbx
testq %rbx, %rbx
jne 0xde99
movl 0x70(%rbp), %eax
movq 0x68(%rbp), %rcx
cltq
leaq (%rcx,%rax,8), %rax
addq $-0x8, %rax
movq (%rax), %rbx
xorl %eax, %eax
callq 0x176e5
cmpl $0x8, (%rax)
jl 0xe0a1
movq 0x60(%rbp), %r15
xorl %eax, %eax
callq 0x176e5
cmpl $0x7, (%rax)
jl 0xdeeb
leaq 0x8(%rbp), %rdi
callq 0x19ba4
leaq 0x15140(%rip), %rsi     # 0x23009
leaq 0x14e8b(%rip), %rdx     # 0x22d5b
leaq 0x152a9(%rip), %r8      # 0x23180
movl $0x7, %edi
movl $0x17d, %ecx            # imm = 0x17D
movq %rax, %r9
xorl %eax, %eax
callq 0x1771e
cmpl $0x0, 0x70(%rbp)
jle 0xe0a1
xorl %r12d, %r12d
movq %rbp, 0x28(%rsp)
movq %rbx, 0x20(%rsp)
movq %r15, 0x18(%rsp)
movq 0x68(%rbp), %rax
movq (%rax,%r12,8), %r13
movq %r15, %rdi
movq %r13, %rsi
callq 0xe138
movl %eax, %r14d
xorl %eax, %eax
callq 0x176e5
cmpl $0x7, (%rax)
jl 0xe089
cmpq %rbx, %r13
leaq 0x15ec7(%rip), %rcx     # 0x23dfe
movq %rcx, %rdx
leaq 0x14e64(%rip), %rax     # 0x22da5
cmoveq %rax, %rdx
movq %rdx, 0x48(%rsp)
testb $0x2, %r14b
leaq 0x14e56(%rip), %rax     # 0x22dab
cmoveq %rcx, %rax
movq %rax, 0x40(%rsp)
movd %r14d, %xmm0
pshufd $0x0, %xmm0, %xmm0      # xmm0 = xmm0[0,0,0,0]
movdqa %xmm0, %xmm1
pand 0x14a1c(%rip), %xmm1    # 0x22990
pand 0x14a24(%rip), %xmm0    # 0x229a0
pxor %xmm2, %xmm2
pcmpeqd %xmm2, %xmm0
pcmpeqd %xmm2, %xmm1
packssdw %xmm0, %xmm1
packsswb %xmm1, %xmm1
pmovmskb %xmm1, %eax
testb $-0x80, %al
leaq 0x14e15(%rip), %rdx     # 0x22db2
cmovneq %rcx, %rdx
movq %rdx, 0x38(%rsp)
testb $0x40, %al
leaq 0x14e0e(%rip), %rdx     # 0x22dbd
cmovneq %rcx, %rdx
movq %rdx, 0x30(%rsp)
testb $0x20, %al
leaq 0x14e07(%rip), %rbp     # 0x22dc8
cmovneq %rcx, %rbp
testb $0x10, %al
leaq 0x14e04(%rip), %r10     # 0x22dd2
cmovneq %rcx, %r10
testb $0x8, %al
leaq 0x14e05(%rip), %r11     # 0x22de0
cmovneq %rcx, %r11
testb $0x4, %al
leaq 0x14e06(%rip), %r15     # 0x22dee
cmovneq %rcx, %r15
testb $0x2, %al
movq %r13, 0x50(%rsp)
leaq 0x14dfd(%rip), %r13     # 0x22df7
cmovneq %rcx, %r13
testb $0x1, %al
leaq 0x14dfd(%rip), %rbx     # 0x22e04
cmovneq %rcx, %rbx
cmpl $0x1000, %r14d          # imm = 0x1000
leaq 0x14df8(%rip), %r14     # 0x22e11
cmovbq %rcx, %r14
leal 0x1(%r12), %r9d
subq $0x8, %rsp
movl $0x7, %edi
leaq 0x14fd7(%rip), %rsi     # 0x23009
leaq 0x14d22(%rip), %rdx     # 0x22d5b
movl $0x18d, %ecx            # imm = 0x18D
leaq 0x1514e(%rip), %r8      # 0x23193
xorl %eax, %eax
pushq %r14
pushq %rbx
movq 0x38(%rsp), %rbx
pushq %r13
movq 0x70(%rsp), %r13
pushq %r15
movq 0x40(%rsp), %r15
pushq %r11
pushq %r10
pushq %rbp
movq 0x68(%rsp), %rbp
pushq 0x70(%rsp)
pushq 0x80(%rsp)
pushq 0x90(%rsp)
pushq 0xa0(%rsp)
callq 0x1771e
addq $0x60, %rsp
incq %r12
movq %r13, %rdi
callq 0x1bd90
movslq 0x70(%rbp), %rax
cmpq %rax, %r12
jl 0xdf07
cmpq $0x1, %rbx
movl $0x0, %esi
sbbl %esi, %esi
xorl %r8d, %r8d
cmpl $0x0, 0x78(%rbp)
sete %r8b
movq 0x20(%rbp), %r9
movq 0x8(%rsp), %rdi
movq %rbx, %rdx
xorl %ecx, %ecx
callq *0x18(%rbp)
movslq 0x70(%rbp), %rax
testq %rax, %rax
jle 0xe0ed
xorl %ecx, %ecx
movq 0x68(%rbp), %rdx
cmpq %rbx, (%rdx,%rcx,8)
jne 0xe0e5
movq $0x0, (%rdx,%rcx,8)
incq %rcx
cmpq %rcx, %rax
jne 0xe0d3
movq %rbp, %rdi
callq 0xd302
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %eax, %eax
callq 0x176e5
cmpl $0x3, (%rax)
jl 0xde2d
leaq 0x14eee(%rip), %rsi     # 0x23009
leaq 0x14e67(%rip), %rdx     # 0x22f89
leaq 0x1578a(%rip), %r8      # 0x238b3
movl $0x3, %edi
movl $0x1fd, %ecx            # imm = 0x1FD
jmp 0xde26