// File: /workspace/github_c_makefile_star2_O3/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 0x2ffce
movq %rax, %r14
leaq 0x3f770(%rip), %rsi     # 0xbfa5d
movq %rbx, %rdi
callq 0x26ae0
testq %rax, %rax
je 0x803ad
movq %rax, %rbx
leaq 0x18(%rsp), %r15
movq %r15, %rdi
movq %rax, %rsi
callq 0x726c2
movq %r15, 0x96680(%rip)     # 0x116998
leaq -0x35fb6(%rip), %rax    # 0x4a369
movq %rax, 0x20(%r15)
leaq 0x80(%rsp), %r12
movl $0x9, %ebp
movq 0x50(%rsp), %rax
cmpq %rbp, %rax
movq %rbp, %r13
cmovbq %rax, %r13
testq %rax, %rax
cmoveq %rbp, %r13
movq %r15, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq *0x18(%rsp)
testq %rax, %rax
je 0x80649
movq 0x38(%rsp), %rax
testq %rax, %rax
je 0x80371
movq %r15, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq *%rax
addq %r13, %r12
addq %r13, 0x48(%rsp)
subq %r13, %rbp
jne 0x80330
movb $0x0, 0x89(%rsp)
movl $0x49444552, %eax       # imm = 0x49444552
xorl 0x80(%rsp), %eax
movzbl 0x84(%rsp), %ecx
xorl $0x53, %ecx
orl %eax, %ecx
je 0x803b7
leaq 0x42906(%rip), %rdi     # 0xc2cae
jmp 0x80662
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x8066e
leaq 0x85(%rsp), %rdi
callq 0x26760
movl %eax, %esi
leal -0x8(%rsi), %eax
cmpl $-0x8, %eax
ja 0x803e1
leaq 0x42905(%rip), %rdi     # 0xc2cda
xorl %eax, %eax
callq 0x7ff89
jmp 0x80669
movq %rsi, 0x8(%rsp)
movq %rbx, %rdi
callq 0x4a2b7
leaq 0x18(%rsp), %r12
movl $0x1, 0x965cb(%rip)     # 0x1169c8
movq %r12, %rdi
callq 0x47b17
movl %eax, %r13d
leal 0x1(%r13), %eax
cmpl $0xf, %eax
ja 0x804ee
movl $0xfc3e, %ecx           # imm = 0xFC3E
btl %eax, %ecx
jae 0x804e6
movl %r13d, 0x9657e(%rip)    # 0x1169a8
movq $-0x1, %rbp
movl $0x3, 0x9658d(%rip)     # 0x1169c8
movq %r12, %rdi
callq 0x487e6
testq %rax, %rax
je 0x80649
movq %rax, %r15
movq %rax, 0x9654a(%rip)     # 0x1169a0
incq 0x96553(%rip)           # 0x1169b0
movl $0x4, 0x96561(%rip)     # 0x1169c8
movl %r13d, %edi
movq %r12, %rsi
callq 0x499b2
testq %rax, %rax
je 0x80649
movq %rax, %r13
leaq 0x81423(%rip), %rax     # 0x1018a8
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 0x804af
incq 0x96513(%rip)           # 0x1169c0
jmp 0x804b5
cmpq $-0x1, %rbp
je 0x804bc
incq 0x964fc(%rip)           # 0x1169b8
movq $0x0, 0x964d9(%rip) # 0x1169a0
movq %r15, %rdi
callq 0x40c8f
movq %r13, %rdi
callq 0x40c8f
movl $0xffffffff, 0x964c7(%rip) # imm = 0xFFFFFFFF
jmp 0x803f3
testl %eax, %eax
je 0x80649
movl %r13d, %eax
addl $0xffffff06, %eax       # imm = 0xFFFFFF06
cmpl $0x5, %eax
ja 0x806d6
leaq 0x4edde(%rip), %rcx     # 0xcf2e4
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl $0x7, 0x964af(%rip)     # 0x1169c8
movq %r12, %rdi
callq 0x487e6
testq %rax, %rax
je 0x80649
movq %rax, %r15
movq %r12, %rdi
callq 0x487e6
testq %rax, %rax
je 0x80649
movq %rax, %r13
movq 0x8(%r15), %rsi
movq 0x8(%rax), %rdx
leaq 0x4ef85(%rip), %rdi     # 0xcf4d5
xorl %eax, %eax
callq 0x800d4
movq %r15, %rdi
callq 0x40c8f
movq %r13, %rdi
callq 0x40c8f
jmp 0x803f3
movl $0x6, 0x96452(%rip)     # 0x1169c8
movq %r12, %rdi
xorl %esi, %esi
callq 0x47db6
cmpl $-0x1, %eax
je 0x80649
movl %eax, %esi
leaq 0x4ef30(%rip), %rdi     # 0xcf4c2
xorl %eax, %eax
callq 0x800d4
jmp 0x803f3
movl $0x2, 0x96420(%rip)     # 0x1169c8
movq %r12, %rdi
callq 0x47c8c
cmpq $-0x1, %rax
je 0x80649
movq %rax, %rbp
movl $0x1, 0x96401(%rip)     # 0x1169c8
movq %r12, %rdi
callq 0x47b17
cmpl $-0x1, %eax
je 0x80649
movl %eax, %r13d
jmp 0x80431
movl $0x2, 0x963e2(%rip)     # 0x1169c8
movq %r12, %rdi
callq 0x47c01
cmpq $-0x1, %rax
je 0x80649
movq %rax, %r15
movl $0x1, 0x963c7(%rip)     # 0x1169c8
movq %r12, %rdi
callq 0x47b17
cmpl $-0x1, %eax
je 0x80649
movl %eax, %r13d
imulq $0x3e8, %r15, %rbp      # imm = 0x3E8
jmp 0x80431
movl $0x6, 0x963a1(%rip)     # 0x1169c8
movq %r12, %rdi
xorl %esi, %esi
callq 0x47db6
cmpl $-0x1, %eax
je 0x80649
movq %r12, %rdi
xorl %esi, %esi
callq 0x47db6
cmpl $-0x1, %eax
jne 0x803f3
cmpl $0x0, 0x9637c(%rip)     # 0x1169cc
je 0x8065b
leaq 0x96377(%rip), %rdi     # 0x1169d0
jmp 0x80662
leaq 0x427e0(%rip), %rdi     # 0xc2e42
xorl %eax, %eax
callq 0x7ff89
movl $0x1, %eax
addq $0x488, %rsp            # imm = 0x488
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x5, 0x8(%rsp)
jl 0x80703
leaq 0x8121a(%rip), %rax     # 0x1018a8
cmpl $0x0, 0x754(%rax)
je 0x80703
leaq 0x18(%rsp), %rdi
movq 0x28(%rdi), %r14
movl $0x5, 0x9631e(%rip)     # 0x1169c8
leaq 0x10(%rsp), %rsi
movl $0x8, %edx
callq 0x80712
testq %rax, %rax
je 0x80649
movq 0x10(%rsp), %rax
testq %rax, %rax
je 0x806e5
cmpq %r14, %rax
jne 0x806f5
leaq 0x4ee2d(%rip), %rdi     # 0xcf501
jmp 0x806ec
leaq 0x4ee0c(%rip), %rdi     # 0xcf4e9
movl %r13d, %esi
jmp 0x803d5
leaq 0x426a6(%rip), %rdi     # 0xc2d92
xorl %eax, %eax
callq 0x800d4
jmp 0x80703
leaq 0x426f7(%rip), %rdi     # 0xc2df3
xorl %eax, %eax
callq 0x7ff89
movq %rbx, %rdi
callq 0x26a10
xorl %eax, %eax
jmp 0x8066e