// File: /workspace/github_c_makefile_star2_O2/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 $0x148, %rsp            # imm = 0x148
movq %rdi, %r15
leaq 0x5ccf7(%rip), %rsi     # 0xb1695
callq 0x26ae0
testq %rax, %rax
je 0x54d3b
movq %rax, %r14
leaq 0xa4ef2(%rip), %rbx     # 0xf98a8
movl 0x668(%rbx), %ebp
movq %rax, %rdi
callq 0x26070
leaq 0xb8(%rsp), %rsi
movl %eax, %edi
callq 0x266e0
cmpl $-0x1, %eax
je 0x549fb
cmpq $0x0, 0xe8(%rsp)
jne 0x549fb
andq $0x0, 0x690(%rbx)
movq %r14, %rdi
callq 0x26a10
pushq $-0x1
popq %rax
jmp 0x54ce2
andl $0x0, 0x668(%rbx)
callq 0x54877
movq %rax, %rbx
movq %r14, %rdi
callq 0x4248e
movq $0x0, 0x10(%rsp)
movl $0x3e8, %r12d           # imm = 0x3E8
leaq 0x30(%rsp), %r13
xorl %eax, %eax
movq %rbx, (%rsp)
movl %ebp, 0xc(%rsp)
movq %r15, 0x20(%rsp)
pushq $0x1
popq %rbp
movq %rax, 0x18(%rsp)
xorl %edx, %edx
divq %r12
testq %rdx, %rdx
jne 0x54a5c
movq %r14, %rdi
callq 0x26bd0
movq %rax, %rdi
callq 0x424f8
callq 0x39254
movq %r13, %rdi
movl $0x80, %esi
movq %r14, %rdx
callq 0x26440
testq %rax, %rax
je 0x54cf4
cmpb $0x2a, 0x30(%rsp)
jne 0x54d1f
cmpb $0x0, 0x31(%rsp)
je 0x54c03
leaq 0x31(%rsp), %rdi
callq 0x26760
testl %eax, %eax
jle 0x54d1f
movl %eax, %r12d
incq 0x18(%rsp)
movl %eax, %eax
movq %rax, 0x28(%rsp)
leaq (,%rax,8), %rdi
callq 0x327b9
movq (%rsp), %rcx
movl %r12d, 0x38(%rcx)
movq %rax, %r12
movq %rax, 0x40(%rcx)
xorl %ebx, %ebx
cmpq %rbx, 0x28(%rsp)
je 0x54b6c
movq %r13, %rdi
movl $0x80, %esi
movq %r14, %rdx
callq 0x26440
testq %rax, %rax
je 0x54bf7
cmpb $0x24, 0x30(%rsp)
jne 0x54d1f
leaq 0x31(%rsp), %rdi
xorl %esi, %esi
pushq $0xa
popq %rdx
callq 0x26720
movq %rax, %r13
xorl %edi, %edi
movq %rax, %rsi
callq 0x31018
movq %rax, %r15
testq %r13, %r13
je 0x54b37
movq %r15, %rdi
movq %r13, %rsi
movq %rbp, %rdx
movq %r14, %rcx
callq 0x26820
testq %rax, %rax
je 0x54bef
xorl %edi, %edi
movq %r15, %rsi
callq 0x39d58
movq %rax, (%r12,%rbx,8)
leaq 0x30(%rsp), %r13
movq %r13, %rdi
pushq $0x2
popq %rsi
movq %rbp, %rdx
movq %r14, %rcx
callq 0x26820
testq %rax, %rax
je 0x54beb
incq %rbx
jmp 0x54aca
movq (%r12), %rax
movq 0x8(%rax), %rdi
callq 0x2e0f6
testq %rax, %rax
je 0x54e24
movq (%rsp), %rbx
movq %rbx, %rdi
callq *0x8(%rax)
cmpl $0x0, 0x1c0(%rbx)
leaq 0xa4d0e(%rip), %r15     # 0xf98a8
movl $0x3e8, %r12d           # imm = 0x3E8
jne 0x54da8
movq 0x68(%rbx), %rax
cmpq $0x0, 0x28(%rax)
jne 0x54da8
testb $0x10, 0x98(%rbx)
jne 0x54e04
movq %rbx, %rdi
callq 0x5491f
cmpl $0x0, 0x6f4(%r15)
je 0x54be1
movq %r14, %rdi
callq 0x26bd0
movq %rax, 0x10(%rsp)
movq 0x18(%rsp), %rax
jmp 0x54a38
incl %ebx
jmp 0x54bf7
movq %r15, %rdi
callq 0x311c8
movq (%rsp), %rdi
movl %ebx, 0x38(%rdi)
callq 0x5491f
movq %r14, %rdi
callq 0x269f0
testl %eax, %eax
movq (%rsp), %r15
leaq 0xa4c90(%rip), %r12     # 0xf98a8
je 0x54d78
cmpl $0x0, 0x6f4(%r12)
je 0x54ddd
leaq 0x61b98(%rip), %rsi     # 0xb67cc
pushq $0x3
popq %rbx
movl %ebx, %edi
xorl %eax, %eax
callq 0x2b699
leaq 0x61bbc(%rip), %rsi     # 0xb6803
movl %ebx, %edi
movq 0x10(%rsp), %rbx
movq %rbx, %rdx
xorl %eax, %eax
callq 0x2b699
cmpq $-0x1, %rbx
je 0x54d50
movq 0x20(%rsp), %rdi
movq %rbx, %rsi
callq 0x26150
cmpl $-0x1, %eax
je 0x54d63
movl 0x6b0(%r12), %edi
cmpl $-0x1, %edi
je 0x54c99
pushq $0x2
popq %rdx
xorl %esi, %esi
callq 0x26a60
cmpq $-0x1, %rax
je 0x54dbd
leaq 0x61bfb(%rip), %rsi     # 0xb689b
pushq $0x3
popq %rdi
xorl %eax, %eax
callq 0x2b699
movq %r14, %rdi
callq 0x26a10
movq %r15, %rdi
callq 0x54950
movl 0xc(%rsp), %eax
movl %eax, 0x668(%r12)
callq 0x4252f
callq 0x54e3b
movq 0x690(%r12), %rax
movq %rax, 0x688(%r12)
xorl %eax, %eax
addq $0x148, %rsp            # imm = 0x148
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r14, %rdi
callq 0x269f0
testl %eax, %eax
je 0x54c03
movq (%rsp), %r15
testb $0x8, 0x98(%r15)
leaq 0xa4b91(%rip), %r12     # 0xf98a8
jne 0x54c1e
jmp 0x54caa
cmpq $0x0, (%rsp)
je 0x54d2f
movq (%rsp), %rdi
callq 0x54950
leaq 0x61c95(%rip), %rsi     # 0xb69cb
jmp 0x54df2
callq 0x26880
movl (%rax), %edi
callq 0x26680
leaq 0x61973(%rip), %rsi     # 0xb66c1
jmp 0x54d99
leaq 0x61ad6(%rip), %rsi     # 0xb682d
pushq $0x3
popq %rdi
xorl %eax, %eax
callq 0x2b699
jmp 0x54ddd
callq 0x26880
movl (%rax), %edi
callq 0x26680
leaq 0x61adc(%rip), %rsi     # 0xb6852
jmp 0x54dd0
testq %r15, %r15
je 0x54d86
movq (%rsp), %rdi
callq 0x54950
callq 0x26880
movl (%rax), %edi
callq 0x26680
leaq 0x619fe(%rip), %rsi     # 0xb6797
pushq $0x3
popq %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x2b699
jmp 0x54dfc
leaq 0x61980(%rip), %rdi     # 0xb672f
leaq 0x614b1(%rip), %rsi     # 0xb6267
movl $0x2cf, %edx            # imm = 0x2CF
jmp 0x54e17
callq 0x26880
movl (%rax), %edi
callq 0x26680
leaq 0x61aa4(%rip), %rsi     # 0xb6874
pushq $0x3
popq %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x2b699
testq %r15, %r15
je 0x54deb
movq (%rsp), %rdi
callq 0x54950
leaq 0x61ae1(%rip), %rsi     # 0xb68d3
pushq $0x3
popq %rdi
xorl %eax, %eax
callq 0x2b699
pushq $0x1
popq %rdi
callq 0x262c0
leaq 0x61962(%rip), %rdi     # 0xb676d
leaq 0x61455(%rip), %rsi     # 0xb6267
movl $0x2d1, %edx            # imm = 0x2D1
callq 0x57ac8
pushq $0x1
popq %rdi
callq 0x263c0
movq (%r12), %rax
movq 0x8(%rax), %rdx
leaq 0x618ca(%rip), %rsi     # 0xb66fd
pushq $0x3
popq %rdi
jmp 0x54d9f