// File: /workspace/github_c_makefile_star2_O2/gplessis[P]dotdeb-redis/src/redis-check-rdb.c
// Function: redis_check_rdb
int redis_check_rdb(char *rdbfilename) {
    uint64_t dbid;
    int type, rdbver;
    char buf[1024];
    long long expiretime, now = mstime();
    FILE *fp;
    rio rdb;

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

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

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

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

        /* Handle special types. */
        if (type == RDB_OPCODE_EXPIRETIME) {
            rdbstate.doing = RDB_CHECK_DOING_READ_EXPIRE;
            /* 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. */
            rdbstate.doing = RDB_CHECK_DOING_READ_TYPE;
            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. */
            rdbstate.doing = RDB_CHECK_DOING_READ_EXPIRE;
            if ((expiretime = rdbLoadMillisecondTime(&rdb)) == -1) goto eoferr;
            /* We read the time so we need to read the object type again. */
            rdbstate.doing = RDB_CHECK_DOING_READ_TYPE;
            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. */
            rdbstate.doing = RDB_CHECK_DOING_READ_LEN;
            if ((dbid = rdbLoadLen(&rdb,NULL)) == RDB_LENERR)
                goto eoferr;
            rdbCheckInfo("Selecting DB ID %d", 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. */
            uint64_t db_size, expires_size;
            rdbstate.doing = RDB_CHECK_DOING_READ_LEN;
            if ((db_size = rdbLoadLen(&rdb,NULL)) == RDB_LENERR)
                goto eoferr;
            if ((expires_size = rdbLoadLen(&rdb,NULL)) == RDB_LENERR)
                goto eoferr;
            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;
            rdbstate.doing = RDB_CHECK_DOING_READ_AUX;
            if ((auxkey = rdbLoadStringObject(&rdb)) == NULL) goto eoferr;
            if ((auxval = rdbLoadStringObject(&rdb)) == NULL) goto eoferr;

            rdbCheckInfo("AUX FIELD %s = '%s'",
                (char*)auxkey->ptr, (char*)auxval->ptr);
            decrRefCount(auxkey);
            decrRefCount(auxval);
            continue; /* Read type again. */
        } else {
            if (!rdbIsObjectType(type)) {
                rdbCheckError("Invalid object type: %d", type);
                return 1;
            }
            rdbstate.key_type = type;
        }

        /* Read key */
        rdbstate.doing = RDB_CHECK_DOING_READ_KEY;
        if ((key = rdbLoadStringObject(&rdb)) == NULL) goto eoferr;
        rdbstate.key = key;
        rdbstate.keys++;
        /* Read value */
        rdbstate.doing = RDB_CHECK_DOING_READ_OBJECT_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)
            rdbstate.already_expired++;
        if (expiretime != -1) rdbstate.expires++;
        rdbstate.key = NULL;
        decrRefCount(key);
        decrRefCount(val);
        rdbstate.key_type = -1;
    }
    /* Verify the checksum if RDB version is >= 5 */
    if (rdbver >= 5 && server.rdb_checksum) {
        uint64_t cksum, expected = rdb.cksum;

        rdbstate.doing = RDB_CHECK_DOING_CHECK_SUM;
        if (rioRead(&rdb,&cksum,8) == 0) goto eoferr;
        memrev64ifbe(&cksum);
        if (cksum == 0) {
            rdbCheckInfo("RDB file was saved with checksum disabled: no check performed.");
        } else if (cksum != expected) {
            rdbCheckError("RDB CRC error");
        } else {
            rdbCheckInfo("Checksum OK");
        }
    }

    fclose(fp);
    return 0;

eoferr: /* unexpected end of file is handled here with a fatal exit */
    if (rdbstate.error_set) {
        rdbCheckError(rdbstate.error);
    } else {
        rdbCheckError("Unexpected EOF reading RDB file");
    }
    return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x488, %rsp            # imm = 0x488
