// File: /workspace/github_c_makefile_star2_O1/zhou-yuxin[P]redis-aof-no-cow/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 0xb8db9(%rip), %r14     # 0x106858
movq 0x28(%r14), %r12
callq 0x330c8
movq %rax, %r15
leaq 0x74f43(%rip), %rsi     # 0xc29f5
movq %rbx, %rdi
callq 0x26af0
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testq %rax, %rax
je 0x4dbf5
movq %r12, 0x8(%rsp)
movq %r15, 0x18(%rsp)
leaq 0x28(%rsp), %r12
movq %r12, %rdi
movq %rax, (%rsp)
movq %rax, %rsi
callq 0x76172
leaq -0xf2(%rip), %rax       # 0x4d9fb
movq %rax, 0x20(%r12)
movq 0x338(%r14), %rax
movq %rax, 0x38(%r12)
movl $0x9, %eax
leaq 0x90(%rsp), %r13
testq %rax, %rax
je 0x4db74
movq %rax, %r15
movq 0x60(%rsp), %rax
cmpq %r15, %rax
movq %r15, %r14
cmovbq %rax, %r14
testq %rax, %rax
cmoveq %r15, %r14
movq %r12, %rdi
movq %r13, %rsi
movq %r14, %rdx
callq *0x28(%rsp)
movq %rax, %rbx
testq %rax, %rax
je 0x4db63
movq 0x48(%rsp), %rax
testq %rax, %rax
je 0x4db53
movq %r12, %rdi
movq %r13, %rsi
movq %r14, %rdx
callq *%rax
addq %r14, %r13
movq %r15, %rax
subq %r14, %rax
addq %r14, 0x58(%rsp)
jmp 0x4db66
movq %r15, %rax
testq %rbx, %rbx
jne 0x4db0b
testq %r15, %r15
jne 0x4df20
movb $0x0, 0x99(%rsp)
movl $0x49444552, %eax       # imm = 0x49444552
xorl 0x90(%rsp), %eax
movzbl 0x94(%rsp), %ecx
xorl $0x53, %ecx
orl %eax, %ecx
je 0x4dbb5
movq (%rsp), %rdi
callq 0x26a20
leaq 0x78087(%rip), %rsi     # 0xc5c2e
movl $0x3, %edi
xorl %eax, %eax
callq 0x32ea6
jmp 0x4dbea
leaq 0x95(%rsp), %rdi
callq 0x26770
movl %eax, %ebx
leal -0x8(%rbx), %eax
cmpl $-0x8, %eax
ja 0x4dc09
movq (%rsp), %rdi
callq 0x26a20
leaq 0x7807e(%rip), %rsi     # 0xc5c5a
movl $0x3, %edi
movl %ebx, %edx
xorl %eax, %eax
callq 0x32ea6
callq 0x26890
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 (%rsp), %rdi
callq 0x4d949
leaq 0x28(%rsp), %r13
leaq 0x77c69(%rip), %r15     # 0xc588c
leaq 0x77c4a(%rip), %r12     # 0xc5874
movq %r13, %rdi
callq 0x4ae06
movl %eax, %r14d
leal -0xfa(%r14), %eax
cmpl $0x5, %eax
ja 0x4dca0
movslq (%r12,%rax,4), %rax
addq %r12, %rax
jmpq *%rax
movq %r13, %rdi
xorl %esi, %esi
callq 0x4bb16
testq %rax, %rax
je 0x4ddfe
movq %rax, %r14
movq %r13, %rdi
xorl %esi, %esi
callq 0x4bb16
testq %rax, %rax
je 0x4ddb1
movq %rax, %r12
movq 0x8(%r14), %rdx
cmpb $0x25, (%rdx)
jne 0x4de3f
movq 0x8(%r12), %rcx
movl $0x2, %edi
leaq 0x78050(%rip), %rsi     # 0xc5ce4
xorl %eax, %eax
callq 0x32ea6
jmp 0x4de4f
cmpl $-0x1, %r14d
je 0x4ddfe
movq $-0x1, %rbp
jmp 0x4dd52
movq %r13, %rdi
xorl %esi, %esi
callq 0x4b10c
movl $0x2, %ecx
cmpl $-0x1, %eax
je 0x4de6b
leaq 0xb8b83(%rip), %rcx     # 0x106858
movl 0x600(%rcx), %edx
cmpl %edx, %eax
jae 0x4df46
movl %eax, %eax
shlq $0x6, %rax
addq 0x28(%rcx), %rax
movq %rax, 0x8(%rsp)
jmp 0x4ddf7
movq %r13, %rdi
callq 0x4afc4
cmpq $-0x1, %rax
je 0x4ddfe
movq %rax, %rbp
movq %r13, %rdi
callq 0x4ae06
cmpl $-0x1, %eax
je 0x4ddfe
movl %eax, %r14d
jmp 0x4dd52
movq %r13, %rdi
callq 0x4af1b
cmpq $-0x1, %rax
je 0x4ddfe
movq %rax, %rbx
movq %r13, %rdi
callq 0x4ae06
cmpl $-0x1, %eax
je 0x4ddfe
movl %eax, %r14d
imulq $0x3e8, %rbx, %rbp      # imm = 0x3E8
movq %r13, %rdi
xorl %esi, %esi
callq 0x4bb16
testq %rax, %rax
je 0x4ddb1
movq %rax, %r12
movl %r14d, %edi
movq %r13, %rsi
callq 0x4cfb6
testq %rax, %rax
je 0x4ddb1
movq %rax, %rbx
leaq 0xb8ada(%rip), %rax     # 0x106858
cmpq $0x0, 0x818(%rax)
sete %al
cmpq $-0x1, %rbp
setne %cl
cmpq 0x18(%rsp), %rbp
setl %dl
andb %cl, %dl
andb %al, %dl
cmpb $0x1, %dl
jne 0x4de0c
movq %r12, %rdi
callq 0x440f3
movq %rbx, %rdi
jmp 0x4de5a
movl $0x2, %ecx
jmp 0x4de64
movq %r13, %rdi
xorl %esi, %esi
callq 0x4b10c
cmpl $-0x1, %eax
je 0x4ddfe
movl %eax, %ebx
movq %r13, %rdi
xorl %esi, %esi
callq 0x4b10c
cmpl $-0x1, %eax
je 0x4ddfe
movl %eax, %ebp
movq 0x8(%rsp), %r14
movq (%r14), %rdi
movl %ebx, %esi
callq 0x317e2
movq 0x8(%r14), %rdi
movl %ebp, %esi
callq 0x317e2
movl $0x3, %ecx
jmp 0x4de6b
movl $0x2, %ecx
jmp 0x4de6b
movl $0x4, %ecx
jmp 0x4de6b
movq 0x8(%rsp), %r14
movq %r14, %rdi
movq %r12, %rsi
movq %rbx, %rdx
callq 0x45007
cmpq $-0x1, %rbp
je 0x4de33
movq %r14, %rdi
movq %r12, %rsi
movq %rbp, %rdx
callq 0x46505
movq %r12, %rdi
callq 0x440f3
xorl %ecx, %ecx
jmp 0x4de64
xorl %edi, %edi
leaq 0x77ea9(%rip), %rsi     # 0xc5cf1
xorl %eax, %eax
callq 0x32ea6
movq %r14, %rdi
callq 0x440f3
movq %r12, %rdi
callq 0x440f3
movl $0x3, %ecx
leaq 0x77a09(%rip), %r12     # 0xc5874
cmpl $0x4, %ecx
ja 0x4dee2
movl %ecx, %eax
movslq (%r15,%rax,4), %rax
addq %r15, %rax
jmpq *%rax
cmpl $0x5, 0x10(%rsp)
jl 0x4deff
leaq 0xb89cf(%rip), %rax     # 0x106858
cmpl $0x0, 0x744(%rax)
je 0x4deff
leaq 0x28(%rsp), %rdi
movq 0x28(%rdi), %r14
leaq 0x20(%rsp), %rsi
movl $0x8, %edx
callq 0x4ae97
testq %rax, %rax
je 0x4df20
movq %rax, %rbx
movq 0x20(%rsp), %rax
testq %rax, %rax
je 0x4dee7
cmpq %r14, %rax
je 0x4defa
leaq 0x77e89(%rip), %rsi     # 0xc5d51
movl $0x3, %edi
xorl %eax, %eax
callq 0x32ea6
leaq 0x77e98(%rip), %rsi     # 0xc5d73
movl $0x58a, %edi            # imm = 0x58A
jmp 0x4df3f
jmp 0x4dbf5
leaq 0x77e24(%rip), %rsi     # 0xc5d12
movl $0x3, %edi
xorl %eax, %eax
callq 0x32ea6
testq %rbx, %rbx
je 0x4df20
movq (%rsp), %rdi
callq 0x26a20
leaq 0xb8949(%rip), %rax     # 0x106858
movl $0x0, 0x31c(%rax)
xorl %ebp, %ebp
jmp 0x4dbf5
leaq 0x77e5a(%rip), %rsi     # 0xc5d81
movl $0x3, %edi
xorl %eax, %eax
callq 0x32ea6
leaq 0x77e88(%rip), %rsi     # 0xc5dc2
movl $0x594, %edi            # imm = 0x594
xorl %eax, %eax
callq 0x4ac2c
leaq 0x77d30(%rip), %rsi     # 0xc5c7d
movl $0x3, %edi
xorl %eax, %eax
callq 0x32ea6
movl $0x1, %edi
callq 0x262d0