// File: /workspace/github_c_makefile_star2_O1/nilyang[P]redis-tdd-annotation/src/aof.c
// Function: feedAppendOnlyFile
void feedAppendOnlyFile(struct redisCommand *cmd, int dictid, robj **argv, int argc) {
    sds buf = sdsempty();
    robj *tmpargv[3];

    /* The DB this command was targeting is not the same as the last command
     * we appendend. To issue a SELECT command is needed. 
     *
     * 使用 SELECT 命令，显式设置数据库，确保之后的命令被设置到正确的数据库
     */
    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;
    }

    // EXPIRE 、 PEXPIRE 和 EXPIREAT 命令
    if (cmd->proc == expireCommand || cmd->proc == pexpireCommand ||
        cmd->proc == expireatCommand) {
        /* Translate EXPIRE/PEXPIRE/EXPIREAT into PEXPIREAT 
         *
         * 将 EXPIRE 、 PEXPIRE 和 EXPIREAT 都翻译成 PEXPIREAT
         */
        buf = catAppendOnlyExpireAtCommand(buf,cmd,argv[1],argv[2]);

    // SETEX 和 PSETEX 命令
    } else if (cmd->proc == setexCommand || cmd->proc == psetexCommand) {
        /* Translate SETEX/PSETEX to SET and PEXPIREAT 
         *
         * 将两个命令都翻译成 SET 和 PEXPIREAT
         */

        // SET
        tmpargv[0] = createStringObject("SET",3);
        tmpargv[1] = argv[1];
        tmpargv[2] = argv[3];
        buf = catAppendOnlyGenericCommand(buf,3,tmpargv);

        // PEXPIREAT
        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. 
     *
     * 将命令追加到 AOF 缓存中，
     * 在重新进入事件循环之前，这些命令会被冲洗到磁盘上，
     * 并向客户端返回一个回复。
     */
    if (server.aof_state == REDIS_AOF_ON)
        server.aof_buf = sdscatlen(server.aof_buf,buf,sdslen(buf));

    /* 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. 
     *
     * 如果 BGREWRITEAOF 正在进行，
     * 那么我们还需要将命令追加到重写缓存中，
     * 从而记录当前正在重写的 AOF 文件和数据库当前状态的差异。
     */
    if (server.aof_child_pid != -1)
        aofRewriteBufferAppend((unsigned char*)buf,sdslen(buf));

    // 释放
    sdsfree(buf);
}

// 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 0x281b2
movq %rax, %r15
leaq 0x80171(%rip), %rbp     # 0xc9fa8
cmpl %r12d, 0x51c(%rbp)
je 0x49e92
leaq 0x5162a(%rip), %rdx     # 0x9b471
movl %r13d, %ebp
movq %rsp, %r13
movl $0x40, %esi
movq %r13, %rdi
movl %r12d, %ecx
xorl %eax, %eax
callq 0x1f120
movq %r13, %rdi
callq 0x1f4f0
leaq 0x50acd(%rip), %rsi     # 0x9a93b
movq %r15, %rdi
movq %rax, %rdx
movq %r13, %rcx
movl %ebp, %r13d
leaq 0x80127(%rip), %rbp     # 0xc9fa8
xorl %eax, %eax
callq 0x28760
movq %rax, %r15
movl %r12d, 0x51c(%rbp)
movq 0x8(%r14), %rax
cmpq 0x7c13b(%rip), %rax     # 0xc5fd8
je 0x49f0b
cmpq 0x7c062(%rip), %rax     # 0xc5f08
je 0x49f0b
cmpq 0x7c101(%rip), %rax     # 0xc5fb0
je 0x49f0b
cmpq 0x7c130(%rip), %rax     # 0xc5fe8
setne %cl
cmpq 0x7c0be(%rip), %rax     # 0xc5f80
setne %al
testb %al, %cl
jne 0x49f20
leaq 0x55d1c(%rip), %rdi     # 0x9fbec
movl $0x3, %esi
callq 0x303b7
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)
movq %r15, %rdi
movl $0x3, %esi
callq 0x49c33
movq %rax, %r15
movq %r12, %rdi
callq 0x30834
movq 0x8(%rbx), %rdx
movq 0x10(%rbx), %rcx
movq %r15, %rdi
movq %r14, %rsi
callq 0x49d19
jmp 0x49f2e
movq %r15, %rdi
movl %r13d, %esi
movq %rbx, %rdx
callq 0x49c33
movq %rax, %rbx
cmpl $0x1, 0x4d0(%rbp)
jne 0x49f54
movq 0x510(%rbp), %rdi
movslq -0x8(%rbx), %rdx
movq %rbx, %rsi
callq 0x28380
movq %rax, 0x510(%rbp)
cmpl $-0x1, 0x504(%rbp)
je 0x49f69
movslq -0x8(%rbx), %rsi
movq %rbx, %rdi
callq 0x493d6
movq %rbx, %rdi
callq 0x28204
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq