// File: /workspace/github_c_makefile_star2_O3/GangZhuo[P]dohclient/src/channel_cache.c
// Function: cache_edit
int cache_edit(channel_t *ctx, const char *key, const ns_msg_t *msg)
{
	cache_t *c = (cache_t*)ctx;
	cache_item_t *item;
	struct rbnode_t *rbn;
	int ttl;
	ns_msg_t *newmsg;

	if (!key || !msg) {
		loge("invalid arguments\n");
		return -1;
	}

	if (!msg->qrs || !msg->rrs ||
		msg->qdcount < 1 || ns_rrcount(msg) < 1) {
		loge("invalid msg (qdcount=%d,ancount=%d,nscount=%d,arcount=%d) - %s\n",
			(int)msg->qdcount,
			(int)msg->ancount,
			(int)msg->nscount,
			(int)msg->arcount,
			key);
		return -1;
	}

	ttl = (int)ns_get_ttl(msg);

	/* no ttl */
	if (ttl < 1) {
		loge("no ttl\n");
		return -1;
	}

	rbn = rbtree_lookup(&c->dic, key);
	if (!rbn) {
		loge("item not exists - %s\n", key);
		return -1;
	}

	item = rbtree_container_of(rbn, cache_item_t, node);
	newmsg = ns_msg_clone(msg);
	if (!newmsg) {
		loge("ns_msg_clone() error\n");
		return -1;
	}
	ns_msg_free(item->msg);
	free(item->msg);
	item->msg = newmsg;
	update_expire(c, item, ttl);
	dllist_remove(&item->entry);
	cache_item_add(c, item);
	logi("cache updated: %s - %s\n", key, msg_answers(msg));
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
testq %rsi, %rsi
setne %al
testq %rdx, %rdx
setne %cl
testb %cl, %al
je 0x8632
movq %rdx, %r14
movq %rsi, %rbx
cmpq $0x0, 0x10(%rdx)
je 0x866b
cmpq $0x0, 0x18(%r14)
je 0x866b
cmpw $0x0, 0x4(%r14)
je 0x866b
movzwl 0x6(%r14), %eax
movzwl 0x8(%r14), %ecx
addl %eax, %ecx
movzwl 0xa(%r14), %eax
orl %ecx, %eax
je 0x866b
movq %rdi, %r15
movq %r14, %rdi
callq 0x194a3
testl %eax, %eax
jle 0x86d2
movl %eax, %ebp
leaq 0x68(%r15), %rdi
movq %rbx, %rsi
callq 0x39f8
testq %rax, %rax
je 0x8707
movq %rax, %r12
movq %r14, %rdi
callq 0x19992
testq %rax, %rax
je 0x8746
movq %rax, %r13
movq 0x38(%r12), %rdi
callq 0x19ae0
movq 0x38(%r12), %rdi
callq 0x3450
movq %r13, 0x38(%r12)
movq %r15, %rdi
movq %r12, %rsi
movl %ebp, %edx
callq 0x877f
movq 0x28(%r12), %rax
movq 0x30(%r12), %rcx
movq %rax, (%rcx)
movq %rcx, 0x8(%rax)
movq %r15, %rdi
movq %r12, %rsi
callq 0x87c7
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x176e5
cmpl $0x6, (%rax)
jl 0x86c1
movq %r14, %rdi
callq 0x19df7
movq %rax, %r10
subq $0x8, %rsp
leaq 0x19ff9(%rip), %rsi     # 0x225fc
leaq 0x1a059(%rip), %rdx     # 0x22663
leaq 0x1a0bb(%rip), %r8      # 0x226cc
xorl %ebp, %ebp
movl $0x6, %edi
movl $0x121, %ecx            # imm = 0x121
movq %rbx, %r9
xorl %eax, %eax
pushq %r10
callq 0x1771e
addq $0x10, %rsp
jmp 0x86c1
xorl %eax, %eax
callq 0x176e5
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpl $0x3, (%rax)
jl 0x86c1
leaq 0x19fb2(%rip), %rsi     # 0x225fc
leaq 0x1a012(%rip), %rdx     # 0x22663
leaq 0x19fc2(%rip), %r8      # 0x2261a
movl $0x3, %edi
movl $0xf8, %ecx
xorl %eax, %eax
callq 0x1771e
jmp 0x86c1
xorl %eax, %eax
callq 0x176e5
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpl $0x3, (%rax)
jl 0x86c1
movzwl 0x4(%r14), %r9d
movzwl 0x6(%r14), %r10d
movzwl 0x8(%r14), %r11d
movzwl 0xa(%r14), %r14d
leaq 0x19f65(%rip), %rsi     # 0x225fc
leaq 0x19fc5(%rip), %rdx     # 0x22663
leaq 0x19fc9(%rip), %r8      # 0x2266e
movl $0x3, %edi
movl $0x103, %ecx            # imm = 0x103
xorl %eax, %eax
pushq %rbx
pushq %r14
pushq %r11
pushq %r10
callq 0x1771e
addq $0x20, %rsp
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %eax, %eax
callq 0x176e5
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpl $0x3, (%rax)
jl 0x86c1
leaq 0x19f12(%rip), %rsi     # 0x225fc
leaq 0x19f72(%rip), %rdx     # 0x22663
leaq 0x19fb6(%rip), %r8      # 0x226ae
movl $0x3, %edi
movl $0x10b, %ecx            # imm = 0x10B
jmp 0x8662
xorl %eax, %eax
callq 0x176e5
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpl $0x3, (%rax)
jl 0x86c1
leaq 0x19edd(%rip), %rsi     # 0x225fc
leaq 0x19f3d(%rip), %rdx     # 0x22663
leaq 0x19f00(%rip), %r8      # 0x2262d
movl $0x3, %edi
movl $0x111, %ecx            # imm = 0x111
movq %rbx, %r9
xorl %eax, %eax
callq 0x1771e
jmp 0x86c1
xorl %eax, %eax
callq 0x176e5
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpl $0x3, (%rax)
jl 0x86c1
leaq 0x19e9a(%rip), %rsi     # 0x225fc
leaq 0x19efa(%rip), %rdx     # 0x22663
leaq 0x19f46(%rip), %r8      # 0x226b6
movl $0x3, %edi
movl $0x118, %ecx            # imm = 0x118
jmp 0x8662