// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x4f0, %rsp            # imm = 0x4F0
movq %rdi, -0x10(%rbp)
leaq 0x121d52(%rip), %rax    # 0x1781d8
movq 0x28(%rax), %rax
movq %rax, -0x28(%rbp)
callq 0x2ff70
movq %rax, -0x440(%rbp)
movq -0x10(%rbp), %rdi
leaq 0xe4324(%rip), %rsi     # 0x13a7c9
callq 0x26ae0
movq %rax, -0x448(%rbp)
cmpq $0x0, %rax
jne 0x564c3
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x56a24
movq -0x448(%rbp), %rsi
leaq -0x4b0(%rbp), %rdi
callq 0x8e620
leaq -0x14d(%rip), %rax      # 0x56390
movq %rax, -0x490(%rbp)
leaq 0x121ced(%rip), %rax    # 0x1781d8
movq 0x338(%rax), %rax
movq %rax, -0x478(%rbp)
leaq -0x430(%rbp), %rsi
leaq -0x4b0(%rbp), %rdi
movl $0x9, %edx
callq 0x52c70
cmpq $0x0, %rax
jne 0x5651c
jmp 0x569f7
movb $0x0, -0x427(%rbp)
leaq -0x430(%rbp), %rdi
leaq 0xe6f0a(%rip), %rsi     # 0x13d43b
movl $0x5, %edx
callq 0x269b0
cmpl $0x0, %eax
je 0x56576
movq -0x448(%rbp), %rdi
callq 0x26a10
movl $0x3, %edi
leaq 0xe6ee9(%rip), %rsi     # 0x13d441
movb $0x0, %al
callq 0x2fc60
callq 0x26880
movl $0x16, (%rax)
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x56a24
leaq -0x430(%rbp), %rdi
addq $0x5, %rdi
callq 0x26760
movl %eax, -0x1c(%rbp)
cmpl $0x1, -0x1c(%rbp)
jl 0x56595
cmpl $0x7, -0x1c(%rbp)
jle 0x565ce
movq -0x448(%rbp), %rdi
callq 0x26a10
movl -0x1c(%rbp), %edx
movl $0x3, %edi
leaq 0xe6ebd(%rip), %rsi     # 0x13d46d
movb $0x0, %al
callq 0x2fc60
callq 0x26880
movl $0x16, (%rax)
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x56a24
movq -0x448(%rbp), %rdi
callq 0x56270
movq $-0x1, -0x438(%rbp)
leaq -0x4b0(%rbp), %rdi
callq 0x52c30
movl %eax, -0x18(%rbp)
cmpl $-0x1, %eax
jne 0x565fe
jmp 0x569f7
cmpl $0xfd, -0x18(%rbp)
jne 0x56655
leaq -0x4b0(%rbp), %rdi
callq 0x52d50
movq %rax, -0x438(%rbp)
cmpq $-0x1, %rax
jne 0x56625
jmp 0x569f7
leaq -0x4b0(%rbp), %rdi
callq 0x52c30
movl %eax, -0x18(%rbp)
cmpl $-0x1, %eax
jne 0x5663e
jmp 0x569f7
imulq $0x3e8, -0x438(%rbp), %rax # imm = 0x3E8
movq %rax, -0x438(%rbp)
jmp 0x56868
cmpl $0xfc, -0x18(%rbp)
jne 0x5669a
leaq -0x4b0(%rbp), %rdi
callq 0x52dc0
movq %rax, -0x438(%rbp)
cmpq $-0x1, %rax
jne 0x5667c
jmp 0x569f7
leaq -0x4b0(%rbp), %rdi
callq 0x52c30
movl %eax, -0x18(%rbp)
cmpl $-0x1, %eax
jne 0x56695
jmp 0x569f7
jmp 0x56866
cmpl $0xff, -0x18(%rbp)
jne 0x566a8
jmp 0x56938
cmpl $0xfe, -0x18(%rbp)
jne 0x56728
leaq -0x4b0(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x52f10
movl %eax, -0x14(%rbp)
cmpl $-0x1, %eax
jne 0x566ce
jmp 0x569f7
movl -0x14(%rbp), %eax
leaq 0x121b00(%rip), %rcx    # 0x1781d8
cmpl 0x610(%rcx), %eax
jb 0x5670a
leaq 0x121af1(%rip), %rax    # 0x1781d8
movl 0x610(%rax), %edx
movl $0x3, %edi
leaq 0xe6d97(%rip), %rsi     # 0x13d490
movb $0x0, %al
callq 0x2fc60
movl $0x1, %edi
callq 0x262d0
leaq 0x121ac7(%rip), %rax    # 0x1781d8
movq 0x28(%rax), %rax
movl -0x14(%rbp), %ecx
shlq $0x6, %rcx
addq %rcx, %rax
movq %rax, -0x28(%rbp)
jmp 0x565da
cmpl $0xfb, -0x18(%rbp)
jne 0x5679f
leaq -0x4b0(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x52f10
movl %eax, -0x4c4(%rbp)
cmpl $-0x1, %eax
jne 0x56751
jmp 0x569f7
leaq -0x4b0(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x52f10
movl %eax, -0x4c8(%rbp)
cmpl $-0x1, %eax
jne 0x56771
jmp 0x569f7
movq -0x28(%rbp), %rax
movq (%rax), %rdi
movl -0x4c4(%rbp), %eax
movl %eax, %esi
callq 0x2d440
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rdi
movl -0x4c8(%rbp), %eax
movl %eax, %esi
callq 0x2d440
jmp 0x565da
cmpl $0xfa, -0x18(%rbp)
jne 0x5685e
leaq -0x4b0(%rbp), %rdi
callq 0x53be0
movq %rax, -0x4d0(%rbp)
cmpq $0x0, %rax
jne 0x567ca
jmp 0x569f7
leaq -0x4b0(%rbp), %rdi
callq 0x53be0
movq %rax, -0x4d8(%rbp)
cmpq $0x0, %rax
jne 0x567e8
jmp 0x569f7
movq -0x4d0(%rbp), %rax
movq 0x8(%rax), %rax
movsbl (%rax), %eax
cmpl $0x25, %eax
jne 0x56826
movq -0x4d0(%rbp), %rax
movq 0x8(%rax), %rdx
movq -0x4d8(%rbp), %rax
movq 0x8(%rax), %rcx
movl $0x2, %edi
leaq 0xe6cda(%rip), %rsi     # 0x13d4f7
movb $0x0, %al
callq 0x2fc60
jmp 0x56841
movq -0x4d0(%rbp), %rax
movq 0x8(%rax), %rdx
xorl %edi, %edi
leaq 0xe6cca(%rip), %rsi     # 0x13d504
movb $0x0, %al
callq 0x2fc60
movq -0x4d0(%rbp), %rdi
callq 0x48f70
movq -0x4d8(%rbp), %rdi
callq 0x48f70
jmp 0x565da
jmp 0x56860
jmp 0x56862
jmp 0x56864
jmp 0x56866
jmp 0x56868
leaq -0x4b0(%rbp), %rdi
callq 0x53be0
movq %rax, -0x4b8(%rbp)
cmpq $0x0, %rax
jne 0x56886
jmp 0x569f7
movl -0x18(%rbp), %edi
leaq -0x4b0(%rbp), %rsi
callq 0x55630
movq %rax, -0x4c0(%rbp)
cmpq $0x0, %rax
jne 0x568a7
jmp 0x569f7
leaq 0x12192a(%rip), %rax    # 0x1781d8
cmpq $0x0, 0x828(%rax)
jne 0x568ef
cmpq $-0x1, -0x438(%rbp)
je 0x568ef
movq -0x438(%rbp), %rax
cmpq -0x440(%rbp), %rax
jge 0x568ef
movq -0x4b8(%rbp), %rdi
callq 0x48f70
movq -0x4c0(%rbp), %rdi
callq 0x48f70
jmp 0x565da
movq -0x28(%rbp), %rdi
movq -0x4b8(%rbp), %rsi
movq -0x4c0(%rbp), %rdx
callq 0x4a7d0
cmpq $-0x1, -0x438(%rbp)
je 0x56927
movq -0x28(%rbp), %rdi
movq -0x4b8(%rbp), %rsi
movq -0x438(%rbp), %rdx
callq 0x4c4f0
movq -0x4b8(%rbp), %rdi
callq 0x48f70
jmp 0x565da
cmpl $0x5, -0x1c(%rbp)
jl 0x569dd
leaq 0x12188f(%rip), %rax    # 0x1781d8
cmpl $0x0, 0x754(%rax)
je 0x569dd
movq -0x488(%rbp), %rax
movq %rax, -0x4e8(%rbp)
leaq -0x4b0(%rbp), %rdi
leaq -0x4e0(%rbp), %rsi
movl $0x8, %edx
callq 0x52c70
cmpq $0x0, %rax
jne 0x56984
jmp 0x569f7
cmpq $0x0, -0x4e0(%rbp)
jne 0x569a3
movl $0x3, %edi
leaq 0xe6b8b(%rip), %rsi     # 0x13d525
movb $0x0, %al
callq 0x2fc60
jmp 0x569db
movq -0x4e0(%rbp), %rax
cmpq -0x4e8(%rbp), %rax
je 0x569d9
movl $0x3, %edi
leaq 0xe6ba5(%rip), %rsi     # 0x13d564
movb $0x0, %al
callq 0x2fc60
movl $0x58a, %edi            # imm = 0x58A
leaq 0xe6bb4(%rip), %rsi     # 0x13d586
movb $0x0, %al
callq 0x52a50
jmp 0x569db
jmp 0x569dd
movq -0x448(%rbp), %rdi
callq 0x26a10
callq 0x56370
movl $0x0, -0x4(%rbp)
jmp 0x56a24
movl $0x3, %edi
leaq 0xe6b91(%rip), %rsi     # 0x13d594
movb $0x0, %al
callq 0x2fc60
movl $0x594, %edi            # imm = 0x594
leaq 0xe6bbf(%rip), %rsi     # 0x13d5d5
movb $0x0, %al
callq 0x52a50
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl -0x4(%rbp), %eax
addq $0x4f0, %rsp            # imm = 0x4F0
popq %rbp
retq