// File: /workspace/github_c_makefile_star2_O1/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 0x210dd(%rip), %rsi     # 0x547f5
callq 0x12500
movq %rax, %rbx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
testq %rbx, %rbx
je 0x338b1
leaq 0x8(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0x33b9e
leaq -0xb5(%rip), %rax       # 0x33690
movq %rax, 0x20(%r14)
leaq 0x3b938(%rip), %rax     # 0x6f088
movq 0x340(%rax), %rax
movq %rax, 0x38(%r14)
movl $0x9, %eax
leaq 0x70(%rsp), %r15
testq %rax, %rax
je 0x337ce
movq %rax, %rbp
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)
movq %rax, %r13
testq %rax, %rax
je 0x337bd
movq 0x28(%rsp), %rax
testq %rax, %rax
je 0x337ad
movq %r14, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq *%rax
addq %r12, %r15
movq %rbp, %rax
subq %r12, %rax
addq %r12, 0x38(%rsp)
jmp 0x337c0
movq %rbp, %rax
testq %r13, %r13
jne 0x33765
testq %rbp, %rbp
jne 0x338e0
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 0x3380b
movq %rbx, %rdi
callq 0x12220
leaq 0x25427(%rip), %rsi     # 0x58c21
movl $0x3, %edi
xorl %eax, %eax
callq 0x19fb1
jmp 0x338a1
leaq 0x76(%rsp), %rdi
callq 0x12830
cmpl $0x1, %eax
jne 0x33880
movq %rbx, %rdi
callq 0x335de
leaq 0x8(%rsp), %r14
movq %r14, %rdi
callq 0x325d9
cmpl $0xfb, %eax
je 0x3385c
cmpl $0xff, %eax
je 0x338c3
cmpl $-0x1, %eax
je 0x338e0
movq %r14, %rdi
callq 0x333c8
cmpl $-0x1, %eax
jne 0x33827
jmp 0x338e0
movq %r14, %rdi
callq 0x3272e
cmpl $-0x1, %eax
je 0x338e0
leaq 0x3b818(%rip), %rcx     # 0x6f088
movq 0x618(%rcx), %rdi
movl %eax, %esi
callq 0x188e4
jmp 0x33827
movl %eax, %r14d
movq %rbx, %rdi
callq 0x12220
leaq 0x253bb(%rip), %rsi     # 0x58c4d
movl $0x3, %edi
movl %r14d, %edx
xorl %eax, %eax
callq 0x19fb1
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
leaq 0x3b7b6(%rip), %rax     # 0x6f088
movl $0x0, 0x320(%rax)
xorl %eax, %eax
jmp 0x338b1
leaq 0x25389(%rip), %rsi     # 0x58c70
movl $0x3, %edi
xorl %eax, %eax
callq 0x19fb1
leaq 0x251c6(%rip), %rsi     # 0x58ac0
leaq 0x253b0(%rip), %rcx     # 0x58cb1
movl $0x3, %edi
movl $0x244, %edx            # imm = 0x244
xorl %eax, %eax
callq 0x19fb1
movl $0x1, %edi
callq 0x12870