// File: /workspace/github_c_makefile_star2_O3/gplessis[P]dotdeb-redis/src/rdb.c
// Function: rdbLoad
int rdbLoad(char *filename) {
    uint32_t dbid;
    int type, rdbver;
    redisDb *db = server.db+0;
    char buf[1024];
    long long expiretime, now = mstime();
    FILE *fp;
    rio rdb;

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

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

    startLoading(fp);
    while(1) {
        robj *key, *val;
        expiretime = -1;

        /* Read type. */
        if ((type = rdbLoadType(&rdb)) == -1) goto eoferr;

        /* Handle special types. */
        if (type == RDB_OPCODE_EXPIRETIME) {
            /* EXPIRETIME: load an expire associated with the next key
             * to load. Note that after loading an expire we need to
             * load the actual type, and continue. */
            if ((expiretime = rdbLoadTime(&rdb)) == -1) goto eoferr;
            /* We read the time so we need to read the object type again. */
            if ((type = rdbLoadType(&rdb)) == -1) goto eoferr;
            /* the EXPIRETIME opcode specifies time in seconds, so convert
             * into milliseconds. */
            expiretime *= 1000;
        } else if (type == RDB_OPCODE_EXPIRETIME_MS) {
            /* EXPIRETIME_MS: milliseconds precision expire times introduced
             * with RDB v3. Like EXPIRETIME but no with more precision. */
            if ((expiretime = rdbLoadMillisecondTime(&rdb)) == -1) goto eoferr;
            /* We read the time so we need to read the object type again. */
            if ((type = rdbLoadType(&rdb)) == -1) goto eoferr;
        } else if (type == RDB_OPCODE_EOF) {
            /* EOF: End of file, exit the main loop. */
            break;
        } else if (type == RDB_OPCODE_SELECTDB) {
            /* SELECTDB: Select the specified database. */
            if ((dbid = rdbLoadLen(&rdb,NULL)) == RDB_LENERR)
                goto eoferr;
            if (dbid >= (unsigned)server.dbnum) {
                serverLog(LL_WARNING,
                    "FATAL: Data file was created with a Redis "
                    "server configured to handle more than %d "
                    "databases. Exiting\n", server.dbnum);
                exit(1);
            }
            db = server.db+dbid;
            continue; /* Read type again. */
        } else if (type == RDB_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, expires_size;
            if ((db_size = rdbLoadLen(&rdb,NULL)) == RDB_LENERR)
                goto eoferr;
            if ((expires_size = rdbLoadLen(&rdb,NULL)) == RDB_LENERR)
                goto eoferr;
            dictExpand(db->dict,db_size);
            dictExpand(db->expires,expires_size);
            continue; /* Read type again. */
        } else if (type == RDB_OPCODE_AUX) {
            /* AUX: generic string-string fields. Use to add state to RDB
             * which is backward compatible. Implementations of RDB loading
             * are requierd to skip AUX fields they don't understand.
             *
             * An AUX field is composed of two strings: key and value. */
            robj *auxkey, *auxval;
            if ((auxkey = rdbLoadStringObject(&rdb)) == NULL) goto eoferr;
            if ((auxval = rdbLoadStringObject(&rdb)) == NULL) goto eoferr;

            if (((char*)auxkey->ptr)[0] == '%') {
                /* All the fields with a name staring with '%' are considered
                 * information fields and are logged at startup with a log
                 * level of NOTICE. */
                serverLog(LL_NOTICE,"RDB '%s': %s",
                    (char*)auxkey->ptr,
                    (char*)auxval->ptr);
            } else {
                /* We ignore fields we don't understand, as by AUX field
                 * contract. */
                serverLog(LL_DEBUG,"Unrecognized RDB AUX field: '%s'",
                    (char*)auxkey->ptr);
            }

            decrRefCount(auxkey);
            decrRefCount(auxval);
            continue; /* Read type again. */
        }

        /* Read key */
        if ((key = rdbLoadStringObject(&rdb)) == NULL) goto eoferr;
        /* Read value */
        if ((val = rdbLoadObject(type,&rdb)) == NULL) goto eoferr;
        /* Check if the key already expired. This function is used when loading
         * an RDB file from disk, either at startup, or when an RDB was
         * received from the master. In the latter case, the master is
         * responsible for key expiry. If we would expire keys here, the
         * snapshot taken by the master may not be reflected on the slave. */
        if (server.masterhost == NULL && expiretime != -1 && expiretime < now) {
            decrRefCount(key);
            decrRefCount(val);
            continue;
        }
        /* Add the new object in the hash table */
        dbAdd(db,key,val);

        /* Set the expire time if needed */
        if (expiretime != -1) setExpire(db,key,expiretime);

        decrRefCount(key);
    }
    /* Verify the checksum if RDB version is >= 5 */
    if (rdbver >= 5 && server.rdb_checksum) {
        uint64_t cksum, expected = rdb.cksum;

        if (rioRead(&rdb,&cksum,8) == 0) goto eoferr;
        memrev64ifbe(&cksum);
        if (cksum == 0) {
            serverLog(LL_WARNING,"RDB file was saved with checksum disabled: no check performed.");
        } else if (cksum != expected) {
            serverLog(LL_WARNING,"Wrong RDB checksum. Aborting now.");
            rdbExitReportCorruptRDB("RDB 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.");
    rdbExitReportCorruptRDB("Unexpected EOF reading RDB file");
    return C_ERR; /* Just to avoid warning */
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x498, %rsp            # imm = 0x498
movq %rdi, %rbx
leaq 0xb749b(%rip), %r14     # 0x1018a8
movq 0x28(%r14), %r15
callq 0x2ffce
movq %rax, 0x18(%rsp)
leaq 0x7563b(%rip), %rsi     # 0xbfa5d
movq %rbx, %rdi
callq 0x26ae0
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testq %rax, %rax
je 0x4a544
leaq 0x28(%rsp), %r12
movq %r12, %rdi
movq %rax, 0x8(%rsp)
movq %rax, %rsi
callq 0x726c2
leaq -0xeb(%rip), %rax       # 0x4a369
movq %rax, 0x20(%r12)
movq 0x338(%r14), %rax
movq %rax, 0x38(%r12)
leaq 0x90(%rsp), %rbx
movl $0x9, %r14d
movq 0x60(%rsp), %rax
cmpq %r14, %rax
movq %r14, %r13
cmovbq %rax, %r13
testq %rax, %rax
cmoveq %r14, %r13
movq %r12, %rdi
movq %rbx, %rsi
movq %r13, %rdx
callq *0x28(%rsp)
testq %rax, %rax
je 0x4a816
movq 0x48(%rsp), %rax
testq %rax, %rax
je 0x4a4b4
movq %r12, %rdi
movq %rbx, %rsi
movq %r13, %rdx
callq *%rax
addq %r13, %rbx
addq %r13, 0x58(%rsp)
subq %r13, %r14
jne 0x4a473
movb $0x0, 0x99(%rsp)
movl $0x49444552, %eax       # imm = 0x49444552
xorl 0x90(%rsp), %eax
movzbl 0x94(%rsp), %ecx
xorl $0x53, %ecx
orl %eax, %ecx
je 0x4a503
movq 0x8(%rsp), %rdi
callq 0x26a10
leaq 0x787b9(%rip), %rsi     # 0xc2cae
movl $0x3, %edi
xorl %eax, %eax
callq 0x2fdad
jmp 0x4a539
leaq 0x95(%rsp), %rdi
callq 0x26760
movl %eax, %ebx
leal -0x8(%rbx), %eax
cmpl $-0x8, %eax
ja 0x4a558
movq 0x8(%rsp), %rdi
callq 0x26a10
leaq 0x787af(%rip), %rsi     # 0xc2cda
movl $0x3, %edi
movl %ebx, %edx
xorl %eax, %eax
callq 0x2fdad
callq 0x26880
movl $0x16, (%rax)
movl %ebp, %eax
addq $0x498, %rsp            # imm = 0x498
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbx, 0x10(%rsp)
movq 0x8(%rsp), %rdi
callq 0x4a2b7
leaq 0x28(%rsp), %r13
leaq 0x78391(%rip), %r14     # 0xc2904
movq %r13, %rdi
callq 0x47b17
movl %eax, %ebx
leal -0xfa(%rbx), %eax
cmpl $0x5, %eax
ja 0x4a5e7
movslq (%r14,%rax,4), %rax
addq %r14, %rax
jmpq *%rax
movq %r13, %rdi
xorl %esi, %esi
callq 0x4867f
testq %rax, %rax
je 0x4a816
movq %rax, %rbx
movq %r13, %rdi
xorl %esi, %esi
callq 0x4867f
testq %rax, %rax
je 0x4a816
movq %rax, %r12
movq 0x8(%rbx), %rdx
cmpb $0x25, (%rdx)
jne 0x4a725
movq 0x8(%r12), %rcx
movl $0x2, %edi
leaq 0x78789(%rip), %rsi     # 0xc2d64
xorl %eax, %eax
callq 0x2fdad
jmp 0x4a735
cmpl $-0x1, %ebx
je 0x4a816
movq $-0x1, %rbp
jmp 0x4a652
movq %r13, %rdi
callq 0x47c8c
cmpq $-0x1, %rax
je 0x4a816
movq %rax, %rbp
movq %r13, %rdi
callq 0x47b17
cmpl $-0x1, %eax
je 0x4a816
movl %eax, %ebx
jmp 0x4a652
movq %r13, %rdi
callq 0x47c01
cmpq $-0x1, %rax
je 0x4a816
movq %rax, %r12
movq %r13, %rdi
callq 0x47b17
cmpl $-0x1, %eax
je 0x4a816
movl %eax, %ebx
imulq $0x3e8, %r12, %rbp      # imm = 0x3E8
movq %r13, %rdi
xorl %esi, %esi
callq 0x4867f
testq %rax, %rax
je 0x4a816
movq %rax, %r12
movl %ebx, %edi
movq %r13, %rsi
callq 0x499b2
testq %rax, %rax
je 0x4a816
movq %rax, %rbx
leaq 0xb7223(%rip), %rax     # 0x1018a8
cmpq $0x0, 0x828(%rax)
sete %al
cmpq $-0x1, %rbp
setne %cl
cmpq 0x18(%rsp), %rbp
setl %dl
andb %cl, %dl
andb %al, %dl
cmpb $0x1, %dl
jne 0x4a6bd
movq %r12, %rdi
callq 0x40c8f
movq %rbx, %rdi
callq 0x40c8f
jmp 0x4a573
movq %r15, %rdi
movq %r12, %rsi
movq %rbx, %rdx
callq 0x41b77
cmpq $-0x1, %rbp
je 0x4a73d
movq %r15, %rdi
movq %r12, %rsi
movq %rbp, %rdx
callq 0x430b0
jmp 0x4a73d
movq %r13, %rdi
xorl %esi, %esi
callq 0x47db6
cmpl $-0x1, %eax
je 0x4a816
movl %eax, %ebx
movq %r13, %rdi
xorl %esi, %esi
callq 0x47db6
cmpl $-0x1, %eax
je 0x4a816
movl %eax, %ebp
movq (%r15), %rdi
movl %ebx, %esi
callq 0x2e6fa
movq 0x8(%r15), %rdi
movl %ebp, %esi
callq 0x2e6fa
jmp 0x4a573
xorl %edi, %edi
leaq 0x78643(%rip), %rsi     # 0xc2d71
xorl %eax, %eax
callq 0x2fdad
movq %rbx, %rdi
callq 0x40c8f
movq %r12, %rdi
callq 0x40c8f
jmp 0x4a573
movq %r13, %rdi
xorl %esi, %esi
callq 0x47db6
cmpl $-0x1, %eax
je 0x4a816
leaq 0xb7144(%rip), %rcx     # 0x1018a8
movl 0x610(%rcx), %edx
cmpl %edx, %eax
jae 0x4a83c
movl %eax, %r15d
shlq $0x6, %r15
addq 0x28(%rcx), %r15
jmp 0x4a573
cmpl $0x5, 0x10(%rsp)
leaq 0xb711a(%rip), %r14     # 0x1018a8
jl 0x4a7fa
cmpl $0x0, 0x754(%r14)
je 0x4a7fa
leaq 0x28(%rsp), %rdi
movq 0x28(%rdi), %rbx
leaq 0x20(%rsp), %rsi
movl $0x8, %edx
callq 0x47b8a
testq %rax, %rax
je 0x4a816
movq 0x20(%rsp), %rax
testq %rax, %rax
je 0x4a7e7
cmpq %rbx, %rax
je 0x4a7fa
leaq 0x78604(%rip), %rsi     # 0xc2dd1
movl $0x3, %edi
xorl %eax, %eax
callq 0x2fdad
leaq 0x78613(%rip), %rsi     # 0xc2df3
movl $0x58a, %edi            # imm = 0x58A
jmp 0x4a835
leaq 0x785a4(%rip), %rsi     # 0xc2d92
movl $0x3, %edi
xorl %eax, %eax
callq 0x2fdad
movq 0x8(%rsp), %rdi
callq 0x26a10
movl $0x0, 0x31c(%r14)
xorl %ebp, %ebp
jmp 0x4a544
leaq 0x785e4(%rip), %rsi     # 0xc2e01
movl $0x3, %edi
xorl %eax, %eax
callq 0x2fdad
leaq 0x78612(%rip), %rsi     # 0xc2e42
movl $0x594, %edi            # imm = 0x594
xorl %eax, %eax
callq 0x47948
leaq 0x784ba(%rip), %rsi     # 0xc2cfd
movl $0x3, %edi
xorl %eax, %eax
callq 0x2fdad
movl $0x1, %edi
callq 0x262c0