// File: /workspace/github_c_makefile_star2_O1/zhou-yuxin[P]redis-aof-no-cow/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 0x60d94(%rip), %rsi     # 0xc29f5
callq 0x26af0
testq %rax, %rax
je 0x620c7
movq %rax, %r14
leaq 0xa4bdf(%rip), %rbx     # 0x106858
movl 0x658(%rbx), %ebp
movq %rax, %rdi
callq 0x26070
leaq 0xb8(%rsp), %rsi
movl %eax, %edi
callq 0x266f0
cmpl $-0x1, %eax
je 0x61cc3
cmpq $0x0, 0xe8(%rsp)
jne 0x61cc3
movq $0x0, 0x680(%rbx)
movq %r14, %rdi
callq 0x26a20
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x6208b
movq %r15, 0x20(%rsp)
movl %ebp, 0x14(%rsp)
movl $0x0, 0x658(%rbx)
callq 0x61b29
movq %rax, %r15
movq %r14, %rdi
callq 0x4d949
movq $0x0, 0x18(%rsp)
movabsq $0x1cac083126e978d5, %r12 # imm = 0x1CAC083126E978D5
leaq 0x30(%rsp), %rbx
xorl %r13d, %r13d
movq %r15, 0x8(%rsp)
movq %r13, %rax
imulq %r12, %rax
rorq $0x3, %rax
movabsq $0x4189374bc6a7ef, %rcx # imm = 0x4189374BC6A7EF
cmpq %rcx, %rax
ja 0x61d35
movq %r14, %rdi
callq 0x26be0
movq %rax, %rdi
callq 0x4d9b2
callq 0x42e34
movq %rbx, %rdi
movl $0x80, %esi
movq %r14, %rdx
callq 0x26450
testq %rax, %rax
je 0x61ecb
cmpb $0x2a, 0x30(%rsp)
jne 0x61ee0
movl $0x4, %ecx
cmpb $0x0, 0x31(%rsp)
je 0x61ee5
leaq 0x31(%rsp), %rdi
callq 0x26770
testl %eax, %eax
jle 0x61ef2
movl %eax, %ebx
movl %eax, %eax
movq %rax, 0x28(%rsp)
leaq (,%rax,8), %rdi
callq 0x3b29d
movq %rax, %rbp
movl %ebx, 0x38(%r15)
movq %rax, 0x40(%r15)
xorl %r12d, %r12d
leaq 0x30(%rsp), %rbx
movq %rbx, %rdi
movl $0x80, %esi
movq %r14, %rdx
callq 0x26450
testq %rax, %rax
je 0x61f17
cmpb $0x24, 0x30(%rsp)
jne 0x61efe
leaq 0x31(%rsp), %rdi
xorl %esi, %esi
movl $0xa, %edx
callq 0x26730
movq %rax, %rbx
xorl %edi, %edi
movq %rax, %rsi
callq 0x3910c
movq %rax, %r15
testq %rbx, %rbx
je 0x61e0a
movl $0x1, %edx
movq %r15, %rdi
movq %rbx, %rsi
movq %r14, %rcx
callq 0x26830
testq %rax, %rax
je 0x61f34
xorl %edi, %edi
movq %r15, %rsi
callq 0x43ab4
movq %rax, (%rbp,%r12,8)
movl $0x2, %esi
movl $0x1, %edx
leaq 0x30(%rsp), %rbx
movq %rbx, %rdi
movq %r14, %rcx
callq 0x26830
testq %rax, %rax
je 0x61f14
incq %r12
cmpq %r12, 0x28(%rsp)
jne 0x61da4
movq (%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x35dc2
testq %rax, %rax
je 0x62187
movq 0x8(%rsp), %r15
movq %r15, %rdi
callq *0x8(%rax)
cmpl $0x0, 0x1c0(%r15)
movabsq $0x1cac083126e978d5, %r12 # imm = 0x1CAC083126E978D5
jne 0x620eb
movq 0x68(%r15), %rax
cmpq $0x0, 0x28(%rax)
jne 0x620eb
testb $0x10, 0x98(%r15)
jne 0x62165
movq %r15, %rdi
callq 0x61bda
xorl %ecx, %ecx
leaq 0xa49a7(%rip), %rax     # 0x106858
cmpl $0x0, 0x6e4(%rax)
je 0x61ee5
movq %r14, %rdi
callq 0x26be0
xorl %ecx, %ecx
movq %rax, 0x18(%rsp)
jmp 0x61ee5
movq %r14, %rdi
callq 0x26a00
cmpl $0x1, %eax
movl $0x0, %ecx
adcl $0x3, %ecx
jmp 0x61ee5
movl $0x5, %ecx
incq %r13
testl %ecx, %ecx
je 0x61d06
jmp 0x61f63
movl $0x5, %ecx
leaq 0x30(%rsp), %rbx
jmp 0x61ee5
movl $0x5, %ecx
movq 0x8(%rsp), %r15
movabsq $0x1cac083126e978d5, %r12 # imm = 0x1CAC083126E978D5
jmp 0x61ee5
incl %r12d
movq 0x8(%rsp), %r15
movl %r12d, 0x38(%r15)
movq %r15, %rdi
callq 0x61bda
movabsq $0x1cac083126e978d5, %r12 # imm = 0x1CAC083126E978D5
jmp 0x61f5c
movq %r15, %rdi
callq 0x392b6
movq 0x8(%rsp), %r15
movl %r12d, 0x38(%r15)
movq %r15, %rdi
callq 0x61bda
movabsq $0x1cac083126e978d5, %r12 # imm = 0x1CAC083126E978D5
leaq 0x30(%rsp), %rbx
movl $0x4, %ecx
jmp 0x61ee5
cmpl $0x3, %ecx
je 0x61fb3
cmpl $0x4, %ecx
je 0x61f7b
cmpl $0x5, %ecx
je 0x6209d
jmp 0x6208b
movq %r14, %rdi
callq 0x26a00
testl %eax, %eax
leaq 0xa48cc(%rip), %rbx     # 0x106858
jne 0x61fc8
testq %r15, %r15
je 0x61f9b
movq %r15, %rdi
callq 0x61c13
callq 0x26890
movl (%rax), %edi
callq 0x26690
leaq 0x6647c(%rip), %rsi     # 0xc842a
jmp 0x620da
testb $0x8, 0x98(%r15)
leaq 0xa4896(%rip), %rbx     # 0x106858
je 0x62057
cmpl $0x0, 0x6e4(%rbx)
je 0x6214c
leaq 0x66483(%rip), %rsi     # 0xc845f
movl $0x3, %edi
xorl %eax, %eax
callq 0x32ea6
leaq 0x664a7(%rip), %rsi     # 0xc8496
movl $0x3, %edi
movq 0x18(%rsp), %r12
movq %r12, %rdx
xorl %eax, %eax
callq 0x32ea6
cmpq $-0x1, %r12
je 0x62100
movq 0x20(%rsp), %rdi
movq %r12, %rsi
callq 0x26150
cmpl $-0x1, %eax
je 0x62115
movl 0x6a0(%rbx), %edi
cmpl $-0x1, %edi
je 0x62044
xorl %esi, %esi
movl $0x2, %edx
callq 0x26a70
cmpq $-0x1, %rax
je 0x6212a
leaq 0x664e3(%rip), %rsi     # 0xc852e
movl $0x3, %edi
xorl %eax, %eax
callq 0x32ea6
movq %r14, %rdi
callq 0x26a20
movq %r15, %rdi
callq 0x61c13
movl 0x14(%rsp), %eax
movl %eax, 0x658(%rbx)
callq 0x4d9e9
callq 0x621a0
movq 0x680(%rbx), %rax
movq %rax, 0x678(%rbx)
xorl %eax, %eax
addq $0x148, %rsp            # imm = 0x148
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
testq %r15, %r15
je 0x620aa
movq %r15, %rdi
callq 0x61c13
leaq 0x665ad(%rip), %rsi     # 0xc865e
movl $0x3, %edi
xorl %eax, %eax
callq 0x32ea6
movl $0x1, %edi
callq 0x262d0
callq 0x26890
movl (%rax), %edi
callq 0x26690
leaq 0x6627a(%rip), %rsi     # 0xc8354
movl $0x3, %edi
movq %rax, %rdx
xorl %eax, %eax
callq 0x32ea6
jmp 0x620bd
leaq 0x662d0(%rip), %rdi     # 0xc83c2
leaq 0x65e16(%rip), %rsi     # 0xc7f0f
movl $0x2f8, %edx            # imm = 0x2F8
jmp 0x62178
leaq 0x663b9(%rip), %rsi     # 0xc84c0
movl $0x3, %edi
xorl %eax, %eax
callq 0x32ea6
jmp 0x6214c
callq 0x26890
movl (%rax), %edi
callq 0x26690
leaq 0x663bd(%rip), %rsi     # 0xc84e5
jmp 0x6213d
callq 0x26890
movl (%rax), %edi
callq 0x26690
leaq 0x663ca(%rip), %rsi     # 0xc8507
movl $0x3, %edi
movq %rax, %rdx
xorl %eax, %eax
callq 0x32ea6
testq %r15, %r15
je 0x62159
movq %r15, %rdi
callq 0x61c13
leaq 0x66406(%rip), %rsi     # 0xc8566
jmp 0x620b1
leaq 0x66294(%rip), %rdi     # 0xc8400
leaq 0x65d9c(%rip), %rsi     # 0xc7f0f
movl $0x2fa, %edx            # imm = 0x2FA
callq 0x654e1
movl $0x1, %edi
callq 0x263d0
movq (%rbp), %rax
movq 0x8(%rax), %rdx
leaq 0x661fa(%rip), %rsi     # 0xc8390
movl $0x3, %edi
jmp 0x620e2