// File: /workspace/github_c_makefile_star2_O3/gplessis[P]dotdeb-redis/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);
        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));

        /* 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:
    aofClosePipes();
    aofRewriteBufferReset();
    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 0x5fa1b
movl %esi, %edx
testl %esi, %esi
setne %al
testl %edi, %edi
sete %cl
leaq 0xa1eb7(%rip), %rsi     # 0x1018a8
movl $0xffffffff, 0x6d8(%rsi) # imm = 0xFFFFFFFF
orb %al, %cl
jne 0x5fb57
leaq 0x662e1(%rip), %rsi     # 0xc5ceb
movl $0x3, %edi
xorl %eax, %eax
callq 0x2fdad
jmp 0x5fd46
callq 0x2ffab
movq %rax, %rbx
leaq 0x66111(%rip), %rsi     # 0xc5b3b
xorl %r14d, %r14d
movl $0x2, %edi
xorl %eax, %eax
callq 0x2fdad
leaq 0xa1e68(%rip), %r12     # 0x1018a8
cmpq $0x0, 0xa18(%r12)
je 0x5fa53
callq 0x2ffce
movq %rax, %r14
movl 0x69c(%r12), %ecx
leaq 0x65f4b(%rip), %rdx     # 0xc59ad
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 0x26690
cmpl $-0x1, %eax
je 0x5fb6f
movl %eax, %ebp
movl %eax, %edi
callq 0x5cf98
cmpq $-0x1, %rax
je 0x5fb8c
cmpq $0x0, 0xa18(%r12)
je 0x5fad9
callq 0x2ffce
subq %r14, %rax
movq 0xa18(%r12), %rcx
testq %rcx, %rcx
sete %dl
cmpq %rcx, %rax
setl %cl
orb %dl, %cl
jne 0x5fad9
leaq 0x66113(%rip), %rdi     # 0xc5be4
movq %rax, %rsi
callq 0x7eb75
callq 0x5cd26
movq %rax, %xmm1
punpckldq 0x62e75(%rip), %xmm1 # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0x62e7d(%rip), %xmm1    # 0xc2970
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
addsd %xmm1, %xmm0
mulsd 0x5ca41(%rip), %xmm0    # 0xbc548
leaq 0x660ed(%rip), %rsi     # 0xc5bfb
movl $0x2, %edi
movb $0x1, %al
callq 0x2fdad
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
cmpl $-0x1, 0x6b0(%r12)
jne 0x5fb42
movq 0x670(%r12), %rdi
movl $0x800, %esi            # imm = 0x800
xorl %eax, %eax
callq 0x26690
movl %eax, %r14d
cmpq $0x0, 0xa18(%r12)
je 0x5fbb5
callq 0x2ffce
movq %rax, %r15
jmp 0x5fbb8
leaq 0x661ba(%rip), %rsi     # 0xc5d18
movl $0x3, %edi
xorl %eax, %eax
callq 0x2fdad
jmp 0x5fd46
callq 0x26880
movl (%rax), %edi
callq 0x26680
leaq 0x65fe8(%rip), %rsi     # 0xc5b6a
movl $0x3, %edi
jmp 0x5fd3c
callq 0x26880
movl (%rax), %edi
callq 0x26680
leaq 0x66006(%rip), %rsi     # 0xc5ba5
movl $0x3, %edi
movq %rax, %rdx
xorl %eax, %eax
callq 0x2fdad
movl %ebp, %edi
jmp 0x5fc7c
xorl %r15d, %r15d
movq 0x670(%r12), %rsi
movq %rsp, %rdi
callq 0x26ca0
cmpl $-0x1, %eax
je 0x5fc38
cmpq $0x0, 0xa18(%r12)
je 0x5fc07
callq 0x2ffce
subq %r15, %rax
movq 0xa18(%r12), %rcx
testq %rcx, %rcx
sete %dl
cmpq %rcx, %rax
setl %cl
orb %dl, %cl
jne 0x5fc07
leaq 0x66082(%rip), %rdi     # 0xc5c81
movq %rax, %rsi
callq 0x7eb75
movl 0x6b0(%r12), %r15d
cmpl $-0x1, %r15d
je 0x5fc86
movl %ebp, 0x6b0(%r12)
movl 0x66c(%r12), %eax
cmpl $0x2, %eax
je 0x5fc92
cmpl $0x1, %eax
jne 0x5fca3
movl %ebp, %edi
callq 0x26570
jmp 0x5fca3
movq 0x670(%r12), %rbx
callq 0x26880
movl (%rax), %edi
callq 0x26680
leaq 0x65ff1(%rip), %rsi     # 0xc5c44
movq %rsp, %rdx
movl $0x3, %edi
movq %rbx, %rcx
movq %rax, %r8
xorl %eax, %eax
callq 0x2fdad
movl %ebp, %edi
callq 0x26180
cmpl $-0x1, %r14d
je 0x5fd46
movl %r14d, %edi
callq 0x26180
jmp 0x5fd46
movl %ebp, %edi
callq 0x26180
movl %r14d, %r15d
jmp 0x5fcde
movslq %ebp, %rsi
movl $0x1, %edi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x72542
movl $0xffffffff, 0x6b4(%r12) # imm = 0xFFFFFFFF
callq 0x5e67f
movq 0x690(%r12), %rax
movq 0x6a8(%r12), %rdi
movq %rax, 0x688(%r12)
callq 0x36330
callq 0x3628d
movq %rax, 0x6a8(%r12)
movl $0x0, 0x6d8(%r12)
leaq 0x65f9b(%rip), %rsi     # 0xc5c8c
movl $0x2, %edi
xorl %eax, %eax
callq 0x2fdad
cmpl $0x2, 0x668(%r12)
jne 0x5fd14
movl $0x1, 0x668(%r12)
cmpl $-0x1, %r15d
je 0x5fd28
movslq %r15d, %rsi
xorl %edi, %edi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x72542
callq 0x2ffab
subq %rbx, %rax
leaq 0x65f82(%rip), %rsi     # 0xc5cb9
movl $0x1, %edi
movq %rax, %rdx
xorl %eax, %eax
callq 0x2fdad
callq 0x5d713
callq 0x5ccf4
leaq 0xa1b51(%rip), %r14     # 0x1018a8
movl 0x69c(%r14), %ecx
leaq 0x65c48(%rip), %rdx     # 0xc59ad
movq %rsp, %rbx
movl $0x100, %esi            # imm = 0x100
movq %rbx, %rdi
xorl %eax, %eax
callq 0x26120
movq %rbx, %rdi
callq 0x263b0
movl $0xffffffff, 0x69c(%r14) # imm = 0xFFFFFFFF
xorl %edi, %edi
callq 0x26cd0
subq 0x6d0(%r14), %rax
movq %rax, 0x6c8(%r14)
movq $-0x1, 0x6d0(%r14)
cmpl $0x2, 0x668(%r14)
jne 0x5fdbf
movl $0x1, 0x698(%r14)
addq $0x100, %rsp            # imm = 0x100
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
nop