// File: /workspace/github_c_makefile_star2_O2/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 0x82a8
movq %rdx, %r14
movq %rsi, %rbx
cmpq $0x0, 0x10(%rdx)
je 0x82dd
cmpq $0x0, 0x18(%r14)
je 0x82dd
cmpw $0x0, 0x4(%r14)
je 0x82dd
movzwl 0x6(%r14), %eax
movzwl 0x8(%r14), %ecx
addl %eax, %ecx
movzwl 0xa(%r14), %eax
orl %ecx, %eax
je 0x82dd
movq %rdi, %r15
movq %r14, %rdi
callq 0x182a3
testl %eax, %eax
jle 0x8340
movl %eax, %ebp
leaq 0x68(%r15), %rdi
movq %rbx, %rsi
callq 0x38ef
testq %rax, %rax
je 0x8371
movq %rax, %r12
movq %r14, %rdi
callq 0x1878a
testq %rax, %rax
je 0x83a9
movq %rax, %r13
movq 0x38(%r12), %rdi
callq 0x188ce
movq 0x38(%r12), %rdi
callq 0x3460
movq %r13, 0x38(%r12)
movq %r15, %rdi
movq %r12, %rsi
movl %ebp, %edx
callq 0x83de
movq 0x28(%r12), %rax
movq 0x30(%r12), %rcx
movq %rax, (%rcx)
movq %rcx, 0x8(%rax)
movq %r15, %rdi
movq %r12, %rsi
callq 0x8426
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x165f5
cmpl $0x6, (%rax)
jl 0x832f
movq %r14, %rdi
callq 0x18bcb
movq %rax, %r10
subq $0x8, %rsp
leaq 0x1837f(%rip), %rsi     # 0x205fc
leaq 0x183df(%rip), %rdx     # 0x20663
leaq 0x18441(%rip), %r8      # 0x206cc
xorl %ebp, %ebp
pushq $0x6
popq %rdi
movl $0x121, %ecx            # imm = 0x121
movq %rbx, %r9
xorl %eax, %eax
pushq %r10
callq 0x1662e
popq %rax
popq %rcx
jmp 0x832f
xorl %eax, %eax
callq 0x165f5
pushq $-0x1
popq %rbp
cmpl $0x3, (%rax)
jl 0x832f
leaq 0x1833e(%rip), %rsi     # 0x205fc
leaq 0x1839e(%rip), %rdx     # 0x20663
leaq 0x1834e(%rip), %r8      # 0x2061a
pushq $0x3
popq %rdi
movl $0xf8, %ecx
xorl %eax, %eax
callq 0x1662e
jmp 0x832f
xorl %eax, %eax
callq 0x165f5
pushq $-0x1
popq %rbp
cmpl $0x3, (%rax)
jl 0x832f
movzwl 0x4(%r14), %r9d
movzwl 0x6(%r14), %r10d
movzwl 0x8(%r14), %r11d
movzwl 0xa(%r14), %r14d
leaq 0x182f5(%rip), %rsi     # 0x205fc
leaq 0x18355(%rip), %rdx     # 0x20663
leaq 0x18359(%rip), %r8      # 0x2066e
pushq $0x3
popq %rdi
movl $0x103, %ecx            # imm = 0x103
xorl %eax, %eax
pushq %rbx
pushq %r14
pushq %r11
pushq %r10
callq 0x1662e
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 0x165f5
pushq $-0x1
popq %rbp
cmpl $0x3, (%rax)
jl 0x832f
leaq 0x182a6(%rip), %rsi     # 0x205fc
leaq 0x18306(%rip), %rdx     # 0x20663
leaq 0x1834a(%rip), %r8      # 0x206ae
pushq $0x3
popq %rdi
movl $0x10b, %ecx            # imm = 0x10B
jmp 0x82d4
xorl %eax, %eax
callq 0x165f5
pushq $-0x1
popq %rbp
cmpl $0x3, (%rax)
jl 0x832f
leaq 0x18275(%rip), %rsi     # 0x205fc
leaq 0x182d5(%rip), %rdx     # 0x20663
leaq 0x18298(%rip), %r8      # 0x2062d
pushq $0x3
popq %rdi
movl $0x111, %ecx            # imm = 0x111
movq %rbx, %r9
xorl %eax, %eax
callq 0x1662e
jmp 0x832f
xorl %eax, %eax
callq 0x165f5
pushq $-0x1
popq %rbp
cmpl $0x3, (%rax)
jl 0x832f
leaq 0x18239(%rip), %rsi     # 0x205fc
leaq 0x18299(%rip), %rdx     # 0x20663
leaq 0x182e5(%rip), %r8      # 0x206b6
pushq $0x3
popq %rdi
movl $0x118, %ecx            # imm = 0x118
jmp 0x82d4