// File: /workspace/github_c_makefile_star2_O1/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 0xe099
leaq 0x10(%rsp), %rsi
movq %r15, %rdi
callq 0x14d77
movl %eax, %r14d
cmpl $0xc8, %eax
jne 0xe099
movl $0x20, %edi
callq 0x34b0
testq %rax, %rax
je 0xe1dd
movq %rax, %r13
movq %rax, %rdi
callq 0x19b12
testl %eax, %eax
je 0xe218
xorl %eax, %eax
callq 0x17d59
cmpl $0x3, (%rax)
jl 0xe274
leaq 0x14f8c(%rip), %rsi     # 0x23001
leaq 0x14f05(%rip), %rdx     # 0x22f81
leaq 0x14549(%rip), %r8      # 0x225cc
movl $0x3, %edi
movl $0x1eb, %ecx            # imm = 0x1EB
xorl %eax, %eax
callq 0x17d92
jmp 0xe274
xorl %eax, %eax
callq 0x17d59
cmpl $0x3, (%rax)
jl 0xe0ef
movq 0x8(%rbp), %r13
movq 0x10(%rsp), %rbx
movq %r12, %rdi
callq 0x14658
movq %rax, %r10
subq $0x8, %rsp
leaq 0x14f3d(%rip), %rsi     # 0x23001
leaq 0x14eb6(%rip), %rdx     # 0x22f81
leaq 0x15084(%rip), %r8      # 0x23156
movl $0x3, %edi
movl $0x20c, %ecx            # imm = 0x20C
movq %r13, %r9
xorl %eax, %eax
pushq %r10
pushq %rbx
pushq %r14
callq 0x17d92
addq $0x20, %rsp
movl $0x1, 0x78(%rbp)
leaq 0x4(%rsp), %rsi
movq %r12, %rdi
callq 0x146b5
movq %rax, %rdi
callq 0x3450
movq %r12, %rdi
callq 0x143e9
movq %r15, %rdi
callq 0x14b16
decl 0x74(%rbp)
jne 0xe50f
movq 0x28(%rbp), %rax
movq 0x30(%rbp), %rcx
movq %rax, (%rcx)
movq 0x28(%rbp), %rax
movq %rcx, 0x8(%rax)
movq 0x8(%rsp), %rbx
leaq 0x68(%rbx), %rdi
leaq 0x38(%rbp), %rsi
callq 0x3946
decl 0x78(%rbx)
cmpq $0x0, 0x18(%rbp)
je 0xe1c3
movl 0x70(%rbp), %eax
testl %eax, %eax
je 0xe1d6
cmpl $0x1, %eax
je 0xe1ca
movq 0x60(%rbp), %r14
cmpq $0x0, 0x20(%r14)
je 0xe1ca
testl %eax, %eax
jle 0xe281
xorl %r15d, %r15d
xorl %ebx, %ebx
movq 0x68(%rbp), %rax
movq (%rax,%r15,8), %r12
movq %r14, %rdi
movq %r12, %rsi
callq 0xe567
cmpl $0xfff, %eax            # imm = 0xFFF
ja 0xe1b2
btl $0xa, %eax
jae 0xe1a9
movl %eax, %ecx
andl $0x12, %ecx
je 0xe1a9
movl %eax, %ecx
andl $0x24, %ecx
jne 0xe2cd
testl $0x400, %eax            # imm = 0x400
cmoveq %r12, %rbx
incq %r15
movslq 0x70(%rbp), %rax
cmpq %rax, %r15
jl 0xe177
jmp 0xe2d0
xorl %ebx, %ebx
jmp 0xe4e2
movq 0x68(%rbp), %rax
movq (%rax), %rbx
jmp 0xe2d9
xorl %ebx, %ebx
jmp 0xe2d9
xorl %eax, %eax
callq 0x17d59
cmpl $0x3, (%rax)
jl 0xe0f6
leaq 0x14e0d(%rip), %rsi     # 0x23001
leaq 0x14d86(%rip), %rdx     # 0x22f81
leaq 0x156a1(%rip), %r8      # 0x238a3
movl $0x3, %edi
movl $0x1e6, %ecx            # imm = 0x1E6
xorl %eax, %eax
callq 0x17d92
jmp 0xe0f6
leaq 0x4(%rsp), %r14
movq %r15, %rdi
movq %r14, %rsi
callq 0x14dd5
movl (%r14), %edx
movq %r13, %rdi
movq %rax, %rsi
callq 0x1a7e0
testl %eax, %eax
je 0xe285
xorl %eax, %eax
callq 0x17d59
cmpl $0x3, (%rax)
jl 0xe26c
leaq 0x14db4(%rip), %rsi     # 0x23001
leaq 0x14d2d(%rip), %rdx     # 0x22f81
leaq 0x14d2e(%rip), %r8      # 0x22f89
movl $0x3, %edi
movl $0x1f3, %ecx            # imm = 0x1F3
xorl %eax, %eax
callq 0x17d92
movq %r13, %rdi
callq 0x1a1dd
movq %r13, %rdi
callq 0x3450
jmp 0xe0f6
xorl %ebx, %ebx
jmp 0xe2d0
cmpq $0x0, 0x68(%rbp)
jne 0xe2b6
movslq 0x74(%rbp), %r14
shlq $0x3, %r14
movq %r14, %rdi
callq 0x34b0
movq %rax, 0x68(%rbp)
testq %rax, %rax
je 0xe533
movq %rax, %rdi
xorl %esi, %esi
movq %r14, %rdx
callq 0x31e0
movq 0x68(%rbp), %rax
movslq 0x70(%rbp), %rcx
leal 0x1(%rcx), %edx
movl %edx, 0x70(%rbp)
movq %r13, (%rax,%rcx,8)
jmp 0xe0f6
movq %r12, %rbx
testq %rbx, %rbx
je 0xe51e
xorl %eax, %eax
callq 0x17d59
cmpl $0x8, (%rax)
jl 0xe4bb
movq 0x60(%rbp), %r15
xorl %eax, %eax
callq 0x17d59
cmpl $0x7, (%rax)
jl 0xe32b
leaq 0x8(%rbp), %rdi
callq 0x1a29d
leaq 0x14cf8(%rip), %rsi     # 0x23001
leaq 0x14a43(%rip), %rdx     # 0x22d53
leaq 0x14e61(%rip), %r8      # 0x23178
movl $0x7, %edi
movl $0x17d, %ecx            # imm = 0x17D
movq %rax, %r9
xorl %eax, %eax
callq 0x17d92
cmpl $0x0, 0x70(%rbp)
jle 0xe4bb
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 0xe567
movl %eax, %r14d
xorl %eax, %eax
callq 0x17d59
cmpl $0x7, (%rax)
jl 0xe4a3
cmpq %rbx, %r13
leaq 0x15a7b(%rip), %rcx     # 0x23df2
movq %rcx, %rdx
leaq 0x14a1c(%rip), %rax     # 0x22d9d
cmoveq %rax, %rdx
movq %rdx, 0x50(%rsp)
testb $0x2, %r14b
leaq 0x14a0e(%rip), %rax     # 0x22da3
cmoveq %rcx, %rax
movq %rax, 0x48(%rsp)
testb $0x4, %r14b
leaq 0x14a01(%rip), %rax     # 0x22daa
cmoveq %rcx, %rax
movq %rax, 0x40(%rsp)
testb $0x8, %r14b
leaq 0x149f8(%rip), %rax     # 0x22db5
cmoveq %rcx, %rax
movq %rax, 0x38(%rsp)
testb $0x10, %r14b
leaq 0x149ef(%rip), %rax     # 0x22dc0
cmoveq %rcx, %rax
movq %rax, 0x30(%rsp)
testb $0x20, %r14b
leaq 0x149e5(%rip), %r10     # 0x22dca
cmoveq %rcx, %r10
testb $0x40, %r14b
leaq 0x149e4(%rip), %r11     # 0x22dd8
cmoveq %rcx, %r11
btl $0x9, %r14d
leaq 0x149e2(%rip), %r15     # 0x22de6
cmovaeq %rcx, %r15
btl $0xa, %r14d
leaq 0x149db(%rip), %rbx     # 0x22def
cmovaeq %rcx, %rbx
btl $0xb, %r14d
leaq 0x149d8(%rip), %rbp     # 0x22dfc
cmovaeq %rcx, %rbp
cmpl $0x1000, %r14d          # imm = 0x1000
leaq 0x149d3(%rip), %r14     # 0x22e09
cmovbq %rcx, %r14
leal 0x1(%r12), %r9d
subq $0x8, %rsp
movl $0x7, %edi
leaq 0x14bb2(%rip), %rsi     # 0x23001
leaq 0x148fd(%rip), %rdx     # 0x22d53
movl $0x18d, %ecx            # imm = 0x18D
leaq 0x14d29(%rip), %r8      # 0x2318b
xorl %eax, %eax
pushq %r14
pushq %rbp
movq 0x40(%rsp), %rbp
pushq %rbx
movq 0x40(%rsp), %rbx
pushq %r15
movq 0x40(%rsp), %r15
pushq %r11
pushq %r10
pushq 0x68(%rsp)
pushq 0x78(%rsp)
pushq 0x88(%rsp)
pushq 0x98(%rsp)
pushq 0xa8(%rsp)
callq 0x17d92
addq $0x60, %rsp
movq %r13, %rdi
callq 0x1c653
incq %r12
movslq 0x70(%rbp), %rax
cmpq %rax, %r12
jl 0xe347
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 0xe507
xorl %ecx, %ecx
movq 0x68(%rbp), %rdx
cmpq %rbx, (%rdx,%rcx,8)
jne 0xe4ff
movq $0x0, (%rdx,%rcx,8)
incq %rcx
cmpq %rcx, %rax
jne 0xe4ed
movq %rbp, %rdi
callq 0xd73d
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x68(%rbp), %rax
movslq 0x70(%rbp), %rcx
leaq (%rax,%rcx,8), %rax
addq $-0x8, %rax
jmp 0xe1ce
xorl %eax, %eax
callq 0x17d59
cmpl $0x3, (%rax)
jl 0xe26c
leaq 0x14ab7(%rip), %rsi     # 0x23001
leaq 0x14a30(%rip), %rdx     # 0x22f81
leaq 0x1534b(%rip), %r8      # 0x238a3
movl $0x3, %edi
movl $0x1fd, %ecx            # imm = 0x1FD
jmp 0xe265