// File: /workspace/github_c_makefile_star2_O1/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 %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x20, %rsp
movq %rsi, %rbx
movq %rdi, %r14
leaq 0x3c24e(%rip), %r13     # 0x6f088
cmpl $0x0, 0x6d4(%r13)
je 0x32e4f
leaq 0xe1e(%rip), %rax       # 0x33c69
movq %rax, 0x20(%r14)
leaq 0x25c96(%rip), %rdx     # 0x58aec
xorl %r15d, %r15d
leaq 0x16(%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 0x3254f
cmpl $-0x1, %eax
je 0x32fc0
movq 0x618(%r13), %rdi
movq 0x28(%rdi), %rax
addq 0x48(%rdi), %rax
jne 0x32f3e
leaq 0x8(%rsp), %r15
movb $-0x1, (%r15)
movl $0x1, %edx
movq %r14, %rdi
movq %r15, %rsi
callq 0x3254f
cmpl $-0x1, %eax
je 0x32f36
movq 0x28(%r14), %rax
movq %rax, 0x8(%rsp)
movl $0x8, %ecx
testq %rcx, %rcx
je 0x32feb
movq %rcx, %r13
movq 0x20(%r14), %rax
movq 0x38(%r14), %rcx
cmpq %r13, %rcx
movq %r13, %r12
cmovbq %rcx, %r12
testq %rcx, %rcx
cmoveq %r13, %r12
testq %rax, %rax
je 0x32f01
movq %r14, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq *%rax
movq %r14, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq *0x8(%r14)
testq %rax, %rax
je 0x32f22
addq %r12, %r15
movq %r13, %rcx
subq %r12, %rcx
addq %r12, 0x30(%r14)
jmp 0x32f25
movq %r13, %rcx
testq %rax, %rax
jne 0x32ecc
xorl %r14d, %r14d
testq %r13, %r13
je 0x32fee
xorl %r15d, %r15d
jmp 0x32fc0
callq 0x19242
testq %rax, %rax
je 0x32fe3
movq %rax, %r15
movq 0x618(%r13), %rax
movq 0x28(%rax), %r13
movq 0x48(%rax), %r12
leaq 0x8(%rsp), %rsi
movb $-0x5, (%rsi)
movl $0x1, %edx
movq %r14, %rdi
callq 0x3254f
cmpl $-0x1, %eax
je 0x32fc0
addq %r13, %r12
movq %r12, %rax
shrq $0x20, %rax
xorl %ecx, %ecx
negq %rax
sbbl %ecx, %ecx
orl %r12d, %ecx
leaq 0x8(%rsp), %rsi
movl %ecx, (%rsi)
movl $0x4, %edx
movq %r14, %rdi
callq 0x3254f
cmpl $-0x1, %eax
je 0x32fc0
movq %r15, %rdi
callq 0x1929d
testq %rax, %rax
je 0x32fff
movq 0x8(%rax), %rsi
movq %r14, %rdi
callq 0x32bdc
jmp 0x32fa0
testq %rbx, %rbx
je 0x32fce
callq 0x120d0
movl (%rax), %eax
movl %eax, (%rbx)
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
testq %r15, %r15
je 0x32fee
movq %r15, %rdi
callq 0x1935a
jmp 0x32fee
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
jmp 0x32fee
xorl %r14d, %r14d
movl %r14d, %eax
addq $0x20, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
movq %r15, %rdi
callq 0x1935a
jmp 0x32ea0