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

	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) {
		logi("no ttl: %s - %s\n", key, msg_answers(msg));
		return -1;
	}

	rbn = rbtree_lookup(&c->dic, key);
	if (!rbn) {
		item = cache_item_new(msg, key);
		if (!item) {
			loge("cache_item_new() error\n");
			return -1;
		}
		if (expire > 0)
			item->expire = expire;
		else
			update_expire(c, item, ttl);
		cache_item_add(c, item);
		rbtree_insert(&c->dic, &item->node);
		logi("cache added: %s - %s\n", key, msg_answers(msg));
	}
	else if (force) {
		item = rbtree_container_of(rbn, cache_item_t, node);
		ns_msg_t* 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;
		if (expire > 0)
			item->expire = expire;
		else
			update_expire(c, item, ttl);
		dllist_remove(&item->entry);
		cache_item_add(c, item);
		logi("cache updated: %s - %s\n", key, msg_answers(msg));
	}
	else {
		logi("cache update ignored: %s\n", key);
	}

	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %ecx, %r12d
testq %rsi, %rsi
setne %al
testq %rdx, %rdx
setne %cl
testb %cl, %al
je 0x8c15
movq %rdx, %r14
movq %rsi, %rbx
cmpq $0x0, 0x10(%rdx)
je 0x8c4e
cmpq $0x0, 0x18(%r14)
je 0x8c4e
cmpw $0x0, 0x4(%r14)
je 0x8c4e
movzwl 0x6(%r14), %eax
movzwl 0x8(%r14), %ecx
addl %eax, %ecx
movzwl 0xa(%r14), %eax
orl %ecx, %eax
je 0x8c4e
movq %r8, %r13
movq %rdi, %r15
movq %r14, %rdi
callq 0x19b1f
testl %eax, %eax
jle 0x8cb5
movl %eax, 0xc(%rsp)
leaq 0x68(%r15), %rbp
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x390c
movq %rax, 0x10(%rsp)
testq %rax, %rax
je 0x8d06
testl %r12d, %r12d
je 0x8d30
movq %r14, %rdi
callq 0x1a070
movq %rax, %r12
testq %rax, %rax
je 0x8d6e
movq 0x10(%rsp), %rbp
movq 0x38(%rbp), %rdi
callq 0x1a1dd
movq 0x38(%rbp), %rdi
callq 0x3450
movq %r12, 0x38(%rbp)
testq %r13, %r13
jle 0x8de2
movq %r13, 0x40(%rbp)
jmp 0x8df1
xorl %eax, %eax
callq 0x17d59
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpl $0x3, (%rax)
jl 0x8ca4
leaq 0x199ef(%rip), %rsi     # 0x2261c
leaq 0x19cbb(%rip), %rdx     # 0x228ef
leaq 0x199ff(%rip), %r8      # 0x2263a
movl $0x3, %edi
movl $0x12e, %ecx            # imm = 0x12E
xorl %eax, %eax
callq 0x17d92
jmp 0x8ca4
xorl %eax, %eax
callq 0x17d59
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpl $0x3, (%rax)
jl 0x8ca4
movzwl 0x4(%r14), %r9d
movzwl 0x6(%r14), %r10d
movzwl 0x8(%r14), %r11d
movzwl 0xa(%r14), %r14d
leaq 0x199a2(%rip), %rsi     # 0x2261c
leaq 0x19c6e(%rip), %rdx     # 0x228ef
leaq 0x19a06(%rip), %r8      # 0x2268e
movl $0x3, %edi
movl $0x139, %ecx            # imm = 0x139
xorl %eax, %eax
pushq %rbx
pushq %r14
pushq %r11
pushq %r10
callq 0x17d92
addq $0x20, %rsp
movl %ebp, %eax
addq $0x18, %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 $0x6, (%rax)
jl 0x8ca4
movq %r14, %rdi
callq 0x1a4f0
movq %rax, %r10
subq $0x8, %rsp
leaq 0x19940(%rip), %rsi     # 0x2261c
leaq 0x19c0c(%rip), %rdx     # 0x228ef
leaq 0x19c1b(%rip), %r8      # 0x22905
movl $0x6, %edi
movl $0x141, %ecx            # imm = 0x141
movq %rbx, %r9
xorl %eax, %eax
pushq %r10
callq 0x17d92
addq $0x10, %rsp
jmp 0x8ca4
movq %r14, %rdi
movq %rbx, %rsi
callq 0x9ce9
testq %rax, %rax
je 0x8da9
movq %rax, %r12
testq %r13, %r13
jle 0x8e68
movq %r13, 0x40(%r12)
jmp 0x8e77
xorl %eax, %eax
callq 0x17d59
cmpl $0x6, (%rax)
jl 0x8ed7
leaq 0x198d5(%rip), %rsi     # 0x2261c
leaq 0x19ba1(%rip), %rdx     # 0x228ef
leaq 0x19bef(%rip), %r8      # 0x22944
movl $0x6, %edi
movl $0x167, %ecx            # imm = 0x167
movq %rbx, %r9
xorl %eax, %eax
callq 0x17d92
jmp 0x8ed7
xorl %eax, %eax
callq 0x17d59
cmpl $0x3, (%rax)
jl 0x8e59
leaq 0x19897(%rip), %rsi     # 0x2261c
leaq 0x19b63(%rip), %rdx     # 0x228ef
leaq 0x19943(%rip), %r8      # 0x226d6
movl $0x3, %edi
movl $0x158, %ecx            # imm = 0x158
xorl %eax, %eax
callq 0x17d92
jmp 0x8e59
xorl %eax, %eax
callq 0x17d59
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpl $0x3, (%rax)
jl 0x8ca4
leaq 0x19857(%rip), %rsi     # 0x2261c
leaq 0x19b23(%rip), %rdx     # 0x228ef
leaq 0x19b43(%rip), %r8      # 0x22916
movl $0x3, %edi
movl $0x149, %ecx            # imm = 0x149
jmp 0x8c45
movq %r15, %rdi
movq %rbp, %rsi
movl 0xc(%rsp), %edx
callq 0x8a94
movq 0x28(%rbp), %rax
movq 0x30(%rbp), %rcx
movq %rax, (%rcx)
movq 0x28(%rbp), %rax
movq %rcx, 0x8(%rax)
movq %r15, %rdi
movq %rbp, %rsi
callq 0x8adc
xorl %eax, %eax
callq 0x17d59
cmpl $0x6, (%rax)
jl 0x8e59
movq %r14, %rdi
callq 0x1a4f0
movq %rax, %r10
subq $0x8, %rsp
leaq 0x197eb(%rip), %rsi     # 0x2261c
leaq 0x19ab7(%rip), %rdx     # 0x228ef
leaq 0x198ad(%rip), %r8      # 0x226ec
movl $0x6, %edi
movl $0x164, %ecx            # imm = 0x164
movq %rbx, %r9
xorl %eax, %eax
pushq %r10
callq 0x17d92
addq $0x10, %rsp
testq %r12, %r12
jne 0x8ed7
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0x8ca4
movq %r15, %rdi
movq %r12, %rsi
movl 0xc(%rsp), %edx
callq 0x8a94
movq %r15, %rdi
movq %r12, %rsi
callq 0x8adc
movq %rbp, %rdi
movq %r12, %rsi
callq 0x379c
xorl %eax, %eax
callq 0x17d59
cmpl $0x6, (%rax)
jl 0x8ed7
movq %r14, %rdi
callq 0x1a4f0
movq %rax, %r10
subq $0x8, %rsp
leaq 0x1976d(%rip), %rsi     # 0x2261c
leaq 0x19a39(%rip), %rdx     # 0x228ef
leaq 0x19a71(%rip), %r8      # 0x2292e
movl $0x6, %edi
movl $0x152, %ecx            # imm = 0x152
movq %rbx, %r9
xorl %eax, %eax
pushq %r10
callq 0x17d92
addq $0x10, %rsp
xorl %ebp, %ebp
jmp 0x8ca4