// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x4b0, %rsp            # imm = 0x4B0
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rdi
leaq 0x68ff7(%rip), %rsi     # 0xa5601
callq 0x124e0
movq %rax, -0x428(%rbp)
cmpq $0x0, %rax
jne 0x3c628
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x3c8a7
movq -0x428(%rbp), %rsi
leaq -0x490(%rbp), %rdi
callq 0x3cc50
leaq -0x102(%rip), %rax      # 0x3c540
movq %rax, -0x470(%rbp)
leaq 0x82a98(%rip), %rax     # 0xbf0e8
movq 0x340(%rax), %rax
movq %rax, -0x458(%rbp)
leaq -0x420(%rbp), %rsi
leaq -0x490(%rbp), %rdi
movl $0x9, %edx
callq 0x3afa0
cmpq $0x0, %rax
jne 0x3c681
jmp 0x3c87c
movb $0x0, -0x417(%rbp)
leaq -0x420(%rbp), %rdi
leaq 0x6cefc(%rip), %rsi     # 0xa9592
movl $0x6, %edx
callq 0x12420
cmpl $0x0, %eax
je 0x3c6db
movq -0x428(%rbp), %rdi
callq 0x12200
movl $0x3, %edi
leaq 0x6cedc(%rip), %rsi     # 0xa9599
movb $0x0, %al
callq 0x18b10
callq 0x120b0
movl $0x16, (%rax)
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x3c8a7
leaq -0x420(%rbp), %rdi
addq $0x6, %rdi
callq 0x12820
movl %eax, -0x18(%rbp)
cmpl $0x1, -0x18(%rbp)
jl 0x3c6fa
cmpl $0x1, -0x18(%rbp)
jle 0x3c733
movq -0x428(%rbp), %rdi
callq 0x12200
movl -0x18(%rbp), %edx
movl $0x3, %edi
leaq 0x6ceb0(%rip), %rsi     # 0xa95c5
movb $0x0, %al
callq 0x18b10
callq 0x120b0
movl $0x16, (%rax)
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x3c8a7
movq -0x428(%rbp), %rdi
callq 0x3c420
leaq -0x490(%rbp), %rdi
callq 0x3af60
movl %eax, -0x14(%rbp)
cmpl $-0x1, %eax
jne 0x3c758
jmp 0x3c87c
cmpl $0xff, -0x14(%rbp)
jne 0x3c763
jmp 0x3c7bf
cmpl $0xfb, -0x14(%rbp)
jne 0x3c7a5
leaq -0x490(%rbp), %rdi
callq 0x3b130
movl %eax, -0x494(%rbp)
cmpl $-0x1, %eax
jne 0x3c788
jmp 0x3c87c
leaq 0x82959(%rip), %rax     # 0xbf0e8
movq 0x618(%rax), %rdi
movl -0x494(%rbp), %eax
movl %eax, %esi
callq 0x16230
jmp 0x3c73f
jmp 0x3c7a7
leaq -0x490(%rbp), %rdi
callq 0x3c1c0
cmpl $-0x1, %eax
jne 0x3c7bd
jmp 0x3c87c
jmp 0x3c73f
cmpl $0x5, -0x18(%rbp)
jl 0x3c862
leaq 0x82918(%rip), %rax     # 0xbf0e8
cmpl $0x0, 0x6d4(%rax)
je 0x3c862
movq -0x468(%rbp), %rax
movq %rax, -0x4a8(%rbp)
leaq -0x490(%rbp), %rdi
leaq -0x4a0(%rbp), %rsi
movl $0x8, %edx
callq 0x3afa0
cmpq $0x0, %rax
jne 0x3c80b
jmp 0x3c87c
cmpq $0x0, -0x4a0(%rbp)
jne 0x3c82a
movl $0x3, %edi
leaq 0x6cdc7(%rip), %rsi     # 0xa95e8
movb $0x0, %al
callq 0x18b10
jmp 0x3c860
movq -0x4a0(%rbp), %rax
cmpq -0x4a8(%rbp), %rax
je 0x3c85e
movl $0x3, %edi
leaq 0x6cde1(%rip), %rsi     # 0xa9627
movb $0x0, %al
callq 0x18b10
leaq 0x6cdf5(%rip), %rdi     # 0xa9649
movl $0x239, %esi            # imm = 0x239
callq 0x3aea0
jmp 0x3c860
jmp 0x3c862
movq -0x428(%rbp), %rdi
callq 0x12200
callq 0x3c520
movl $0x0, -0x4(%rbp)
jmp 0x3c8a7
movl $0x3, %edi
leaq 0x6cdcf(%rip), %rsi     # 0xa9657
movb $0x0, %al
callq 0x18b10
leaq 0x6ce02(%rip), %rdi     # 0xa9698
movl $0x244, %esi            # imm = 0x244
callq 0x3aea0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl -0x4(%rbp), %eax
addq $0x4b0, %rsp            # imm = 0x4B0
popq %rbp
retq
nopw %cs:(%rax,%rax)