// File: /workspace/github_c_makefile_star2_O2/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 0x55f76
movl %esi, %edx
testl %esi, %esi
setne %al
testl %edi, %edi
sete %cl
leaq 0xa3957(%rip), %rsi     # 0xf98a8
orl $-0x1, 0x6d8(%rsi)
orb %al, %cl
jne 0x56082
leaq 0x61070(%rip), %rsi     # 0xb6fd7
pushq $0x3
popq %rdi
xorl %eax, %eax
callq 0x2b699
jmp 0x56280
callq 0x2b88b
movq %rax, %rbx
leaq 0x60ea2(%rip), %rsi     # 0xb6e27
xorl %r14d, %r14d
pushq $0x2
popq %rdi
xorl %eax, %eax
callq 0x2b699
leaq 0xa390f(%rip), %r12     # 0xf98a8
cmpq $0x0, 0xa18(%r12)
je 0x55fac
callq 0x2b8ae
movq %rax, %r14
movl 0x69c(%r12), %ecx
leaq 0x60cde(%rip), %rdx     # 0xb6c99
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 0x56098
movl %eax, %ebp
movl %eax, %edi
callq 0x53a2f
cmpq $-0x1, %rax
je 0x560b2
cmpq $0x0, 0xa18(%r12)
je 0x56032
callq 0x2b8ae
subq %r14, %rax
movq 0xa18(%r12), %rcx
testq %rcx, %rcx
sete %dl
cmpq %rcx, %rax
setl %cl
orb %dl, %cl
jne 0x56032
leaq 0x60ea6(%rip), %rdi     # 0xb6ed0
movq %rax, %rsi
callq 0x71e85
callq 0x537ca
movq %rax, %xmm1
punpckldq 0x5dfdc(%rip), %xmm1 # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0x5dfe4(%rip), %xmm1    # 0xb4030
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
addsd %xmm1, %xmm0
mulsd 0x583d8(%rip), %xmm0    # 0xae438
leaq 0x60e80(%rip), %rsi     # 0xb6ee7
pushq $0x2
popq %rdi
movb $0x1, %al
callq 0x2b699
cmpl $-0x1, 0x6b0(%r12)
je 0x560d9
pushq $-0x1
popq %r14
jmp 0x560f0
leaq 0x60f7b(%rip), %rsi     # 0xb7004
pushq $0x3
popq %rdi
xorl %eax, %eax
callq 0x2b699
jmp 0x56280
callq 0x26880
movl (%rax), %edi
callq 0x26680
leaq 0x60dab(%rip), %rsi     # 0xb6e56
pushq $0x3
jmp 0x56275
callq 0x26880
movl (%rax), %edi
callq 0x26680
leaq 0x60dcc(%rip), %rsi     # 0xb6e91
pushq $0x3
popq %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x2b699
movl %ebp, %edi
jmp 0x561ca
movq 0x670(%r12), %rdi
movl $0x800, %esi            # imm = 0x800
xorl %eax, %eax
callq 0x26690
movl %eax, %r14d
cmpq $0x0, 0xa18(%r12)
je 0x56105
callq 0x2b8ae
movq %rax, %r15
jmp 0x56108
xorl %r15d, %r15d
movq 0x670(%r12), %rsi
movq %rsp, %rdi
callq 0x26ca0
cmpl $-0x1, %eax
je 0x56188
cmpq $0x0, 0xa18(%r12)
je 0x56157
callq 0x2b8ae
subq %r15, %rax
movq 0xa18(%r12), %rcx
testq %rcx, %rcx
sete %dl
cmpq %rcx, %rax
setl %cl
orb %dl, %cl
jne 0x56157
leaq 0x60e1e(%rip), %rdi     # 0xb6f6d
movq %rax, %rsi
callq 0x71e85
movl 0x6b0(%r12), %r15d
cmpl $-0x1, %r15d
je 0x561d4
movl %ebp, 0x6b0(%r12)
movl 0x66c(%r12), %eax
cmpl $0x2, %eax
je 0x561e0
cmpl $0x1, %eax
jne 0x561e7
movl %ebp, %edi
callq 0x26570
jmp 0x561e7
movq 0x670(%r12), %rbx
callq 0x26880
movl (%rax), %edi
callq 0x26680
leaq 0x60d8d(%rip), %rsi     # 0xb6f30
pushq $0x3
popq %rdi
movq %rsp, %rdx
movq %rbx, %rcx
movq %rax, %r8
xorl %eax, %eax
callq 0x2b699
movl %ebp, %edi
callq 0x26180
cmpl $-0x1, %r14d
je 0x56280
movl %r14d, %edi
callq 0x26180
jmp 0x56280
movl %ebp, %edi
callq 0x26180
movl %r14d, %r15d
jmp 0x5621f
movl %ebp, %edi
callq 0x53aa7
orl $-0x1, 0x6b4(%r12)
callq 0x54e3b
movq 0x690(%r12), %rax
movq 0x6a8(%r12), %rdi
movq %rax, 0x688(%r12)
callq 0x311c8
callq 0x3114c
movq %rax, 0x6a8(%r12)
andl $0x0, 0x6d8(%r12)
leaq 0x60d49(%rip), %rsi     # 0xb6f78
pushq $0x2
popq %rdi
xorl %eax, %eax
callq 0x2b699
cmpl $0x2, 0x668(%r12)
jne 0x56250
movl $0x1, 0x668(%r12)
cmpl $-0x1, %r15d
je 0x56264
movslq %r15d, %rsi
xorl %edi, %edi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x66949
callq 0x2b88b
subq %rbx, %rax
leaq 0x60d32(%rip), %rsi     # 0xb6fa5
pushq $0x1
popq %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x2b699
callq 0x54062
callq 0x53798
leaq 0xa3617(%rip), %rbx     # 0xf98a8
movl 0x69c(%rbx), %edi
callq 0x5402e
orl $-0x1, 0x69c(%rbx)
xorl %edi, %edi
callq 0x26cd0
subq 0x6d0(%rbx), %rax
movq %rax, 0x6c8(%rbx)
orq $-0x1, 0x6d0(%rbx)
cmpl $0x2, 0x668(%rbx)
jne 0x562d3
movl $0x1, 0x698(%rbx)
addq $0x100, %rsp            # imm = 0x100
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
nop