// File: /workspace/github_c_makefile_star2_O2/zhou-yuxin[P]redis-aof-no-cow/src/aof.c
// Function: backgroundRewriteDoneHandler
void backgroundRewriteDoneHandler(int exitcode, int bysignal) {
    if (!bysignal && exitcode == 0) {
        int newfd, oldfd;
        char tmpfile[256];
        long long now = ustime();
        mstime_t latency;

        serverLog(LL_NOTICE,
            "Background AOF rewrite terminated with success");

        /* Flush the differences accumulated by the parent to the
         * rewritten AOF. */
        latencyStartMonitor(latency);
        snprintf(tmpfile,256,"temp-rewriteaof-bg-%d.aof",
            (int)server.aof_child_pid);

#ifndef AOF_NO_COW
        newfd = open(tmpfile,O_WRONLY|O_APPEND);
        if (newfd == -1) {
            serverLog(LL_WARNING,
                "Unable to open the temporary AOF produced by the child: %s", strerror(errno));
            goto cleanup;
        }

        if (aofRewriteBufferWrite(newfd) == -1) {
            serverLog(LL_WARNING,
                "Error trying to flush the parent diff to the rewritten AOF: %s", strerror(errno));
            close(newfd);
            goto cleanup;
        }
        latencyEndMonitor(latency);
        latencyAddSampleIfNeeded("aof-rewrite-diff-write",latency);

        serverLog(LL_NOTICE,
            "Residual parent diff successfully flushed to the rewritten AOF (%.2f MB)", (double) aofRewriteBufferSize() / (1024*1024));
#else
        if(server.aof_no_cow.enable)
        {
            newfd = server.aof_no_cow.fd;
            size_t total_len = 0;
            rioPipeBlock* block;
            while((block = rioPopPipeBlock(&(server.aof_no_cow.pipe))))
            {
                total_len += block->len;
                int ret = write(newfd, block->buffer, block->len) == (ssize_t)block->len;
                zfree(block);
                if(!ret)
                {
                    serverLog(LL_WARNING,
                        "Error trying to write the rest blocks to the rewritten AOF: %s", strerror(errno));
                    close(newfd);
                    goto cleanup;
                }
            }
            latencyEndMonitor(latency);
            latencyAddSampleIfNeeded("aof-rewrite-rest-blocks-write", latency);

            serverLog(LL_NOTICE,
                "Residual rest blocks successfully flushed to the rewritten AOF (%.2f MB)", 
                (double) total_len / (1024*1024));
        }
        else
        {
            newfd = open(tmpfile,O_WRONLY|O_APPEND);
            if (newfd == -1) {
                serverLog(LL_WARNING,
                    "Unable to open the temporary AOF produced by the child: %s", strerror(errno));
                goto cleanup;
            }

            if (aofRewriteBufferWrite(newfd) == -1) {
                serverLog(LL_WARNING,
                    "Error trying to flush the parent diff to the rewritten AOF: %s", strerror(errno));
                close(newfd);
                goto cleanup;
            }
            latencyEndMonitor(latency);
            latencyAddSampleIfNeeded("aof-rewrite-diff-write",latency);

            serverLog(LL_NOTICE,
                "Residual parent diff successfully flushed to the rewritten AOF (%.2f MB)", 
                (double) aofRewriteBufferSize() / (1024*1024));
        }
#endif

        /* The only remaining thing to do is to rename the temporary file to
         * the configured file and switch the file descriptor used to do AOF
         * writes. We don't want close(2) or rename(2) calls to block the
         * server on old file deletion.
         *
         * There are two possible scenarios:
         *
         * 1) AOF is DISABLED and this was a one time rewrite. The temporary
         * file will be renamed to the configured file. When this file already
         * exists, it will be unlinked, which may block the server.
         *
         * 2) AOF is ENABLED and the rewritten AOF will immediately start
         * receiving writes. After the temporary file is renamed to the
         * configured file, the original AOF file descriptor will be closed.
         * Since this will be the last reference to that file, closing it
         * causes the underlying file to be unlinked, which may block the
         * server.
         *
         * To mitigate the blocking effect of the unlink operation (either
         * caused by rename(2) in scenario 1, or by close(2) in scenario 2), we
         * use a background thread to take care of this. First, we
         * make scenario 1 identical to scenario 2 by opening the target file
         * when it exists. The unlink operation after the rename(2) will then
         * be executed upon calling close(2) for its descriptor. Everything to
         * guarantee atomicity for this switch has already happened by then, so
         * we don't care what the outcome or duration of that close operation
         * is, as long as the file descriptor is released again. */
        if (server.aof_fd == -1) {
            /* AOF disabled */

             /* Don't care if this fails: oldfd will be -1 and we handle that.
              * One notable case of -1 return is if the old file does
              * not exist. */
             oldfd = open(server.aof_filename,O_RDONLY|O_NONBLOCK);
        } else {
            /* AOF enabled */
            oldfd = -1; /* We'll set this to the current AOF filedes later. */
        }

        /* Rename the temporary file. This will not unlink the target file if
         * it exists, because we reference it with "oldfd". */
        latencyStartMonitor(latency);
        if (rename(tmpfile,server.aof_filename) == -1) {
            serverLog(LL_WARNING,
                "Error trying to rename the temporary AOF file %s into %s: %s",
                tmpfile,
                server.aof_filename,
                strerror(errno));
            close(newfd);
            if (oldfd != -1) close(oldfd);
            goto cleanup;
        }
        latencyEndMonitor(latency);
        latencyAddSampleIfNeeded("aof-rename",latency);

        if (server.aof_fd == -1) {
            /* AOF disabled, we don't need to set the AOF file descriptor
             * to this new file, so we can close it. */
            close(newfd);
        } else {
            /* AOF enabled, replace the old fd with the new one. */
            oldfd = server.aof_fd;
            server.aof_fd = newfd;
            if (server.aof_fsync == AOF_FSYNC_ALWAYS)
                aof_fsync(newfd);
            else if (server.aof_fsync == AOF_FSYNC_EVERYSEC)
                aof_background_fsync(newfd);
            server.aof_selected_db = -1; /* Make sure SELECT is re-issued */
            aofUpdateCurrentSize();
            server.aof_rewrite_base_size = server.aof_current_size;

            /* Clear regular AOF buffer since its contents was just written to
             * the new AOF from the background rewrite buffer. */
            sdsfree(server.aof_buf);
            server.aof_buf = sdsempty();
        }

        server.aof_lastbgrewrite_status = C_OK;

        serverLog(LL_NOTICE, "Background AOF rewrite finished successfully");
        /* Change state from WAIT_REWRITE to ON if needed */
        if (server.aof_state == AOF_WAIT_REWRITE)
            server.aof_state = AOF_ON;

        /* Asynchronously close the overwritten AOF. */
        if (oldfd != -1) bioCreateBackgroundJob(BIO_CLOSE_FILE,(void*)(long)oldfd,NULL,NULL);

        serverLog(LL_VERBOSE,
            "Background AOF rewrite signal handler took %lldus", ustime()-now);
    } else if (!bysignal && exitcode != 0) {
        /* SIGUSR1 is whitelisted, so we have a way to kill a child without
         * tirggering an error conditon. */
        if (bysignal != SIGUSR1)
            server.aof_lastbgrewrite_status = C_ERR;
        serverLog(LL_WARNING,
            "Background AOF rewrite terminated with error");
    } else {
        server.aof_lastbgrewrite_status = C_ERR;

        serverLog(LL_WARNING,
            "Background AOF rewrite terminated by signal %d", bysignal);
    }

cleanup:
#ifndef AOF_NO_COW
    aofClosePipes();
    aofRewriteBufferReset();
#else
    if(server.aof_no_cow.enable)
    {
        zfree(server.aof_no_cow.stack);
        rioCloseWithPipe(&(server.aof_no_cow.pipe));
        if(server.aof_no_cow.iter)
            dictReleaseBucketIterator(server.aof_no_cow.iter);
    }
    else
    {
        aofClosePipes();
        aofRewriteBufferReset();
    }
#endif
    aofRemoveTempFile(server.aof_child_pid);
    server.aof_child_pid = -1;
    server.aof_rewrite_time_last = time(NULL)-server.aof_rewrite_time_start;
    server.aof_rewrite_time_start = -1;
    /* Schedule a new rewrite if we are waiting for it to switch the AOF ON. */
    if (server.aof_state == AOF_WAIT_REWRITE)
        server.aof_rewrite_scheduled = 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x100, %rsp            # imm = 0x100
movl %esi, %eax
orl %edi, %eax
je 0x55d5e
movl %esi, %edx
testl %esi, %esi
setne %al
testl %edi, %edi
sete %cl
leaq 0xa8b1f(%rip), %rsi     # 0xfe858
orl $-0x1, 0x6c8(%rsi)
orb %al, %cl
jne 0x55e6a
leaq 0x63253(%rip), %rsi     # 0xb8fa2
pushq $0x3
popq %rdi
xorl %eax, %eax
callq 0x2b6a1
jmp 0x56068
callq 0x2b893
movq %rax, %rbx
leaq 0x63085(%rip), %rsi     # 0xb8df2
xorl %r14d, %r14d
pushq $0x2
popq %rdi
xorl %eax, %eax
callq 0x2b6a1
leaq 0xa8ad7(%rip), %r12     # 0xfe858
cmpq $0x0, 0xa08(%r12)
je 0x55d94
callq 0x2b8b6
movq %rax, %r14
movl 0x68c(%r12), %ecx
leaq 0x62ec1(%rip), %rdx     # 0xb8c64
movq %rsp, %r15
movl $0x100, %esi            # imm = 0x100
movq %r15, %rdi
xorl %eax, %eax
callq 0x26120
movq %r15, %rdi
movl $0x401, %esi            # imm = 0x401
xorl %eax, %eax
callq 0x266a0
cmpl $-0x1, %eax
je 0x55e80
movl %eax, %ebp
movl %eax, %edi
callq 0x53924
cmpq $-0x1, %rax
je 0x55e9a
cmpq $0x0, 0xa08(%r12)
je 0x55e1a
callq 0x2b8b6
subq %r14, %rax
movq 0xa08(%r12), %rcx
testq %rcx, %rcx
sete %dl
cmpq %rcx, %rax
setl %cl
orb %dl, %cl
jne 0x55e1a
leaq 0x63089(%rip), %rdi     # 0xb8e9b
movq %rax, %rsi
callq 0x71c25
callq 0x536c6
movq %rax, %xmm1
punpckldq 0x601d4(%rip), %xmm1 # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0x601dc(%rip), %xmm1    # 0xb6010
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
addsd %xmm1, %xmm0
mulsd 0x5a5f0(%rip), %xmm0    # 0xb0438
leaq 0x63063(%rip), %rsi     # 0xb8eb2
pushq $0x2
popq %rdi
movb $0x1, %al
callq 0x2b6a1
cmpl $-0x1, 0x6a0(%r12)
je 0x55ec1
pushq $-0x1
popq %r14
jmp 0x55ed8
leaq 0x6315e(%rip), %rsi     # 0xb8fcf
pushq $0x3
popq %rdi
xorl %eax, %eax
callq 0x2b6a1
jmp 0x56068
callq 0x26890
movl (%rax), %edi
callq 0x26690
leaq 0x62f8e(%rip), %rsi     # 0xb8e21
pushq $0x3
jmp 0x5605d
callq 0x26890
movl (%rax), %edi
callq 0x26690
leaq 0x62faf(%rip), %rsi     # 0xb8e5c
pushq $0x3
popq %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x2b6a1
movl %ebp, %edi
jmp 0x55fb2
movq 0x660(%r12), %rdi
movl $0x800, %esi            # imm = 0x800
xorl %eax, %eax
callq 0x266a0
movl %eax, %r14d
cmpq $0x0, 0xa08(%r12)
je 0x55eed
callq 0x2b8b6
movq %rax, %r15
jmp 0x55ef0
xorl %r15d, %r15d
movq 0x660(%r12), %rsi
movq %rsp, %rdi
callq 0x26cb0
cmpl $-0x1, %eax
je 0x55f70
cmpq $0x0, 0xa08(%r12)
je 0x55f3f
callq 0x2b8b6
subq %r15, %rax
movq 0xa08(%r12), %rcx
testq %rcx, %rcx
sete %dl
cmpq %rcx, %rax
setl %cl
orb %dl, %cl
jne 0x55f3f
leaq 0x63001(%rip), %rdi     # 0xb8f38
movq %rax, %rsi
callq 0x71c25
movl 0x6a0(%r12), %r15d
cmpl $-0x1, %r15d
je 0x55fbc
movl %ebp, 0x6a0(%r12)
movl 0x65c(%r12), %eax
cmpl $0x2, %eax
je 0x55fc8
cmpl $0x1, %eax
jne 0x55fcf
movl %ebp, %edi
callq 0x26580
jmp 0x55fcf
movq 0x660(%r12), %rbx
callq 0x26890
movl (%rax), %edi
callq 0x26690
leaq 0x62f70(%rip), %rsi     # 0xb8efb
pushq $0x3
popq %rdi
movq %rsp, %rdx
movq %rbx, %rcx
movq %rax, %r8
xorl %eax, %eax
callq 0x2b6a1
movl %ebp, %edi
callq 0x26180
cmpl $-0x1, %r14d
je 0x56068
movl %r14d, %edi
callq 0x26180
jmp 0x56068
movl %ebp, %edi
callq 0x26180
movl %r14d, %r15d
jmp 0x56007
movl %ebp, %edi
callq 0x5399c
orl $-0x1, 0x6a4(%r12)
callq 0x54c23
movq 0x680(%r12), %rax
movq 0x698(%r12), %rdi
movq %rax, 0x678(%r12)
callq 0x31164
callq 0x310e8
movq %rax, 0x698(%r12)
andl $0x0, 0x6c8(%r12)
leaq 0x62f2c(%rip), %rsi     # 0xb8f43
pushq $0x2
popq %rdi
xorl %eax, %eax
callq 0x2b6a1
cmpl $0x2, 0x658(%r12)
jne 0x56038
movl $0x1, 0x658(%r12)
cmpl $-0x1, %r15d
je 0x5604c
movslq %r15d, %rsi
xorl %edi, %edi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x666f1
callq 0x2b893
subq %rbx, %rax
leaq 0x62f15(%rip), %rsi     # 0xb8f70
pushq $0x1
popq %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x2b6a1
callq 0x53f57
callq 0x53694
leaq 0xa87df(%rip), %rbx     # 0xfe858
movl 0x68c(%rbx), %edi
callq 0x53f23
orl $-0x1, 0x68c(%rbx)
xorl %edi, %edi
callq 0x26ce0
subq 0x6c0(%rbx), %rax
movq %rax, 0x6b8(%rbx)
orq $-0x1, 0x6c0(%rbx)
cmpl $0x2, 0x658(%rbx)
jne 0x560bb
movl $0x1, 0x688(%rbx)
addq $0x100, %rsp            # imm = 0x100
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
nop