// File: /workspace/github_c_makefile_star2_O1/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 0x63600
movl %esi, %edx
testl %esi, %esi
setne %al
testl %edi, %edi
sete %cl
leaq 0xa3282(%rip), %rsi     # 0x106858
movl $0xffffffff, 0x6c8(%rsi) # imm = 0xFFFFFFFF
orb %al, %cl
jne 0x6373f
leaq 0x6567b(%rip), %rsi     # 0xc8c6a
movl $0x3, %edi
xorl %eax, %eax
callq 0x32ea6
jmp 0x63931
callq 0x330a5
movq %rax, %rbx
leaq 0x654ab(%rip), %rsi     # 0xc8aba
xorl %r14d, %r14d
movl $0x2, %edi
xorl %eax, %eax
callq 0x32ea6
leaq 0xa3233(%rip), %r12     # 0x106858
cmpq $0x0, 0xa08(%r12)
je 0x63638
callq 0x330c8
movq %rax, %r14
movl 0x68c(%r12), %ecx
leaq 0x652e5(%rip), %rdx     # 0xc892c
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 0x63757
movl %eax, %ebp
movl %eax, %edi
callq 0x60bad
cmpq $-0x1, %rax
je 0x63774
cmpq $0x0, 0xa08(%r12)
je 0x6369a
callq 0x330c8
subq %r14, %rax
movq %rax, %r14
movq 0xa08(%r12), %rax
testq %rax, %rax
sete %cl
cmpq %rax, %r14
setl %al
orb %cl, %al
jne 0x636c1
leaq 0x654aa(%rip), %rdi     # 0xc8b63
movq %r14, %rsi
callq 0x825dd
callq 0x60942
movq %rax, %xmm1
punpckldq 0x6220d(%rip), %xmm1 # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0x62215(%rip), %xmm1    # 0xc58f0
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
addsd %xmm1, %xmm0
mulsd 0x5be09(%rip), %xmm0    # 0xbf4f8
leaq 0x65484(%rip), %rsi     # 0xc8b7a
movl $0x2, %edi
movb $0x1, %al
callq 0x32ea6
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
cmpl $-0x1, 0x6a0(%r12)
jne 0x6372a
movq 0x660(%r12), %rdi
movl $0x800, %esi            # imm = 0x800
xorl %eax, %eax
callq 0x266a0
movl %eax, %r14d
cmpq $0x0, 0xa08(%r12)
je 0x6379d
callq 0x330c8
movq %rax, %r15
jmp 0x637a0
leaq 0x65551(%rip), %rsi     # 0xc8c97
movl $0x3, %edi
xorl %eax, %eax
callq 0x32ea6
jmp 0x63931
callq 0x26890
movl (%rax), %edi
callq 0x26690
leaq 0x6537f(%rip), %rsi     # 0xc8ae9
movl $0x3, %edi
jmp 0x63927
callq 0x26890
movl (%rax), %edi
callq 0x26690
leaq 0x6539d(%rip), %rsi     # 0xc8b24
movl $0x3, %edi
movq %rax, %rdx
xorl %eax, %eax
callq 0x32ea6
movl %ebp, %edi
jmp 0x63867
xorl %r15d, %r15d
movq 0x660(%r12), %rsi
movq %rsp, %rdi
callq 0x26cb0
cmpl $-0x1, %eax
je 0x63823
cmpq $0x0, 0xa08(%r12)
je 0x637cb
callq 0x330c8
subq %r15, %rax
movq %rax, %r15
movq 0xa08(%r12), %rax
testq %rax, %rax
sete %cl
cmpq %rax, %r15
setl %al
orb %cl, %al
jne 0x637f2
leaq 0x65416(%rip), %rdi     # 0xc8c00
movq %r15, %rsi
callq 0x825dd
movl 0x6a0(%r12), %r15d
cmpl $-0x1, %r15d
je 0x63871
movl %ebp, 0x6a0(%r12)
movl 0x65c(%r12), %eax
cmpl $0x2, %eax
je 0x6387d
cmpl $0x1, %eax
jne 0x6388e
movl %ebp, %edi
callq 0x26580
jmp 0x6388e
movq 0x660(%r12), %rbx
callq 0x26890
movl (%rax), %edi
callq 0x26690
leaq 0x65385(%rip), %rsi     # 0xc8bc3
movq %rsp, %rdx
movl $0x3, %edi
movq %rbx, %rcx
movq %rax, %r8
xorl %eax, %eax
callq 0x32ea6
movl %ebp, %edi
callq 0x26180
cmpl $-0x1, %r14d
je 0x63931
movl %r14d, %edi
callq 0x26180
jmp 0x63931
movl %ebp, %edi
callq 0x26180
movl %r14d, %r15d
jmp 0x638c9
movslq %ebp, %rsi
movl $0x1, %edi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x75ff6
movl $0xffffffff, 0x6a4(%r12) # imm = 0xFFFFFFFF
callq 0x621a0
movq 0x680(%r12), %rax
movq 0x698(%r12), %rdi
movq %rax, 0x678(%r12)
callq 0x392b6
callq 0x39222
movq %rax, 0x698(%r12)
movl $0x0, 0x6c8(%r12)
leaq 0x6532f(%rip), %rsi     # 0xc8c0b
movl $0x2, %edi
xorl %eax, %eax
callq 0x32ea6
cmpl $0x2, 0x658(%r12)
jne 0x638ff
movl $0x1, 0x658(%r12)
cmpl $-0x1, %r15d
je 0x63913
movslq %r15d, %rsi
xorl %edi, %edi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x75ff6
callq 0x330a5
subq %rbx, %rax
leaq 0x65316(%rip), %rsi     # 0xc8c38
movl $0x1, %edi
movq %rax, %rdx
xorl %eax, %eax
callq 0x32ea6
callq 0x61327
callq 0x60910
leaq 0xa2f16(%rip), %r14     # 0x106858
movl 0x68c(%r14), %ecx
leaq 0x64fdc(%rip), %rdx     # 0xc892c
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 0x639aa
movl $0x1, 0x688(%r14)
addq $0x100, %rsp            # imm = 0x100
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
nop