// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x130, %rsp            # imm = 0x130
movl %edi, -0x4(%rbp)
movl %esi, -0x8(%rbp)
cmpl $0x0, -0x8(%rbp)
jne 0x743a8
cmpl $0x0, -0x4(%rbp)
jne 0x743a8
callq 0x2ff30
movq %rax, -0x118(%rbp)
movl $0x2, %edi
leaq 0xdef65(%rip), %rsi     # 0x152ef2
movb $0x0, %al
callq 0x2fc60
leaq 0x11a1fd(%rip), %rax    # 0x18e198
cmpq $0x0, 0xa08(%rax)
je 0x73fb3
callq 0x2ff70
movq %rax, -0x120(%rbp)
jmp 0x73fbe
movq $0x0, -0x120(%rbp)
leaq -0x110(%rbp), %rdi
leaq 0x11a1cc(%rip), %rax    # 0x18e198
movl 0x68c(%rax), %ecx
movl $0x100, %esi            # imm = 0x100
leaq 0xded86(%rip), %rdx     # 0x152d64
movb $0x0, %al
callq 0x26120
leaq -0x110(%rbp), %rdi
movl $0x401, %esi            # imm = 0x401
movb $0x0, %al
callq 0x266b0
movl %eax, -0xc(%rbp)
cmpl $-0x1, -0xc(%rbp)
jne 0x74028
callq 0x26890
movl (%rax), %edi
callq 0x266a0
movq %rax, %rdx
movl $0x3, %edi
leaq 0xdef05(%rip), %rsi     # 0x152f21
movb $0x0, %al
callq 0x2fc60
jmp 0x7440b
movl -0xc(%rbp), %edi
callq 0x70820
cmpq $-0x1, %rax
jne 0x74065
callq 0x26890
movl (%rax), %edi
callq 0x266a0
movq %rax, %rdx
movl $0x3, %edi
leaq 0xdef0b(%rip), %rsi     # 0x152f5c
movb $0x0, %al
callq 0x2fc60
movl -0xc(%rbp), %edi
callq 0x26180
jmp 0x7440b
leaq 0x11a12c(%rip), %rax    # 0x18e198
cmpq $0x0, 0xa08(%rax)
je 0x74089
callq 0x2ff70
subq -0x120(%rbp), %rax
movq %rax, -0x120(%rbp)
leaq 0x11a108(%rip), %rax    # 0x18e198
cmpq $0x0, 0xa08(%rax)
je 0x740c4
movq -0x120(%rbp), %rax
leaq 0x11a0f0(%rip), %rcx    # 0x18e198
cmpq 0xa08(%rcx), %rax
jl 0x740c4
movq -0x120(%rbp), %rsi
leaq 0xdeedc(%rip), %rdi     # 0x152f9b
callq 0xa0e80
callq 0x70480
movq %rax, %xmm0
movaps 0xd63cb(%rip), %xmm1    # 0x14a4a0
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movapd 0xd63cf(%rip), %xmm1    # 0x14a4b0
subpd %xmm1, %xmm0
movaps %xmm0, %xmm1
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
addsd %xmm1, %xmm0
movsd 0xd6390(%rip), %xmm1    # 0x14a488
divsd %xmm1, %xmm0
movl $0x2, %edi
leaq 0xdeeaa(%rip), %rsi     # 0x152fb2
movb $0x1, %al
callq 0x2fc60
leaq 0x11a082(%rip), %rax    # 0x18e198
cmpl $-0x1, 0x6a0(%rax)
jne 0x7413e
leaq 0x11a072(%rip), %rax    # 0x18e198
movq 0x660(%rax), %rdi
movl $0x800, %esi            # imm = 0x800
movb $0x0, %al
callq 0x266b0
movl %eax, -0x10(%rbp)
jmp 0x74145
movl $0xffffffff, -0x10(%rbp) # imm = 0xFFFFFFFF
leaq 0x11a04c(%rip), %rax    # 0x18e198
cmpq $0x0, 0xa08(%rax)
je 0x74164
callq 0x2ff70
movq %rax, -0x120(%rbp)
jmp 0x7416f
movq $0x0, -0x120(%rbp)
leaq -0x110(%rbp), %rdi
leaq 0x11a01b(%rip), %rax    # 0x18e198
movq 0x660(%rax), %rsi
callq 0x26cc0
cmpl $-0x1, %eax
jne 0x741fc
leaq -0x110(%rbp), %rax
movq %rax, -0x130(%rbp)
leaq 0x119ff5(%rip), %rax    # 0x18e198
movq 0x660(%rax), %rax
movq %rax, -0x128(%rbp)
callq 0x26890
movl (%rax), %edi
callq 0x266a0
movq -0x130(%rbp), %rdx
movq -0x128(%rbp), %rcx
movq %rax, %r8
movl $0x3, %edi
leaq 0xdee21(%rip), %rsi     # 0x152ffb
movb $0x0, %al
callq 0x2fc60
movl -0xc(%rbp), %edi
callq 0x26180
cmpl $-0x1, -0x10(%rbp)
je 0x741f7
movl -0x10(%rbp), %edi
callq 0x26180
jmp 0x7440b
leaq 0x119f95(%rip), %rax    # 0x18e198
cmpq $0x0, 0xa08(%rax)
je 0x74220
callq 0x2ff70
subq -0x120(%rbp), %rax
movq %rax, -0x120(%rbp)
leaq 0x119f71(%rip), %rax    # 0x18e198
cmpq $0x0, 0xa08(%rax)
je 0x7425b
movq -0x120(%rbp), %rax
leaq 0x119f59(%rip), %rcx    # 0x18e198
cmpq 0xa08(%rcx), %rax
jl 0x7425b
movq -0x120(%rbp), %rsi
leaq 0xdede2(%rip), %rdi     # 0x153038
callq 0xa0e80
leaq 0x119f36(%rip), %rax    # 0x18e198
cmpl $-0x1, 0x6a0(%rax)
jne 0x74278
movl -0xc(%rbp), %edi
callq 0x26180
jmp 0x74327
leaq 0x119f19(%rip), %rax    # 0x18e198
movl 0x6a0(%rax), %eax
movl %eax, -0x10(%rbp)
movl -0xc(%rbp), %ecx
leaq 0x119f06(%rip), %rax    # 0x18e198
movl %ecx, 0x6a0(%rax)
leaq 0x119ef9(%rip), %rax    # 0x18e198
cmpl $0x1, 0x65c(%rax)
jne 0x742b2
movl -0xc(%rbp), %edi
callq 0x26570
jmp 0x742cc
leaq 0x119edf(%rip), %rax    # 0x18e198
cmpl $0x2, 0x65c(%rax)
jne 0x742ca
movl -0xc(%rbp), %edi
callq 0x708e0
jmp 0x742cc
leaq 0x119ec5(%rip), %rax    # 0x18e198
movl $0xffffffff, 0x6a4(%rax) # imm = 0xFFFFFFFF
callq 0x72590
leaq 0x119eaf(%rip), %rax    # 0x18e198
movq 0x680(%rax), %rcx
leaq 0x119ea1(%rip), %rax    # 0x18e198
movq %rcx, 0x678(%rax)
leaq 0x119e93(%rip), %rax    # 0x18e198
movq 0x698(%rax), %rdi
callq 0x385b0
callq 0x38480
movq %rax, %rcx
leaq 0x119e78(%rip), %rax    # 0x18e198
movq %rcx, 0x698(%rax)
leaq 0x119e6a(%rip), %rax    # 0x18e198
movl $0x0, 0x6c8(%rax)
movl $0x2, %edi
leaq 0xdecff(%rip), %rsi     # 0x153043
movb $0x0, %al
callq 0x2fc60
leaq 0x119e46(%rip), %rax    # 0x18e198
cmpl $0x2, 0x658(%rax)
jne 0x7436c
leaq 0x119e36(%rip), %rax    # 0x18e198
movl $0x1, 0x658(%rax)
cmpl $-0x1, -0x10(%rbp)
je 0x74384
movslq -0x10(%rbp), %rsi
xorl %edi, %edi
xorl %eax, %eax
movl %eax, %ecx
movq %rcx, %rdx
callq 0x8e080
callq 0x2ff30
movq %rax, %rdx
subq -0x118(%rbp), %rdx
movl $0x1, %edi
leaq 0xdecd1(%rip), %rsi     # 0x153070
movb $0x0, %al
callq 0x2fc60
jmp 0x74409
cmpl $0x0, -0x8(%rbp)
jne 0x743e0
cmpl $0x0, -0x4(%rbp)
je 0x743e0
cmpl $0xa, -0x8(%rbp)
je 0x743cb
leaq 0x119dd7(%rip), %rax    # 0x18e198
movl $0xffffffff, 0x6c8(%rax) # imm = 0xFFFFFFFF
movl $0x3, %edi
leaq 0xdeccb(%rip), %rsi     # 0x1530a2
movb $0x0, %al
callq 0x2fc60
jmp 0x74407
leaq 0x119db1(%rip), %rax    # 0x18e198
movl $0xffffffff, 0x6c8(%rax) # imm = 0xFFFFFFFF
movl -0x8(%rbp), %edx
movl $0x3, %edi
leaq 0xdeccf(%rip), %rsi     # 0x1530cf
movb $0x0, %al
callq 0x2fc60
jmp 0x74409
jmp 0x7440b
callq 0x71170
callq 0x70420
leaq 0x119d7c(%rip), %rax    # 0x18e198
movl 0x68c(%rax), %edi
callq 0x71130
leaq 0x119d6a(%rip), %rax    # 0x18e198
movl $0xffffffff, 0x68c(%rax) # imm = 0xFFFFFFFF
xorl %eax, %eax
movl %eax, %edi
callq 0x26cf0
movq %rax, %rcx
leaq 0x119d4d(%rip), %rax    # 0x18e198
subq 0x6c0(%rax), %rcx
leaq 0x119d3f(%rip), %rax    # 0x18e198
movq %rcx, 0x6b8(%rax)
leaq 0x119d31(%rip), %rax    # 0x18e198
movq $-0x1, 0x6c0(%rax)
leaq 0x119d1f(%rip), %rax    # 0x18e198
cmpl $0x2, 0x658(%rax)
jne 0x74493
leaq 0x119d0f(%rip), %rax    # 0x18e198
movl $0x1, 0x688(%rax)
addq $0x130, %rsp            # imm = 0x130
popq %rbp
retq
nopl (%rax)