// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x130, %rsp            # imm = 0x130
movl %edi, -0x4(%rbp)
movl %esi, -0x8(%rbp)
cmpl $0x0, -0x8(%rbp)
jne 0x746d8
cmpl $0x0, -0x4(%rbp)
jne 0x746d8
callq 0x2ff30
movq %rax, -0x118(%rbp)
movl $0x2, %edi
leaq 0xcbc7a(%rip), %rsi     # 0x13ff37
movb $0x0, %al
callq 0x2fc60
leaq 0x103f0d(%rip), %rax    # 0x1781d8
cmpq $0x0, 0xa18(%rax)
je 0x742e3
callq 0x2ff70
movq %rax, -0x120(%rbp)
jmp 0x742ee
movq $0x0, -0x120(%rbp)
leaq -0x110(%rbp), %rdi
leaq 0x103edc(%rip), %rax    # 0x1781d8
movl 0x69c(%rax), %ecx
movl $0x100, %esi            # imm = 0x100
leaq 0xcba9b(%rip), %rdx     # 0x13fda9
movb $0x0, %al
callq 0x26120
leaq -0x110(%rbp), %rdi
movl $0x401, %esi            # imm = 0x401
movb $0x0, %al
callq 0x266a0
movl %eax, -0xc(%rbp)
cmpl $-0x1, -0xc(%rbp)
jne 0x74358
callq 0x26880
movl (%rax), %edi
callq 0x26690
movq %rax, %rdx
movl $0x3, %edi
leaq 0xcbc1a(%rip), %rsi     # 0x13ff66
movb $0x0, %al
callq 0x2fc60
jmp 0x7473b
movl -0xc(%rbp), %edi
callq 0x709b0
cmpq $-0x1, %rax
jne 0x74395
callq 0x26880
movl (%rax), %edi
callq 0x26690
movq %rax, %rdx
movl $0x3, %edi
leaq 0xcbc20(%rip), %rsi     # 0x13ffa1
movb $0x0, %al
callq 0x2fc60
movl -0xc(%rbp), %edi
callq 0x26180
jmp 0x7473b
leaq 0x103e3c(%rip), %rax    # 0x1781d8
cmpq $0x0, 0xa18(%rax)
je 0x743b9
callq 0x2ff70
subq -0x120(%rbp), %rax
movq %rax, -0x120(%rbp)
leaq 0x103e18(%rip), %rax    # 0x1781d8
cmpq $0x0, 0xa18(%rax)
je 0x743f4
movq -0x120(%rbp), %rax
leaq 0x103e00(%rip), %rcx    # 0x1781d8
cmpq 0xa18(%rcx), %rax
jl 0x743f4
movq -0x120(%rbp), %rsi
leaq 0xcbbf1(%rip), %rdi     # 0x13ffe0
callq 0xa1210
callq 0x70600
movq %rax, %xmm0
movaps 0xc309b(%rip), %xmm1    # 0x1374a0
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movapd 0xc309f(%rip), %xmm1    # 0x1374b0
subpd %xmm1, %xmm0
movaps %xmm0, %xmm1
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
addsd %xmm1, %xmm0
movsd 0xc3060(%rip), %xmm1    # 0x137488
divsd %xmm1, %xmm0
movl $0x2, %edi
leaq 0xcbbbf(%rip), %rsi     # 0x13fff7
movb $0x1, %al
callq 0x2fc60
leaq 0x103d92(%rip), %rax    # 0x1781d8
cmpl $-0x1, 0x6b0(%rax)
jne 0x7446e
leaq 0x103d82(%rip), %rax    # 0x1781d8
movq 0x670(%rax), %rdi
movl $0x800, %esi            # imm = 0x800
movb $0x0, %al
callq 0x266a0
movl %eax, -0x10(%rbp)
jmp 0x74475
movl $0xffffffff, -0x10(%rbp) # imm = 0xFFFFFFFF
leaq 0x103d5c(%rip), %rax    # 0x1781d8
cmpq $0x0, 0xa18(%rax)
je 0x74494
callq 0x2ff70
movq %rax, -0x120(%rbp)
jmp 0x7449f
movq $0x0, -0x120(%rbp)
leaq -0x110(%rbp), %rdi
leaq 0x103d2b(%rip), %rax    # 0x1781d8
movq 0x670(%rax), %rsi
callq 0x26cb0
cmpl $-0x1, %eax
jne 0x7452c
leaq -0x110(%rbp), %rax
movq %rax, -0x130(%rbp)
leaq 0x103d05(%rip), %rax    # 0x1781d8
movq 0x670(%rax), %rax
movq %rax, -0x128(%rbp)
callq 0x26880
movl (%rax), %edi
callq 0x26690
movq -0x130(%rbp), %rdx
movq -0x128(%rbp), %rcx
movq %rax, %r8
movl $0x3, %edi
leaq 0xcbb36(%rip), %rsi     # 0x140040
movb $0x0, %al
callq 0x2fc60
movl -0xc(%rbp), %edi
callq 0x26180
cmpl $-0x1, -0x10(%rbp)
je 0x74527
movl -0x10(%rbp), %edi
callq 0x26180
jmp 0x7473b
leaq 0x103ca5(%rip), %rax    # 0x1781d8
cmpq $0x0, 0xa18(%rax)
je 0x74550
callq 0x2ff70
subq -0x120(%rbp), %rax
movq %rax, -0x120(%rbp)
leaq 0x103c81(%rip), %rax    # 0x1781d8
cmpq $0x0, 0xa18(%rax)
je 0x7458b
movq -0x120(%rbp), %rax
leaq 0x103c69(%rip), %rcx    # 0x1781d8
cmpq 0xa18(%rcx), %rax
jl 0x7458b
movq -0x120(%rbp), %rsi
leaq 0xcbaf7(%rip), %rdi     # 0x14007d
callq 0xa1210
leaq 0x103c46(%rip), %rax    # 0x1781d8
cmpl $-0x1, 0x6b0(%rax)
jne 0x745a8
movl -0xc(%rbp), %edi
callq 0x26180
jmp 0x74657
leaq 0x103c29(%rip), %rax    # 0x1781d8
movl 0x6b0(%rax), %eax
movl %eax, -0x10(%rbp)
movl -0xc(%rbp), %ecx
leaq 0x103c16(%rip), %rax    # 0x1781d8
movl %ecx, 0x6b0(%rax)
leaq 0x103c09(%rip), %rax    # 0x1781d8
cmpl $0x1, 0x66c(%rax)
jne 0x745e2
movl -0xc(%rbp), %edi
callq 0x26560
jmp 0x745fc
leaq 0x103bef(%rip), %rax    # 0x1781d8
cmpl $0x2, 0x66c(%rax)
jne 0x745fa
movl -0xc(%rbp), %edi
callq 0x70a70
jmp 0x745fc
leaq 0x103bd5(%rip), %rax    # 0x1781d8
movl $0xffffffff, 0x6b4(%rax) # imm = 0xFFFFFFFF
callq 0x728c0
leaq 0x103bbf(%rip), %rax    # 0x1781d8
movq 0x690(%rax), %rcx
leaq 0x103bb1(%rip), %rax    # 0x1781d8
movq %rcx, 0x688(%rax)
leaq 0x103ba3(%rip), %rax    # 0x1781d8
movq 0x6a8(%rax), %rdi
callq 0x38620
callq 0x384f0
movq %rax, %rcx
leaq 0x103b88(%rip), %rax    # 0x1781d8
movq %rcx, 0x6a8(%rax)
leaq 0x103b7a(%rip), %rax    # 0x1781d8
movl $0x0, 0x6d8(%rax)
movl $0x2, %edi
leaq 0xcba14(%rip), %rsi     # 0x140088
movb $0x0, %al
callq 0x2fc60
leaq 0x103b56(%rip), %rax    # 0x1781d8
cmpl $0x2, 0x668(%rax)
jne 0x7469c
leaq 0x103b46(%rip), %rax    # 0x1781d8
movl $0x1, 0x668(%rax)
cmpl $-0x1, -0x10(%rbp)
je 0x746b4
movslq -0x10(%rbp), %rsi
xorl %edi, %edi
xorl %eax, %eax
movl %eax, %ecx
movq %rcx, %rdx
callq 0x8e3e0
callq 0x2ff30
movq %rax, %rdx
subq -0x118(%rbp), %rdx
movl $0x1, %edi
leaq 0xcb9e6(%rip), %rsi     # 0x1400b5
movb $0x0, %al
callq 0x2fc60
jmp 0x74739
cmpl $0x0, -0x8(%rbp)
jne 0x74710
cmpl $0x0, -0x4(%rbp)
je 0x74710
cmpl $0xa, -0x8(%rbp)
je 0x746fb
leaq 0x103ae7(%rip), %rax    # 0x1781d8
movl $0xffffffff, 0x6d8(%rax) # imm = 0xFFFFFFFF
movl $0x3, %edi
leaq 0xcb9e0(%rip), %rsi     # 0x1400e7
movb $0x0, %al
callq 0x2fc60
jmp 0x74737
leaq 0x103ac1(%rip), %rax    # 0x1781d8
movl $0xffffffff, 0x6d8(%rax) # imm = 0xFFFFFFFF
movl -0x8(%rbp), %edx
movl $0x3, %edi
leaq 0xcb9e4(%rip), %rsi     # 0x140114
movb $0x0, %al
callq 0x2fc60
jmp 0x74739
jmp 0x7473b
callq 0x71300
callq 0x705a0
leaq 0x103a8c(%rip), %rax    # 0x1781d8
movl 0x69c(%rax), %edi
callq 0x712c0
leaq 0x103a7a(%rip), %rax    # 0x1781d8
movl $0xffffffff, 0x69c(%rax) # imm = 0xFFFFFFFF
xorl %eax, %eax
movl %eax, %edi
callq 0x26ce0
movq %rax, %rcx
leaq 0x103a5d(%rip), %rax    # 0x1781d8
subq 0x6d0(%rax), %rcx
leaq 0x103a4f(%rip), %rax    # 0x1781d8
movq %rcx, 0x6c8(%rax)
leaq 0x103a41(%rip), %rax    # 0x1781d8
movq $-0x1, 0x6d0(%rax)
leaq 0x103a2f(%rip), %rax    # 0x1781d8
cmpl $0x2, 0x668(%rax)
jne 0x747c3
leaq 0x103a1f(%rip), %rax    # 0x1781d8
movl $0x1, 0x698(%rax)
addq $0x130, %rsp            # imm = 0x130
popq %rbp
retq
nopl (%rax)