// File: /workspace/github_c_makefile_star2_O2/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 0xb72c6(%rip), %r14     # 0xf98a8
movq 0x28(%r14), %r15
callq 0x2b8ae
movq %rax, 0x18(%rsp)
leaq 0x6f09e(%rip), %rsi     # 0xb1695
movq %rbx, %rdi
callq 0x26ae0
pushq $-0x1
popq %rbp
testq %rax, %rax
je 0x426cd
leaq 0x28(%rsp), %rbx
movq %rbx, %rdi
movq %rax, 0x8(%rsp)
movq %rax, %rsi
callq 0x66ab5
leaq -0xe9(%rip), %rax       # 0x4253e
movq %rax, 0x20(%rbx)
movq 0x338(%r14), %rax
movq %rax, 0x38(%rbx)
leaq 0x90(%rsp), %rsi
pushq $0x9
popq %rdx
movq %rbx, %rdi
callq 0x40497
testq %rax, %rax
je 0x42996
leaq 0x90(%rsp), %rdi
movb $0x0, 0x9(%rdi)
leaq 0x71d03(%rip), %rsi     # 0xb4368
pushq $0x5
popq %rdx
callq 0x266f0
testl %eax, %eax
je 0x4268e
movq 0x8(%rsp), %rdi
callq 0x26a10
leaq 0x71cec(%rip), %rsi     # 0xb436e
pushq $0x3
popq %rdi
xorl %eax, %eax
callq 0x2b699
jmp 0x426c2
leaq 0x95(%rsp), %rdi
callq 0x26760
movl %eax, %ebx
leal -0x8(%rbx), %eax
cmpl $-0x8, %eax
ja 0x426e1
movq 0x8(%rsp), %rdi
callq 0x26a10
leaq 0x71ce4(%rip), %rsi     # 0xb439a
pushq $0x3
popq %rdi
movl %ebx, %edx
xorl %eax, %eax
callq 0x2b699
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 0x4248e
leaq 0x28(%rsp), %r13
leaq 0x718cc(%rip), %r14     # 0xb3fc8
movq %r13, %rdi
callq 0x4046d
movl %eax, %ebx
leal -0xfa(%rbx), %eax
cmpl $0x5, %eax
ja 0x4276a
movslq (%r14,%rax,4), %rax
addq %r14, %rax
jmpq *%rax
movq %r13, %rdi
callq 0x40dda
testq %rax, %rax
je 0x42996
movq %rax, %rbx
movq %r13, %rdi
callq 0x40dda
testq %rax, %rax
je 0x42996
movq %rax, %r12
movq 0x8(%rbx), %rdx
cmpb $0x25, (%rdx)
jne 0x428ae
movq 0x8(%r12), %rcx
pushq $0x2
popq %rdi
leaq 0x71cc6(%rip), %rsi     # 0xb4424
xorl %eax, %eax
callq 0x2b699
jmp 0x428be
cmpl $-0x1, %ebx
je 0x42996
pushq $-0x1
popq %r12
jmp 0x427d2
movq %r13, %rdi
callq 0x4054b
cmpq $-0x1, %rax
je 0x42996
movq %rax, %r12
movq %r13, %rdi
callq 0x4046d
cmpl $-0x1, %eax
je 0x42996
movl %eax, %ebx
jmp 0x427d2
movq %r13, %rdi
callq 0x4050a
cmpq $-0x1, %rax
je 0x42996
movq %rax, %r12
movq %r13, %rdi
callq 0x4046d
cmpl $-0x1, %eax
je 0x42996
movl %eax, %ebx
imulq $0x3e8, %r12, %r12      # imm = 0x3E8
movq %r13, %rdi
callq 0x40dda
testq %rax, %rax
je 0x42996
movq %rax, %rbp
movl %ebx, %edi
movq %r13, %rsi
callq 0x41d5b
testq %rax, %rax
je 0x42996
movq %rax, %rbx
leaq 0xb70a5(%rip), %rax     # 0xf98a8
cmpq $0x0, 0x828(%rax)
sete %al
cmpq $-0x1, %r12
setne %cl
cmpq 0x18(%rsp), %r12
setl %dl
andb %cl, %dl
andb %al, %dl
cmpb $0x1, %dl
jne 0x4283b
movq %rbp, %rdi
callq 0x3a265
movq %rbx, %rdi
callq 0x3a265
jmp 0x426fc
movq %r15, %rdi
movq %rbp, %rsi
movq %rbx, %rdx
callq 0x3af45
cmpq $-0x1, %r12
je 0x4285d
movq %r15, %rdi
movq %rbp, %rsi
movq %r12, %rdx
callq 0x3c14f
movq %rbp, %rdi
callq 0x3a265
jmp 0x426fc
movq %r13, %rdi
xorl %esi, %esi
callq 0x4061c
cmpl $-0x1, %eax
je 0x42996
movl %eax, %ebx
movq %r13, %rdi
xorl %esi, %esi
callq 0x4061c
cmpl $-0x1, %eax
je 0x42996
movl %eax, %ebp
movq (%r15), %rdi
movl %ebx, %esi
callq 0x2a1b7
movq 0x8(%r15), %rdi
movl %ebp, %esi
callq 0x2a1b7
jmp 0x426fc
xorl %edi, %edi
leaq 0x71b7a(%rip), %rsi     # 0xb4431
xorl %eax, %eax
callq 0x2b699
movq %rbx, %rdi
callq 0x3a265
movq %r12, %rdi
callq 0x3a265
jmp 0x426fc
movq %r13, %rdi
xorl %esi, %esi
callq 0x4061c
cmpl $-0x1, %eax
je 0x42996
leaq 0xb6fbb(%rip), %rcx     # 0xf98a8
movl 0x610(%rcx), %edx
cmpl %edx, %eax
jae 0x429ba
movl %eax, %r15d
shlq $0x6, %r15
addq 0x28(%rcx), %r15
jmp 0x426fc
cmpl $0x5, 0x10(%rsp)
leaq 0xb6f91(%rip), %r14     # 0xf98a8
jl 0x4297d
cmpl $0x0, 0x754(%r14)
je 0x4297d
leaq 0x28(%rsp), %rdi
movq 0x28(%rdi), %rbx
leaq 0x20(%rsp), %rsi
pushq $0x8
popq %rdx
callq 0x40497
testq %rax, %rax
je 0x42996
movq 0x20(%rsp), %rax
testq %rax, %rax
je 0x4296c
cmpq %rbx, %rax
je 0x4297d
leaq 0x71b3d(%rip), %rsi     # 0xb4491
pushq $0x3
popq %rdi
xorl %eax, %eax
callq 0x2b699
leaq 0x71b4e(%rip), %rsi     # 0xb44b3
movl $0x58a, %edi            # imm = 0x58A
jmp 0x429b3
leaq 0x71adf(%rip), %rsi     # 0xb4452
pushq $0x3
popq %rdi
xorl %eax, %eax
callq 0x2b699
movq 0x8(%rsp), %rdi
callq 0x26a10
andl $0x0, 0x31c(%r14)
xorl %ebp, %ebp
jmp 0x426cd
leaq 0x71b24(%rip), %rsi     # 0xb44c1
pushq $0x3
popq %rdi
xorl %eax, %eax
callq 0x2b699
leaq 0x71b54(%rip), %rsi     # 0xb4502
movl $0x594, %edi            # imm = 0x594
xorl %eax, %eax
callq 0x40308
leaq 0x719fc(%rip), %rsi     # 0xb43bd
pushq $0x3
popq %rdi
xorl %eax, %eax
callq 0x2b699
pushq $0x1
popq %rdi
callq 0x262c0