movq %rdi, %rbx
callq 0x2b8ae
movq %rax, %r14
leaq 0x3e29e(%rip), %rsi     # 0xb1695
movq %rbx, %rdi
callq 0x26ae0
testq %rax, %rax
je 0x73470
movq %rax, %rbx
leaq 0x18(%rsp), %r15
movq %r15, %rdi
movq %rax, %rsi
callq 0x66ab5
movq %r15, 0x9b57a(%rip)     # 0x10e998
leaq -0x30ee7(%rip), %rax    # 0x4253e
movq %rax, 0x20(%r15)
leaq 0x80(%rsp), %rsi
pushq $0x9
popq %rdx
movq %r15, %rdi
callq 0x737c4
testq %rax, %rax
je 0x736ff
leaq 0x80(%rsp), %rdi
movb $0x0, 0x9(%rdi)
leaq 0x40f10(%rip), %rsi     # 0xb4368
pushq $0x5
popq %rdx
callq 0x266f0
testl %eax, %eax
je 0x73477
leaq 0x40f03(%rip), %rdi     # 0xb436e
jmp 0x73718
pushq $-0x1
jmp 0x73721
leaq 0x85(%rsp), %rdi
callq 0x26760
movl %eax, %esi
leal -0x8(%rsi), %eax
cmpl $-0x8, %eax
ja 0x734a1
leaq 0x40f05(%rip), %rdi     # 0xb439a
xorl %eax, %eax
callq 0x7309d
jmp 0x7371f
movq %rsi, 0x8(%rsp)
movq %rbx, %rdi
callq 0x4248e
leaq 0x18(%rsp), %r12
movl $0x1, 0x9b50b(%rip)     # 0x10e9c8
movq %r12, %rdi
callq 0x4046d
movl %eax, %r13d
leal 0x1(%r13), %eax
cmpl $0xf, %eax
ja 0x735a4
movl $0xfc3e, %ecx           # imm = 0xFC3E
btl %eax, %ecx
jae 0x7359c
movl %r13d, 0x9b4be(%rip)    # 0x10e9a8
pushq $-0x1
popq %rbp
movl $0x3, 0x9b4d1(%rip)     # 0x10e9c8
movq %r12, %rdi
callq 0x40dda
testq %rax, %rax
je 0x736ff
movq %rax, %r15
movq %rax, 0x9b48e(%rip)     # 0x10e9a0
incq 0x9b497(%rip)           # 0x10e9b0
movl $0x4, 0x9b4a5(%rip)     # 0x10e9c8
movl %r13d, %edi
movq %r12, %rsi
callq 0x41d5b
testq %rax, %rax
je 0x736ff
movq %rax, %r13
leaq 0x86367(%rip), %rax     # 0xf98a8
cmpq $0x0, 0x828(%rax)
sete %al
cmpq $-0x1, %rbp
setne %cl
cmpq %r14, %rbp
setl %dl
andb %cl, %dl
andb %al, %dl
cmpb $0x1, %dl
jne 0x7356b
incq 0x9b457(%rip)           # 0x10e9c0
jmp 0x73571
cmpq $-0x1, %rbp
je 0x73578
incq 0x9b440(%rip)           # 0x10e9b8
andq $0x0, 0x9b420(%rip)     # 0x10e9a0
movq %r15, %rdi
callq 0x3a265
movq %r13, %rdi
callq 0x3a265
orl $-0x1, 0x9b411(%rip)    # 0x10e9a8
jmp 0x734b3
testl %eax, %eax
je 0x736ff
movl %r13d, %eax
addl $0xffffff06, %eax       # imm = 0xFFFFFF06
cmpl $0x5, %eax
ja 0x73788
leaq 0x4c8f0(%rip), %rcx     # 0xbfeac
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl $0x7, 0x9b3f9(%rip)     # 0x10e9c8
movq %r12, %rdi
callq 0x40dda
testq %rax, %rax
je 0x736ff
movq %rax, %r15
movq %r12, %rdi
callq 0x40dda
testq %rax, %rax
je 0x736ff
movq %rax, %r13
movq 0x8(%r15), %rsi
movq 0x8(%rax), %rdx
leaq 0x4ca97(%rip), %rdi     # 0xc009d
xorl %eax, %eax
callq 0x731e8
movq %r15, %rdi
callq 0x3a265
movq %r13, %rdi
callq 0x3a265
jmp 0x734b3
movl $0x6, 0x9b39c(%rip)     # 0x10e9c8
movq %r12, %rdi
xorl %esi, %esi
callq 0x4061c
cmpl $-0x1, %eax
je 0x736ff
movl %eax, %esi
leaq 0x4ca42(%rip), %rdi     # 0xc008a
xorl %eax, %eax
callq 0x731e8
jmp 0x734b3
movl $0x2, 0x9b36a(%rip)     # 0x10e9c8
movq %r12, %rdi
callq 0x4054b
cmpq $-0x1, %rax
je 0x736ff
movq %rax, %rbp
movl $0x1, 0x9b34b(%rip)     # 0x10e9c8
movq %r12, %rdi
callq 0x4046d
cmpl $-0x1, %eax
je 0x736ff
movl %eax, %r13d
jmp 0x734ed
movl $0x2, 0x9b32c(%rip)     # 0x10e9c8
movq %r12, %rdi
callq 0x4050a
cmpq $-0x1, %rax
je 0x736ff
movq %rax, %r15
movl $0x1, 0x9b311(%rip)     # 0x10e9c8
movq %r12, %rdi
callq 0x4046d
cmpl $-0x1, %eax
je 0x736ff
movl %eax, %r13d
imulq $0x3e8, %r15, %rbp      # imm = 0x3E8
jmp 0x734ed
movl $0x6, 0x9b2eb(%rip)     # 0x10e9c8
movq %r12, %rdi
xorl %esi, %esi
callq 0x4061c
cmpl $-0x1, %eax
je 0x736ff
movq %r12, %rdi
xorl %esi, %esi
callq 0x4061c
cmpl $-0x1, %eax
jne 0x734b3
cmpl $0x0, 0x9b2c6(%rip)     # 0x10e9cc
je 0x73711
leaq 0x9b2c1(%rip), %rdi     # 0x10e9d0
jmp 0x73718
leaq 0x40dea(%rip), %rdi     # 0xb4502
xorl %eax, %eax
callq 0x7309d
pushq $0x1
popq %rax
addq $0x488, %rsp            # imm = 0x488
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x5, 0x8(%rsp)
jl 0x737b5
leaq 0x86166(%rip), %rax     # 0xf98a8
cmpl $0x0, 0x754(%rax)
je 0x737b5
leaq 0x18(%rsp), %rdi
movq 0x28(%rdi), %r14
movl $0x5, 0x9b26a(%rip)     # 0x10e9c8
leaq 0x10(%rsp), %rsi
pushq $0x8
popq %rdx
callq 0x737c4
testq %rax, %rax
je 0x736ff
movq 0x10(%rsp), %rax
testq %rax, %rax
je 0x73797
cmpq %r14, %rax
jne 0x737a7
leaq 0x4c943(%rip), %rdi     # 0xc00c9
jmp 0x7379e
leaq 0x4c922(%rip), %rdi     # 0xc00b1
movl %r13d, %esi
jmp 0x73495
leaq 0x40cb4(%rip), %rdi     # 0xb4452
xorl %eax, %eax
callq 0x731e8
jmp 0x737b5
leaq 0x40d05(%rip), %rdi     # 0xb44b3
xorl %eax, %eax
callq 0x7309d
movq %rbx, %rdi
callq 0x26a10
xorl %eax, %eax
jmp 0x73722