// File: /workspace/github_c_makefile_star2_O2/erikdubbelboer[P]discnt/src/ddb.c
// Function: ddbSaveRio
int ddbSaveRio(rio *ddb, int *error) {
    dictIterator *di = NULL;
    dictEntry *de;
    char magic[10];
    uint64_t cksum;

    if (server.ddb_checksum)
        ddb->update_cksum = rioGenericUpdateChecksum;
    snprintf(magic,sizeof(magic),"DISCNT%03d",DDB_VERSION);
    if (ddbWriteRaw(ddb,magic,9) == -1) goto werr;

    if (dictSize(server.counters) == 0) goto done;
    di = dictGetIterator(server.counters);
    if (!di) return C_ERR;

    /* Write the RESIZE DB opcode. We trim the size to UINT32_MAX, which
     * is currently the largest type we are able to represent in DDB sizes.
     * However this does not limit the actual size of the DB to load since
     * these sizes are just hints to resize the hash tables. */
    uint32_t db_size;
    db_size = (dictSize(server.counters) <= UINT32_MAX) ?
        dictSize(server.counters) : UINT32_MAX;
    if (ddbSaveType(ddb,DDB_OPCODE_RESIZEDB) == -1) goto werr;
    if (ddbSaveLen(ddb,db_size) == -1) goto werr;

    /* Iterate this DB writing every entry */
    while((de = dictNext(di)) != NULL) {
        counter* cntr = dictGetVal(de);

        if (ddbSaveCounter(ddb,cntr) == -1) goto werr;
    }
    dictReleaseIterator(di);
    
done:
    di = NULL; /* So that we don't release it again on error. */

    /* EOF opcode */
    if (ddbSaveType(ddb,DDB_OPCODE_EOF) == -1) goto werr;

    /* CRC64 checksum. It will be zero if checksum computation is disabled, the
     * loading code skips the check in this case. */
    cksum = ddb->cksum;
    memrev64ifbe(&cksum);
    if (rioWrite(ddb,&cksum,8) == 0) goto werr;
    return C_OK;

werr:
    if (error) *error = errno;
    if (di) dictReleaseIterator(di);
    return C_ERR;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, %rbx
movq %rdi, %r15
leaq 0x3ec7c(%rip), %r13     # 0x6a088
cmpl $0x0, 0x6d4(%r13)
je 0x2b421
leaq 0xc7f(%rip), %rax       # 0x2c09c
movq %rax, 0x20(%r15)
leaq 0x26d54(%rip), %rdx     # 0x5217c
xorl %r14d, %r14d
leaq 0xe(%rsp), %r12
pushq $0xa
popq %rsi
pushq $0x1
popq %rcx
movq %r12, %rdi
xorl %eax, %eax
callq 0x122d0
pushq $0x9
popq %rdx
movq %r15, %rdi
movq %r12, %rsi
callq 0x2ae09
pushq $-0x1
popq %rbp
cmpl $-0x1, %eax
je 0x2b50f
movq 0x618(%r13), %rdi
movq 0x28(%rdi), %rax
addq 0x48(%rdi), %rax
jne 0x2b4a5
movq %r15, %rdi
movl $0xff, %esi
callq 0x2adf3
xorl %r14d, %r14d
cmpl $-0x1, %eax
je 0x2b50f
movq 0x28(%r15), %rax
movq %rsp, %rsi
movq %rax, (%rsi)
pushq $0x8
popq %rdx
movq %r15, %rdi
callq 0x2b548
testq %rax, %rax
je 0x2b50f
xorl %ebp, %ebp
jmp 0x2b52a
callq 0x14f27
testq %rax, %rax
je 0x2b52a
movq %rax, %r14
movq 0x618(%r13), %rax
movq 0x28(%rax), %r13
movq 0x48(%rax), %r12
movq %r15, %rdi
movl $0xfb, %esi
callq 0x2adf3
cmpl $-0x1, %eax
je 0x2b50f
addq %r13, %r12
movq %r12, %rax
shrq $0x20, %rax
xorl %esi, %esi
negq %rax
sbbl %esi, %esi
orl %r12d, %esi
movq %r15, %rdi
callq 0x2af06
cmpl $-0x1, %eax
je 0x2b50f
movq %r14, %rdi
callq 0x14f5b
testq %rax, %rax
je 0x2b53b
movq 0x8(%rax), %rsi
movq %r15, %rdi
callq 0x2b1ea
jmp 0x2b4ef
testq %rbx, %rbx
je 0x2b51d
callq 0x120d0
movl (%rax), %eax
movl %eax, (%rbx)
testq %r14, %r14
je 0x2b52a
movq %r14, %rdi
callq 0x1501c
movl %ebp, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r14, %rdi
callq 0x1501c
jmp 0x2b46b