// File: /workspace/github_c_makefile_star2_O3/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, %r14
leaq 0x3c8a1(%rip), %r13     # 0x6f088
cmpl $0x0, 0x6d4(%r13)
je 0x327fc
leaq 0xdd5(%rip), %rax       # 0x335cd
movq %rax, 0x20(%r14)
leaq 0x262c9(%rip), %rdx     # 0x58acc
xorl %r15d, %r15d
leaq 0xe(%rsp), %r12
movl $0xa, %esi
movq %r12, %rdi
movl $0x1, %ecx
xorl %eax, %eax
callq 0x122d0
movl $0x9, %edx
movq %r14, %rdi
movq %r12, %rsi
callq 0x31fab
cmpl $-0x1, %eax
je 0x32951
movq 0x618(%r13), %rdi
movq 0x28(%rdi), %rax
addq 0x48(%rdi), %rax
jne 0x328d0
movq %rsp, %r12
movb $-0x1, (%r12)
movl $0x1, %edx
movq %r14, %rdi
movq %r12, %rsi
callq 0x31fab
cmpl $-0x1, %eax
je 0x3294e
movq 0x28(%r14), %rax
movq %rax, (%rsp)
movl $0x8, %ebp
xorl %r15d, %r15d
movq 0x20(%r14), %rax
movq 0x38(%r14), %rcx
cmpq %rbp, %rcx
movq %rbp, %r13
cmovbq %rcx, %r13
testq %rcx, %rcx
cmoveq %rbp, %r13
testq %rax, %rax
je 0x328a7
movq %r14, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq *%rax
movq %r14, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq *0x8(%r14)
testq %rax, %rax
je 0x32951
addq %r13, %r12
addq %r13, 0x30(%r14)
subq %r13, %rbp
jne 0x3287e
xorl %ebx, %ebx
jmp 0x32971
callq 0x18e9d
testq %rax, %rax
je 0x32982
movq %rax, %r15
movq 0x618(%r13), %rax
movq 0x28(%rax), %r13
movq 0x48(%rax), %r12
movq %rsp, %rsi
movb $-0x5, (%rsi)
movl $0x1, %edx
movq %r14, %rdi
callq 0x31fab
cmpl $-0x1, %eax
je 0x32951
addq %r13, %r12
movq %r12, %rax
shrq $0x20, %rax
xorl %ecx, %ecx
negq %rax
sbbl %ecx, %ecx
orl %r12d, %ecx
movq %rsp, %rsi
movl %ecx, (%rsi)
movl $0x4, %edx
movq %r14, %rdi
callq 0x31fab
cmpl $-0x1, %eax
je 0x32951
movq %r15, %rdi
callq 0x18ef7
testq %rax, %rax
je 0x32989
movq 0x8(%rax), %rsi
movq %r14, %rdi
callq 0x3257d
jmp 0x3292e
xorl %r15d, %r15d
testq %rbx, %rbx
je 0x3295f
callq 0x120d0
movl (%rax), %eax
movl %eax, (%rbx)
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
testq %r15, %r15
je 0x32971
movq %r15, %rdi
callq 0x18fbf
movl %ebx, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
jmp 0x32971
movq %r15, %rdi
callq 0x18fbf
jmp 0x3284d