// File: /workspace/github_c_makefile_star2_O3/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 0x62a14
movl %esi, %edx
testl %esi, %esi
setne %al
testl %edi, %edi
sete %cl
leaq 0xa3e6e(%rip), %rsi     # 0x106858
movl $0xffffffff, 0x6c8(%rsi) # imm = 0xFFFFFFFF
orb %al, %cl
jne 0x62b50
leaq 0x662a3(%rip), %rsi     # 0xc8ca6
movl $0x3, %edi
xorl %eax, %eax
callq 0x33005
jmp 0x62d3f
callq 0x33203
movq %rax, %rbx
leaq 0x660d3(%rip), %rsi     # 0xc8af6
xorl %r14d, %r14d
movl $0x2, %edi
xorl %eax, %eax
callq 0x33005
leaq 0xa3e1f(%rip), %r12     # 0x106858
cmpq $0x0, 0xa08(%r12)
je 0x62a4c
callq 0x33226
movq %rax, %r14
movl 0x68c(%r12), %ecx
leaq 0x65f0d(%rip), %rdx     # 0xc8968
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 0x62b68
movl %eax, %ebp
movl %eax, %edi
callq 0x600bc
cmpq $-0x1, %rax
je 0x62b85
cmpq $0x0, 0xa08(%r12)
je 0x62ad2
callq 0x33226
subq %r14, %rax
movq 0xa08(%r12), %rcx
testq %rcx, %rcx
sete %dl
cmpq %rcx, %rax
setl %cl
orb %dl, %cl
jne 0x62ad2
leaq 0x660d5(%rip), %rdi     # 0xc8b9f
movq %rax, %rsi
callq 0x81af1
callq 0x5fe4e
movq %rax, %xmm1
punpckldq 0x62e4c(%rip), %xmm1 # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0x62e54(%rip), %xmm1    # 0xc5940
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
addsd %xmm1, %xmm0
mulsd 0x5ca48(%rip), %xmm0    # 0xbf548
leaq 0x660af(%rip), %rsi     # 0xc8bb6
movl $0x2, %edi
movb $0x1, %al
callq 0x33005
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
cmpl $-0x1, 0x6a0(%r12)
jne 0x62b3b
movq 0x660(%r12), %rdi
movl $0x800, %esi            # imm = 0x800
xorl %eax, %eax
callq 0x266a0
movl %eax, %r14d
cmpq $0x0, 0xa08(%r12)
je 0x62bae
callq 0x33226
movq %rax, %r15
jmp 0x62bb1
leaq 0x6617c(%rip), %rsi     # 0xc8cd3
movl $0x3, %edi
xorl %eax, %eax
callq 0x33005
jmp 0x62d3f
callq 0x26890
movl (%rax), %edi
callq 0x26690
leaq 0x65faa(%rip), %rsi     # 0xc8b25
movl $0x3, %edi
jmp 0x62d35
callq 0x26890
movl (%rax), %edi
callq 0x26690
leaq 0x65fc8(%rip), %rsi     # 0xc8b60
movl $0x3, %edi
movq %rax, %rdx
xorl %eax, %eax
callq 0x33005
movl %ebp, %edi
jmp 0x62c75
xorl %r15d, %r15d
movq 0x660(%r12), %rsi
movq %rsp, %rdi
callq 0x26cb0
cmpl $-0x1, %eax
je 0x62c31
cmpq $0x0, 0xa08(%r12)
je 0x62c00
callq 0x33226
subq %r15, %rax
movq 0xa08(%r12), %rcx
testq %rcx, %rcx
sete %dl
cmpq %rcx, %rax
setl %cl
orb %dl, %cl
jne 0x62c00
leaq 0x66044(%rip), %rdi     # 0xc8c3c
movq %rax, %rsi
callq 0x81af1
movl 0x6a0(%r12), %r15d
cmpl $-0x1, %r15d
je 0x62c7f
movl %ebp, 0x6a0(%r12)
movl 0x65c(%r12), %eax
cmpl $0x2, %eax
je 0x62c8b
cmpl $0x1, %eax
jne 0x62c9c
movl %ebp, %edi
callq 0x26580
jmp 0x62c9c
movq 0x660(%r12), %rbx
callq 0x26890
movl (%rax), %edi
callq 0x26690
leaq 0x65fb3(%rip), %rsi     # 0xc8bff
movq %rsp, %rdx
movl $0x3, %edi
movq %rbx, %rcx
movq %rax, %r8
xorl %eax, %eax
callq 0x33005
movl %ebp, %edi
callq 0x26180
cmpl $-0x1, %r14d
je 0x62d3f
movl %r14d, %edi
callq 0x26180
jmp 0x62d3f
movl %ebp, %edi
callq 0x26180
movl %r14d, %r15d
jmp 0x62cd7
movslq %ebp, %rsi
movl $0x1, %edi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x754c6
movl $0xffffffff, 0x6a4(%r12) # imm = 0xFFFFFFFF
callq 0x61678
movq 0x680(%r12), %rax
movq 0x698(%r12), %rdi
movq %rax, 0x678(%r12)
callq 0x39548
callq 0x394a5
movq %rax, 0x698(%r12)
movl $0x0, 0x6c8(%r12)
leaq 0x65f5d(%rip), %rsi     # 0xc8c47
movl $0x2, %edi
xorl %eax, %eax
callq 0x33005
cmpl $0x2, 0x658(%r12)
jne 0x62d0d
movl $0x1, 0x658(%r12)
cmpl $-0x1, %r15d
je 0x62d21
movslq %r15d, %rsi
xorl %edi, %edi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x754c6
callq 0x33203
subq %rbx, %rax
leaq 0x65f44(%rip), %rsi     # 0xc8c74
movl $0x1, %edi
movq %rax, %rdx
xorl %eax, %eax
callq 0x33005
callq 0x60837
callq 0x5fe1c
leaq 0xa3b08(%rip), %r14     # 0x106858
movl 0x68c(%r14), %ecx
leaq 0x65c0a(%rip), %rdx     # 0xc8968
movq %rsp, %rbx
movl $0x100, %esi            # imm = 0x100
movq %rbx, %rdi
xorl %eax, %eax
callq 0x26120
movq %rbx, %rdi
callq 0x263c0
movl $0xffffffff, 0x68c(%r14) # imm = 0xFFFFFFFF
xorl %edi, %edi
callq 0x26ce0
subq 0x6c0(%r14), %rax
movq %rax, 0x6b8(%r14)
movq $-0x1, 0x6c0(%r14)
cmpl $0x2, 0x658(%r14)
jne 0x62db8
movl $0x1, 0x688(%r14)
addq $0x100, %rsp            # imm = 0x100
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq