// File: /workspace/github_c_makefile_star2_O1/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 0x8f81
cmpb $0x0, (%r14)
je 0x8f81
movq %rdi, %r15
leaq 0x19818(%rip), %rsi     # 0x2272d
movq %r14, %rdi
callq 0x3430
testq %rax, %rax
je 0x8fcc
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
movl $0x402, %esi            # imm = 0x402
callq 0x1d737
testl %eax, %eax
je 0x8ffe
xorl %eax, %eax
callq 0x17d59
cmpl $0x3, (%rax)
jl 0x8f72
leaq 0x196c9(%rip), %rsi     # 0x2261c
leaq 0x197aa(%rip), %rdx     # 0x22704
leaq 0x197e4(%rip), %r8      # 0x22745
movl $0x3, %edi
movl $0x188, %ecx            # imm = 0x188
xorl %eax, %eax
callq 0x17d92
movq %rbx, %rdi
callq 0x3380
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0x8fbb
xorl %eax, %eax
callq 0x17d59
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpl $0x3, (%rax)
jl 0x8fbb
leaq 0x19683(%rip), %rsi     # 0x2261c
leaq 0x19764(%rip), %rdx     # 0x22704
leaq 0x19770(%rip), %r8      # 0x22717
movl $0x3, %edi
movl $0x17d, %ecx            # imm = 0x17D
movq %r14, %r9
xorl %eax, %eax
callq 0x17d92
movl %ebp, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %eax, %eax
callq 0x17d59
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpl $0x3, (%rax)
jl 0x8fbb
leaq 0x19638(%rip), %rsi     # 0x2261c
leaq 0x19719(%rip), %rdx     # 0x22704
leaq 0x1973e(%rip), %r8      # 0x22730
movl $0x3, %edi
movl $0x183, %ecx            # imm = 0x183
jmp 0x8fb1
leaq 0x19758(%rip), %rsi     # 0x2275d
leaq 0x10(%rsp), %rdi
movl $0x5, %edx
callq 0x1d7ad
cmpl $-0x1, %eax
je 0x9189
leaq 0x10(%rsp), %rdi
movl $0x2, %esi
movl $0x2, %edx
callq 0x1d916
cmpl $-0x1, %eax
je 0x91bd
xorps %xmm0, %xmm0
leaq 0x30(%rsp), %rsi
movaps %xmm0, (%rsi)
movb $0x0, 0x10(%rsi)
leaq 0x10(%rsp), %rdi
movl $0x11, %edx
callq 0x1d7ad
cmpl $-0x1, %eax
je 0x91f1
movq 0x10(%rsp), %rdi
movslq 0x18(%rsp), %rdx
movl $0x1, %esi
movq %rbx, %rcx
callq 0x3660
cmpl %eax, 0x18(%rsp)
jne 0x9225
movq 0x58(%r15), %r12
addq $0x58, %r15
xorl %ebp, %ebp
cmpq %r15, %r12
je 0x9135
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 0x1d916
movq 0x18(%r13), %rsi
movq %rbx, %rdi
movl $0x8, %edx
callq 0x1d98f
movq 0x10(%r13), %rsi
movq %rbx, %r13
movq %rbx, %rdi
xorl %edx, %edx
callq 0x1bdbb
testl %eax, %eax
jle 0x9271
movl $0x0, 0x1c(%rsp)
movq %r13, %rdi
xorl %esi, %esi
movl %eax, %edx
movl $0x2, %ecx
callq 0x1da17
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 0x92aa
incl %ebp
cmpq %r15, %r12
jne 0x909f
movq %rbx, %rdi
callq 0x3380
leaq 0x10(%rsp), %rdi
callq 0x1d5f9
xorl %eax, %eax
callq 0x17d59
cmpl $0x5, (%rax)
jl 0x8fbb
movq %r14, (%rsp)
leaq 0x194ba(%rip), %rsi     # 0x2261c
leaq 0x1959b(%rip), %rdx     # 0x22704
leaq 0x19632(%rip), %r8      # 0x227a2
movl $0x5, %edi
movl $0x1d0, %ecx            # imm = 0x1D0
movl %ebp, %r9d
xorl %eax, %eax
callq 0x17d92
jmp 0x8fbb
xorl %eax, %eax
callq 0x17d59
cmpl $0x3, (%rax)
jl 0x8f72
leaq 0x1947c(%rip), %rsi     # 0x2261c
leaq 0x1955d(%rip), %rdx     # 0x22704
leaq 0x195b4(%rip), %r8      # 0x22762
movl $0x3, %edi
movl $0x190, %ecx            # imm = 0x190
jmp 0x8f6b
xorl %eax, %eax
callq 0x17d59
cmpl $0x3, (%rax)
jl 0x8f72
leaq 0x19448(%rip), %rsi     # 0x2261c
leaq 0x19529(%rip), %rdx     # 0x22704
leaq 0x19580(%rip), %r8      # 0x22762
movl $0x3, %edi
movl $0x197, %ecx            # imm = 0x197
jmp 0x8f6b
xorl %eax, %eax
callq 0x17d59
cmpl $0x3, (%rax)
jl 0x8f72
leaq 0x19414(%rip), %rsi     # 0x2261c
leaq 0x194f5(%rip), %rdx     # 0x22704
leaq 0x1954c(%rip), %r8      # 0x22762
movl $0x3, %edi
movl $0x1a0, %ecx            # imm = 0x1A0
jmp 0x8f6b
xorl %eax, %eax
callq 0x17d59
cmpl $0x3, (%rax)
jl 0x925a
leaq 0x193e4(%rip), %rsi     # 0x2261c
leaq 0x194c5(%rip), %rdx     # 0x22704
leaq 0x19532(%rip), %r8      # 0x22778
movl $0x3, %edi
movl $0x1a8, %ecx            # imm = 0x1A8
movq %r14, %r9
xorl %eax, %eax
callq 0x17d92
movq %rbx, %rdi
callq 0x3380
leaq 0x10(%rsp), %rdi
callq 0x1d5f9
jmp 0x8f7a
xorl %eax, %eax
callq 0x17d59
cmpl $0x3, (%rax)
movq 0x28(%rsp), %rbx
jl 0x925a
leaq 0x19393(%rip), %rsi     # 0x2261c
leaq 0x19474(%rip), %rdx     # 0x22704
leaq 0x194f5(%rip), %r8      # 0x2278c
movl $0x3, %edi
movl $0x1b9, %ecx            # imm = 0x1B9
xorl %eax, %eax
callq 0x17d92
jmp 0x925a
xorl %eax, %eax
callq 0x17d59
cmpl $0x3, (%rax)
jl 0x925a
leaq 0x1935f(%rip), %rsi     # 0x2261c
leaq 0x19440(%rip), %rdx     # 0x22704
leaq 0x194ad(%rip), %r8      # 0x22778
movl $0x3, %edi
movl $0x1c4, %ecx            # imm = 0x1C4
jmp 0x9250