// File: /workspace/github_c_makefile_star2_O2/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, %r13d
testq %rsi, %rsi
setne %al
testq %rdx, %rdx
setne %cl
testb %cl, %al
je 0x855b
movq %rdx, %r14
movq %rsi, %rbx
cmpq $0x0, 0x10(%rdx)
je 0x8590
cmpq $0x0, 0x18(%r14)
je 0x8590
cmpw $0x0, 0x4(%r14)
je 0x8590
movzwl 0x6(%r14), %eax
movzwl 0x8(%r14), %ecx
addl %eax, %ecx
movzwl 0xa(%r14), %eax
orl %ecx, %eax
je 0x8590
movq %r8, %r12
movq %rdi, %r15
movq %r14, %rdi
callq 0x182a3
testl %eax, %eax
jle 0x85f3
movl %eax, 0xc(%rsp)
leaq 0x68(%r15), %rbp
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x38ef
movq %rax, 0x10(%rsp)
testq %rax, %rax
je 0x8633
testl %r13d, %r13d
je 0x867d
movq %r14, %rdi
callq 0x1878a
testq %rax, %rax
je 0x86bd
movq %rax, %r13
movq 0x10(%rsp), %rbp
movq 0x38(%rbp), %rdi
callq 0x188ce
movq 0x38(%rbp), %rdi
callq 0x3460
movq %r13, 0x38(%rbp)
testq %r12, %r12
jle 0x8721
movq %r12, 0x40(%rbp)
jmp 0x8730
xorl %eax, %eax
callq 0x165f5
pushq $-0x1
popq %rbp
cmpl $0x3, (%rax)
jl 0x85e2
leaq 0x1808b(%rip), %rsi     # 0x205fc
leaq 0x18357(%rip), %rdx     # 0x208cf
leaq 0x1809b(%rip), %r8      # 0x2061a
pushq $0x3
popq %rdi
movl $0x12e, %ecx            # imm = 0x12E
xorl %eax, %eax
callq 0x1662e
jmp 0x85e2
xorl %eax, %eax
callq 0x165f5
pushq $-0x1
popq %rbp
cmpl $0x3, (%rax)
jl 0x85e2
movzwl 0x4(%r14), %r9d
movzwl 0x6(%r14), %r10d
movzwl 0x8(%r14), %r11d
movzwl 0xa(%r14), %r14d
leaq 0x18042(%rip), %rsi     # 0x205fc
leaq 0x1830e(%rip), %rdx     # 0x208cf
leaq 0x180a6(%rip), %r8      # 0x2066e
pushq $0x3
popq %rdi
movl $0x139, %ecx            # imm = 0x139
xorl %eax, %eax
pushq %rbx
pushq %r14
pushq %r11
pushq %r10
callq 0x1662e
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 0x165f5
pushq $-0x1
popq %rbp
cmpl $0x6, (%rax)
jl 0x85e2
movq %r14, %rdi
callq 0x18bcb
movq %rax, %r10
subq $0x8, %rsp
leaq 0x17fe4(%rip), %rsi     # 0x205fc
leaq 0x182b0(%rip), %rdx     # 0x208cf
leaq 0x182bf(%rip), %r8      # 0x208e5
pushq $0x6
popq %rdi
movl $0x141, %ecx            # imm = 0x141
jmp 0x8857
pushq $0x1
popq %rdi
pushq $0x48
popq %rsi
callq 0x3250
testq %rax, %rax
je 0x86f2
movq %rax, %r13
movq %r14, %rdi
callq 0x1878a
movq %rax, 0x38(%r13)
testq %rax, %rax
je 0x878f
movq %rbx, %rdi
callq 0x3620
movq %rax, (%r13)
testq %r12, %r12
jle 0x87f2
movq %r12, 0x40(%r13)
jmp 0x8801
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x165f5
cmpl $0x6, (%rax)
jl 0x85e2
leaq 0x17f66(%rip), %rsi     # 0x205fc
leaq 0x18232(%rip), %rdx     # 0x208cf
leaq 0x18280(%rip), %r8      # 0x20924
xorl %ebp, %ebp
pushq $0x6
popq %rdi
movl $0x167, %ecx            # imm = 0x167
movq %rbx, %r9
xorl %eax, %eax
callq 0x1662e
jmp 0x85e2
xorl %eax, %eax
callq 0x165f5
pushq $-0x1
popq %rbp
cmpl $0x3, (%rax)
jl 0x85e2
leaq 0x17f25(%rip), %rsi     # 0x205fc
leaq 0x181f1(%rip), %rdx     # 0x208cf
leaq 0x17fd1(%rip), %r8      # 0x206b6
pushq $0x3
popq %rdi
movl $0x158, %ecx            # imm = 0x158
jmp 0x8587
xorl %eax, %eax
callq 0x165f5
cmpl $0x3, (%rax)
jl 0x87bd
leaq 0x17ef3(%rip), %rsi     # 0x205fc
leaq 0x1822e(%rip), %rdx     # 0x2093e
leaq 0x1919c(%rip), %r8      # 0x218b3
pushq $0x3
popq %rdi
pushq $0x27
jmp 0x87b5
movq %r15, %rdi
movq %rbp, %rsi
movl 0xc(%rsp), %edx
callq 0x83de
movq 0x28(%rbp), %rax
movq 0x30(%rbp), %rcx
movq %rax, (%rcx)
movq %rcx, 0x8(%rax)
movq %r15, %rdi
movq %rbp, %rsi
callq 0x8426
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x165f5
cmpl $0x6, (%rax)
jl 0x85e2
movq %r14, %rdi
callq 0x18bcb
movq %rax, %r10
subq $0x8, %rsp
leaq 0x17e8a(%rip), %rsi     # 0x205fc
leaq 0x18156(%rip), %rdx     # 0x208cf
leaq 0x17f4c(%rip), %r8      # 0x206cc
xorl %ebp, %ebp
pushq $0x6
popq %rdi
movl $0x164, %ecx            # imm = 0x164
jmp 0x8857
xorl %eax, %eax
callq 0x165f5
cmpl $0x3, (%rax)
jl 0x87bd
leaq 0x17e5a(%rip), %rsi     # 0x205fc
leaq 0x18195(%rip), %rdx     # 0x2093e
leaq 0x17f06(%rip), %r8      # 0x206b6
pushq $0x3
popq %rdi
pushq $0x2f
popq %rcx
xorl %eax, %eax
callq 0x1662e
xorl %eax, %eax
callq 0x165f5
pushq $-0x1
popq %rbp
cmpl $0x3, (%rax)
jl 0x85e2
leaq 0x17e25(%rip), %rsi     # 0x205fc
leaq 0x180f1(%rip), %rdx     # 0x208cf
leaq 0x18111(%rip), %r8      # 0x208f6
pushq $0x3
popq %rdi
movl $0x149, %ecx            # imm = 0x149
jmp 0x8587
movq %r15, %rdi
movq %r13, %rsi
movl 0xc(%rsp), %edx
callq 0x83de
movq %r15, %rdi
movq %r13, %rsi
callq 0x8426
movq %rbp, %rdi
movq %r13, %rsi
callq 0x378c
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x165f5
cmpl $0x6, (%rax)
jl 0x85e2
movq %r14, %rdi
callq 0x18bcb
movq %rax, %r10
subq $0x8, %rsp
leaq 0x17dbd(%rip), %rsi     # 0x205fc
leaq 0x18089(%rip), %rdx     # 0x208cf
leaq 0x180c1(%rip), %r8      # 0x2090e
xorl %ebp, %ebp
pushq $0x6
popq %rdi
movl $0x152, %ecx            # imm = 0x152
movq %rbx, %r9
xorl %eax, %eax
pushq %r10
callq 0x1662e
popq %rax
popq %rcx
jmp 0x85e2