// File: /workspace/github_c_makefile_star2_O1/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 0x2110
leaq 0x2020(%rip), %rdx      # 0xc781
leaq 0x40(%rsp), %r14
movl $0x20, %esi
movq %r14, %rdi
movl %eax, %ecx
xorl %eax, %eax
callq 0x21a0
leaq 0x18ef(%rip), %rsi      # 0xc06d
movq %r14, %rdi
callq 0x23c0
testq %rax, %rax
je 0xaa48
movq %rax, %r14
leaq 0x20be(%rip), %rdx      # 0xc857
leaq 0x10(%rsp), %r15
movl $0x10, %esi
movq %r15, %rdi
movl $0x1, %ecx
xorl %eax, %eax
callq 0x21a0
movl $0xb, %esi
movl $0x1, %edx
movq %r15, %rdi
movq %r14, %rcx
callq 0x2460
testq %rax, %rax
je 0xaa6f
movq %rbx, 0x38(%rsp)
leaq 0x7ab4(%rip), %rbx      # 0x12290
movq (%rbx), %rax
movq 0x168(%rax), %rdi
leaq 0x10(%rsp), %r15
movq %r15, %rsi
callq 0x8a40
movq %r15, %rdi
callq 0x89ff
testq %rax, %rax
je 0xa8b0
leaq 0x28(%rsp), %r15
leaq 0x10(%rsp), %r12
movq 0x10(%rax), %r13
testb $0x40, 0x40(%r13)
jne 0xa89f
movb $0x1, 0x28(%rsp)
movl $0x1, %esi
movl $0x1, %edx
movq %r15, %rdi
movq %r14, %rcx
callq 0x2460
testq %rax, %rax
je 0xaa6f
movq %r13, %rdi
callq 0x2160
leal 0x1(%rax), %edx
movq %r14, %rdi
movq %r13, %rsi
callq 0xaf75
cmpl $-0x1, %eax
je 0xaa6f
leaq 0x20(%r13), %rbp
movq %rbp, %rdi
callq 0x2160
leal 0x1(%rax), %edx
movq %r14, %rdi
movq %rbp, %rsi
callq 0xaf75
cmpl $-0x1, %eax
je 0xaa6f
addq $0x40, %r13
movq %r14, %rdi
movq %r13, %rsi
movl $0x8, %edx
callq 0xaf75
cmpl $-0x1, %eax
je 0xaa6f
movq %r12, %rdi
callq 0x89ff
testq %rax, %rax
jne 0xa80e
movq (%rbx), %rax
movq 0x170(%rax), %rdi
leaq 0x10(%rsp), %r15
movq %r15, %rsi
callq 0x8a40
movq %r15, %rdi
callq 0x89ff
testq %rax, %rax
je 0xaaa3
leaq 0x10(%rsp), %r15
leaq 0x8(%rsp), %r12
leaq 0x28(%rsp), %r13
movq 0x10(%rax), %rbp
testb $0x8, 0x48(%rbp)
jne 0xa903
movq %r15, %rdi
callq 0x89ff
testq %rax, %rax
jne 0xa8e7
jmp 0xaaa3
movq %rbp, %rdi
callq 0x93a1
movl %eax, 0x28(%rsp)
movb $0x2, 0x8(%rsp)
movl $0x1, %esi
movl $0x1, %edx
movq %r12, %rdi
movq %r14, %rcx
callq 0x2460
testq %rax, %rax
je 0xaa6f
movq %rbp, %rdi
callq 0x2160
leal 0x1(%rax), %edx
movq %r14, %rdi
movq %rbp, %rsi
callq 0xaf75
cmpl $-0x1, %eax
je 0xaa6f
leaq 0x40(%rbp), %rsi
movq %r14, %rdi
movl $0x4, %edx
callq 0xaf75
cmpl $-0x1, %eax
je 0xaa6f
leaq 0x44(%rbp), %rsi
movq %r14, %rdi
movl $0x4, %edx
callq 0xaf75
cmpl $-0x1, %eax
je 0xaa6f
leaq 0x48(%rbp), %rsi
movq %r14, %rdi
movl $0x4, %edx
callq 0xaf75
cmpl $-0x1, %eax
je 0xaa6f
movq %r14, %rdi
movq %r13, %rsi
movl $0x4, %edx
callq 0xaf75
cmpl $-0x1, %eax
je 0xaa6f
movq %r15, %rbx
movq %r13, %r15
movq 0x58(%rbp), %rdi
movl $0x1, %esi
callq 0x8c3d
movq %rax, %rbp
movq %rbp, %rdi
callq 0x8c6f
testq %rax, %rax
je 0xaa2e
movq 0x10(%rax), %r13
movb $0x3, 0x8(%rsp)
movl $0x1, %esi
movl $0x1, %edx
movq %r12, %rdi
movq %r14, %rcx
callq 0x2460
testq %rax, %rax
je 0xaa67
leaq 0x14(%r13), %rsi
movq %r14, %rdi
movl $0x4, %edx
callq 0xaf75
cmpl $-0x1, %eax
je 0xaa67
movq (%r13), %rax
movq (%rax), %rsi
movl 0x8(%rax), %edx
movq %r14, %rdi
callq 0xaf75
cmpl $-0x1, %eax
jne 0xa9cf
jmp 0xaa67
movq %rbp, %rdi
callq 0x8c6a
movq %r15, %r13
movq %rbx, %r15
leaq 0x784d(%rip), %rbx      # 0x12290
jmp 0xa8f1
callq 0x2050
movl (%rax), %edi
callq 0x2480
leaq 0x1d35(%rip), %rdi      # 0xc790
movq %rax, %rsi
xorl %eax, %eax
callq 0x4734
jmp 0xaa8f
movq %rbp, %rdi
callq 0x8c6a
leaq 0x1d6c(%rip), %rdi      # 0xc7e2
xorl %eax, %eax
callq 0x4734
movq %r14, %rdi
callq 0x2130
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 0x8a40
movq %r15, %rdi
callq 0x89ff
testq %rax, %rax
je 0xac55
leaq 0x28(%rsp), %rbp
leaq 0x10(%rsp), %r12
leaq 0x20(%rsp), %r15
movq 0x10(%rax), %r13
testb $0x4, 0x40(%r13)
je 0xac44
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 0x2460
testq %rax, %rax
je 0xaa6f
movq %r13, %rdi
callq 0x2160
leal 0x1(%rax), %edx
movq %r14, %rdi
movq %r13, %rsi
callq 0xaf75
cmpl $-0x1, %eax
je 0xaa6f
leaq 0x40(%r13), %rsi
movq %r14, %rdi
movl $0x4, %edx
callq 0xaf75
cmpl $-0x1, %eax
je 0xaa6f
movq %r14, %rdi
movq %r15, %rsi
movl $0x4, %edx
callq 0xaf75
cmpl $-0x1, %eax
je 0xaa6f
movq %r15, %rbx
movq 0x50(%r13), %rdi
leaq 0x8(%rsp), %r15
movq %r15, %rsi
callq 0x86b7
movq %r15, %rdi
callq 0x86be
testq %rax, %rax
je 0xac41
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 0x2460
testq %rax, %rax
je 0xaa6f
movq %r15, %rdi
callq 0x2160
leal 0x1(%rax), %edx
movq %r14, %rdi
movq %r15, %rsi
callq 0xaf75
cmpl $-0x1, %eax
je 0xaa6f
movq %r14, %rdi
leaq 0x24(%rsp), %rsi
movl $0x4, %edx
callq 0xaf75
cmpl $-0x1, %eax
je 0xaa6f
movq %r13, %rdi
movq %r12, %rsi
callq 0x8a40
movq %r12, %rdi
callq 0x89ff
testq %rax, %rax
je 0xac37
movq 0x10(%rax), %r15
movq %r15, %rdi
callq 0x2160
leal 0x1(%rax), %edx
movq %r14, %rdi
movq %r15, %rsi
callq 0xaf75
cmpl $-0x1, %eax
jne 0xac06
jmp 0xaa6f
leaq 0x8(%rsp), %rdi
jmp 0xab80
movq %rbx, %r15
movq %rbp, %rdi
callq 0x89ff
testq %rax, %rax
jne 0xaada
movq %r14, %rdi
callq 0xacf6
testl %eax, %eax
movq 0x38(%rsp), %rbx
jne 0xaa6f
leaq 0x10(%rsp), %rdi
movb $-0x1, (%rdi)
movl $0x1, %esi
movl $0x1, %edx
movq %r14, %rcx
callq 0x2460
testq %rax, %rax
je 0xaa6f
movq %r14, %rdi
callq 0x2310
movq %r14, %rdi
callq 0x22c0
movl %eax, %edi
callq 0x2390
movq %r14, %rdi
callq 0x2130
leaq 0x40(%rsp), %rdi
movq %rbx, %rsi
callq 0x2400
movl %eax, %ecx
xorl %eax, %eax
cmpl $-0x1, %ecx
jne 0xaa94
callq 0x2050
movl (%rax), %edi
callq 0x2480
leaq 0x1add(%rip), %rdi      # 0xc7b6
leaq 0x40(%rsp), %r14
movq %r14, %rsi
movq %rbx, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x4734
movq %r14, %rdi
jmp 0xaa8a