// File: /workspace/github_c_makefile_star2_O3/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 0x88fc
movq %rdx, %r14
movq %rsi, %rbx
cmpq $0x0, 0x10(%rdx)
je 0x8935
cmpq $0x0, 0x18(%r14)
je 0x8935
cmpw $0x0, 0x4(%r14)
je 0x8935
movzwl 0x6(%r14), %eax
movzwl 0x8(%r14), %ecx
addl %eax, %ecx
movzwl 0xa(%r14), %eax
orl %ecx, %eax
je 0x8935
movq %r8, %r12
movq %rdi, %r15
movq %r14, %rdi
callq 0x194a3
testl %eax, %eax
jle 0x899c
movl %eax, 0xc(%rsp)
leaq 0x68(%r15), %rbp
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x39f8
movq %rax, 0x10(%rsp)
testq %rax, %rax
je 0x89e0
testl %r13d, %r13d
je 0x8a09
movq %r14, %rdi
callq 0x19992
testq %rax, %rax
je 0x8a4b
movq %rax, %r13
movq 0x10(%rsp), %rbp
movq 0x38(%rbp), %rdi
callq 0x19ae0
movq 0x38(%rbp), %rdi
callq 0x3450
movq %r13, 0x38(%rbp)
testq %r12, %r12
jle 0x8abd
movq %r12, 0x40(%rbp)
jmp 0x8acc
xorl %eax, %eax
callq 0x176e5
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpl $0x3, (%rax)
jl 0x898b
leaq 0x19ce8(%rip), %rsi     # 0x225fc
leaq 0x19fb4(%rip), %rdx     # 0x228cf
leaq 0x19cf8(%rip), %r8      # 0x2261a
movl $0x3, %edi
movl $0x12e, %ecx            # imm = 0x12E
xorl %eax, %eax
callq 0x1771e
jmp 0x898b
xorl %eax, %eax
callq 0x176e5
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpl $0x3, (%rax)
jl 0x898b
movzwl 0x4(%r14), %r9d
movzwl 0x6(%r14), %r10d
movzwl 0x8(%r14), %r11d
movzwl 0xa(%r14), %r14d
leaq 0x19c9b(%rip), %rsi     # 0x225fc
leaq 0x19f67(%rip), %rdx     # 0x228cf
leaq 0x19cff(%rip), %r8      # 0x2266e
movl $0x3, %edi
movl $0x139, %ecx            # imm = 0x139
xorl %eax, %eax
pushq %rbx
pushq %r14
pushq %r11
pushq %r10
callq 0x1771e
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 0x176e5
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpl $0x6, (%rax)
jl 0x898b
movq %r14, %rdi
callq 0x19df7
movq %rax, %r10
subq $0x8, %rsp
leaq 0x19c39(%rip), %rsi     # 0x225fc
leaq 0x19f05(%rip), %rdx     # 0x228cf
leaq 0x19f14(%rip), %r8      # 0x228e5
movl $0x6, %edi
movl $0x141, %ecx            # imm = 0x141
jmp 0x8b91
movq %r14, %rdi
movq %rbx, %rsi
callq 0x9986
testq %rax, %rax
je 0x8a84
movq %rax, %r13
testq %r12, %r12
jle 0x8b2a
movq %r12, 0x40(%r13)
jmp 0x8b39
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x176e5
cmpl $0x6, (%rax)
jl 0x898b
leaq 0x19bda(%rip), %rsi     # 0x225fc
leaq 0x19ea6(%rip), %rdx     # 0x228cf
leaq 0x19ef4(%rip), %r8      # 0x22924
xorl %ebp, %ebp
movl $0x6, %edi
movl $0x167, %ecx            # imm = 0x167
movq %rbx, %r9
xorl %eax, %eax
callq 0x1771e
jmp 0x898b
xorl %eax, %eax
callq 0x176e5
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpl $0x3, (%rax)
jl 0x898b
leaq 0x19b95(%rip), %rsi     # 0x225fc
leaq 0x19e61(%rip), %rdx     # 0x228cf
leaq 0x19c41(%rip), %r8      # 0x226b6
movl $0x3, %edi
movl $0x158, %ecx            # imm = 0x158
jmp 0x892c
xorl %eax, %eax
callq 0x176e5
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpl $0x3, (%rax)
jl 0x898b
leaq 0x19b5c(%rip), %rsi     # 0x225fc
leaq 0x19e28(%rip), %rdx     # 0x228cf
leaq 0x19e48(%rip), %r8      # 0x228f6
movl $0x3, %edi
movl $0x149, %ecx            # imm = 0x149
jmp 0x892c
movq %r15, %rdi
movq %rbp, %rsi
movl 0xc(%rsp), %edx
callq 0x877f
movq 0x28(%rbp), %rax
movq 0x30(%rbp), %rcx
movq %rax, (%rcx)
movq %rcx, 0x8(%rax)
movq %r15, %rdi
movq %rbp, %rsi
callq 0x87c7
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x176e5
cmpl $0x6, (%rax)
jl 0x898b
movq %r14, %rdi
callq 0x19df7
movq %rax, %r10
subq $0x8, %rsp
leaq 0x19aee(%rip), %rsi     # 0x225fc
leaq 0x19dba(%rip), %rdx     # 0x228cf
leaq 0x19bb0(%rip), %r8      # 0x226cc
xorl %ebp, %ebp
movl $0x6, %edi
movl $0x164, %ecx            # imm = 0x164
jmp 0x8b91
movq %r15, %rdi
movq %r13, %rsi
movl 0xc(%rsp), %edx
callq 0x877f
movq %r15, %rdi
movq %r13, %rsi
callq 0x87c7
movq %rbp, %rdi
movq %r13, %rsi
callq 0x379c
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x176e5
cmpl $0x6, (%rax)
jl 0x898b
movq %r14, %rdi
callq 0x19df7
movq %rax, %r10
subq $0x8, %rsp
leaq 0x19a85(%rip), %rsi     # 0x225fc
leaq 0x19d51(%rip), %rdx     # 0x228cf
leaq 0x19d89(%rip), %r8      # 0x2290e
xorl %ebp, %ebp
movl $0x6, %edi
movl $0x152, %ecx            # imm = 0x152
movq %rbx, %r9
xorl %eax, %eax
pushq %r10
callq 0x1771e
addq $0x10, %rsp
jmp 0x898b