// File: /workspace/github_c_makefile_star2_O3/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 $0x458, %rsp            # imm = 0x458
movq %rsi, %r14
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
movaps %xmm0, 0x20(%rsp)
testq %rsi, %rsi
je 0x906c
cmpb $0x0, (%r14)
je 0x906c
movl %edx, %ebp
movq %rdi, %r12
leaq 0x197d9(%rip), %rsi     # 0x227bb
movq %r14, %rdi
callq 0x3430
testq %rax, %rax
je 0x90ba
movq %rax, %r13
leaq 0x50(%rsp), %rdi
movl $0x1, %esi
movl $0x18, %edx
movq %rax, %rcx
callq 0x3530
cmpl $0x18, %eax
jne 0x9028
movl $0x42444344, %eax       # imm = 0x42444344
xorl 0x50(%rsp), %eax
movzbl 0x54(%rsp), %ecx
orl %eax, %ecx
je 0x90ec
xorl %eax, %eax
callq 0x176e5
cmpl $0x3, (%rax)
jl 0x905d
leaq 0x195c1(%rip), %rsi     # 0x225fc
leaq 0x19766(%rip), %rdx     # 0x227a8
leaq 0x19775(%rip), %r8      # 0x227be
movl $0x3, %edi
movl $0x1f0, %ecx            # imm = 0x1F0
movq %r14, %r9
xorl %eax, %eax
callq 0x1771e
movq %r13, %rdi
callq 0x3380
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0x90a6
xorl %eax, %eax
callq 0x176e5
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpl $0x3, (%rax)
jl 0x90a6
leaq 0x19578(%rip), %rsi     # 0x225fc
leaq 0x1971d(%rip), %rdx     # 0x227a8
leaq 0x19665(%rip), %r8      # 0x226f7
movl $0x3, %edi
movl $0x1e3, %ecx            # imm = 0x1E3
movq %r14, %r9
xorl %eax, %eax
callq 0x1771e
movl %ebp, %eax
addq $0x458, %rsp            # imm = 0x458
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %eax, %eax
callq 0x176e5
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpl $0x3, (%rax)
jl 0x90a6
leaq 0x1952a(%rip), %rsi     # 0x225fc
leaq 0x196cf(%rip), %rdx     # 0x227a8
leaq 0x19630(%rip), %r8      # 0x22710
movl $0x3, %edi
movl $0x1e9, %ecx            # imm = 0x1E9
jmp 0x909c
movzwl 0x55(%rsp), %eax
rolw $0x8, %ax
movzwl %ax, %ebx
leal -0x3(%rbx), %eax
cmpl $-0x3, %eax
ja 0x9142
xorl %eax, %eax
callq 0x176e5
cmpl $0x3, (%rax)
jl 0x905d
movq %r14, (%rsp)
leaq 0x194e1(%rip), %rsi     # 0x225fc
leaq 0x19686(%rip), %rdx     # 0x227a8
leaq 0x196a9(%rip), %r8      # 0x227d2
movl $0x3, %edi
movl $0x1fa, %ecx            # imm = 0x1FA
movl %ebx, %r9d
xorl %eax, %eax
callq 0x1771e
jmp 0x905d
cmpl $0x2, %ebx
jne 0x9153
xorl %edi, %edi
callq 0x3310
movq %rax, 0x48(%rsp)
leaq 0x50(%rsp), %rdi
movl $0x1, %esi
movl $0x2, %edx
movq %r13, %rcx
callq 0x3530
testl %eax, %eax
jle 0x9300
movl %ebp, 0x1c(%rsp)
xorl %ebp, %ebp
movq %r13, 0x8(%rsp)
movq %rbx, 0x10(%rsp)
cmpl $0x2, %eax
jne 0x934c
movzwl 0x50(%rsp), %eax
rolw $0x8, %ax
testw %ax, %ax
je 0x9302
movzwl %ax, %ebx
cmpl $0x401, %ebx            # imm = 0x401
jae 0x9380
cmpl $0x2, 0x10(%rsp)
je 0x91bd
xorl %r13d, %r13d
leaq 0x50(%rsp), %r15
jmp 0x91fc
movl $0x1, %esi
movl $0x8, %edx
leaq 0x50(%rsp), %rdi
movq %r13, %rcx
callq 0x3530
cmpl $0x8, %eax
jne 0x9420
xorl %eax, %eax
xorl %r13d, %r13d
leaq 0x50(%rsp), %r15
shlq $0x8, %r13
movzbl 0x50(%rsp,%rax), %ecx
orq %rcx, %r13
incq %rax
cmpq $0x8, %rax
jne 0x91e7
movl %ebx, %edx
movl $0x1, %esi
movq %r15, %rdi
movq 0x8(%rsp), %rcx
callq 0x3530
cmpl %eax, %ebx
jne 0x93b4
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
movaps %xmm0, 0x20(%rsp)
leaq 0x20(%rsp), %rdi
movq %r15, %rsi
movl %ebx, %edx
callq 0x1a0ca
cmpl $-0x1, %eax
je 0x93e1
cmpw $0x0, 0x24(%rsp)
je 0x925e
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
movq 0x10(%rsp), %rbx
je 0x9268
callq 0x19ba4
movq %rax, %r15
jmp 0x926b
xorl %r15d, %r15d
movq 0x10(%rsp), %rbx
jmp 0x926b
xorl %r15d, %r15d
cmpl $0x2, %ebx
jne 0x92ae
cmpq 0x48(%rsp), %r13
jg 0x92ae
xorl %eax, %eax
callq 0x176e5
cmpl $0x5, (%rax)
jl 0x92ce
movl $0x5, %edi
leaq 0x1936d(%rip), %rsi     # 0x225fc
leaq 0x19512(%rip), %rdx     # 0x227a8
movl $0x23f, %ecx            # imm = 0x23F
leaq 0x1954c(%rip), %r8      # 0x227ee
movq %r15, %r9
xorl %eax, %eax
callq 0x1771e
jmp 0x92ce
movq %r12, %rdi
movq %r15, %rsi
leaq 0x20(%rsp), %rdx
movl 0x1c(%rsp), %ecx
movq %r13, %r8
callq 0x8815
cmpl $-0x1, %eax
je 0x9454
leaq 0x20(%rsp), %rdi
callq 0x19ae0
incl %ebp
movl $0x1, %esi
movl $0x2, %edx
leaq 0x50(%rsp), %rdi
movq 0x8(%rsp), %r13
movq %r13, %rcx
callq 0x3530
testl %eax, %eax
jg 0x9182
jmp 0x9302
xorl %ebp, %ebp
movq %r13, %rdi
callq 0x3380
xorl %eax, %eax
callq 0x176e5
cmpl $0x5, (%rax)
jl 0x90a6
movq %r14, (%rsp)
leaq 0x192d7(%rip), %rsi     # 0x225fc
leaq 0x1947c(%rip), %rdx     # 0x227a8
leaq 0x194f2(%rip), %r8      # 0x22825
movl $0x5, %edi
movl $0x253, %ecx            # imm = 0x253
movl %ebp, %r9d
xorl %eax, %eax
callq 0x1771e
jmp 0x90a6
xorl %eax, %eax
callq 0x176e5
cmpl $0x3, (%rax)
jl 0x905d
leaq 0x19299(%rip), %rsi     # 0x225fc
leaq 0x1943e(%rip), %rdx     # 0x227a8
leaq 0x1944d(%rip), %r8      # 0x227be
movl $0x3, %edi
movl $0x205, %ecx            # imm = 0x205
jmp 0x9053
xorl %eax, %eax
callq 0x176e5
cmpl $0x3, (%rax)
jl 0x905d
leaq 0x19265(%rip), %rsi     # 0x225fc
leaq 0x1940a(%rip), %rdx     # 0x227a8
leaq 0x19419(%rip), %r8      # 0x227be
movl $0x3, %edi
movl $0x213, %ecx            # imm = 0x213
jmp 0x9053
xorl %eax, %eax
callq 0x176e5
cmpl $0x3, (%rax)
jl 0x9416
leaq 0x19235(%rip), %rsi     # 0x225fc
leaq 0x193da(%rip), %rdx     # 0x227a8
leaq 0x193e9(%rip), %r8      # 0x227be
movl $0x3, %edi
movl $0x22b, %ecx            # imm = 0x22B
jmp 0x940c
xorl %eax, %eax
callq 0x176e5
cmpl $0x3, (%rax)
jl 0x9416
leaq 0x19208(%rip), %rsi     # 0x225fc
leaq 0x193ad(%rip), %rdx     # 0x227a8
leaq 0x193bc(%rip), %r8      # 0x227be
movl $0x3, %edi
movl $0x233, %ecx            # imm = 0x233
movq %r14, %r9
xorl %eax, %eax
callq 0x1771e
movq 0x8(%rsp), %rdi
jmp 0x9060
xorl %eax, %eax
callq 0x176e5
cmpl $0x3, (%rax)
jl 0x905d
leaq 0x191c5(%rip), %rsi     # 0x225fc
leaq 0x1936a(%rip), %rdx     # 0x227a8
leaq 0x19379(%rip), %r8      # 0x227be
movl $0x3, %edi
movl $0x21b, %ecx            # imm = 0x21B
jmp 0x9053
xorl %eax, %eax
callq 0x176e5
cmpl $0x3, (%rax)
movq 0x8(%rsp), %rbx
jl 0x949c
leaq 0x20(%rsp), %rdi
callq 0x19df7
movq %rax, (%rsp)
leaq 0x19182(%rip), %rsi     # 0x225fc
leaq 0x19327(%rip), %rdx     # 0x227a8
leaq 0x1937e(%rip), %r8      # 0x22806
movl $0x3, %edi
movl $0x247, %ecx            # imm = 0x247
movq %r15, %r9
xorl %eax, %eax
callq 0x1771e
movq %rbx, %rdi
jmp 0x9060