// File: /workspace/github_c_makefile_star2_O3/gplessis[P]dotdeb-redis/src/aof.c
// Function: loadAppendOnlyFile
int loadAppendOnlyFile(char *filename) {
    struct client *fakeClient;
    FILE *fp = fopen(filename,"r");
    struct redis_stat sb;
    int old_aof_state = server.aof_state;
    long loops = 0;
    off_t valid_up_to = 0; /* Offset of the latest well-formed command loaded. */

    if (fp && redis_fstat(fileno(fp),&sb) != -1 && sb.st_size == 0) {
        server.aof_current_size = 0;
        fclose(fp);
        return C_ERR;
    }

    if (fp == NULL) {
        serverLog(LL_WARNING,"Fatal error: can't open the append log file for reading: %s",strerror(errno));
        exit(1);
    }

    /* Temporarily disable AOF, to prevent EXEC from feeding a MULTI
     * to the same file we're about to read. */
    server.aof_state = AOF_OFF;

    fakeClient = createFakeClient();
    startLoading(fp);

    while(1) {
        int argc, j;
        unsigned long len;
        robj **argv;
        char buf[128];
        sds argsds;
        struct redisCommand *cmd;

        /* Serve the clients from time to time */
        if (!(loops++ % 1000)) {
            loadingProgress(ftello(fp));
            processEventsWhileBlocked();
        }

        if (fgets(buf,sizeof(buf),fp) == NULL) {
            if (feof(fp))
                break;
            else
                goto readerr;
        }
        if (buf[0] != '*') goto fmterr;
        if (buf[1] == '\0') goto readerr;
        argc = atoi(buf+1);
        if (argc < 1) goto fmterr;

        argv = zmalloc(sizeof(robj*)*argc);
        fakeClient->argc = argc;
        fakeClient->argv = argv;

        for (j = 0; j < argc; j++) {
            if (fgets(buf,sizeof(buf),fp) == NULL) {
                fakeClient->argc = j; /* Free up to j-1. */
                freeFakeClientArgv(fakeClient);
                goto readerr;
            }
            if (buf[0] != '$') goto fmterr;
            len = strtol(buf+1,NULL,10);
            argsds = sdsnewlen(NULL,len);
            if (len && fread(argsds,len,1,fp) == 0) {
                sdsfree(argsds);
                fakeClient->argc = j; /* Free up to j-1. */
                freeFakeClientArgv(fakeClient);
                goto readerr;
            }
            argv[j] = createObject(OBJ_STRING,argsds);
            if (fread(buf,2,1,fp) == 0) {
                fakeClient->argc = j+1; /* Free up to j. */
                freeFakeClientArgv(fakeClient);
                goto readerr; /* discard CRLF */
            }
        }

        /* Command lookup */
        cmd = lookupCommand(argv[0]->ptr);
        if (!cmd) {
            serverLog(LL_WARNING,"Unknown command '%s' reading the append only file", (char*)argv[0]->ptr);
            exit(1);
        }

        /* Run the command in the context of a fake client */
        cmd->proc(fakeClient);

        /* The fake client should not have a reply */
        serverAssert(fakeClient->bufpos == 0 && listLength(fakeClient->reply) == 0);
        /* The fake client should never get blocked */
        serverAssert((fakeClient->flags & CLIENT_BLOCKED) == 0);

        /* Clean up. Command code may have changed argv/argc so we use the
         * argv/argc of the client instead of the local variables. */
        freeFakeClientArgv(fakeClient);
        if (server.aof_load_truncated) valid_up_to = ftello(fp);
    }

    /* This point can only be reached when EOF is reached without errors.
     * If the client is in the middle of a MULTI/EXEC, log error and quit. */
    if (fakeClient->flags & CLIENT_MULTI) goto uxeof;

loaded_ok: /* DB loaded, cleanup and return C_OK to the caller. */
    fclose(fp);
    freeFakeClient(fakeClient);
    server.aof_state = old_aof_state;
    stopLoading();
    aofUpdateCurrentSize();
    server.aof_rewrite_base_size = server.aof_current_size;
    return C_OK;

readerr: /* Read error. If feof(fp) is true, fall through to unexpected EOF. */
    if (!feof(fp)) {
        if (fakeClient) freeFakeClient(fakeClient); /* avoid valgrind warning */
        serverLog(LL_WARNING,"Unrecoverable error reading the append only file: %s", strerror(errno));
        exit(1);
    }

uxeof: /* Unexpected AOF end of file. */
    if (server.aof_load_truncated) {
        serverLog(LL_WARNING,"!!! Warning: short read while loading the AOF file !!!");
        serverLog(LL_WARNING,"!!! Truncating the AOF at offset %llu !!!",
            (unsigned long long) valid_up_to);
        if (valid_up_to == -1 || truncate(filename,valid_up_to) == -1) {
            if (valid_up_to == -1) {
                serverLog(LL_WARNING,"Last valid command offset is invalid");
            } else {
                serverLog(LL_WARNING,"Error truncating the AOF file: %s",
                    strerror(errno));
            }
        } else {
            /* Make sure the AOF file descriptor points to the end of the
             * file after the truncate call. */
            if (server.aof_fd != -1 && lseek(server.aof_fd,0,SEEK_END) == -1) {
                serverLog(LL_WARNING,"Can't seek the end of the AOF file: %s",
                    strerror(errno));
            } else {
                serverLog(LL_WARNING,
                    "AOF loaded anyway because aof-load-truncated is enabled");
                goto loaded_ok;
            }
        }
    }
    if (fakeClient) freeFakeClient(fakeClient); /* avoid valgrind warning */
    serverLog(LL_WARNING,"Unexpected end of file reading the append only file. You can: 1) Make a backup of your AOF file, then use ./redis-check-aof --fix <filename>. 2) Alternatively you can set the 'aof-load-truncated' configuration option to yes and restart the server.");
    exit(1);

fmterr: /* Format error. */
    if (fakeClient) freeFakeClient(fakeClient); /* avoid valgrind warning */
    serverLog(LL_WARNING,"Bad file format reading the append only file: make a backup of your AOF file, then use ./redis-check-aof --fix <filename>");
    exit(1);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x158, %rsp            # imm = 0x158
movq %rdi, %r12
leaq 0x618e5(%rip), %rsi     # 0xbfa5d
callq 0x26ae0
testq %rax, %rax
je 0x5e56f
leaq 0xa371b(%rip), %rbx     # 0x1018a8
movl 0x668(%rbx), %ebp
movq %rax, 0x10(%rsp)
movq %rax, %rdi
callq 0x26070
leaq 0xc8(%rsp), %rsi
movl %eax, %edi
callq 0x266e0
cmpl $-0x1, %eax
je 0x5e1de
cmpq $0x0, 0xf8(%rsp)
jne 0x5e1de
movq $0x0, 0x690(%rbx)
movq 0x10(%rsp), %rdi
callq 0x26a10
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x5e510
movl $0x0, 0x668(%rbx)
callq 0x5e040
movq %rax, %rbx
movq 0x10(%rsp), %rdi
callq 0x4a2b7
movq $0x0, 0x20(%rsp)
movabsq $0x1cac083126e978d5, %r13 # imm = 0x1CAC083126E978D5
movabsq $0x4189374bc6a7ef, %rcx # imm = 0x4189374BC6A7EF
leaq 0x40(%rsp), %r15
xorl %r14d, %r14d
movq %rbx, 0x8(%rsp)
movl %ebp, 0x1c(%rsp)
movq %r12, 0x30(%rsp)
movq 0x10(%rsp), %rbp
movq %r14, %rax
imulq %r13, %rax
rorq $0x3, %rax
cmpq %rcx, %rax
ja 0x5e259
movq 0x10(%rsp), %rdi
callq 0x26bd0
movq %rax, %rdi
callq 0x4a320
callq 0x3f9e8
movq %r15, %rdi
movl $0x80, %esi
movq 0x10(%rsp), %rbx
movq %rbx, %rdx
callq 0x26440
testq %rax, %rax
je 0x5e522
movq %r14, 0x28(%rsp)
cmpb $0x2a, 0x40(%rsp)
jne 0x5e551
cmpb $0x0, 0x41(%rsp)
je 0x5e42a
leaq 0x41(%rsp), %rdi
callq 0x26760
testl %eax, %eax
jle 0x5e551
movl %eax, %ebx
incq 0x28(%rsp)
movl %eax, %eax
movq %rax, 0x38(%rsp)
leaq (,%rax,8), %rdi
callq 0x382d1
movq %rax, %r12
movq 0x8(%rsp), %rax
movl %ebx, 0x38(%rax)
movq %r12, 0x40(%rax)
xorl %r13d, %r13d
movq 0x10(%rsp), %rdx
movq %r15, %rdi
movl $0x80, %esi
callq 0x26440
testq %rax, %rax
je 0x5e41c
cmpb $0x24, 0x40(%rsp)
jne 0x5e551
movq %r15, %r14
leaq 0x41(%rsp), %rdi
xorl %esi, %esi
movl $0xa, %edx
callq 0x26720
movq %rax, %rbx
xorl %edi, %edi
movq %rax, %rsi
callq 0x36178
movq %rax, %r15
testq %rbx, %rbx
je 0x5e33c
movl $0x1, %edx
movq %r15, %rdi
movq %rbx, %rsi
movq %rbp, %rcx
callq 0x26820
testq %rax, %rax
je 0x5e414
xorl %edi, %edi
movq %r15, %rsi
callq 0x40650
movq %rax, (%r12,%r13,8)
movl $0x2, %esi
movl $0x1, %edx
movq %r14, %r15
movq %r14, %rdi
movq %rbp, %rcx
callq 0x26820
testq %rax, %rax
je 0x5e40f
movq %rbp, %rdx
incq %r13
cmpq %r13, 0x38(%rsp)
jne 0x5e2d6
movq (%r12), %rax
movq 0x8(%rax), %rdi
callq 0x32d3c
testq %rax, %rax
je 0x5e666
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
callq *0x8(%rax)
cmpl $0x0, 0x1c0(%rbx)
movabsq $0x1cac083126e978d5, %r13 # imm = 0x1CAC083126E978D5
jne 0x5e5e1
movq 0x68(%rbx), %rax
cmpq $0x0, 0x28(%rax)
jne 0x5e5e1
testb $0x10, 0x98(%rbx)
leaq 0xa34d7(%rip), %r15     # 0x1018a8
jne 0x5e644
movq %rbx, %rdi
callq 0x5e0f1
cmpl $0x0, 0x6f4(%r15)
je 0x5e3f8
movq 0x10(%rsp), %rdi
callq 0x26bd0
movq %rax, 0x20(%rsp)
movabsq $0x4189374bc6a7ef, %rcx # imm = 0x4189374BC6A7EF
movq %r14, %r15
movq 0x28(%rsp), %r14
jmp 0x5e232
incl %r13d
jmp 0x5e41c
movq %r15, %rdi
callq 0x36330
movq 0x8(%rsp), %rdi
movl %r13d, 0x38(%rdi)
callq 0x5e0f1
movq 0x10(%rsp), %rdi
callq 0x269f0
testl %eax, %eax
movq 0x8(%rsp), %rbx
leaq 0xa3466(%rip), %r15     # 0x1018a8
movl 0x1c(%rsp), %ebp
je 0x5e5ae
cmpl $0x0, 0x6f4(%r15)
je 0x5e618
leaq 0x6707f(%rip), %rsi     # 0xc54e0
movl $0x3, %edi
xorl %eax, %eax
callq 0x2fdad
leaq 0x670a3(%rip), %rsi     # 0xc5517
movl $0x3, %edi
movq 0x20(%rsp), %r12
movq %r12, %rdx
xorl %eax, %eax
callq 0x2fdad
cmpq $-0x1, %r12
movq 0x30(%rsp), %rdi
je 0x5e584
movq %r12, %rsi
callq 0x26150
cmpl $-0x1, %eax
je 0x5e599
movl 0x6b0(%r15), %edi
cmpl $-0x1, %edi
je 0x5e4ca
xorl %esi, %esi
movl $0x2, %edx
callq 0x26a60
cmpq $-0x1, %rax
je 0x5e5f6
leaq 0x670de(%rip), %rsi     # 0xc55af
movl $0x3, %edi
xorl %eax, %eax
callq 0x2fdad
movq 0x10(%rsp), %rdi
callq 0x26a10
movq %rbx, %rdi
callq 0x5e12a
movl %ebp, 0x668(%r15)
callq 0x4a357
callq 0x5e67f
movq 0x690(%r15), %rax
movq %rax, 0x688(%r15)
xorl %eax, %eax
addq $0x158, %rsp            # imm = 0x158
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbx, %rdi
callq 0x269f0
testl %eax, %eax
je 0x5e42a
movq 0x8(%rsp), %rbx
testb $0x8, 0x98(%rbx)
leaq 0xa3363(%rip), %r15     # 0x1018a8
movl 0x1c(%rsp), %ebp
jne 0x5e44c
jmp 0x5e4dd
cmpq $0x0, 0x8(%rsp)
je 0x5e563
movq 0x8(%rsp), %rdi
callq 0x5e12a
leaq 0x67175(%rip), %rsi     # 0xc56df
jmp 0x5e62e
callq 0x26880
movl (%rax), %edi
callq 0x26680
leaq 0x66e53(%rip), %rsi     # 0xc53d5
jmp 0x5e5d0
leaq 0x66fb6(%rip), %rsi     # 0xc5541
movl $0x3, %edi
xorl %eax, %eax
callq 0x2fdad
jmp 0x5e618
callq 0x26880
movl (%rax), %edi
callq 0x26680
leaq 0x66fba(%rip), %rsi     # 0xc5566
jmp 0x5e609
testq %rbx, %rbx
je 0x5e5bd
movq 0x8(%rsp), %rdi
callq 0x5e12a
callq 0x26880
movl (%rax), %edi
callq 0x26680
leaq 0x66edb(%rip), %rsi     # 0xc54ab
movl $0x3, %edi
movq %rax, %rdx
xorl %eax, %eax
callq 0x2fdad
jmp 0x5e63a
leaq 0x66e5b(%rip), %rdi     # 0xc5443
leaq 0x6698c(%rip), %rsi     # 0xc4f7b
movl $0x2cf, %edx            # imm = 0x2CF
jmp 0x5e657
callq 0x26880
movl (%rax), %edi
callq 0x26680
leaq 0x66f7f(%rip), %rsi     # 0xc5588
movl $0x3, %edi
movq %rax, %rdx
xorl %eax, %eax
callq 0x2fdad
testq %rbx, %rbx
je 0x5e627
movq 0x8(%rsp), %rdi
callq 0x5e12a
leaq 0x66fb9(%rip), %rsi     # 0xc55e7
movl $0x3, %edi
xorl %eax, %eax
callq 0x2fdad
movl $0x1, %edi
callq 0x262c0
leaq 0x66e36(%rip), %rdi     # 0xc5481
leaq 0x66929(%rip), %rsi     # 0xc4f7b
movl $0x2d1, %edx            # imm = 0x2D1
callq 0x61908
movl $0x1, %edi
callq 0x263c0
movq (%r12), %rax
movq 0x8(%rax), %rdx
leaq 0x66d9c(%rip), %rsi     # 0xc5411
movl $0x3, %edi
jmp 0x5e5d8