// File: /workspace/github_c_makefile_star2_O2/zhou-yuxin[P]redis-aof-no-cow/src/aof.c
// Function: feedAppendOnlyFile
void feedAppendOnlyFile(struct redisCommand *cmd, int dictid, robj **argv, int argc) {
#ifdef AOF_NO_COW
    sds select_buf = NULL;
#endif
    sds buf = sdsempty();
    robj *tmpargv[3];

    /* The DB this command was targeting is not the same as the last command
     * we appended. To issue a SELECT command is needed. */
    if (dictid != server.aof_selected_db) {
        char seldb[64];

        snprintf(seldb,sizeof(seldb),"%d",dictid);
        buf = sdscatprintf(buf,"*2\r\n$6\r\nSELECT\r\n$%lu\r\n%s\r\n",
            (unsigned long)strlen(seldb),seldb);
        server.aof_selected_db = dictid;
#ifdef AOF_NO_COW
        if(server.aof_child_pid != -1 && server.aof_no_cow.enable)
        {
            select_buf = buf;
            buf = sdsempty();
        }
#endif
    }

    if (cmd->proc == expireCommand || cmd->proc == pexpireCommand ||
        cmd->proc == expireatCommand) {
        /* Translate EXPIRE/PEXPIRE/EXPIREAT into PEXPIREAT */
        buf = catAppendOnlyExpireAtCommand(buf,cmd,argv[1],argv[2]);
    } else if (cmd->proc == setexCommand || cmd->proc == psetexCommand) {
        /* Translate SETEX/PSETEX to SET and PEXPIREAT */
        tmpargv[0] = createStringObject("SET",3);
        tmpargv[1] = argv[1];
        tmpargv[2] = argv[3];
        buf = catAppendOnlyGenericCommand(buf,3,tmpargv);
        decrRefCount(tmpargv[0]);
        buf = catAppendOnlyExpireAtCommand(buf,cmd,argv[1],argv[2]);
    } else {
        /* All the other commands don't need translation or need the
         * same translation already operated in the command vector
         * for the replication itself. */
        buf = catAppendOnlyGenericCommand(buf,argc,argv);
    }

    /* Append to the AOF buffer. This will be flushed on disk just before
     * of re-entering the event loop, so before the client will get a
     * positive reply about the operation performed. */
    if (server.aof_state == AOF_ON)
#ifndef AOF_NO_COW
        server.aof_buf = sdscatlen(server.aof_buf,buf,sdslen(buf));
#else
    {
        if(select_buf)
            server.aof_buf = sdscatlen(server.aof_buf, select_buf, sdslen(select_buf));
        server.aof_buf = sdscatlen(server.aof_buf, buf, sdslen(buf));
    }
#endif

    /* If a background append only file rewriting is in progress we want to
     * accumulate the differences between the child DB and the current one
     * in a buffer, so that when the child process will do its work we
     * can append the differences to the new append only file. */
    if (server.aof_child_pid != -1)
#ifndef AOF_NO_COW
        aofRewriteBufferAppend((unsigned char*)buf,sdslen(buf));
#else
    {
        if(server.aof_no_cow.enable)
        {
            if(!aofNoCowSelectDB(dictid))
                goto werr;
            int ret;
            PROTECTED_BY_SPINLOCK(ret, server.aof_no_cow.lock,
                rioWrite(&(server.aof_no_cow.pipe), buf, sdslen(buf)));
            if(!ret)
                goto werr;
        }
        else
        {
            if(select_buf)
                aofRewriteBufferAppend((unsigned char*)select_buf, sdslen(select_buf));
            aofRewriteBufferAppend((unsigned char*)buf, sdslen(buf));
        }
    }
#endif
    sdsfree(buf);

#ifdef AOF_NO_COW
    if(select_buf)
        sdsfree(select_buf);
    return;

werr:
    serverLog(LL_WARNING, "Error writing pipe in feedAppendOnlyFile()");
    server.aof_no_cow.error = 1;
#endif
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movl %ecx, %r13d
movq %rdx, %rbx
movl %esi, %r12d
movq %rdi, %r14
callq 0x310e8
movq %rax, %r15
leaq 0xaa34d(%rip), %rbp     # 0xfe858
cmpl %r12d, 0x6a4(%rbp)
je 0x54564
leaq 0x65791(%rip), %rdx     # 0xb9cac
movl %r13d, %ebp
movq %rsp, %r13
pushq $0x40
popq %rsi
movq %r13, %rdi
movl %r12d, %ecx
xorl %eax, %eax
callq 0x26120
movq %r13, %rdi
callq 0x26510
leaq 0x64131(%rip), %rsi     # 0xb8671
movq %r15, %rdi
movq %rax, %rdx
movq %r13, %rcx
movl %ebp, %r13d
leaq 0xaa305(%rip), %rbp     # 0xfe858
xorl %eax, %eax
callq 0x319d4
movq %rax, %r15
movl %r12d, 0x6a4(%rbp)
movq 0x8(%r14), %rax
cmpq 0xa5a69(%rip), %rax     # 0xf9fd8
je 0x545d9
cmpq 0xa59b0(%rip), %rax     # 0xf9f28
je 0x545d9
cmpq 0xa5a37(%rip), %rax     # 0xf9fb8
je 0x545d9
cmpq 0xa5a5e(%rip), %rax     # 0xf9fe8
setne %cl
cmpq 0xa59fc(%rip), %rax     # 0xf9f90
setne %al
testb %al, %cl
jne 0x545ee
leaq 0x6ba1e(%rip), %rdi     # 0xbffc0
pushq $0x3
popq %rsi
callq 0x39dcd
movq %rax, %r12
movq %rsp, %rdx
movq %rax, (%rdx)
movq 0x8(%rbx), %rax
movq %rax, 0x8(%rdx)
movq 0x18(%rbx), %rax
movq %rax, 0x10(%rdx)
pushq $0x3
popq %rsi
movq %r15, %rdi
callq 0x542f3
movq %rax, %r15
movq %r12, %rdi
callq 0x3a209
movq 0x8(%rbx), %rdx
movq 0x10(%rbx), %rcx
movq %r15, %rdi
movq %r14, %rsi
callq 0x543f3
jmp 0x545fc
movq %r15, %rdi
movl %r13d, %esi
movq %rbx, %rdx
callq 0x542f3
movq %rax, %rbx
cmpl $0x1, 0x658(%rbp)
jne 0x5462c
movq 0x698(%rbp), %r14
movq %rbx, %rdi
callq 0x542bb
movq %r14, %rdi
movq %rbx, %rsi
movq %rax, %rdx
callq 0x316f4
movq %rax, 0x698(%rbp)
cmpl $-0x1, 0x68c(%rbp)
je 0x54648
movq %rbx, %rdi
callq 0x542bb
movq %rbx, %rdi
movq %rax, %rsi
callq 0x537cf
movq %rbx, %rdi
callq 0x31164
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq