// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq $0x0, -0x20(%rbp)
leaq 0x836f9(%rip), %rax     # 0xbf0e8
cmpl $0x0, 0x6d4(%rax)
je 0x3ba07
movq -0x10(%rbp), %rax
leaq 0x13ad(%rip), %rcx      # 0x3cdb0
movq %rcx, 0x20(%rax)
leaq -0x32(%rbp), %rdi
movl $0xa, %esi
leaq 0x6da4d(%rip), %rdx     # 0xa9464
movl $0x1, %ecx
movb $0x0, %al
callq 0x122c0
movq -0x10(%rbp), %rdi
leaq -0x32(%rbp), %rsi
movl $0x9, %edx
callq 0x3af10
cmpl $-0x1, %eax
jne 0x3ba3f
jmp 0x3bbbc
leaq 0x836a2(%rip), %rax     # 0xbf0e8
movq 0x618(%rax), %rax
movq 0x28(%rax), %rax
leaq 0x83690(%rip), %rcx     # 0xbf0e8
movq 0x618(%rcx), %rcx
addq 0x48(%rcx), %rax
cmpq $0x0, %rax
jne 0x3ba6e
jmp 0x3bb70
leaq 0x83673(%rip), %rax     # 0xbf0e8
movq 0x618(%rax), %rdi
callq 0x17300
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
jne 0x3ba98
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x3bbe7
leaq 0x83649(%rip), %rax     # 0xbf0e8
movq 0x618(%rax), %rax
movq 0x28(%rax), %rax
leaq 0x83637(%rip), %rcx     # 0xbf0e8
movq 0x618(%rcx), %rcx
addq 0x48(%rcx), %rax
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
cmpq %rcx, %rax
ja 0x3baf0
leaq 0x8361b(%rip), %rax     # 0xbf0e8
movq 0x618(%rax), %rax
movq 0x28(%rax), %rax
leaq 0x83609(%rip), %rcx     # 0xbf0e8
movq 0x618(%rcx), %rcx
addq 0x48(%rcx), %rax
movq %rax, -0x58(%rbp)
jmp 0x3bafb
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movq %rax, -0x58(%rbp)
jmp 0x3bafb
movq -0x58(%rbp), %rax
movl %eax, -0x44(%rbp)
movq -0x10(%rbp), %rdi
movl $0xfb, %esi
callq 0x3aee0
cmpl $-0x1, %eax
jne 0x3bb1a
jmp 0x3bbbc
movq -0x10(%rbp), %rdi
movl -0x44(%rbp), %esi
callq 0x3b100
cmpl $-0x1, %eax
jne 0x3bb30
jmp 0x3bbbc
jmp 0x3bb32
movq -0x20(%rbp), %rdi
callq 0x173a0
movq %rax, -0x28(%rbp)
cmpq $0x0, %rax
je 0x3bb67
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x50(%rbp)
movq -0x10(%rbp), %rdi
movq -0x50(%rbp), %rsi
callq 0x3b650
cmpl $-0x1, %eax
jne 0x3bb65
jmp 0x3bbbc
jmp 0x3bb32
movq -0x20(%rbp), %rdi
callq 0x17510
movq $0x0, -0x20(%rbp)
movq -0x10(%rbp), %rdi
movl $0xff, %esi
callq 0x3aee0
cmpl $-0x1, %eax
jne 0x3bb8d
jmp 0x3bbbc
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movq %rax, -0x40(%rbp)
movq -0x10(%rbp), %rdi
leaq -0x40(%rbp), %rsi
movl $0x8, %edx
callq 0x3bbf0
cmpq $0x0, %rax
jne 0x3bbb3
jmp 0x3bbbc
movl $0x0, -0x4(%rbp)
jmp 0x3bbe7
cmpq $0x0, -0x18(%rbp)
je 0x3bbd0
callq 0x120b0
movl (%rax), %ecx
movq -0x18(%rbp), %rax
movl %ecx, (%rax)
cmpq $0x0, -0x20(%rbp)
je 0x3bbe0
movq -0x20(%rbp), %rdi
callq 0x17510
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq