// File: /workspace/github_c_makefile_star2_O3/erikdubbelboer[P]discnt/src/ddb.c
// Function: ddbSave
int ddbSave(char *filename) {
    char tmpfile[256];
    FILE *fp;
    rio ddb;
    int error = 0;

    snprintf(tmpfile,256,"temp-%d.ddb", (int) getpid());
    fp = fopen(tmpfile,"w");
    if (!fp) {
        serverLog(LL_WARNING, "Failed opening .ddb for saving: %s",
            strerror(errno));
        return C_ERR;
    }

    rioInitWithFile(&ddb,fp);
    if (ddbSaveRio(&ddb,&error) == C_ERR) {
        errno = error;
        goto werr;
    }

    /* Make sure data will not remain on the OS's output buffers */
    if (fflush(fp) == EOF) goto werr;
    if (fsync(fileno(fp)) == -1) goto werr;
    if (fclose(fp) == EOF) goto werr;

    /* Use RENAME to make sure the DB file is changed atomically only
     * if the generate DB file is ok. */
    if (rename(tmpfile,filename) == -1) {
        serverLog(LL_WARNING,"Error moving temp DB file on the final destination: %s", strerror(errno));
        unlink(tmpfile);
        return C_ERR;
    }
    serverLog(LL_NOTICE,"DB saved on disk");
    server.dirty = 0;
    server.lastsave = time(NULL);
    server.lastbgsave_status = C_OK;
    return C_OK;

werr:
    serverLog(LL_WARNING,"Write error saving DB on disk: %s", strerror(errno));
    fclose(fp);
    unlink(tmpfile);
    return C_ERR;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0x178, %rsp            # imm = 0x178
movq %rdi, %r14
movl $0x0, 0xc(%rsp)
callq 0x121f0
leaq 0x2611d(%rip), %rdx     # 0x58ad7
leaq 0x10(%rsp), %rbx
movl $0x100, %esi            # imm = 0x100
movq %rbx, %rdi
movl %eax, %ecx
xorl %eax, %eax
callq 0x122d0
leaq 0x22af2(%rip), %rsi     # 0x554c9
movq %rbx, %rdi
callq 0x124f0
testq %rax, %rax
je 0x32aa6
movq %rax, %rbx
leaq 0x110(%rsp), %r15
movq %r15, %rdi
movq %rax, %rsi
callq 0x33502
leaq 0xc(%rsp), %rsi
movq %r15, %rdi
callq 0x327cc
cmpl $-0x1, %eax
je 0x32aca
movq %rbx, %rdi
callq 0x125d0
cmpl $-0x1, %eax
je 0x32ad5
movq %rbx, %rdi
callq 0x12550
movl %eax, %edi
callq 0x12730
cmpl $-0x1, %eax
je 0x32ad5
movq %rbx, %rdi
callq 0x12220
cmpl $-0x1, %eax
je 0x32ad5
leaq 0x10(%rsp), %rdi
movq %r14, %rsi
callq 0x127a0
cmpl $-0x1, %eax
je 0x32b1e
leaq 0x260d2(%rip), %rsi     # 0x58b3d
xorl %ebx, %ebx
movl $0x2, %edi
xorl %eax, %eax
callq 0x19c06
leaq 0x3c608(%rip), %r14     # 0x6f088
movq $0x0, 0x6a0(%r14)
xorl %edi, %edi
callq 0x12540
movq %rax, 0x6d8(%r14)
movl $0x0, 0x6fc(%r14)
jmp 0x32b0e
callq 0x120d0
movl (%rax), %edi
callq 0x128e0
leaq 0x2602a(%rip), %rsi     # 0x58ae3
movl $0x3, %edi
movq %rax, %rdx
xorl %eax, %eax
callq 0x19c06
jmp 0x32b09
movl 0xc(%rsp), %ebp
callq 0x120d0
movl %ebp, (%rax)
callq 0x120d0
movl (%rax), %edi
callq 0x128e0
leaq 0x26066(%rip), %rsi     # 0x58b4e
movl $0x3, %edi
movq %rax, %rdx
xorl %eax, %eax
callq 0x19c06
movq %rbx, %rdi
callq 0x12220
leaq 0x10(%rsp), %rdi
callq 0x120f0
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movl %ebx, %eax
addq $0x178, %rsp            # imm = 0x178
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
callq 0x120d0
movl (%rax), %edi
callq 0x128e0
leaq 0x25fd5(%rip), %rsi     # 0x58b06
movl $0x3, %edi
movq %rax, %rdx
xorl %eax, %eax
callq 0x19c06
jmp 0x32aff