// File: /workspace/github_c_makefile_star2_O2/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 $0x58, %rsp
movq %rsi, %r14
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
andq $0x0, 0x20(%rsp)
testq %rsi, %rsi
je 0x8906
cmpb $0x0, (%r14)
je 0x8906
movq %rdi, %r15
leaq 0x17e6f(%rip), %rsi     # 0x2070d
movq %r14, %rdi
callq 0x3440
testq %rax, %rax
je 0x894d
movq %rax, %r12
leaq 0x10(%rsp), %rdi
movl $0x402, %esi            # imm = 0x402
callq 0x1b9d4
testl %eax, %eax
je 0x897b
xorl %eax, %eax
callq 0x165f5
cmpl $0x3, (%rax)
jl 0x88f9
leaq 0x17d20(%rip), %rsi     # 0x205fc
leaq 0x17e01(%rip), %rdx     # 0x206e4
leaq 0x17e3b(%rip), %r8      # 0x20725
pushq $0x3
popq %rdi
movl $0x188, %ecx            # imm = 0x188
xorl %eax, %eax
callq 0x1662e
movq %r12, %rdi
callq 0x3390
pushq $-0x1
popq %rbp
jmp 0x893c
xorl %eax, %eax
callq 0x165f5
pushq $-0x1
popq %rbp
cmpl $0x3, (%rax)
jl 0x893c
leaq 0x17ce0(%rip), %rsi     # 0x205fc
leaq 0x17dc1(%rip), %rdx     # 0x206e4
leaq 0x17dcd(%rip), %r8      # 0x206f7
pushq $0x3
popq %rdi
movl $0x17d, %ecx            # imm = 0x17D
movq %r14, %r9
xorl %eax, %eax
callq 0x1662e
movl %ebp, %eax
addq $0x58, %rsp
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 0x893c
leaq 0x17c99(%rip), %rsi     # 0x205fc
leaq 0x17d7a(%rip), %rdx     # 0x206e4
leaq 0x17d9f(%rip), %r8      # 0x20710
pushq $0x3
popq %rdi
movl $0x183, %ecx            # imm = 0x183
jmp 0x8932
leaq 0x17dbb(%rip), %rsi     # 0x2073d
leaq 0x10(%rsp), %rdi
pushq $0x5
popq %rdx
callq 0x1ba37
cmpl $-0x1, %eax
je 0x8aa6
leaq 0x10(%rsp), %rdi
pushq $0x2
popq %rdx
movl %edx, %esi
callq 0x1bbcd
cmpl $-0x1, %eax
je 0x8ad8
xorps %xmm0, %xmm0
leaq 0x40(%rsp), %rsi
movaps %xmm0, (%rsi)
movb $0x0, 0x10(%rsi)
leaq 0x10(%rsp), %rdi
pushq $0x11
popq %rdx
callq 0x1ba37
cmpl $-0x1, %eax
je 0x8b0a
movq 0x10(%rsp), %rdi
movslq 0x18(%rsp), %rdx
pushq $0x1
popq %rsi
movq %r12, %rcx
callq 0x3670
cmpl %eax, 0x18(%rsp)
jne 0x8b3c
movq 0x58(%r15), %r13
addq $0x58, %r15
xorl %ebp, %ebp
pushq $0x2
popq %rax
movq %r15, 0x30(%rsp)
cmpq %r15, %r13
je 0x8b6e
movq %r12, %r15
movq (%r13), %rcx
movq %rcx, 0x38(%rsp)
andq $0x0, 0x18(%rsp)
leaq 0x10(%rsp), %rbx
movq %rbx, %rdi
xorl %esi, %esi
movl %eax, %r12d
movl %eax, %edx
callq 0x1bbcd
movq 0x18(%r13), %rsi
movq %rbx, %rdi
pushq $0x8
popq %rdx
callq 0x1bc50
movq 0x10(%r13), %rsi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x1a2cd
testl %eax, %eax
jle 0x8bc0
andl $0x0, 0x1c(%rsp)
movq %rbx, %rdi
xorl %esi, %esi
movl %eax, %edx
movl %r12d, %ebx
movl %r12d, %ecx
callq 0x1bcd8
movq 0x10(%rsp), %rdi
movslq 0x18(%rsp), %rdx
pushq $0x1
popq %rsi
movq %r15, %r12
movq %r15, %rcx
callq 0x3670
cmpl %eax, 0x18(%rsp)
jne 0x8bf5
incl %ebp
movq 0x38(%rsp), %r13
movq 0x30(%rsp), %r15
movl %ebx, %eax
jmp 0x8a06
xorl %eax, %eax
callq 0x165f5
cmpl $0x3, (%rax)
jl 0x88f9
leaq 0x17b3f(%rip), %rsi     # 0x205fc
leaq 0x17c20(%rip), %rdx     # 0x206e4
leaq 0x17c77(%rip), %r8      # 0x20742
pushq $0x3
popq %rdi
movl $0x190, %ecx            # imm = 0x190
jmp 0x88f2
xorl %eax, %eax
callq 0x165f5
cmpl $0x3, (%rax)
jl 0x88f9
leaq 0x17b0d(%rip), %rsi     # 0x205fc
leaq 0x17bee(%rip), %rdx     # 0x206e4
leaq 0x17c45(%rip), %r8      # 0x20742
pushq $0x3
popq %rdi
movl $0x197, %ecx            # imm = 0x197
jmp 0x88f2
xorl %eax, %eax
callq 0x165f5
cmpl $0x3, (%rax)
jl 0x88f9
leaq 0x17adb(%rip), %rsi     # 0x205fc
leaq 0x17bbc(%rip), %rdx     # 0x206e4
leaq 0x17c13(%rip), %r8      # 0x20742
pushq $0x3
popq %rdi
movl $0x1a0, %ecx            # imm = 0x1A0
jmp 0x88f2
xorl %eax, %eax
callq 0x165f5
cmpl $0x3, (%rax)
jl 0x8c28
leaq 0x17aa9(%rip), %rsi     # 0x205fc
leaq 0x17b8a(%rip), %rdx     # 0x206e4
leaq 0x17bf7(%rip), %r8      # 0x20758
pushq $0x3
popq %rdi
movl $0x1a8, %ecx            # imm = 0x1A8
jmp 0x8c1e
movq %r12, %rdi
callq 0x3390
leaq 0x10(%rsp), %rdi
callq 0x1b8a6
xorl %eax, %eax
callq 0x165f5
cmpl $0x5, (%rax)
jl 0x893c
movq %r14, (%rsp)
leaq 0x17a61(%rip), %rsi     # 0x205fc
leaq 0x17b42(%rip), %rdx     # 0x206e4
leaq 0x17bd9(%rip), %r8      # 0x20782
pushq $0x5
popq %rdi
movl $0x1d0, %ecx            # imm = 0x1D0
movl %ebp, %r9d
xorl %eax, %eax
callq 0x1662e
jmp 0x893c
xorl %eax, %eax
callq 0x165f5
cmpl $0x3, (%rax)
jl 0x8bf0
leaq 0x17a29(%rip), %rsi     # 0x205fc
leaq 0x17b0a(%rip), %rdx     # 0x206e4
leaq 0x17b8b(%rip), %r8      # 0x2076c
pushq $0x3
popq %rdi
movl $0x1b9, %ecx            # imm = 0x1B9
xorl %eax, %eax
callq 0x1662e
movq %r15, %rdi
jmp 0x8c2b
xorl %eax, %eax
callq 0x165f5
cmpl $0x3, (%rax)
jl 0x8c28
leaq 0x179f4(%rip), %rsi     # 0x205fc
leaq 0x17ad5(%rip), %rdx     # 0x206e4
leaq 0x17b42(%rip), %r8      # 0x20758
pushq $0x3
popq %rdi
movl $0x1c4, %ecx            # imm = 0x1C4
movq %r14, %r9
xorl %eax, %eax
callq 0x1662e
movq %r12, %rdi
callq 0x3390
leaq 0x10(%rsp), %rdi
callq 0x1b8a6
jmp 0x8901