// File: /workspace/github_c_makefile_star2_O3/GangZhuo[P]dohclient/src/channel_chndoh.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_chndoh_t* c = rq->ctx;
	doh_server_t* doh = NULL;
	ns_msg_t* result = NULL;
	char* data;
	int datalen;
	int http_status = 0;
	const char* status_text = NULL;

	doh = (doh_server_t*)http_request_get_state(request);

	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;
		}

		result->id = doh == &c->chndoh ? 0 : 1;

		logd("%d. query %s success: %s - %s (%lu ms)\n",
			2 - rq->wait_num,
			rq->qr.qname,
			msg_answers(result),
			doh->name ? doh->name : (doh->host ? doh->host : doh->addrstr),
			OS_GetTickCount() - rq->start_time);
	}
	else {
		loge("%d. query %s failed: HTTP %d %s - %s (%lu ms)\n",
			2 - rq->wait_num,
			rq->qr.qname,
			http_status,
			status_text,
			doh->name ? doh->name : (doh->host ? doh->host : doh->addrstr),
			OS_GetTickCount() - rq->start_time);
	}

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

	query_cb(NULL, result ? 0 : -1, result, FALSE, !!result, rq);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rcx, %rbx
movq %rdx, %r13
movl %edi, %r14d
movq 0x60(%rcx), %r12
movq $0x0, 0x20(%rsp)
movq %rsi, 0x30(%rsp)
movq %rsi, %rdi
callq 0x140ef
movq %rax, %r15
xorl %ebp, %ebp
testl %r14d, %r14d
jne 0xc05a
leaq 0x20(%rsp), %rsi
movq %r13, %rdi
callq 0x147fa
movl %eax, %ebp
cmpl $0xc8, %eax
jne 0xc05a
movq %r12, %rbp
movl $0x20, %edi
callq 0x34b0
testq %rax, %rax
je 0xc15b
movq %rax, %r12
movq %rax, %rdi
callq 0x19496
testl %eax, %eax
je 0xc198
xorl %eax, %eax
callq 0x176e5
cmpl $0x3, (%rax)
jl 0xc1f4
leaq 0x1697a(%rip), %rsi     # 0x229b0
leaq 0x16f4c(%rip), %rdx     # 0x22f89
leaq 0x16568(%rip), %r8      # 0x225ac
movl $0x3, %edi
movl $0x2e0, %ecx            # imm = 0x2E0
xorl %eax, %eax
callq 0x1771e
jmp 0xc1f4
xorl %r12d, %r12d
xorl %eax, %eax
callq 0x176e5
cmpl $0x3, (%rax)
jl 0xc10d
movq %r13, 0x10(%rsp)
movl $0x2, %r14d
subl 0x74(%rbx), %r14d
movq %rbx, %r13
movq 0x8(%rbx), %rax
movq %rax, 0x8(%rsp)
movq 0x20(%rsp), %rax
movq %rax, 0x28(%rsp)
movq 0x450(%r15), %rbx
testq %rbx, %rbx
jne 0xc0b1
movq 0x460(%r15), %rbx
testq %rbx, %rbx
jne 0xc0b1
movq 0x458(%r15), %rbx
xorl %r12d, %r12d
xorl %eax, %eax
callq 0x193ab
movq %rax, %r10
subq 0x80(%r13), %r10
subq $0x8, %rsp
leaq 0x168e0(%rip), %rsi     # 0x229b0
leaq 0x16eb2(%rip), %rdx     # 0x22f89
leaq 0x16ec5(%rip), %r8      # 0x22fa3
movl $0x3, %edi
movl $0x2ff, %ecx            # imm = 0x2FF
movl %r14d, %r9d
movl $0x0, %eax
pushq %r10
pushq %rbx
movq %r13, %rbx
pushq 0x40(%rsp)
pushq %rbp
pushq 0x30(%rsp)
callq 0x1771e
addq $0x30, %rsp
movq 0x10(%rsp), %r13
leaq 0x1c(%rsp), %rsi
movq 0x30(%rsp), %r14
movq %r14, %rdi
callq 0x1417e
movq %rax, %rdi
callq 0x3450
movq %r14, %rdi
callq 0x13ee7
movq %r13, %rdi
callq 0x145c8
xorl %esi, %esi
cmpq $0x1, %r12
sbbl %esi, %esi
xorl %edi, %edi
movq %r12, %rdx
movq %rbx, %r9
callq 0xb762
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %r12d, %r12d
xorl %eax, %eax
callq 0x176e5
cmpl $0x3, (%rax)
jl 0xc10d
leaq 0x1683f(%rip), %rsi     # 0x229b0
leaq 0x16e11(%rip), %rdx     # 0x22f89
leaq 0x17734(%rip), %r8      # 0x238b3
xorl %r12d, %r12d
movl $0x3, %edi
movl $0x2db, %ecx            # imm = 0x2DB
xorl %eax, %eax
callq 0x1771e
jmp 0xc10d
leaq 0x1c(%rsp), %r14
movq %r13, %rdi
movq %r14, %rsi
callq 0x14855
movl (%r14), %edx
movq %r12, %rdi
movq %rax, %rsi
callq 0x1a0ca
testl %eax, %eax
je 0xc204
xorl %eax, %eax
callq 0x176e5
cmpl $0x3, (%rax)
jl 0xc1ec
leaq 0x167e3(%rip), %rsi     # 0x229b0
leaq 0x16db5(%rip), %rdx     # 0x22f89
leaq 0x16db6(%rip), %r8      # 0x22f91
movl $0x3, %edi
movl $0x2e8, %ecx            # imm = 0x2E8
xorl %eax, %eax
callq 0x1771e
movq %r12, %rdi
callq 0x19ae0
movq %r12, %rdi
callq 0x3450
xorl %r12d, %r12d
jmp 0xc10d
subq $-0x80, %rbp
xorl %eax, %eax
cmpq %rbp, %r15
setne %al
movw %ax, (%r12)
xorl %eax, %eax
callq 0x176e5
cmpl $0x7, (%rax)
jl 0xc10d
movq %r13, 0x10(%rsp)
movl $0x2, %ebp
subl 0x74(%rbx), %ebp
movq 0x8(%rbx), %r13
movq %r12, %rdi
callq 0x19df7
movq %rax, 0x8(%rsp)
movq 0x450(%r15), %r14
testq %r14, %r14
jne 0xc262
movq 0x460(%r15), %r14
testq %r14, %r14
jne 0xc262
movq 0x458(%r15), %r14
xorl %eax, %eax
callq 0x193ab
movq %rax, %r10
subq 0x80(%rbx), %r10
leaq 0x16736(%rip), %rsi     # 0x229b0
leaq 0x16d08(%rip), %rdx     # 0x22f89
leaq 0x16a88(%rip), %r8      # 0x22d10
movl $0x7, %edi
movl $0x2f6, %ecx            # imm = 0x2F6
movl %ebp, %r9d
xorl %eax, %eax
pushq %r10
pushq %r14
pushq 0x18(%rsp)
pushq %r13
callq 0x1771e
addq $0x20, %rsp
jmp 0xc108
nop