// File: /workspace/github_c_makefile_star2_O3/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 %r13
pushq %r12
pushq %rbx
subq $0x478, %rsp            # imm = 0x478
leaq 0x21792(%rip), %rsi     # 0x5483d
callq 0x124f0
movq %rax, %rbx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
testq %rbx, %rbx
je 0x3321b
leaq 0x8(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0x33502
leaq -0xb5(%rip), %rax       # 0x33023
movq %rax, 0x20(%r14)
leaq 0x3bfa5(%rip), %r13     # 0x6f088
movq 0x340(%r13), %rax
movq %rax, 0x38(%r14)
leaq 0x70(%rsp), %r15
movl $0x9, %ebp
movq 0x40(%rsp), %rax
cmpq %rbp, %rax
movq %rbp, %r12
cmovbq %rax, %r12
testq %rax, %rax
cmoveq %rbp, %r12
movq %r14, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq *0x8(%rsp)
testq %rax, %rax
je 0x33244
movq 0x28(%rsp), %rax
testq %rax, %rax
je 0x33139
movq %r14, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq *%rax
addq %r12, %r15
addq %r12, 0x38(%rsp)
subq %r12, %rbp
jne 0x330f8
movb $0x0, 0x79(%rsp)
movl $0x43534944, %eax       # imm = 0x43534944
xorl 0x70(%rsp), %eax
movzwl 0x74(%rsp), %ecx
xorl $0x544e, %ecx           # imm = 0x544E
orl %eax, %ecx
je 0x33183
movq %rbx, %rdi
callq 0x12220
leaq 0x25a8f(%rip), %rsi     # 0x58c01
movl $0x3, %edi
xorl %eax, %eax
callq 0x19c06
jmp 0x3320b
leaq 0x76(%rsp), %rdi
callq 0x12820
cmpl $0x1, %eax
jne 0x331ea
movq %rbx, %rdi
callq 0x32f71
leaq 0x8(%rsp), %r14
movq %r14, %rdi
callq 0x3202a
cmpl $0xfb, %eax
je 0x331cd
cmpl $0xff, %eax
je 0x3322d
cmpl $-0x1, %eax
je 0x33244
movq %r14, %rdi
callq 0x32d52
cmpl $-0x1, %eax
jne 0x3319f
jmp 0x33244
movq %r14, %rdi
callq 0x3214d
cmpl $-0x1, %eax
je 0x33244
movq 0x618(%r13), %rdi
movl %eax, %esi
callq 0x18544
jmp 0x3319f
movl %eax, %r14d
movq %rbx, %rdi
callq 0x12220
leaq 0x25a31(%rip), %rsi     # 0x58c2d
movl $0x3, %edi
movl %r14d, %edx
xorl %eax, %eax
callq 0x19c06
callq 0x120d0
movl $0x16, (%rax)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x478, %rsp            # imm = 0x478
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbx, %rdi
callq 0x12220
movl $0x0, 0x320(%r13)
xorl %eax, %eax
jmp 0x3321b
leaq 0x25a05(%rip), %rsi     # 0x58c50
movl $0x3, %edi
xorl %eax, %eax
callq 0x19c06
leaq 0x25842(%rip), %rsi     # 0x58aa0
leaq 0x25a2c(%rip), %rcx     # 0x58c91
movl $0x3, %edi
movl $0x244, %edx            # imm = 0x244
xorl %eax, %eax
callq 0x19c06
movl $0x1, %edi
callq 0x12860