// File: /workspace/github_c_makefile_star2_O2/GangZhuo[P]dohclient/src/channel_cache.c
// Function: cache_load_cachedb
int cache_load_cachedb(channel_t *ctx, const char *filename, int override)
{
	FILE *fp;
	int msglen, n;
	unsigned char buf[NS_PAYLOAD_SIZE];
	ns_msg_t msg[1] = {0};
	const char *key;
	int dbver = 0;
	int added = 0;
	long long expire;
	int i;
	time_t now;

	if (!filename || !*filename) {
		loge("Invalid filename: %s\n", filename);
		return -1;
	}

	fp = fopen(filename, "rb");
	if (fp == NULL) {
		loge("Can't open file: %s\n", filename);
		return -1;
	}

	n = fread(buf, 1, CACHEDB_HEAD_SIZE, fp);
	if (n != CACHEDB_HEAD_SIZE ||
			memcmp(buf, CACHEDB_MAGIC, sizeof(CACHEDB_MAGIC))) {
		loge("Invalid format: %s\n", filename);
		fclose(fp);
		return -1;
	}

	dbver = buf[sizeof(CACHEDB_MAGIC)];
	dbver <<= 8;
	dbver |= buf[sizeof(CACHEDB_MAGIC) + 1];
	if (dbver != CACHEDB_VERSION_V1 && dbver != CACHEDB_VERSION_V2) {
		loge("Unsupport version: %d - %s\n",
				dbver, filename);
		fclose(fp);
		return -1;
	}

	if (dbver == CACHEDB_VERSION_V2) {
		now = time(NULL);
	}

	while ((n = fread(buf, 1, 2, fp)) > 0) {
		if (n != 2) {
			loge("Invalid format: %s\n", filename);
			fclose(fp);
			return -1;
		}

		msglen = (int)buf[0] & 0xff;
		msglen <<= 8;
		msglen |= (int)buf[1] & 0xff;

		if (msglen == 0) {
			break;
		}

		if (msglen > NS_PAYLOAD_SIZE) {
			loge("Invalid format: %s\n", filename);
			fclose(fp);
			return -1;
		}

		if (dbver == CACHEDB_VERSION_V2) {
			n = fread(buf, 1, 8, fp);
			if (n != 8) {
				loge("Invalid format: %s\n", filename);
				fclose(fp);
				return -1;
			}
			expire = 0;
			for (i = 0; i < 8; i++) {
				expire <<= 8;
				expire |= buf[i];
			}
		}
		else {
			expire = 0;
		}

		n = fread(buf, 1, msglen, fp);
		if (n != msglen) {
			loge("Invalid format: %s\n", filename);
			fclose(fp);
			return -1;
		}

		memset(msg, 0, sizeof(ns_msg_t));

		if (ns_parse(msg, buf, msglen) == -1) {
			loge("Invalid format: %s\n", filename);
			fclose(fp);
			return -1;
		}

		key = msg_key(msg);
		if (dbver == CACHEDB_VERSION_V2) {
			if (expire > now) {
				n = cache_add_with_expire(ctx, key, msg, override, expire);
			}
			else {
				n = 0;
				logn("Skip %s, since expired\n", key);
			}
		}
		else {
			n = cache_add_with_expire(ctx, key, msg, override, expire);
		}
		if (n == -1) {
			loge("cache_add() error: key=%s, %s\n",
					key, msg_answers(msg));
			fclose(fp);
			return -1;
		}

		ns_msg_free(msg);

		added++;
	}

	fclose(fp);

	logn("Load %d item(s) from cache database %s\n", added, filename);

	return added;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x468, %rsp            # imm = 0x468
movq %rsi, %r14
xorps %xmm0, %xmm0
movaps %xmm0, 0x50(%rsp)
movaps %xmm0, 0x40(%rsp)
testq %rsi, %rsi
je 0x8d07
cmpb $0x0, (%r14)
je 0x8d07
movl %edx, %ebp
movq %rdi, %r15
leaq 0x17b3c(%rip), %rsi     # 0x207bb
movq %r14, %rdi
callq 0x3440
testq %rax, %rax
je 0x8d51
movq %rax, %r13
leaq 0x60(%rsp), %rdi
pushq $0x1
popq %rsi
pushq $0x18
popq %rdx
movq %rax, %rcx
callq 0x3540
cmpl $0x18, %eax
jne 0x8cc7
leaq 0x17a8b(%rip), %rsi     # 0x2073d
leaq 0x60(%rsp), %rdi
pushq $0x5
popq %rdx
callq 0x33b0
testl %eax, %eax
je 0x8d7f
xorl %eax, %eax
callq 0x165f5
cmpl $0x3, (%rax)
jl 0x8cfa
leaq 0x17922(%rip), %rsi     # 0x205fc
leaq 0x17ac7(%rip), %rdx     # 0x207a8
leaq 0x17ad6(%rip), %r8      # 0x207be
pushq $0x3
popq %rdi
movl $0x1f0, %ecx            # imm = 0x1F0
movq %r14, %r9
xorl %eax, %eax
callq 0x1662e
movq %r13, %rdi
callq 0x3390
pushq $-0x1
popq %rbp
jmp 0x8d3d
xorl %eax, %eax
callq 0x165f5
pushq $-0x1
popq %rbp
cmpl $0x3, (%rax)
jl 0x8d3d
leaq 0x178df(%rip), %rsi     # 0x205fc
leaq 0x17a84(%rip), %rdx     # 0x207a8
leaq 0x179cc(%rip), %r8      # 0x206f7
pushq $0x3
popq %rdi
movl $0x1e3, %ecx            # imm = 0x1E3
movq %r14, %r9
xorl %eax, %eax
callq 0x1662e
movl %ebp, %eax
addq $0x468, %rsp            # imm = 0x468
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 0x8d3d
leaq 0x17895(%rip), %rsi     # 0x205fc
leaq 0x17a3a(%rip), %rdx     # 0x207a8
leaq 0x1799b(%rip), %r8      # 0x20710
pushq $0x3
popq %rdi
movl $0x1e9, %ecx            # imm = 0x1E9
jmp 0x8d33
movzwl 0x65(%rsp), %eax
rolw $0x8, %ax
movzwl %ax, %ebx
leal -0x3(%rbx), %eax
cmpl $-0x3, %eax
ja 0x8dd3
xorl %eax, %eax
callq 0x165f5
cmpl $0x3, (%rax)
jl 0x8cfa
movq %r14, (%rsp)
leaq 0x1784e(%rip), %rsi     # 0x205fc
leaq 0x179f3(%rip), %rdx     # 0x207a8
leaq 0x17a16(%rip), %r8      # 0x207d2
pushq $0x3
popq %rdi
movl $0x1fa, %ecx            # imm = 0x1FA
movl %ebx, %r9d
xorl %eax, %eax
callq 0x1662e
jmp 0x8cfa
movq %r15, 0x30(%rsp)
movl %ebp, 0x1c(%rsp)
cmpl $0x2, %ebx
jne 0x8ded
xorl %edi, %edi
callq 0x3320
movq %rax, 0x38(%rsp)
leaq 0x60(%rsp), %r15
xorl %ebp, %ebp
movq %r13, 0x10(%rsp)
movq %rbx, 0x28(%rsp)
movq %r15, %rdi
pushq $0x1
popq %rsi
pushq $0x2
popq %rdx
movq %r13, %rcx
callq 0x3540
testl %eax, %eax
jle 0x8f7c
cmpl $0x2, %eax
jne 0x8fc4
movzwl 0x60(%rsp), %eax
rolw $0x8, %ax
testw %ax, %ax
je 0x8f7c
movzwl %ax, %ebx
cmpl $0x401, %ebx            # imm = 0x401
jae 0x8ff6
cmpl $0x2, 0x28(%rsp)
je 0x8f04
xorl %r13d, %r13d
movl %ebx, %edx
leaq 0x60(%rsp), %r12
movq %r12, %rdi
pushq $0x1
popq %rsi
movq 0x10(%rsp), %rcx
callq 0x3540
cmpl %eax, %ebx
jne 0x9028
xorps %xmm0, %xmm0
movaps %xmm0, 0x50(%rsp)
movaps %xmm0, 0x40(%rsp)
leaq 0x40(%rsp), %r15
movq %r15, %rdi
movq %r12, %rsi
movl %ebx, %edx
callq 0x18e8f
cmpl $-0x1, %eax
je 0x9053
movq %r15, %rdi
callq 0x9107
movq 0x28(%rsp), %rbx
cmpl $0x2, %ebx
movq %rax, 0x20(%rsp)
jne 0x8f40
cmpq 0x38(%rsp), %r13
jg 0x8f40
movq %r15, %r12
xorl %eax, %eax
callq 0x165f5
cmpl $0x5, (%rax)
movq 0x10(%rsp), %r13
leaq 0x60(%rsp), %r15
jl 0x8f6d
pushq $0x5
popq %rdi
leaq 0x17719(%rip), %rsi     # 0x205fc
leaq 0x178be(%rip), %rdx     # 0x207a8
movl $0x23f, %ecx            # imm = 0x23F
leaq 0x178f8(%rip), %r8      # 0x207ee
movq 0x20(%rsp), %r9
xorl %eax, %eax
callq 0x1662e
jmp 0x8f6d
leaq 0x60(%rsp), %rdi
pushq $0x1
popq %rsi
pushq $0x8
popq %rdx
movq %r13, %rcx
callq 0x3540
cmpl $0x8, %eax
jne 0x9090
xorl %eax, %eax
xorl %r13d, %r13d
cmpq $0x8, %rax
je 0x8e4f
shlq $0x8, %r13
movzbl 0x60(%rsp,%rax), %ecx
orq %rcx, %r13
incq %rax
jmp 0x8f25
movq 0x30(%rsp), %rdi
movq %rax, %rsi
movq %r15, %r12
movq %r15, %rdx
movl 0x1c(%rsp), %ecx
movq %r13, %r8
callq 0x8474
cmpl $-0x1, %eax
movq 0x10(%rsp), %r13
leaq 0x60(%rsp), %r15
je 0x90c2
movq %r12, %rdi
callq 0x188ce
incl %ebp
jmp 0x8dfe
movq %r13, %rdi
callq 0x3390
xorl %eax, %eax
callq 0x165f5
cmpl $0x5, (%rax)
jl 0x8d3d
movq %r14, (%rsp)
leaq 0x1765d(%rip), %rsi     # 0x205fc
leaq 0x17802(%rip), %rdx     # 0x207a8
leaq 0x17878(%rip), %r8      # 0x20825
pushq $0x5
popq %rdi
movl $0x253, %ecx            # imm = 0x253
movl %ebp, %r9d
xorl %eax, %eax
callq 0x1662e
jmp 0x8d3d
xorl %eax, %eax
callq 0x165f5
cmpl $0x3, (%rax)
jl 0x8cfa
leaq 0x17621(%rip), %rsi     # 0x205fc
leaq 0x177c6(%rip), %rdx     # 0x207a8
leaq 0x177d5(%rip), %r8      # 0x207be
pushq $0x3
popq %rdi
movl $0x205, %ecx            # imm = 0x205
jmp 0x8cf0
xorl %eax, %eax
callq 0x165f5
cmpl $0x3, (%rax)
jl 0x8cfa
leaq 0x175ef(%rip), %rsi     # 0x205fc
leaq 0x17794(%rip), %rdx     # 0x207a8
leaq 0x177a3(%rip), %r8      # 0x207be
pushq $0x3
popq %rdi
movl $0x213, %ecx            # imm = 0x213
jmp 0x8cf0
xorl %eax, %eax
callq 0x165f5
cmpl $0x3, (%rax)
jl 0x9086
leaq 0x175c1(%rip), %rsi     # 0x205fc
leaq 0x17766(%rip), %rdx     # 0x207a8
leaq 0x17775(%rip), %r8      # 0x207be
pushq $0x3
popq %rdi
movl $0x22b, %ecx            # imm = 0x22B
jmp 0x907c
xorl %eax, %eax
callq 0x165f5
cmpl $0x3, (%rax)
jl 0x9086
leaq 0x17596(%rip), %rsi     # 0x205fc
leaq 0x1773b(%rip), %rdx     # 0x207a8
leaq 0x1774a(%rip), %r8      # 0x207be
pushq $0x3
popq %rdi
movl $0x233, %ecx            # imm = 0x233
movq %r14, %r9
xorl %eax, %eax
callq 0x1662e
movq 0x10(%rsp), %rdi
jmp 0x8cfd
xorl %eax, %eax
callq 0x165f5
cmpl $0x3, (%rax)
jl 0x8cfa
leaq 0x17555(%rip), %rsi     # 0x205fc
leaq 0x176fa(%rip), %rdx     # 0x207a8
leaq 0x17709(%rip), %r8      # 0x207be
pushq $0x3
popq %rdi
movl $0x21b, %ecx            # imm = 0x21B
jmp 0x8cf0
xorl %eax, %eax
callq 0x165f5
cmpl $0x3, (%rax)
jl 0x8cfa
leaq 0x40(%rsp), %rdi
callq 0x18bcb
movq %rax, (%rsp)
leaq 0x17515(%rip), %rsi     # 0x205fc
leaq 0x176ba(%rip), %rdx     # 0x207a8
leaq 0x17711(%rip), %r8      # 0x20806
pushq $0x3
popq %rdi
movl $0x247, %ecx            # imm = 0x247
movq 0x20(%rsp), %r9
jmp 0x8cf3