// File: /workspace/github_c_makefile_star2_O2/erikdubbelboer[P]discnt/src/ddb.c
// Function: ddbLoad
int ddbLoad(char *filename) {
    int type, ddbver;
    char buf[1024];
    FILE *fp;
    rio ddb;

    if ((fp = fopen(filename,"r")) == NULL) return C_ERR;

    rioInitWithFile(&ddb,fp);
    ddb.update_cksum = ddbLoadProgressCallback;
    ddb.max_processing_chunk = server.loading_process_events_interval_bytes;
    if (rioRead(&ddb,buf,9) == 0) goto eoferr;
    buf[9] = '\0';
    if (memcmp(buf,"DISCNT",6) != 0) {
        fclose(fp);
        serverLog(LL_WARNING,"Wrong signature trying to load DB from file");
        errno = EINVAL;
        return C_ERR;
    }
    ddbver = atoi(buf+6);
    if (ddbver < 1 || ddbver > DDB_VERSION) {
        fclose(fp);
        serverLog(LL_WARNING,"Can't handle DDB format version %d",ddbver);
        errno = EINVAL;
        return C_ERR;
    }

    startLoading(fp);
    while(1) {
        /* Read type. */
        if ((type = ddbLoadType(&ddb)) == -1) goto eoferr;

        if (type == DDB_OPCODE_EOF) {
            /* EOF: End of file, exit the main loop. */
            break;
        } else if (type == DDB_OPCODE_RESIZEDB) {
            /* RESIZEDB: Hint about the size of the keys in the currently
             * selected data base, in order to avoid useless rehashing. */
            uint32_t db_size;
            if ((db_size = ddbLoadLen(&ddb)) == DDB_LENERR)
                goto eoferr;
            dictExpand(server.counters,db_size);
            continue; /* Read type again. */
        }

        if (ddbLoadCounter(&ddb) == -1) goto eoferr;
    }
    /* Verify the checksum if DDB version is >= 5 */
    if (ddbver >= 5 && server.ddb_checksum) {
        uint64_t cksum, expected = ddb.cksum;

        if (rioRead(&ddb,&cksum,8) == 0) goto eoferr;
        memrev64ifbe(&cksum);
        if (cksum == 0) {
            serverLog(LL_WARNING,"DDB file was saved with checksum disabled: no check performed.");
        } else if (cksum != expected) {
            serverLog(LL_WARNING,"Wrong DDB checksum. Aborting now.");
            ddbExitReportCorruptDDB("DDB CRC error");
        }
    }

    fclose(fp);

    stopLoading();
    return C_OK;

eoferr: /* unexpected end of file is handled here with a fatal exit */
    serverLog(LL_WARNING,"Short read or OOM loading DB. Unrecoverable error, aborting now.");
    ddbExitReportCorruptDDB("Unexpected EOF reading DDB file");
    return C_ERR; /* Just to avoid warning */
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0x478, %rsp            # imm = 0x478
leaq 0x2285f(%rip), %rsi     # 0x4e4a1
callq 0x124f0
pushq $-0x1
popq %rbp
testq %rax, %rax
je 0x2bd63
movq %rax, %rbx
leaq 0x8(%rsp), %r14
movq %r14, %rdi
movq %rax, %rsi
callq 0x2bfd5
leaq -0xaf(%rip), %rax       # 0x2bbbe
movq %rax, 0x20(%r14)
leaq 0x3e410(%rip), %r15     # 0x6a088
movq 0x340(%r15), %rax
movq %rax, 0x38(%r14)
leaq 0x70(%rsp), %rsi
pushq $0x9
popq %rdx
movq %r14, %rdi
callq 0x2ae50
testq %rax, %rax
je 0x2bd87
leaq 0x70(%rsp), %rdi
movb $0x0, 0x9(%rdi)
leaq 0x265fe(%rip), %rsi     # 0x522aa
pushq $0x6
popq %rdx
callq 0x12610
testl %eax, %eax
je 0x2bcd6
movq %rbx, %rdi
callq 0x12220
leaq 0x265ea(%rip), %rsi     # 0x522b1
pushq $0x3
popq %rdi
xorl %eax, %eax
callq 0x15b9e
jmp 0x2bd58
leaq 0x76(%rsp), %rdi
callq 0x12820
cmpl $0x1, %eax
jne 0x2bd39
movq %rbx, %rdi
callq 0x2bb0e
leaq 0x8(%rsp), %r14
movq %r14, %rdi
callq 0x2ae26
cmpl $0xfb, %eax
je 0x2bd1c
cmpl $0xff, %eax
je 0x2bd73
cmpl $-0x1, %eax
je 0x2bd87
movq %r14, %rdi
callq 0x2b942
cmpl $-0x1, %eax
jne 0x2bcf2
jmp 0x2bd87
movq %r14, %rdi
callq 0x2af1b
cmpl $-0x1, %eax
je 0x2bd87
movq 0x618(%r15), %rdi
movl %eax, %esi
callq 0x146ad
jmp 0x2bcf2
movl %eax, %r14d
movq %rbx, %rdi
callq 0x12220
leaq 0x26592(%rip), %rsi     # 0x522dd
pushq $0x3
popq %rdi
movl %r14d, %edx
xorl %eax, %eax
callq 0x15b9e
callq 0x120d0
movl $0x16, (%rax)
movl %ebp, %eax
addq $0x478, %rsp            # imm = 0x478
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
movq %rbx, %rdi
callq 0x12220
andl $0x0, 0x320(%r15)
xorl %ebp, %ebp
jmp 0x2bd63
leaq 0x26572(%rip), %rsi     # 0x52300
pushq $0x3
popq %rdi
xorl %eax, %eax
callq 0x15b9e
leaq 0x265a2(%rip), %rdi     # 0x52341
movl $0x244, %esi            # imm = 0x244
callq 0x2add4