// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
pushq %rbx
subq $0x98, %rsp
movq %rdi, -0x18(%rbp)
movq %rsi, -0x20(%rbp)
movq %rdx, -0x28(%rbp)
movl %ecx, -0x2c(%rbp)
movq %r8, -0x38(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x20(%rbp)
je 0xc775
cmpq $0x0, -0x28(%rbp)
jne 0xc7b7
jmp 0xc777
movb $0x0, %al
callq 0x242d0
cmpl $0x3, (%rax)
jl 0xc7a9
movl $0x3, %edi
leaq 0x29463(%rip), %rsi     # 0x35bf2
leaq 0x2972f(%rip), %rdx     # 0x35ec5
movl $0x12e, %ecx            # imm = 0x12E
leaq 0x2946e(%rip), %r8      # 0x35c10
movb $0x0, %al
callq 0x243b0
jmp 0xc7ab
movl $0xffffffff, -0xc(%rbp) # imm = 0xFFFFFFFF
jmp 0xcb6f
movq -0x28(%rbp), %rax
cmpq $0x0, 0x10(%rax)
je 0xc7fb
movq -0x28(%rbp), %rax
cmpq $0x0, 0x18(%rax)
je 0xc7fb
movq -0x28(%rbp), %rax
movzwl 0x4(%rax), %eax
cmpl $0x1, %eax
jl 0xc7fb
movq -0x28(%rbp), %rax
movzwl 0x6(%rax), %eax
movq -0x28(%rbp), %rcx
movzwl 0x8(%rcx), %ecx
addl %ecx, %eax
movq -0x28(%rbp), %rcx
movzwl 0xa(%rcx), %ecx
addl %ecx, %eax
cmpl $0x1, %eax
jge 0xc876
jmp 0xc7fd
movb $0x0, %al
callq 0x242d0
cmpl $0x3, (%rax)
jl 0xc868
movq -0x28(%rbp), %rax
movzwl 0x4(%rax), %r9d
movq -0x28(%rbp), %rax
movzwl 0x6(%rax), %ebx
movq -0x28(%rbp), %rax
movzwl 0x8(%rax), %r11d
movq -0x28(%rbp), %rax
movzwl 0xa(%rax), %r10d
movq -0x20(%rbp), %rax
movl $0x3, %edi
leaq 0x293b6(%rip), %rsi     # 0x35bf2
leaq 0x29682(%rip), %rdx     # 0x35ec5
movl $0x139, %ecx            # imm = 0x139
leaq 0x29415(%rip), %r8      # 0x35c64
movl %ebx, (%rsp)
movl %r11d, 0x8(%rsp)
movl %r10d, 0x10(%rsp)
movq %rax, 0x18(%rsp)
movb $0x0, %al
callq 0x243b0
jmp 0xc86a
movl $0xffffffff, -0xc(%rbp) # imm = 0xFFFFFFFF
jmp 0xcb6f
movq -0x28(%rbp), %rdi
callq 0x27120
movl %eax, -0x54(%rbp)
cmpl $0x1, -0x54(%rbp)
jge 0xc8e3
jmp 0xc88a
movb $0x0, %al
callq 0x242d0
cmpl $0x6, (%rax)
jl 0xc8d5
movq -0x20(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x28(%rbp), %rdi
callq 0x27de0
movq -0x68(%rbp), %r9
movl $0x6, %edi
leaq 0x2933b(%rip), %rsi     # 0x35bf2
leaq 0x29607(%rip), %rdx     # 0x35ec5
movl $0x141, %ecx            # imm = 0x141
leaq 0x29611(%rip), %r8      # 0x35edb
movq %rax, (%rsp)
movb $0x0, %al
callq 0x243b0
jmp 0xc8d7
movl $0xffffffff, -0xc(%rbp) # imm = 0xFFFFFFFF
jmp 0xcb6f
movq -0x40(%rbp), %rdi
addq $0x68, %rdi
movq -0x20(%rbp), %rsi
callq 0x3b10
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x50(%rbp)
jne 0xc9f2
movq -0x28(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0xde40
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
jne 0xc95d
jmp 0xc91d
movb $0x0, %al
callq 0x242d0
cmpl $0x3, (%rax)
jl 0xc94f
movl $0x3, %edi
leaq 0x292bd(%rip), %rsi     # 0x35bf2
leaq 0x29589(%rip), %rdx     # 0x35ec5
movl $0x149, %ecx            # imm = 0x149
leaq 0x295a4(%rip), %r8      # 0x35eec
movb $0x0, %al
callq 0x243b0
jmp 0xc951
movl $0xffffffff, -0xc(%rbp) # imm = 0xFFFFFFFF
jmp 0xcb6f
cmpq $0x0, -0x38(%rbp)
jle 0xc972
movq -0x38(%rbp), %rcx
movq -0x48(%rbp), %rax
movq %rcx, 0x40(%rax)
jmp 0xc982
movq -0x40(%rbp), %rdi
movq -0x48(%rbp), %rsi
movl -0x54(%rbp), %edx
callq 0xc550
movq -0x40(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0xc5d0
movq -0x40(%rbp), %rdi
addq $0x68, %rdi
movq -0x48(%rbp), %rsi
callq 0x3790
movb $0x0, %al
callq 0x242d0
cmpl $0x6, (%rax)
jl 0xc9eb
movq -0x20(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x28(%rbp), %rdi
callq 0x27de0
movq -0x70(%rbp), %r9
movl $0x6, %edi
leaq 0x29225(%rip), %rsi     # 0x35bf2
leaq 0x294f1(%rip), %rdx     # 0x35ec5
movl $0x152, %ecx            # imm = 0x152
leaq 0x29524(%rip), %r8      # 0x35f04
movq %rax, (%rsp)
movb $0x0, %al
callq 0x243b0
jmp 0xc9ed
jmp 0xcb68
cmpl $0x0, -0x2c(%rbp)
je 0xcb2a
movq -0x50(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x28(%rbp), %rdi
callq 0x27660
movq %rax, -0x60(%rbp)
cmpq $0x0, -0x60(%rbp)
jne 0xca5a
jmp 0xca1a
movb $0x0, %al
callq 0x242d0
cmpl $0x3, (%rax)
jl 0xca4c
movl $0x3, %edi
leaq 0x291c0(%rip), %rsi     # 0x35bf2
leaq 0x2948c(%rip), %rdx     # 0x35ec5
movl $0x158, %ecx            # imm = 0x158
leaq 0x29267(%rip), %r8      # 0x35cac
movb $0x0, %al
callq 0x243b0
jmp 0xca4e
movl $0xffffffff, -0xc(%rbp) # imm = 0xFFFFFFFF
jmp 0xcb6f
movq -0x48(%rbp), %rax
movq 0x38(%rax), %rdi
callq 0x27870
movq -0x48(%rbp), %rax
movq 0x38(%rax), %rdi
callq 0x3460
movq -0x60(%rbp), %rcx
movq -0x48(%rbp), %rax
movq %rcx, 0x38(%rax)
cmpq $0x0, -0x38(%rbp)
jle 0xca95
movq -0x38(%rbp), %rcx
movq -0x48(%rbp), %rax
movq %rcx, 0x40(%rax)
jmp 0xcaa5
movq -0x40(%rbp), %rdi
movq -0x48(%rbp), %rsi
movl -0x54(%rbp), %edx
callq 0xc550
jmp 0xcaa7
movq -0x48(%rbp), %rax
movq 0x28(%rax), %rcx
movq -0x48(%rbp), %rax
movq 0x30(%rax), %rax
movq %rcx, (%rax)
movq -0x48(%rbp), %rax
movq 0x30(%rax), %rcx
movq -0x48(%rbp), %rax
movq 0x28(%rax), %rax
movq %rcx, 0x8(%rax)
movq -0x40(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0xc5d0
movb $0x0, %al
callq 0x242d0
cmpl $0x6, (%rax)
jl 0xcb26
movq -0x20(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x28(%rbp), %rdi
callq 0x27de0
movq -0x78(%rbp), %r9
movl $0x6, %edi
leaq 0x290ea(%rip), %rsi     # 0x35bf2
leaq 0x293b6(%rip), %rdx     # 0x35ec5
movl $0x164, %ecx            # imm = 0x164
leaq 0x291a7(%rip), %r8      # 0x35cc2
movq %rax, (%rsp)
movb $0x0, %al
callq 0x243b0
jmp 0xcb28
jmp 0xcb66
jmp 0xcb2c
movb $0x0, %al
callq 0x242d0
cmpl $0x6, (%rax)
jl 0xcb62
movq -0x20(%rbp), %r9
movl $0x6, %edi
leaq 0x290aa(%rip), %rsi     # 0x35bf2
leaq 0x29376(%rip), %rdx     # 0x35ec5
movl $0x167, %ecx            # imm = 0x167
leaq 0x293bf(%rip), %r8      # 0x35f1a
movb $0x0, %al
callq 0x243b0
jmp 0xcb64
jmp 0xcb66
jmp 0xcb68
movl $0x0, -0xc(%rbp)
movl -0xc(%rbp), %eax
addq $0x98, %rsp
popq %rbx
popq %rbp
retq
nopl (%rax)