// File: /workspace/github_c_makefile_star2_O2/yakushstanislav[P]EagleMQ/src/storage.c
// Function: storage_save
int storage_save(const char *filename)
{
	FILE *fp;
	char tmpfile[32];

	snprintf(tmpfile, sizeof(tmpfile), "eaglemq-%d.dat", (int)getpid());

	fp = fopen(tmpfile, "w");
	if (!fp) {
		warning("Failed opening storage for saving: %s",  strerror(errno));
		return EG_STATUS_ERR;
	}

	if (storage_write_magic(fp) != EG_STATUS_OK)
		goto error;

	if (storage_save_users(fp) != EG_STATUS_OK)
		goto error;

	if (storage_save_queues(fp) != EG_STATUS_OK)
		goto error;

	if (storage_save_routes(fp) != EG_STATUS_OK)
		goto error;

	if (storage_save_channels(fp) != EG_STATUS_OK)
		goto error;

	if (storage_write_type(fp, EG_STORAGE_EOF) == -1)
		goto error;

	fflush(fp);
	fsync(fileno(fp));
	fclose(fp);

	if (rename(tmpfile, filename) == -1)
	{
		warning("Error rename temp storage file %s to %s: %s", tmpfile, filename, strerror(errno));
		unlink(tmpfile);
		return EG_STATUS_ERR;
	}

	return EG_STATUS_OK;

error:
	warning("Error write data to the storage");

	fclose(fp);
	unlink(tmpfile);

	return EG_STATUS_ERR;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rdi, %rbx
callq 0x2100
leaq 0x2a62(%rip), %rdx      # 0xc77d
leaq 0x40(%rsp), %r14
pushq $0x20
popq %rsi
movq %r14, %rdi
movl %eax, %ecx
xorl %eax, %eax
callq 0x2190
leaq 0x2337(%rip), %rsi      # 0xc06d
movq %r14, %rdi
callq 0x23c0
testq %rax, %rax
je 0x9e38
movq %rax, %r14
leaq 0x2b02(%rip), %rdx      # 0xc853
leaq 0x10(%rsp), %r15
pushq $0x10
popq %rsi
pushq $0x1
popq %rcx
movq %r15, %rdi
xorl %eax, %eax
callq 0x2190
pushq $0xb
popq %rdx
movq %r14, %rdi
movq %r15, %rsi
callq 0xa42f
cmpl $-0x1, %eax
je 0x9fb5
movq %rbx, 0x20(%rsp)
leaq 0x8507(%rip), %rax      # 0x12290
movq (%rax), %rax
movq 0x168(%rax), %rdi
leaq 0x10(%rsp), %r15
movq %r15, %rsi
callq 0x8226
pushq $0x1
popq %rbp
pushq $0x8
popq %r12
movq %r15, %rdi
callq 0x81e5
testq %rax, %rax
je 0x9e5a
movq 0x10(%rax), %r13
testb $0x40, 0x40(%r13)
jne 0x9da7
movq %r14, %rdi
movl %ebp, %esi
callq 0xa26d
cmpl $-0x1, %eax
je 0x9fb5
movq %r13, %rdi
callq 0x2150
leal 0x1(%rax), %edx
movq %r14, %rdi
movq %r13, %rsi
callq 0xa452
cmpl $-0x1, %eax
je 0x9fb5
leaq 0x20(%r13), %rbx
movq %rbx, %rdi
callq 0x2150
leal 0x1(%rax), %edx
movq %r14, %rdi
movq %rbx, %rsi
callq 0xa452
cmpl $-0x1, %eax
je 0x9fb5
addq $0x40, %r13
movq %r14, %rdi
movq %r13, %rsi
movl %r12d, %edx
callq 0xa452
cmpl $-0x1, %eax
jne 0x9da7
jmp 0x9fb5
callq 0x2050
movl (%rax), %edi
callq 0x2480
leaq 0x2941(%rip), %rdi      # 0xc78c
movq %rax, %rsi
xorl %eax, %eax
callq 0x4548
jmp 0x9fd5
leaq 0x842f(%rip), %rax      # 0x12290
movq (%rax), %rax
movq 0x170(%rax), %rdi
leaq 0x10(%rsp), %r15
movq %r15, %rsi
callq 0x8226
pushq $0x2
popq %rbx
pushq $0x4
popq %r12
pushq $0x3
popq %r13
movq %r15, %rdi
callq 0x81e5
testq %rax, %rax
je 0x9fe7
movq 0x10(%rax), %rbp
testb $0x8, 0x48(%rbp)
je 0x9e83
movq %rbp, %rdi
callq 0x8ad4
movl %eax, 0x30(%rsp)
movq %r14, %rdi
movl %ebx, %esi
callq 0xa26d
cmpl $-0x1, %eax
je 0x9fb5
movq %rbp, %rdi
callq 0x2150
leal 0x1(%rax), %edx
movq %r14, %rdi
movq %rbp, %rsi
callq 0xa452
cmpl $-0x1, %eax
je 0x9fb5
leaq 0x40(%rbp), %rsi
movq %r14, %rdi
movl %r12d, %edx
callq 0xa452
cmpl $-0x1, %eax
je 0x9fb5
leaq 0x44(%rbp), %rsi
movq %r14, %rdi
movl %r12d, %edx
callq 0xa452
cmpl $-0x1, %eax
je 0x9fb5
leaq 0x48(%rbp), %rsi
movq %r14, %rdi
movl %r12d, %edx
callq 0xa452
cmpl $-0x1, %eax
je 0x9fb5
movq %r14, %rdi
leaq 0x30(%rsp), %rsi
movl %r12d, %edx
callq 0xa452
cmpl $-0x1, %eax
je 0x9fb5
movl %ebx, %r15d
movq 0x58(%rbp), %rdi
pushq $0x1
popq %rsi
callq 0x8406
movq %rax, %rbp
movq %rbp, %rdi
callq 0x8436
testq %rax, %rax
je 0x9f98
movq 0x10(%rax), %rbx
movq %r14, %rdi
movl %r13d, %esi
callq 0xa26d
cmpl $-0x1, %eax
je 0x9fad
leaq 0x14(%rbx), %rsi
movq %r14, %rdi
movl %r12d, %edx
callq 0xa452
cmpl $-0x1, %eax
je 0x9fad
movq (%rbx), %rax
movq (%rax), %rsi
movl 0x8(%rax), %edx
movq %r14, %rdi
callq 0xa452
cmpl $-0x1, %eax
jne 0x9f4b
jmp 0x9fad
movq %rbp, %rdi
callq 0x8431
movl %r15d, %ebx
leaq 0x10(%rsp), %r15
jmp 0x9e83
movq %rbp, %rdi
callq 0x8431
leaq 0x2822(%rip), %rdi      # 0xc7de
xorl %eax, %eax
callq 0x4548
movq %r14, %rdi
callq 0x2120
leaq 0x40(%rsp), %rdi
callq 0x2060
pushq $-0x1
popq %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x82a2(%rip), %rax      # 0x12290
movq (%rax), %rax
movq 0x178(%rax), %rdi
leaq 0x30(%rsp), %r15
movq %r15, %rsi
callq 0x8226
pushq $0x4
popq %rbp
leaq 0x28(%rsp), %rbx
leaq 0x10(%rsp), %r12
movq %r15, %rdi
callq 0x81e5
testq %rax, %rax
je 0xa15e
movq 0x10(%rax), %r13
testb $0x4, 0x40(%r13)
je 0xa012
movq 0x50(%r13), %rax
movl 0x20(%rax), %eax
movl %eax, 0x8(%rsp)
movq %r14, %rdi
movl %ebp, %esi
callq 0xa26d
cmpl $-0x1, %eax
je 0x9fb5
movq %r13, %rdi
callq 0x2150
leal 0x1(%rax), %edx
movq %r14, %rdi
movq %r13, %rsi
callq 0xa452
cmpl $-0x1, %eax
je 0x9fb5
leaq 0x40(%r13), %rsi
movq %r14, %rdi
movl %ebp, %edx
callq 0xa452
cmpl $-0x1, %eax
je 0x9fb5
movq %r14, %rdi
leaq 0x8(%rsp), %rsi
movl %ebp, %edx
callq 0xa452
cmpl $-0x1, %eax
je 0x9fb5
movq 0x50(%r13), %rdi
movq %rbx, %rsi
callq 0x7f19
movq %rbx, %rdi
callq 0x7f20
testq %rax, %rax
je 0xa012
movq %r15, %rbx
movq 0x10(%rax), %r15
movq 0x18(%rax), %r13
movl 0x20(%r13), %eax
movl %eax, 0xc(%rsp)
movq %r14, %rdi
pushq $0x5
popq %rsi
callq 0xa26d
cmpl $-0x1, %eax
je 0x9fb5
movq %r15, %rdi
callq 0x2150
leal 0x1(%rax), %edx
movq %r14, %rdi
movq %r15, %rsi
callq 0xa452
cmpl $-0x1, %eax
je 0x9fb5
movq %r14, %rdi
leaq 0xc(%rsp), %rsi
movl %ebp, %edx
callq 0xa452
cmpl $-0x1, %eax
je 0x9fb5
movq %r13, %rdi
movq %r12, %rsi
callq 0x8226
movq %rbx, %r15
movq %r12, %rdi
callq 0x81e5
testq %rax, %rax
je 0xa154
movq 0x10(%rax), %rbx
movq %rbx, %rdi
callq 0x2150
leal 0x1(%rax), %edx
movq %r14, %rdi
movq %rbx, %rsi
callq 0xa452
cmpl $-0x1, %eax
jne 0xa123
jmp 0x9fb5
leaq 0x28(%rsp), %rbx
jmp 0xa0a6
leaq 0x812b(%rip), %rax      # 0x12290
movq (%rax), %rax
movq 0x180(%rax), %rdi
leaq 0x10(%rsp), %r15
movq %r15, %rsi
callq 0x8226
pushq $0x6
popq %rbp
pushq $0x4
popq %r12
movq %r15, %rdi
callq 0x81e5
testq %rax, %rax
je 0xa1e9
movq 0x10(%rax), %r13
testb $0x4, 0x40(%r13)
je 0xa183
movq %r14, %rdi
movl %ebp, %esi
callq 0xa26d
cmpl $-0x1, %eax
je 0x9fb5
movq %r13, %rdi
callq 0x2150
leal 0x1(%rax), %edx
movq %r14, %rdi
movq %r13, %rsi
callq 0xa452
cmpl $-0x1, %eax
je 0x9fb5
addq $0x40, %r13
movq %r14, %rdi
movq %r13, %rsi
movl %r12d, %edx
callq 0xa452
cmpl $-0x1, %eax
jne 0xa183
jmp 0x9fb5
movq %r14, %rdi
movl $0xff, %esi
callq 0xa26d
cmpl $-0x1, %eax
movq 0x20(%rsp), %r15
je 0x9fb5
movq %r14, %rdi
callq 0x2300
movq %r14, %rdi
callq 0x22b0
movl %eax, %edi
callq 0x2390
movq %r14, %rdi
callq 0x2120
leaq 0x40(%rsp), %rdi
movq %r15, %rsi
callq 0x2400
movl %eax, %ecx
xorl %eax, %eax
cmpl $-0x1, %ecx
jne 0x9fd8
callq 0x2050
movl (%rax), %edi
callq 0x2480
leaq 0x2562(%rip), %rdi      # 0xc7b2
leaq 0x40(%rsp), %rbx
movq %rbx, %rsi
movq %r15, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x4548
movq %rbx, %rdi
jmp 0x9fd0