// File: /workspace/github_c_makefile_star2_O1/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 0x8947
movq %rdx, %r14
movq %rsi, %rbx
cmpq $0x0, 0x10(%rdx)
je 0x8980
cmpq $0x0, 0x18(%r14)
je 0x8980
cmpw $0x0, 0x4(%r14)
je 0x8980
movzwl 0x6(%r14), %eax
movzwl 0x8(%r14), %ecx
addl %eax, %ecx
movzwl 0xa(%r14), %eax
orl %ecx, %eax
je 0x8980
movq %rdi, %r15
movq %r14, %rdi
callq 0x19b1f
testl %eax, %eax
jle 0x89e7
movl %eax, %ebp
leaq 0x68(%r15), %rdi
movq %rbx, %rsi
callq 0x390c
testq %rax, %rax
je 0x8a1c
movq %rax, %r12
movq %r14, %rdi
callq 0x1a070
testq %rax, %rax
je 0x8a5b
movq %rax, %r13
movq 0x38(%r12), %rdi
callq 0x1a1dd
movq 0x38(%r12), %rdi
callq 0x3450
movq %r13, 0x38(%r12)
movq %r15, %rdi
movq %r12, %rsi
movl %ebp, %edx
callq 0x8a94
movq 0x28(%r12), %rax
movq 0x30(%r12), %rcx
movq %rax, (%rcx)
movq 0x28(%r12), %rax
movq %rcx, 0x8(%rax)
movq %r15, %rdi
movq %r12, %rsi
callq 0x8adc
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x17d59
cmpl $0x6, (%rax)
jl 0x89d6
movq %r14, %rdi
callq 0x1a4f0
movq %rax, %r10
subq $0x8, %rsp
leaq 0x19d04(%rip), %rsi     # 0x2261c
leaq 0x19d64(%rip), %rdx     # 0x22683
leaq 0x19dc6(%rip), %r8      # 0x226ec
xorl %ebp, %ebp
movl $0x6, %edi
movl $0x121, %ecx            # imm = 0x121
movq %rbx, %r9
xorl %eax, %eax
pushq %r10
callq 0x17d92
addq $0x10, %rsp
jmp 0x89d6
xorl %eax, %eax
callq 0x17d59
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpl $0x3, (%rax)
jl 0x89d6
leaq 0x19cbd(%rip), %rsi     # 0x2261c
leaq 0x19d1d(%rip), %rdx     # 0x22683
leaq 0x19ccd(%rip), %r8      # 0x2263a
movl $0x3, %edi
movl $0xf8, %ecx
xorl %eax, %eax
callq 0x17d92
jmp 0x89d6
xorl %eax, %eax
callq 0x17d59
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpl $0x3, (%rax)
jl 0x89d6
movzwl 0x4(%r14), %r9d
movzwl 0x6(%r14), %r10d
movzwl 0x8(%r14), %r11d
movzwl 0xa(%r14), %r14d
leaq 0x19c70(%rip), %rsi     # 0x2261c
leaq 0x19cd0(%rip), %rdx     # 0x22683
leaq 0x19cd4(%rip), %r8      # 0x2268e
movl $0x3, %edi
movl $0x103, %ecx            # imm = 0x103
xorl %eax, %eax
pushq %rbx
pushq %r14
pushq %r11
pushq %r10
callq 0x17d92
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 0x17d59
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpl $0x3, (%rax)
jl 0x89d6
leaq 0x19c1d(%rip), %rsi     # 0x2261c
leaq 0x19c7d(%rip), %rdx     # 0x22683
leaq 0x19cc1(%rip), %r8      # 0x226ce
movl $0x3, %edi
movl $0x10b, %ecx            # imm = 0x10B
jmp 0x8977
xorl %eax, %eax
callq 0x17d59
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpl $0x3, (%rax)
jl 0x89d6
leaq 0x19be8(%rip), %rsi     # 0x2261c
leaq 0x19c48(%rip), %rdx     # 0x22683
leaq 0x19c0b(%rip), %r8      # 0x2264d
movl $0x3, %edi
movl $0x111, %ecx            # imm = 0x111
movq %rbx, %r9
xorl %eax, %eax
callq 0x17d92
jmp 0x89d6
xorl %eax, %eax
callq 0x17d59
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpl $0x3, (%rax)
jl 0x89d6
leaq 0x19ba5(%rip), %rsi     # 0x2261c
leaq 0x19c05(%rip), %rdx     # 0x22683
leaq 0x19c51(%rip), %r8      # 0x226d6
movl $0x3, %edi
movl $0x118, %ecx            # imm = 0x118
jmp 0x8977