// File: /workspace/github_c_makefile_star2_O3/GangZhuo[P]dohclient/src/channel_cache.c
// Function: cache_save_cachedb
int cache_save_cachedb(channel_t *ctx, const char *filename)
{
	FILE *fp;
	int msglen, n;
	cache_t *c = (cache_t*)ctx;
	dlitem_t *cur, *nxt;
	cache_item_t *item;
	stream_t s[1] = {0};
	int saved = 0;

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

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

	if (stream_set_cap(s, NS_PAYLOAD_SIZE + 2)) {
		loge("stream_set_cap() error\n");
		fclose(fp);
		return -1;
	}

	/* File header (24 bytes) */
	/* MAGIC (5 bytes) */
	if (stream_write(s, CACHEDB_MAGIC, sizeof(CACHEDB_MAGIC)) == -1) {
		loge("stream_write() error\n");
		fclose(fp);
		return -1;
	}

	/* Version (2 bytes) */
	if (stream_writei(s, CACHEDB_VERSION_V2, 2) == -1) {
		loge("stream_write() error\n");
		fclose(fp);
		return -1;
	}

	/* Padding (17 bytes) */
	{
		char padding[17] = {0};
		if (stream_write(s, padding, sizeof(padding)) == -1) {
			loge("stream_write() error\n");
			fclose(fp);
			return -1;
		}
	}

	n = fwrite(s->array, 1, s->size, fp);
	if (n != s->size) {
		loge("fwrite() error: %s\n", filename);
		fclose(fp);
		stream_free(s);
		return -1;
	}

	dllist_foreach(&c->items, cur, nxt,
		cache_item_t, item, entry) {

		stream_reset(s);

		/* write length */
		stream_writei16(s, 0);

		stream_writei64(s, item->expire);

		if ((msglen = ns_serialize(s, item->msg, 0)) <= 0) {
			loge("ns_serialize() error\n");
			fclose(fp);
			stream_free(s);
			return -1;
		}

		s->pos = 0;
		stream_seti16(s, 0, msglen);

		n = fwrite(s->array, 1, s->size, fp);
		if (n != s->size) {
			loge("fwrite() error: %s\n", filename);
			fclose(fp);
			stream_free(s);
			return -1;
		}

		saved++;
	}

	fclose(fp);
	stream_free(s);

	logn("Save %d item(s) to cache database %s\n", saved, filename);

	return saved;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rsi, %r14
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
movq $0x0, 0x20(%rsp)
testq %rsi, %rsi
je 0x8c49
cmpb $0x0, (%r14)
je 0x8c49
movq %rdi, %r15
leaq 0x19b30(%rip), %rsi     # 0x2270d
movq %r14, %rdi
callq 0x3430
testq %rax, %rax
je 0x8c94
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
movl $0x402, %esi            # imm = 0x402
callq 0x1cf07
testl %eax, %eax
je 0x8cc6
xorl %eax, %eax
callq 0x176e5
cmpl $0x3, (%rax)
jl 0x8c3a
leaq 0x199e1(%rip), %rsi     # 0x225fc
leaq 0x19ac2(%rip), %rdx     # 0x226e4
leaq 0x19afc(%rip), %r8      # 0x22725
movl $0x3, %edi
movl $0x188, %ecx            # imm = 0x188
xorl %eax, %eax
callq 0x1771e
movq %rbx, %rdi
callq 0x3380
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0x8c83
xorl %eax, %eax
callq 0x176e5
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpl $0x3, (%rax)
jl 0x8c83
leaq 0x1999b(%rip), %rsi     # 0x225fc
leaq 0x19a7c(%rip), %rdx     # 0x226e4
leaq 0x19a88(%rip), %r8      # 0x226f7
movl $0x3, %edi
movl $0x17d, %ecx            # imm = 0x17D
movq %r14, %r9
xorl %eax, %eax
callq 0x1771e
movl %ebp, %eax
addq $0x48, %rsp
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 0x8c83
leaq 0x19950(%rip), %rsi     # 0x225fc
leaq 0x19a31(%rip), %rdx     # 0x226e4
leaq 0x19a56(%rip), %r8      # 0x22710
movl $0x3, %edi
movl $0x183, %ecx            # imm = 0x183
jmp 0x8c79
leaq 0x19a70(%rip), %rsi     # 0x2273d
leaq 0x10(%rsp), %rdi
movl $0x5, %edx
callq 0x1cf7d
cmpl $-0x1, %eax
je 0x8e51
leaq 0x10(%rsp), %rdi
movl $0x2, %esi
movl $0x2, %edx
callq 0x1d115
cmpl $-0x1, %eax
je 0x8e85
xorps %xmm0, %xmm0
leaq 0x30(%rsp), %rsi
movaps %xmm0, (%rsi)
movb $0x0, 0x10(%rsi)
leaq 0x10(%rsp), %rdi
movl $0x11, %edx
callq 0x1cf7d
cmpl $-0x1, %eax
je 0x8eb9
movq 0x10(%rsp), %rdi
movslq 0x18(%rsp), %rdx
movl $0x1, %esi
movq %rbx, %rcx
callq 0x3660
cmpl %eax, 0x18(%rsp)
jne 0x8eed
movq 0x58(%r15), %r12
addq $0x58, %r15
xorl %ebp, %ebp
cmpq %r15, %r12
je 0x8dfd
leaq 0x10(%rsp), %r13
movq %rbx, 0x28(%rsp)
movq %r13, %rbx
movq %r12, %r13
movq (%r12), %r12
movq $0x0, 0x18(%rsp)
movq %rbx, %rdi
xorl %esi, %esi
movl $0x2, %edx
callq 0x1d115
movq 0x18(%r13), %rsi
movq %rbx, %rdi
movl $0x8, %edx
callq 0x1d194
movq 0x10(%r13), %rsi
movq %rbx, %r13
movq %rbx, %rdi
xorl %edx, %edx
callq 0x1b58c
testl %eax, %eax
jle 0x8f39
movl $0x0, 0x1c(%rsp)
movq %r13, %rdi
xorl %esi, %esi
movl %eax, %edx
movl $0x2, %ecx
callq 0x1d224
movq 0x10(%rsp), %rdi
movslq 0x18(%rsp), %rdx
movl $0x1, %esi
movq 0x28(%rsp), %rbx
movq %rbx, %rcx
callq 0x3660
cmpl %eax, 0x18(%rsp)
jne 0x8f72
incl %ebp
cmpq %r15, %r12
jne 0x8d67
movq %rbx, %rdi
callq 0x3380
leaq 0x10(%rsp), %rdi
callq 0x1cdc9
xorl %eax, %eax
callq 0x176e5
cmpl $0x5, (%rax)
jl 0x8c83
movq %r14, (%rsp)
leaq 0x197d2(%rip), %rsi     # 0x225fc
leaq 0x198b3(%rip), %rdx     # 0x226e4
leaq 0x1994a(%rip), %r8      # 0x22782
movl $0x5, %edi
movl $0x1d0, %ecx            # imm = 0x1D0
movl %ebp, %r9d
xorl %eax, %eax
callq 0x1771e
jmp 0x8c83
xorl %eax, %eax
callq 0x176e5
cmpl $0x3, (%rax)
jl 0x8c3a
leaq 0x19794(%rip), %rsi     # 0x225fc
leaq 0x19875(%rip), %rdx     # 0x226e4
leaq 0x198cc(%rip), %r8      # 0x22742
movl $0x3, %edi
movl $0x190, %ecx            # imm = 0x190
jmp 0x8c33
xorl %eax, %eax
callq 0x176e5
cmpl $0x3, (%rax)
jl 0x8c3a
leaq 0x19760(%rip), %rsi     # 0x225fc
leaq 0x19841(%rip), %rdx     # 0x226e4
leaq 0x19898(%rip), %r8      # 0x22742
movl $0x3, %edi
movl $0x197, %ecx            # imm = 0x197
jmp 0x8c33
xorl %eax, %eax
callq 0x176e5
cmpl $0x3, (%rax)
jl 0x8c3a
leaq 0x1972c(%rip), %rsi     # 0x225fc
leaq 0x1980d(%rip), %rdx     # 0x226e4
leaq 0x19864(%rip), %r8      # 0x22742
movl $0x3, %edi
movl $0x1a0, %ecx            # imm = 0x1A0
jmp 0x8c33
xorl %eax, %eax
callq 0x176e5
cmpl $0x3, (%rax)
jl 0x8f22
leaq 0x196fc(%rip), %rsi     # 0x225fc
leaq 0x197dd(%rip), %rdx     # 0x226e4
leaq 0x1984a(%rip), %r8      # 0x22758
movl $0x3, %edi
movl $0x1a8, %ecx            # imm = 0x1A8
movq %r14, %r9
xorl %eax, %eax
callq 0x1771e
movq %rbx, %rdi
callq 0x3380
leaq 0x10(%rsp), %rdi
callq 0x1cdc9
jmp 0x8c42
xorl %eax, %eax
callq 0x176e5
cmpl $0x3, (%rax)
movq 0x28(%rsp), %rbx
jl 0x8f22
leaq 0x196ab(%rip), %rsi     # 0x225fc
leaq 0x1978c(%rip), %rdx     # 0x226e4
leaq 0x1980d(%rip), %r8      # 0x2276c
movl $0x3, %edi
movl $0x1b9, %ecx            # imm = 0x1B9
xorl %eax, %eax
callq 0x1771e
jmp 0x8f22
xorl %eax, %eax
callq 0x176e5
cmpl $0x3, (%rax)
jl 0x8f22
leaq 0x19677(%rip), %rsi     # 0x225fc
leaq 0x19758(%rip), %rdx     # 0x226e4
leaq 0x197c5(%rip), %r8      # 0x22758
movl $0x3, %edi
movl $0x1c4, %ecx            # imm = 0x1C4
jmp 0x8f18