// File: /workspace/github_c_makefile_star2_O2/gplessis[P]dotdeb-redis/src/aof.c
// Function: flushAppendOnlyFile
void flushAppendOnlyFile(int force) {
    ssize_t nwritten;
    int sync_in_progress = 0;
    mstime_t latency;

    if (sdslen(server.aof_buf) == 0) return;

    if (server.aof_fsync == AOF_FSYNC_EVERYSEC)
        sync_in_progress = bioPendingJobsOfType(BIO_AOF_FSYNC) != 0;

    if (server.aof_fsync == AOF_FSYNC_EVERYSEC && !force) {
        /* With this append fsync policy we do background fsyncing.
         * If the fsync is still in progress we can try to delay
         * the write for a couple of seconds. */
        if (sync_in_progress) {
            if (server.aof_flush_postponed_start == 0) {
                /* No previous write postponing, remember that we are
                 * postponing the flush and return. */
                server.aof_flush_postponed_start = server.unixtime;
                return;
            } else if (server.unixtime - server.aof_flush_postponed_start < 2) {
                /* We were already waiting for fsync to finish, but for less
                 * than two seconds this is still ok. Postpone again. */
                return;
            }
            /* Otherwise fall trough, and go write since we can't wait
             * over two seconds. */
            server.aof_delayed_fsync++;
            serverLog(LL_NOTICE,"Asynchronous AOF fsync is taking too long (disk is busy?). Writing the AOF buffer without waiting for fsync to complete, this may slow down Redis.");
        }
    }
    /* We want to perform a single write. This should be guaranteed atomic
     * at least if the filesystem we are writing is a real physical one.
     * While this will save us against the server being killed I don't think
     * there is much to do about the whole server stopping for power problems
     * or alike */

    latencyStartMonitor(latency);
    nwritten = write(server.aof_fd,server.aof_buf,sdslen(server.aof_buf));
    latencyEndMonitor(latency);
    /* We want to capture different events for delayed writes:
     * when the delay happens with a pending fsync, or with a saving child
     * active, and when the above two conditions are missing.
     * We also use an additional event name to save all samples which is
     * useful for graphing / monitoring purposes. */
    if (sync_in_progress) {
        latencyAddSampleIfNeeded("aof-write-pending-fsync",latency);
    } else if (server.aof_child_pid != -1 || server.rdb_child_pid != -1) {
        latencyAddSampleIfNeeded("aof-write-active-child",latency);
    } else {
        latencyAddSampleIfNeeded("aof-write-alone",latency);
    }
    latencyAddSampleIfNeeded("aof-write",latency);

    /* We performed the write so reset the postponed flush sentinel to zero. */
    server.aof_flush_postponed_start = 0;

    if (nwritten != (signed)sdslen(server.aof_buf)) {
        static time_t last_write_error_log = 0;
        int can_log = 0;

        /* Limit logging rate to 1 line per AOF_WRITE_LOG_ERROR_RATE seconds. */
        if ((server.unixtime - last_write_error_log) > AOF_WRITE_LOG_ERROR_RATE) {
            can_log = 1;
            last_write_error_log = server.unixtime;
        }

        /* Log the AOF write error and record the error code. */
        if (nwritten == -1) {
            if (can_log) {
                serverLog(LL_WARNING,"Error writing to the AOF file: %s",
                    strerror(errno));
                server.aof_last_write_errno = errno;
            }
        } else {
            if (can_log) {
                serverLog(LL_WARNING,"Short write while writing to "
                                       "the AOF file: (nwritten=%lld, "
                                       "expected=%lld)",
                                       (long long)nwritten,
                                       (long long)sdslen(server.aof_buf));
            }

            if (ftruncate(server.aof_fd, server.aof_current_size) == -1) {
                if (can_log) {
                    serverLog(LL_WARNING, "Could not remove short write "
                             "from the append-only file.  Redis may refuse "
                             "to load the AOF the next time it starts.  "
                             "ftruncate: %s", strerror(errno));
                }
            } else {
                /* If the ftruncate() succeeded we can set nwritten to
                 * -1 since there is no longer partial data into the AOF. */
                nwritten = -1;
            }
            server.aof_last_write_errno = ENOSPC;
        }

        /* Handle the AOF write error. */
        if (server.aof_fsync == AOF_FSYNC_ALWAYS) {
            /* We can't recover when the fsync policy is ALWAYS since the
             * reply for the client is already in the output buffers, and we
             * have the contract with the user that on acknowledged write data
             * is synced on disk. */
            serverLog(LL_WARNING,"Can't recover from AOF write error when the AOF fsync policy is 'always'. Exiting...");
            exit(1);
        } else {
            /* Recover from failed write leaving data into the buffer. However
             * set an error to stop accepting writes as long as the error
             * condition is not cleared. */
            server.aof_last_write_status = C_ERR;

            /* Trim the sds buffer if there was a partial write, and there
             * was no way to undo it with ftruncate(2). */
            if (nwritten > 0) {
                server.aof_current_size += nwritten;
                sdsrange(server.aof_buf,nwritten,-1);
            }
            return; /* We'll try again on the next call... */
        }
    } else {
        /* Successful write(2). If AOF was in error state, restore the
         * OK state and log the event. */
        if (server.aof_last_write_status == C_ERR) {
            serverLog(LL_WARNING,
                "AOF write error looks solved, Redis can write again.");
            server.aof_last_write_status = C_OK;
        }
    }
    server.aof_current_size += nwritten;

    /* Re-use AOF buffer when it is small enough. The maximum comes from the
     * arena size of 4k minus some overhead (but is otherwise arbitrary). */
    if ((sdslen(server.aof_buf)+sdsavail(server.aof_buf)) < 4000) {
        sdsclear(server.aof_buf);
    } else {
        sdsfree(server.aof_buf);
        server.aof_buf = sdsempty();
    }

    /* Don't fsync if no-appendfsync-on-rewrite is set to yes and there are
     * children doing I/O in the background. */
    if (server.aof_no_fsync_on_rewrite &&
        (server.aof_child_pid != -1 || server.rdb_child_pid != -1))
            return;

    /* Perform the fsync if needed. */
    if (server.aof_fsync == AOF_FSYNC_ALWAYS) {
        /* aof_fsync is defined as fdatasync() for Linux in order to avoid
         * flushing metadata. */
        latencyStartMonitor(latency);
        aof_fsync(server.aof_fd); /* Let's try to get this data on the disk */
        latencyEndMonitor(latency);
        latencyAddSampleIfNeeded("aof-fsync-always",latency);
        server.aof_last_fsync = server.unixtime;
    } else if ((server.aof_fsync == AOF_FSYNC_EVERYSEC &&
                server.unixtime > server.aof_last_fsync)) {
        if (!sync_in_progress) aof_background_fsync(server.aof_fd);
        server.aof_last_fsync = server.unixtime;
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
movl %edi, %ebx
leaq 0xa5cfe(%rip), %r12     # 0xf98a8
movq 0x6a8(%r12), %rdi
callq 0x543cb
testq %rax, %rax
je 0x5400c
cmpl $0x2, 0x66c(%r12)
jne 0x53c32
pushq $0x1
popq %rdi
callq 0x669db
testq %rax, %rax
setne %bpl
movl 0x66c(%r12), %eax
xorl $0x2, %eax
orl %eax, %ebx
sete %al
andb %bpl, %al
cmpb $0x1, %al
jne 0x53c34
movq 0x6b8(%r12), %rcx
movq 0x978(%r12), %rax
testq %rcx, %rcx
je 0x53e5e
subq %rcx, %rax
cmpq $0x2, %rax
jl 0x5400c
incq 0x6e0(%r12)
leaq 0x627fc(%rip), %rsi     # 0xb6422
pushq $0x2
popq %rdi
xorl %eax, %eax
callq 0x2b699
jmp 0x53c34
xorl %ebp, %ebp
cmpq $0x0, 0xa18(%r12)
je 0x53c49
callq 0x2b8ae
movq %rax, %r14
jmp 0x53c4c
xorl %r14d, %r14d
movl 0x6b0(%r12), %ebx
movq 0x6a8(%r12), %r15
movq %r15, %rdi
callq 0x543cb
movl %ebx, %edi
movq %r15, %rsi
movq %rax, %rdx
callq 0x26bb0
movq %rax, %rbx
cmpq $0x0, 0xa18(%r12)
je 0x53c8a
callq 0x2b8ae
subq %r14, %rax
movq %rax, %r14
testb %bpl, %bpl
je 0x53cb0
movq 0xa18(%r12), %rax
testq %rax, %rax
sete %cl
cmpq %rax, %r14
setl %al
orb %cl, %al
jne 0x53cf7
leaq 0x62807(%rip), %rdi     # 0xb64b5
jmp 0x53cef
movl 0x730(%r12), %eax
andl 0x69c(%r12), %eax
movq 0xa18(%r12), %rcx
testq %rcx, %rcx
sete %dl
cmpq %rcx, %r14
setl %cl
orb %dl, %cl
cmpl $-0x1, %eax
leaq 0x627ed(%rip), %rax     # 0xb64cd
leaq 0x627fd(%rip), %rdi     # 0xb64e4
cmovneq %rax, %rdi
testb %cl, %cl
jne 0x53cf7
movq %r14, %rsi
callq 0x71e85
movq 0xa18(%r12), %rax
testq %rax, %rax
sete %cl
cmpq %rax, %r14
setl %al
orb %cl, %al
jne 0x53d1e
leaq 0x627de(%rip), %rdi     # 0xb64f4
movq %r14, %rsi
callq 0x71e85
andq $0x0, 0x6b8(%r12)
movq 0x6a8(%r12), %r14
movq %r14, %rdi
callq 0x543cb
movslq %eax, %rcx
cmpq %rcx, %rbx
jne 0x53dae
cmpl $-0x1, 0x6ec(%r12)
jne 0x53d6c
leaq 0x628f0(%rip), %rsi     # 0xb6641
pushq $0x3
popq %rdi
xorl %eax, %eax
callq 0x2b699
andl $0x0, 0x6ec(%r12)
movq 0x6a8(%r12), %r14
addq %rbx, 0x690(%r12)
movq %r14, %rdi
callq 0x543cb
movzbl -0x1(%r14), %ecx
andl $0x7, %ecx
decl %ecx
cmpl $0x3, %ecx
ja 0x53e8e
leaq 0x6245a(%rip), %rdx     # 0xb61f0
movslq (%rdx,%rcx,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
movzbl -0x2(%r14), %ecx
movzbl -0x3(%r14), %edx
jmp 0x53e89
movq 0x978(%r12), %rcx
movq %rcx, %r14
subq 0xba468(%rip), %r14     # 0x10e228
cmpq $0x1e, %r14
jg 0x53dd4
cmpq $-0x1, %rbx
jne 0x53dfc
pushq $-0x1
popq %rbx
jmp 0x53f74
movq %rcx, 0xba44d(%rip)     # 0x10e228
cmpq $-0x1, %rbx
je 0x53f44
leaq 0x62734(%rip), %rsi     # 0xb6520
pushq $0x3
popq %rdi
movq %rbx, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x2b699
cmpq $0x1f, %r14
setl %bpl
movl 0x6b0(%r12), %edi
movq 0x690(%r12), %rsi
callq 0x26730
cmpl $-0x1, %eax
setne %cl
pushq $-0x1
popq %rdx
movq %rbx, %rax
cmovneq %rdx, %rax
orb %bpl, %cl
pushq $0x1c
popq %rbp
je 0x53e39
movq %rax, %rbx
jmp 0x53f6c
callq 0x26880
movl (%rax), %edi
callq 0x26680
leaq 0x6271e(%rip), %rsi     # 0xb656a
pushq $0x3
popq %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x2b699
jmp 0x53f6c
movq %rax, 0x6b8(%r12)
jmp 0x5400c
movl -0x5(%r14), %ecx
subl -0x9(%r14), %ecx
jmp 0x53e90
movq -0x9(%r14), %rcx
subq -0x11(%r14), %rcx
jmp 0x53e90
movzwl -0x3(%r14), %ecx
movzwl -0x5(%r14), %edx
subq %rdx, %rcx
jmp 0x53e90
xorl %ecx, %ecx
addq %rax, %rcx
movq %r14, %rdi
cmpq $0xf9f, %rcx            # imm = 0xF9F
ja 0x53ea6
callq 0x31233
jmp 0x53eb8
callq 0x311c8
callq 0x3114c
movq %rax, 0x6a8(%r12)
cmpl $0x0, 0x678(%r12)
je 0x53ee1
cmpl $-0x1, 0x69c(%r12)
jne 0x5400c
cmpl $-0x1, 0x730(%r12)
jne 0x5400c
movl 0x66c(%r12), %eax
cmpl $0x2, %eax
je 0x53f13
cmpl $0x1, %eax
jne 0x5400c
cmpq $0x0, 0xa18(%r12)
je 0x53fb3
callq 0x2b8ae
movq %rax, %rbx
jmp 0x53fb5
movq 0x978(%r12), %rax
cmpq 0x6c0(%r12), %rax
jle 0x5400c
testb %bpl, %bpl
jne 0x54004
movl 0x6b0(%r12), %edi
callq 0x53aa7
jmp 0x53ffc
callq 0x26880
movq %rax, %rbx
movl (%rax), %edi
callq 0x26680
leaq 0x625a4(%rip), %rsi     # 0xb64fe
pushq $0x3
popq %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x2b699
movl (%rbx), %ebp
pushq $-0x1
popq %rbx
movl %ebp, 0x6f0(%r12)
cmpl $0x1, 0x66c(%r12)
je 0x54015
orl $-0x1, 0x6ec(%r12)
testq %rbx, %rbx
jle 0x5400c
addq %rbx, 0x690(%r12)
movq 0x6a8(%r12), %rdi
pushq $-0x1
popq %rdx
movl %ebx, %esi
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x31e8e
xorl %ebx, %ebx
movl 0x6b0(%r12), %edi
callq 0x26570
cmpq $0x0, 0xa18(%r12)
je 0x53ffc
callq 0x2b8ae
subq %rbx, %rax
movq 0xa18(%r12), %rcx
testq %rcx, %rcx
sete %dl
cmpq %rcx, %rax
setl %cl
orb %dl, %cl
jne 0x53ffc
leaq 0x62682(%rip), %rdi     # 0xb6676
movq %rax, %rsi
callq 0x71e85
movq 0x978(%r12), %rax
movq %rax, 0x6c0(%r12)
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
leaq 0x625d0(%rip), %rsi     # 0xb65ec
pushq $0x3
popq %rdi
xorl %eax, %eax
callq 0x2b699
pushq $0x1
popq %rdi
callq 0x262c0