// File: /workspace/github_c_makefile_star2_O1/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 0x93a5
cmpb $0x0, (%r14)
je 0x93a5
movl %edx, %ebp
movq %rdi, %r12
leaq 0x194c1(%rip), %rsi     # 0x227db
movq %r14, %rdi
callq 0x3430
testq %rax, %rax
je 0x93f5
movq %rax, %r13
leaq 0x50(%rsp), %rdi
movl $0x1, %esi
movl $0x18, %edx
movq %rax, %rcx
callq 0x3530
cmpl $0x18, %eax
jne 0x9360
movl $0x42444344, %eax       # imm = 0x42444344
xorl 0x50(%rsp), %eax
movzbl 0x54(%rsp), %ecx
orl %eax, %ecx
je 0x9428
xorl %eax, %eax
callq 0x17d59
cmpl $0x3, (%rax)
jl 0x9395
leaq 0x192a9(%rip), %rsi     # 0x2261c
leaq 0x1944e(%rip), %rdx     # 0x227c8
leaq 0x1945d(%rip), %r8      # 0x227de
movl $0x3, %edi
movl $0x1f0, %ecx            # imm = 0x1F0
movq %r14, %r9
xorl %eax, %eax
callq 0x17d92
movq %r13, %rdi
callq 0x3380
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
jmp 0x93e0
xorl %eax, %eax
callq 0x17d59
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
cmpl $0x3, (%rax)
jl 0x93e0
leaq 0x1925e(%rip), %rsi     # 0x2261c
leaq 0x19403(%rip), %rdx     # 0x227c8
leaq 0x1934b(%rip), %r8      # 0x22717
movl $0x3, %edi
movl $0x1e3, %ecx            # imm = 0x1E3
movq %r14, %r9
xorl %eax, %eax
callq 0x17d92
movl %r15d, %eax
addq $0x458, %rsp            # imm = 0x458
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %eax, %eax
callq 0x17d59
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
cmpl $0x3, (%rax)
jl 0x93e0
leaq 0x1920e(%rip), %rsi     # 0x2261c
leaq 0x193b3(%rip), %rdx     # 0x227c8
leaq 0x19314(%rip), %r8      # 0x22730
movl $0x3, %edi
movl $0x1e9, %ecx            # imm = 0x1E9
jmp 0x93d6
movzwl 0x55(%rsp), %eax
rolw $0x8, %ax
movzwl %ax, %ebx
leal -0x3(%rbx), %eax
cmpl $-0x3, %eax
ja 0x947e
xorl %eax, %eax
callq 0x17d59
cmpl $0x3, (%rax)
jl 0x9395
movq %r14, (%rsp)
leaq 0x191c5(%rip), %rsi     # 0x2261c
leaq 0x1936a(%rip), %rdx     # 0x227c8
leaq 0x1938d(%rip), %r8      # 0x227f2
movl $0x3, %edi
movl $0x1fa, %ecx            # imm = 0x1FA
movl %ebx, %r9d
xorl %eax, %eax
callq 0x17d92
jmp 0x9395
cmpl $0x2, %ebx
jne 0x948f
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 0x9636
movl %ebp, 0x14(%rsp)
movq %rbx, 0x18(%rsp)
xorl %r15d, %r15d
movq %r13, 0x8(%rsp)
cmpl $0x2, %eax
jne 0x9683
movzwl 0x50(%rsp), %eax
rolw $0x8, %ax
testw %ax, %ax
je 0x9639
movzwl %ax, %ebx
cmpl $0x401, %ebx            # imm = 0x401
jae 0x96b7
cmpl $0x2, 0x18(%rsp)
jne 0x952c
movl $0x1, %esi
movl $0x8, %edx
leaq 0x50(%rsp), %rdi
movq %r13, %rcx
callq 0x3530
cmpl $0x8, %eax
jne 0x979c
xorl %eax, %eax
xorl %r13d, %r13d
shlq $0x8, %r13
movzbl 0x50(%rsp,%rax), %ecx
orq %rcx, %r13
incq %rax
cmpq $0x8, %rax
jne 0x9515
jmp 0x952f
xorl %r13d, %r13d
movl %ebx, %edx
movl $0x1, %esi
leaq 0x50(%rsp), %rbp
movq %rbp, %rdi
movq 0x8(%rsp), %rcx
callq 0x3530
cmpl %eax, %ebx
jne 0x96eb
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
movaps %xmm0, 0x20(%rsp)
leaq 0x20(%rsp), %rdi
movq %rbp, %rsi
movl %ebx, %edx
callq 0x1a7e0
cmpl $-0x1, %eax
je 0x9718
cmpw $0x0, 0x24(%rsp)
je 0x9591
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x9591
callq 0x1a29d
movq %rax, %rbp
jmp 0x9593
xorl %ebp, %ebp
cmpl $0x2, 0x18(%rsp)
jne 0x95a1
cmpq 0x48(%rsp), %r13
jle 0x95f6
movq %r12, %rdi
movq %rbp, %rsi
leaq 0x20(%rsp), %rdx
movl 0x14(%rsp), %ecx
movq %r13, %r8
callq 0x8b2e
movl %eax, %ebx
movq 0x8(%rsp), %r13
cmpl $-0x1, %ebx
je 0x9757
leaq 0x20(%rsp), %rdi
callq 0x1a1dd
incl %r15d
movl $0x1, %esi
movl $0x2, %edx
leaq 0x50(%rsp), %rdi
movq %r13, %rcx
callq 0x3530
testl %eax, %eax
jg 0x94bf
jmp 0x9639
xorl %ebx, %ebx
xorl %eax, %eax
callq 0x17d59
cmpl $0x5, (%rax)
movq 0x8(%rsp), %r13
jl 0x95bf
xorl %ebx, %ebx
movl $0x5, %edi
leaq 0x19005(%rip), %rsi     # 0x2261c
leaq 0x191aa(%rip), %rdx     # 0x227c8
movl $0x23f, %ecx            # imm = 0x23F
leaq 0x191e4(%rip), %r8      # 0x2280e
movq %rbp, %r9
xorl %eax, %eax
callq 0x17d92
jmp 0x95bf
xorl %r15d, %r15d
movq %r13, %rdi
callq 0x3380
xorl %eax, %eax
callq 0x17d59
cmpl $0x5, (%rax)
jl 0x93e0
movq %r14, (%rsp)
leaq 0x18fc0(%rip), %rsi     # 0x2261c
leaq 0x19165(%rip), %rdx     # 0x227c8
leaq 0x191db(%rip), %r8      # 0x22845
movl $0x5, %edi
movl $0x253, %ecx            # imm = 0x253
movl %r15d, %r9d
xorl %eax, %eax
callq 0x17d92
jmp 0x93e0
xorl %eax, %eax
callq 0x17d59
cmpl $0x3, (%rax)
jl 0x9395
leaq 0x18f82(%rip), %rsi     # 0x2261c
leaq 0x19127(%rip), %rdx     # 0x227c8
leaq 0x19136(%rip), %r8      # 0x227de
movl $0x3, %edi
movl $0x205, %ecx            # imm = 0x205
jmp 0x938b
xorl %eax, %eax
callq 0x17d59
cmpl $0x3, (%rax)
jl 0x9395
leaq 0x18f4e(%rip), %rsi     # 0x2261c
leaq 0x190f3(%rip), %rdx     # 0x227c8
leaq 0x19102(%rip), %r8      # 0x227de
movl $0x3, %edi
movl $0x213, %ecx            # imm = 0x213
jmp 0x938b
xorl %eax, %eax
callq 0x17d59
cmpl $0x3, (%rax)
jl 0x974d
leaq 0x18f1e(%rip), %rsi     # 0x2261c
leaq 0x190c3(%rip), %rdx     # 0x227c8
leaq 0x190d2(%rip), %r8      # 0x227de
movl $0x3, %edi
movl $0x22b, %ecx            # imm = 0x22B
jmp 0x9743
xorl %eax, %eax
callq 0x17d59
cmpl $0x3, (%rax)
jl 0x974d
leaq 0x18ef1(%rip), %rsi     # 0x2261c
leaq 0x19096(%rip), %rdx     # 0x227c8
leaq 0x190a5(%rip), %r8      # 0x227de
movl $0x3, %edi
movl $0x233, %ecx            # imm = 0x233
movq %r14, %r9
xorl %eax, %eax
callq 0x17d92
movq 0x8(%rsp), %rdi
jmp 0x9398
xorl %eax, %eax
callq 0x17d59
cmpl $0x3, (%rax)
jl 0x9395
leaq 0x20(%rsp), %rdi
callq 0x1a4f0
movq %rax, (%rsp)
leaq 0x18ea0(%rip), %rsi     # 0x2261c
leaq 0x19045(%rip), %rdx     # 0x227c8
leaq 0x1909c(%rip), %r8      # 0x22826
movl $0x3, %edi
movl $0x247, %ecx            # imm = 0x247
movq %rbp, %r9
jmp 0x938e
xorl %eax, %eax
callq 0x17d59
cmpl $0x3, (%rax)
jl 0x9395
leaq 0x18e69(%rip), %rsi     # 0x2261c
leaq 0x1900e(%rip), %rdx     # 0x227c8
leaq 0x1901d(%rip), %r8      # 0x227de
movl $0x3, %edi
movl $0x21b, %ecx            # imm = 0x21B
jmp 0x938b