// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x30(%rbp)
leaq -0x60(%rbp), %rdi
xorl %esi, %esi
movl $0x18, %edx
callq 0x3200
movl $0x0, -0x64(%rbp)
cmpq $0x0, -0x18(%rbp)
je 0xcbc2
movq -0x18(%rbp), %rax
cmpb $0x0, (%rax)
jne 0xcc08
jmp 0xcbc4
movb $0x0, %al
callq 0x242d0
cmpl $0x3, (%rax)
jl 0xcbfa
movq -0x18(%rbp), %r9
movl $0x3, %edi
leaq 0x29012(%rip), %rsi     # 0x35bf2
leaq 0x290f3(%rip), %rdx     # 0x35cda
movl $0x17d, %ecx            # imm = 0x17D
leaq 0x290fa(%rip), %r8      # 0x35ced
movb $0x0, %al
callq 0x243b0
jmp 0xcbfc
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xd082
movq -0x18(%rbp), %rdi
leaq 0x290f0(%rip), %rsi     # 0x35d03
callq 0x3440
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
jne 0xcc69
jmp 0xcc25
movb $0x0, %al
callq 0x242d0
cmpl $0x3, (%rax)
jl 0xcc5b
movq -0x18(%rbp), %r9
movl $0x3, %edi
leaq 0x28fb1(%rip), %rsi     # 0x35bf2
leaq 0x29092(%rip), %rdx     # 0x35cda
movl $0x183, %ecx            # imm = 0x183
leaq 0x290b2(%rip), %r8      # 0x35d06
movb $0x0, %al
callq 0x243b0
jmp 0xcc5d
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xd082
leaq -0x60(%rbp), %rdi
movl $0x402, %esi            # imm = 0x402
callq 0x2d900
cmpl $0x0, %eax
je 0xccc7
jmp 0xcc7e
movb $0x0, %al
callq 0x242d0
cmpl $0x3, (%rax)
jl 0xccb0
movl $0x3, %edi
leaq 0x28f5c(%rip), %rsi     # 0x35bf2
leaq 0x2903d(%rip), %rdx     # 0x35cda
movl $0x188, %ecx            # imm = 0x188
leaq 0x29072(%rip), %r8      # 0x35d1b
movb $0x0, %al
callq 0x243b0
jmp 0xccb2
movq -0x20(%rbp), %rdi
callq 0x33a0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xd082
leaq -0x60(%rbp), %rdi
leaq 0x29061(%rip), %rsi     # 0x35d33
movl $0x5, %edx
callq 0x2d9e0
cmpl $-0x1, %eax
jne 0xcd2c
jmp 0xcce3
movb $0x0, %al
callq 0x242d0
cmpl $0x3, (%rax)
jl 0xcd15
movl $0x3, %edi
leaq 0x28ef7(%rip), %rsi     # 0x35bf2
leaq 0x28fd8(%rip), %rdx     # 0x35cda
movl $0x190, %ecx            # imm = 0x190
leaq 0x2902a(%rip), %r8      # 0x35d38
movb $0x0, %al
callq 0x243b0
jmp 0xcd17
movq -0x20(%rbp), %rdi
callq 0x33a0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xd082
leaq -0x60(%rbp), %rdi
movl $0x2, %edx
movl %edx, %esi
callq 0x2dc90
cmpl $-0x1, %eax
jne 0xcd8c
jmp 0xcd43
movb $0x0, %al
callq 0x242d0
cmpl $0x3, (%rax)
jl 0xcd75
movl $0x3, %edi
leaq 0x28e97(%rip), %rsi     # 0x35bf2
leaq 0x28f78(%rip), %rdx     # 0x35cda
movl $0x197, %ecx            # imm = 0x197
leaq 0x28fca(%rip), %r8      # 0x35d38
movb $0x0, %al
callq 0x243b0
jmp 0xcd77
movq -0x20(%rbp), %rdi
callq 0x33a0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xd082
leaq -0x80(%rbp), %rdi
xorl %esi, %esi
movl $0x11, %edx
callq 0x3200
leaq -0x60(%rbp), %rdi
leaq -0x80(%rbp), %rsi
movl $0x11, %edx
callq 0x2d9e0
cmpl $-0x1, %eax
jne 0xcdfe
jmp 0xcdb5
movb $0x0, %al
callq 0x242d0
cmpl $0x3, (%rax)
jl 0xcde7
movl $0x3, %edi
leaq 0x28e25(%rip), %rsi     # 0x35bf2
leaq 0x28f06(%rip), %rdx     # 0x35cda
movl $0x1a0, %ecx            # imm = 0x1A0
leaq 0x28f58(%rip), %r8      # 0x35d38
movb $0x0, %al
callq 0x243b0
jmp 0xcde9
movq -0x20(%rbp), %rdi
callq 0x33a0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xd082
movq -0x60(%rbp), %rdi
movslq -0x58(%rbp), %rdx
movq -0x20(%rbp), %rcx
movl $0x1, %esi
callq 0x3670
movl %eax, -0x28(%rbp)
movl -0x28(%rbp), %eax
cmpl -0x58(%rbp), %eax
je 0xce77
jmp 0xce21
movb $0x0, %al
callq 0x242d0
cmpl $0x3, (%rax)
jl 0xce57
movq -0x18(%rbp), %r9
movl $0x3, %edi
leaq 0x28db5(%rip), %rsi     # 0x35bf2
leaq 0x28e96(%rip), %rdx     # 0x35cda
movl $0x1a8, %ecx            # imm = 0x1A8
leaq 0x28efe(%rip), %r8      # 0x35d4e
movb $0x0, %al
callq 0x243b0
jmp 0xce59
movq -0x20(%rbp), %rdi
callq 0x33a0
leaq -0x60(%rbp), %rdi
callq 0x2d640
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xd082
movq -0x30(%rbp), %rax
movq 0x58(%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x40(%rbp)
xorl %eax, %eax
movq %rax, %rcx
addq $0x28, %rcx
subq %rcx, %rax
addq -0x38(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x38(%rbp), %rax
movq -0x30(%rbp), %rcx
addq $0x58, %rcx
cmpq %rcx, %rax
sete %al
xorb $-0x1, %al
testb $0x1, %al
jne 0xcebf
jmp 0xd02a
movl $0x0, -0x54(%rbp)
movl $0x0, -0x58(%rbp)
leaq -0x60(%rbp), %rdi
xorl %esi, %esi
movl $0x2, %edx
callq 0x2dc90
leaq -0x60(%rbp), %rdi
movq -0x48(%rbp), %rax
movq 0x40(%rax), %rsi
movl $0x8, %edx
callq 0x2dd70
leaq -0x60(%rbp), %rdi
movq -0x48(%rbp), %rax
movq 0x38(%rax), %rsi
xorl %edx, %edx
callq 0x29ec0
movl %eax, -0x24(%rbp)
cmpl $0x0, %eax
jg 0xcf62
jmp 0xcf10
movb $0x0, %al
callq 0x242d0
cmpl $0x3, (%rax)
jl 0xcf42
movl $0x3, %edi
leaq 0x28cca(%rip), %rsi     # 0x35bf2
leaq 0x28dab(%rip), %rdx     # 0x35cda
movl $0x1b9, %ecx            # imm = 0x1B9
leaq 0x28e27(%rip), %r8      # 0x35d62
movb $0x0, %al
callq 0x243b0
jmp 0xcf44
movq -0x20(%rbp), %rdi
callq 0x33a0
leaq -0x60(%rbp), %rdi
callq 0x2d640
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xd082
movl $0x0, -0x54(%rbp)
leaq -0x60(%rbp), %rdi
movl -0x24(%rbp), %edx
xorl %esi, %esi
movl $0x2, %ecx
callq 0x2de60
movq -0x60(%rbp), %rdi
movslq -0x58(%rbp), %rdx
movq -0x20(%rbp), %rcx
movl $0x1, %esi
callq 0x3670
movl %eax, -0x28(%rbp)
movl -0x28(%rbp), %eax
cmpl -0x58(%rbp), %eax
je 0xcff5
jmp 0xcf9f
movb $0x0, %al
callq 0x242d0
cmpl $0x3, (%rax)
jl 0xcfd5
movq -0x18(%rbp), %r9
movl $0x3, %edi
leaq 0x28c37(%rip), %rsi     # 0x35bf2
leaq 0x28d18(%rip), %rdx     # 0x35cda
movl $0x1c4, %ecx            # imm = 0x1C4
leaq 0x28d80(%rip), %r8      # 0x35d4e
movb $0x0, %al
callq 0x243b0
jmp 0xcfd7
movq -0x20(%rbp), %rdi
callq 0x33a0
leaq -0x60(%rbp), %rdi
callq 0x2d640
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xd082
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
movq -0x40(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x40(%rbp)
xorl %eax, %eax
movq %rax, %rcx
addq $0x28, %rcx
subq %rcx, %rax
addq -0x38(%rbp), %rax
movq %rax, -0x48(%rbp)
jmp 0xcea2
movq -0x20(%rbp), %rdi
callq 0x33a0
leaq -0x60(%rbp), %rdi
callq 0x2d640
movb $0x0, %al
callq 0x242d0
cmpl $0x5, (%rax)
jl 0xd07a
movl -0x64(%rbp), %r9d
movq -0x18(%rbp), %rax
movl $0x5, %edi
leaq 0x28b96(%rip), %rsi     # 0x35bf2
leaq 0x28c77(%rip), %rdx     # 0x35cda
movl $0x1d0, %ecx            # imm = 0x1D0
leaq 0x28d09(%rip), %r8      # 0x35d78
movq %rax, (%rsp)
movb $0x0, %al
callq 0x243b0
jmp 0xd07c
movl -0x64(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x90, %rsp
popq %rbp
retq
nop