// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x490, %rsp            # imm = 0x490
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
leaq -0x450(%rbp), %rdi
xorl %esi, %esi
movl $0x20, %edx
callq 0x3200
movl $0x0, -0x45c(%rbp)
movl $0x0, -0x460(%rbp)
cmpq $0x0, -0x18(%rbp)
je 0xd0dd
movq -0x18(%rbp), %rax
cmpb $0x0, (%rax)
jne 0xd123
jmp 0xd0df
movb $0x0, %al
callq 0x242d0
cmpl $0x3, (%rax)
jl 0xd115
movq -0x18(%rbp), %r9
movl $0x3, %edi
leaq 0x28af7(%rip), %rsi     # 0x35bf2
leaq 0x28c9c(%rip), %rdx     # 0x35d9e
movl $0x1e3, %ecx            # imm = 0x1E3
leaq 0x28bdf(%rip), %r8      # 0x35ced
movb $0x0, %al
callq 0x243b0
jmp 0xd117
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xd74f
movq -0x18(%rbp), %rdi
leaq 0x28c83(%rip), %rsi     # 0x35db1
callq 0x3440
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0xd184
jmp 0xd140
movb $0x0, %al
callq 0x242d0
cmpl $0x3, (%rax)
jl 0xd176
movq -0x18(%rbp), %r9
movl $0x3, %edi
leaq 0x28a96(%rip), %rsi     # 0x35bf2
leaq 0x28c3b(%rip), %rdx     # 0x35d9e
movl $0x1e9, %ecx            # imm = 0x1E9
leaq 0x28b97(%rip), %r8      # 0x35d06
movb $0x0, %al
callq 0x243b0
jmp 0xd178
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xd74f
leaq -0x430(%rbp), %rdi
movq -0x28(%rbp), %rcx
movl $0x1, %esi
movl $0x18, %edx
callq 0x3550
movl %eax, -0x30(%rbp)
cmpl $0x18, -0x30(%rbp)
jne 0xd1c4
leaq -0x430(%rbp), %rdi
leaq 0x28b7e(%rip), %rsi     # 0x35d33
movl $0x5, %edx
callq 0x3190
cmpl $0x0, %eax
je 0xd213
jmp 0xd1c6
movb $0x0, %al
callq 0x242d0
cmpl $0x3, (%rax)
jl 0xd1fc
movq -0x18(%rbp), %r9
movl $0x3, %edi
leaq 0x28a10(%rip), %rsi     # 0x35bf2
leaq 0x28bb5(%rip), %rdx     # 0x35d9e
movl $0x1f0, %ecx            # imm = 0x1F0
leaq 0x28bbf(%rip), %r8      # 0x35db4
movb $0x0, %al
callq 0x243b0
jmp 0xd1fe
movq -0x28(%rbp), %rdi
callq 0x33a0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xd74f
movzbl -0x42b(%rbp), %eax
movl %eax, -0x45c(%rbp)
movl -0x45c(%rbp), %eax
shll $0x8, %eax
movl %eax, -0x45c(%rbp)
movzbl -0x42a(%rbp), %eax
orl -0x45c(%rbp), %eax
movl %eax, -0x45c(%rbp)
cmpl $0x1, -0x45c(%rbp)
je 0xd2ae
cmpl $0x2, -0x45c(%rbp)
je 0xd2ae
jmp 0xd256
movb $0x0, %al
callq 0x242d0
cmpl $0x3, (%rax)
jl 0xd297
movl -0x45c(%rbp), %r9d
movq -0x18(%rbp), %rax
movl $0x3, %edi
leaq 0x28979(%rip), %rsi     # 0x35bf2
leaq 0x28b1e(%rip), %rdx     # 0x35d9e
movl $0x1fa, %ecx            # imm = 0x1FA
leaq 0x28b3c(%rip), %r8      # 0x35dc8
movq %rax, (%rsp)
movb $0x0, %al
callq 0x243b0
jmp 0xd299
movq -0x28(%rbp), %rdi
callq 0x33a0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xd74f
cmpl $0x2, -0x45c(%rbp)
jne 0xd2c7
xorl %eax, %eax
movl %eax, %edi
callq 0x3330
movq %rax, -0x478(%rbp)
jmp 0xd2c9
leaq -0x430(%rbp), %rdi
movq -0x28(%rbp), %rcx
movl $0x1, %esi
movl $0x2, %edx
callq 0x3550
movl %eax, -0x30(%rbp)
cmpl $0x0, %eax
jle 0xd6fa
cmpl $0x2, -0x30(%rbp)
je 0xd344
jmp 0xd2f7
movb $0x0, %al
callq 0x242d0
cmpl $0x3, (%rax)
jl 0xd32d
movq -0x18(%rbp), %r9
movl $0x3, %edi
leaq 0x288df(%rip), %rsi     # 0x35bf2
leaq 0x28a84(%rip), %rdx     # 0x35d9e
movl $0x205, %ecx            # imm = 0x205
leaq 0x28a8e(%rip), %r8      # 0x35db4
movb $0x0, %al
callq 0x243b0
jmp 0xd32f
movq -0x28(%rbp), %rdi
callq 0x33a0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xd74f
movzbl -0x430(%rbp), %eax
andl $0xff, %eax
movl %eax, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
shll $0x8, %eax
movl %eax, -0x2c(%rbp)
movzbl -0x42f(%rbp), %eax
andl $0xff, %eax
orl -0x2c(%rbp), %eax
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
jne 0xd379
jmp 0xd6fa
cmpl $0x400, -0x2c(%rbp)     # imm = 0x400
jle 0xd3d1
jmp 0xd384
movb $0x0, %al
callq 0x242d0
cmpl $0x3, (%rax)
jl 0xd3ba
movq -0x18(%rbp), %r9
movl $0x3, %edi
leaq 0x28852(%rip), %rsi     # 0x35bf2
leaq 0x289f7(%rip), %rdx     # 0x35d9e
movl $0x213, %ecx            # imm = 0x213
leaq 0x28a01(%rip), %r8      # 0x35db4
movb $0x0, %al
callq 0x243b0
jmp 0xd3bc
movq -0x28(%rbp), %rdi
callq 0x33a0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xd74f
cmpl $0x2, -0x45c(%rbp)
jne 0xd4b0
leaq -0x430(%rbp), %rdi
movq -0x28(%rbp), %rcx
movl $0x1, %esi
movl $0x8, %edx
callq 0x3550
movl %eax, -0x30(%rbp)
cmpl $0x8, -0x30(%rbp)
je 0xd450
jmp 0xd403
movb $0x0, %al
callq 0x242d0
cmpl $0x3, (%rax)
jl 0xd439
movq -0x18(%rbp), %r9
movl $0x3, %edi
leaq 0x287d3(%rip), %rsi     # 0x35bf2
leaq 0x28978(%rip), %rdx     # 0x35d9e
movl $0x21b, %ecx            # imm = 0x21B
leaq 0x28982(%rip), %r8      # 0x35db4
movb $0x0, %al
callq 0x243b0
jmp 0xd43b
movq -0x28(%rbp), %rdi
callq 0x33a0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xd74f
movq $0x0, -0x468(%rbp)
movl $0x0, -0x46c(%rbp)
cmpl $0x8, -0x46c(%rbp)
jge 0xd4ae
movq -0x468(%rbp), %rax
shlq $0x8, %rax
movq %rax, -0x468(%rbp)
movslq -0x46c(%rbp), %rax
movzbl -0x430(%rbp,%rax), %eax
orq -0x468(%rbp), %rax
movq %rax, -0x468(%rbp)
movl -0x46c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x46c(%rbp)
jmp 0xd465
jmp 0xd4bb
movq $0x0, -0x468(%rbp)
leaq -0x430(%rbp), %rdi
movslq -0x2c(%rbp), %rdx
movq -0x28(%rbp), %rcx
movl $0x1, %esi
callq 0x3550
movl %eax, -0x30(%rbp)
movl -0x30(%rbp), %eax
cmpl -0x2c(%rbp), %eax
je 0xd52e
jmp 0xd4e1
movb $0x0, %al
callq 0x242d0
cmpl $0x3, (%rax)
jl 0xd517
movq -0x18(%rbp), %r9
movl $0x3, %edi
leaq 0x286f5(%rip), %rsi     # 0x35bf2
leaq 0x2889a(%rip), %rdx     # 0x35d9e
movl $0x22b, %ecx            # imm = 0x22B
leaq 0x288a4(%rip), %r8      # 0x35db4
movb $0x0, %al
callq 0x243b0
jmp 0xd519
movq -0x28(%rbp), %rdi
callq 0x33a0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xd74f
leaq -0x450(%rbp), %rdi
xorl %esi, %esi
movl $0x20, %edx
callq 0x3200
leaq -0x450(%rbp), %rdi
leaq -0x430(%rbp), %rsi
movl -0x2c(%rbp), %edx
callq 0x284b0
cmpl $-0x1, %eax
jne 0xd5ab
jmp 0xd55e
movb $0x0, %al
callq 0x242d0
cmpl $0x3, (%rax)
jl 0xd594
movq -0x18(%rbp), %r9
movl $0x3, %edi
leaq 0x28678(%rip), %rsi     # 0x35bf2
leaq 0x2881d(%rip), %rdx     # 0x35d9e
movl $0x233, %ecx            # imm = 0x233
leaq 0x28827(%rip), %r8      # 0x35db4
movb $0x0, %al
callq 0x243b0
jmp 0xd596
movq -0x28(%rbp), %rdi
callq 0x33a0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xd74f
leaq -0x450(%rbp), %rdi
callq 0xd760
movq %rax, -0x458(%rbp)
cmpl $0x2, -0x45c(%rbp)
jne 0xd643
movq -0x468(%rbp), %rax
cmpq -0x478(%rbp), %rax
jle 0xd5fd
movq -0x10(%rbp), %rdi
movq -0x458(%rbp), %rsi
leaq -0x450(%rbp), %rdx
movl -0x1c(%rbp), %ecx
movq -0x468(%rbp), %r8
callq 0xc740
movl %eax, -0x30(%rbp)
jmp 0xd641
movl $0x0, -0x30(%rbp)
movb $0x0, %al
callq 0x242d0
cmpl $0x5, (%rax)
jl 0xd63d
movq -0x458(%rbp), %r9
movl $0x5, %edi
leaq 0x285cf(%rip), %rsi     # 0x35bf2
leaq 0x28774(%rip), %rdx     # 0x35d9e
movl $0x23f, %ecx            # imm = 0x23F
leaq 0x287ae(%rip), %r8      # 0x35de4
movb $0x0, %al
callq 0x243b0
jmp 0xd63f
jmp 0xd641
jmp 0xd667
movq -0x10(%rbp), %rdi
movq -0x458(%rbp), %rsi
leaq -0x450(%rbp), %rdx
movl -0x1c(%rbp), %ecx
movq -0x468(%rbp), %r8
callq 0xc740
movl %eax, -0x30(%rbp)
cmpl $-0x1, -0x30(%rbp)
jne 0xd6da
jmp 0xd66f
movb $0x0, %al
callq 0x242d0
cmpl $0x3, (%rax)
jl 0xd6c6
movq -0x458(%rbp), %rax
movq %rax, -0x480(%rbp)
leaq -0x450(%rbp), %rdi
callq 0x27de0
movq -0x480(%rbp), %r9
movl $0x3, %edi
leaq 0x2854a(%rip), %rsi     # 0x35bf2
leaq 0x286ef(%rip), %rdx     # 0x35d9e
movl $0x247, %ecx            # imm = 0x247
leaq 0x28741(%rip), %r8      # 0x35dfc
movq %rax, (%rsp)
movb $0x0, %al
callq 0x243b0
jmp 0xd6c8
movq -0x28(%rbp), %rdi
callq 0x33a0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xd74f
leaq -0x450(%rbp), %rdi
callq 0x27870
movl -0x460(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x460(%rbp)
jmp 0xd2c9
movq -0x28(%rbp), %rdi
callq 0x33a0
movb $0x0, %al
callq 0x242d0
cmpl $0x5, (%rax)
jl 0xd744
movl -0x460(%rbp), %r9d
movq -0x18(%rbp), %rax
movl $0x5, %edi
leaq 0x284cc(%rip), %rsi     # 0x35bf2
leaq 0x28671(%rip), %rdx     # 0x35d9e
movl $0x253, %ecx            # imm = 0x253
leaq 0x286e2(%rip), %r8      # 0x35e1b
movq %rax, (%rsp)
movb $0x0, %al
callq 0x243b0
jmp 0xd746
movl -0x460(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x490, %rsp            # imm = 0x490
popq %rbp
retq
nopl (%rax,%rax)