// File: /workspace/github_c_makefile_star2_O3/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 0x1c1e(%rip), %rdx      # 0xc7bd
leaq 0x40(%rsp), %r14
movl $0x20, %esi
movq %r14, %rdi
movl %eax, %ecx
xorl %eax, %eax
callq 0x2190
leaq 0x14b1(%rip), %rsi      # 0xc06d
movq %r14, %rdi
callq 0x23b0
testq %rax, %rax
je 0xae86
movq %rax, %r14
leaq 0x1cbc(%rip), %rdx      # 0xc893
leaq 0x10(%rsp), %r15
movl $0x10, %esi
movq %r15, %rdi
movl $0x1, %ecx
xorl %eax, %eax
callq 0x2190
movl $0xb, %esi
movl $0x1, %edx
movq %r15, %rdi
movq %r14, %rcx
callq 0x2450
testq %rax, %rax
je 0xaead
movq %rbx, 0x38(%rsp)
leaq 0x7676(%rip), %rbx      # 0x12290
movq (%rbx), %rax
movq 0x168(%rax), %rdi
leaq 0x10(%rsp), %r15
movq %r15, %rsi
callq 0x8ed9
movq %r15, %rdi
callq 0x8e98
testq %rax, %rax
je 0xacee
leaq 0x28(%rsp), %r15
leaq 0x10(%rsp), %r12
movq 0x10(%rax), %r13
testb $0x40, 0x40(%r13)
jne 0xacdd
movb $0x1, 0x28(%rsp)
movl $0x1, %esi
movl $0x1, %edx
movq %r15, %rdi
movq %r14, %rcx
callq 0x2450
testq %rax, %rax
je 0xaead
movq %r13, %rdi
callq 0x2150
leal 0x1(%rax), %edx
movq %r14, %rdi
movq %r13, %rsi
callq 0xb3b3
cmpl $-0x1, %eax
je 0xaead
leaq 0x20(%r13), %rbp
movq %rbp, %rdi
callq 0x2150
leal 0x1(%rax), %edx
movq %r14, %rdi
movq %rbp, %rsi
callq 0xb3b3
cmpl $-0x1, %eax
je 0xaead
addq $0x40, %r13
movq %r14, %rdi
movq %r13, %rsi
movl $0x8, %edx
callq 0xb3b3
cmpl $-0x1, %eax
je 0xaead
movq %r12, %rdi
callq 0x8e98
testq %rax, %rax
jne 0xac4c
movq (%rbx), %rax
movq 0x170(%rax), %rdi
leaq 0x10(%rsp), %r15
movq %r15, %rsi
callq 0x8ed9
movq %r15, %rdi
callq 0x8e98
testq %rax, %rax
je 0xaee1
leaq 0x10(%rsp), %r15
leaq 0x8(%rsp), %r12
leaq 0x28(%rsp), %r13
movq 0x10(%rax), %rbp
testb $0x8, 0x48(%rbp)
jne 0xad41
movq %r15, %rdi
callq 0x8e98
testq %rax, %rax
jne 0xad25
jmp 0xaee1
movq %rbp, %rdi
callq 0x9839
movl %eax, 0x28(%rsp)
movb $0x2, 0x8(%rsp)
movl $0x1, %esi
movl $0x1, %edx
movq %r12, %rdi
movq %r14, %rcx
callq 0x2450
testq %rax, %rax
je 0xaead
movq %rbp, %rdi
callq 0x2150
leal 0x1(%rax), %edx
movq %r14, %rdi
movq %rbp, %rsi
callq 0xb3b3
cmpl $-0x1, %eax
je 0xaead
leaq 0x40(%rbp), %rsi
movq %r14, %rdi
movl $0x4, %edx
callq 0xb3b3
cmpl $-0x1, %eax
je 0xaead
leaq 0x44(%rbp), %rsi
movq %r14, %rdi
movl $0x4, %edx
callq 0xb3b3
cmpl $-0x1, %eax
je 0xaead
leaq 0x48(%rbp), %rsi
movq %r14, %rdi
movl $0x4, %edx
callq 0xb3b3
cmpl $-0x1, %eax
je 0xaead
movq %r14, %rdi
movq %r13, %rsi
movl $0x4, %edx
callq 0xb3b3
cmpl $-0x1, %eax
je 0xaead
movq %r15, %rbx
movq %r13, %r15
movq 0x58(%rbp), %rdi
movl $0x1, %esi
callq 0x90d5
movq %rax, %rbp
movq %rbp, %rdi
callq 0x9107
testq %rax, %rax
je 0xae6c
movq 0x10(%rax), %r13
movb $0x3, 0x8(%rsp)
movl $0x1, %esi
movl $0x1, %edx
movq %r12, %rdi
movq %r14, %rcx
callq 0x2450
testq %rax, %rax
je 0xaea5
leaq 0x14(%r13), %rsi
movq %r14, %rdi
movl $0x4, %edx
callq 0xb3b3
cmpl $-0x1, %eax
je 0xaea5
movq (%r13), %rax
movq (%rax), %rsi
movl 0x8(%rax), %edx
movq %r14, %rdi
callq 0xb3b3
cmpl $-0x1, %eax
jne 0xae0d
jmp 0xaea5
movq %rbp, %rdi
callq 0x9102
movq %r15, %r13
movq %rbx, %r15
leaq 0x740f(%rip), %rbx      # 0x12290
jmp 0xad2f
callq 0x2050
movl (%rax), %edi
callq 0x2470
leaq 0x1933(%rip), %rdi      # 0xc7cc
movq %rax, %rsi
xorl %eax, %eax
callq 0x4bc8
jmp 0xaecd
movq %rbp, %rdi
callq 0x9102
leaq 0x196a(%rip), %rdi      # 0xc81e
xorl %eax, %eax
callq 0x4bc8
movq %r14, %rdi
callq 0x2120
leaq 0x40(%rsp), %rdi
callq 0x2060
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq (%rbx), %rax
movq 0x178(%rax), %rdi
leaq 0x28(%rsp), %r15
movq %r15, %rsi
callq 0x8ed9
movq %r15, %rdi
callq 0x8e98
testq %rax, %rax
je 0xb093
leaq 0x28(%rsp), %rbp
leaq 0x10(%rsp), %r12
leaq 0x20(%rsp), %r15
movq 0x10(%rax), %r13
testb $0x4, 0x40(%r13)
je 0xb082
movq 0x50(%r13), %rax
movl 0x20(%rax), %eax
movl %eax, 0x20(%rsp)
movb $0x4, 0x10(%rsp)
movl $0x1, %esi
movl $0x1, %edx
movq %r12, %rdi
movq %r14, %rcx
callq 0x2450
testq %rax, %rax
je 0xaead
movq %r13, %rdi
callq 0x2150
leal 0x1(%rax), %edx
movq %r14, %rdi
movq %r13, %rsi
callq 0xb3b3
cmpl $-0x1, %eax
je 0xaead
leaq 0x40(%r13), %rsi
movq %r14, %rdi
movl $0x4, %edx
callq 0xb3b3
cmpl $-0x1, %eax
je 0xaead
movq %r14, %rdi
movq %r15, %rsi
movl $0x4, %edx
callq 0xb3b3
cmpl $-0x1, %eax
je 0xaead
movq %r15, %rbx
movq 0x50(%r13), %rdi
leaq 0x8(%rsp), %r15
movq %r15, %rsi
callq 0x8b54
movq %r15, %rdi
callq 0x8b5b
testq %rax, %rax
je 0xb07f
movq 0x10(%rax), %r15
movq 0x18(%rax), %r13
movl 0x20(%r13), %eax
movl %eax, 0x24(%rsp)
movb $0x5, 0x10(%rsp)
movl $0x1, %esi
movl $0x1, %edx
movq %r12, %rdi
movq %r14, %rcx
callq 0x2450
testq %rax, %rax
je 0xaead
movq %r15, %rdi
callq 0x2150
leal 0x1(%rax), %edx
movq %r14, %rdi
movq %r15, %rsi
callq 0xb3b3
cmpl $-0x1, %eax
je 0xaead
movq %r14, %rdi
leaq 0x24(%rsp), %rsi
movl $0x4, %edx
callq 0xb3b3
cmpl $-0x1, %eax
je 0xaead
movq %r13, %rdi
movq %r12, %rsi
callq 0x8ed9
movq %r12, %rdi
callq 0x8e98
testq %rax, %rax
je 0xb075
movq 0x10(%rax), %r15
movq %r15, %rdi
callq 0x2150
leal 0x1(%rax), %edx
movq %r14, %rdi
movq %r15, %rsi
callq 0xb3b3
cmpl $-0x1, %eax
jne 0xb044
jmp 0xaead
leaq 0x8(%rsp), %rdi
jmp 0xafbe
movq %rbx, %r15
movq %rbp, %rdi
callq 0x8e98
testq %rax, %rax
jne 0xaf18
movq %r14, %rdi
callq 0xb134
testl %eax, %eax
movq 0x38(%rsp), %rbx
jne 0xaead
leaq 0x10(%rsp), %rdi
movb $-0x1, (%rdi)
movl $0x1, %esi
movl $0x1, %edx
movq %r14, %rcx
callq 0x2450
testq %rax, %rax
je 0xaead
movq %r14, %rdi
callq 0x2300
movq %r14, %rdi
callq 0x22b0
movl %eax, %edi
callq 0x2380
movq %r14, %rdi
callq 0x2120
leaq 0x40(%rsp), %rdi
movq %rbx, %rsi
callq 0x23f0
movl %eax, %ecx
xorl %eax, %eax
cmpl $-0x1, %ecx
jne 0xaed2
callq 0x2050
movl (%rax), %edi
callq 0x2470
leaq 0x16db(%rip), %rdi      # 0xc7f2
leaq 0x40(%rsp), %r14
movq %r14, %rsi
movq %rbx, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x4bc8
movq %r14, %rdi
jmp 0xaec8