// File: /workspace/github_c_makefile_star2_O3/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 %rbx
pushq %rax
leaq 0xa4741(%rip), %r15     # 0x1018a8
movq 0x6a8(%r15), %rcx
movzbl -0x1(%rcx), %eax
movl %eax, %edx
andl $0x7, %edx
cmpl $0x4, %edx
ja 0x5d6b7
movl %edi, %ebx
leaq 0x67cb3(%rip), %rsi     # 0xc4e3c
movslq (%rsi,%rdx,4), %rdx
addq %rsi, %rdx
jmpq *%rdx
shrl $0x3, %eax
jmp 0x5d1ac
movq -0x11(%rcx), %rax
jmp 0x5d1ac
movzwl -0x5(%rcx), %eax
jmp 0x5d1ac
movl -0x9(%rcx), %eax
jmp 0x5d1ac
movzbl -0x3(%rcx), %eax
testq %rax, %rax
je 0x5d6b7
cmpl $0x2, 0x66c(%r15)
jne 0x5d229
movl $0x1, %edi
callq 0x725db
testq %rax, %rax
setne %bpl
movl 0x66c(%r15), %eax
xorl $0x2, %eax
orl %eax, %ebx
sete %al
andb %bpl, %al
cmpb $0x1, %al
jne 0x5d22b
movq 0x6b8(%r15), %rcx
movq 0x978(%r15), %rax
testq %rcx, %rcx
je 0x5d44d
subq %rcx, %rax
cmpq $0x2, %rax
jl 0x5d6b7
incq 0x6e0(%r15)
leaq 0x67f1e(%rip), %rsi     # 0xc5136
movl $0x2, %edi
xorl %eax, %eax
callq 0x2fdad
movb $0x1, %bpl
jmp 0x5d22b
xorl %ebp, %ebp
cmpq $0x0, 0xa18(%r15)
je 0x5d23f
callq 0x2ffce
movq %rax, %r14
jmp 0x5d242
xorl %r14d, %r14d
movl 0x6b0(%r15), %edi
movq 0x6a8(%r15), %rsi
movzbl -0x1(%rsi), %edx
movl %edx, %eax
andl $0x7, %eax
cmpl $0x4, %eax
ja 0x5d28a
leaq 0x67beb(%rip), %rcx     # 0xc4e50
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
shrl $0x3, %edx
jmp 0x5d28c
movq -0x11(%rsi), %rdx
jmp 0x5d28c
movzwl -0x5(%rsi), %edx
jmp 0x5d28c
movl -0x9(%rsi), %edx
jmp 0x5d28c
movzbl -0x3(%rsi), %edx
jmp 0x5d28c
xorl %edx, %edx
callq 0x26bb0
movq %rax, %rbx
cmpq $0x0, 0xa18(%r15)
je 0x5d2a9
callq 0x2ffce
subq %r14, %rax
movq %rax, %r14
testb %bpl, %bpl
je 0x5d2ce
movq 0xa18(%r15), %rax
testq %rax, %rax
sete %cl
cmpq %rax, %r14
setl %al
orb %cl, %al
jne 0x5d312
leaq 0x67efd(%rip), %rdi     # 0xc51c9
jmp 0x5d30a
movl 0x730(%r15), %eax
andl 0x69c(%r15), %eax
movq 0xa18(%r15), %rcx
testq %rcx, %rcx
sete %dl
cmpq %rcx, %r14
setl %cl
orb %dl, %cl
cmpl $-0x1, %eax
leaq 0x67ee6(%rip), %rax     # 0xc51e1
leaq 0x67ef6(%rip), %rdi     # 0xc51f8
cmovneq %rax, %rdi
testb %cl, %cl
jne 0x5d312
movq %r14, %rsi
callq 0x7eb75
movq 0xa18(%r15), %rax
testq %rax, %rax
sete %cl
cmpq %rax, %r14
setl %al
orb %cl, %al
jne 0x5d338
leaq 0x67ed8(%rip), %rdi     # 0xc5208
movq %r14, %rsi
callq 0x7eb75
movq $0x0, 0x6b8(%r15)
movq 0x6a8(%r15), %rdi
movzbl -0x1(%rdi), %ecx
movl %ecx, %eax
andl $0x7, %eax
cmpl $0x4, %eax
ja 0x5d386
leaq 0x67b05(%rip), %rdx     # 0xc4e64
movslq (%rdx,%rax,4), %rsi
addq %rdx, %rsi
jmpq *%rsi
movl %ecx, %edx
shrl $0x3, %edx
jmp 0x5d388
movq -0x11(%rdi), %rdx
jmp 0x5d388
movzwl -0x5(%rdi), %edx
jmp 0x5d388
movl -0x9(%rdi), %edx
jmp 0x5d388
movzbl -0x3(%rdi), %edx
jmp 0x5d388
xorl %edx, %edx
movslq %edx, %rdx
cmpq %rdx, %rbx
jne 0x5d3f0
cmpl $-0x1, 0x6ec(%r15)
jne 0x5d3bf
leaq 0x67fb4(%rip), %rsi     # 0xc5355
movl $0x3, %edi
xorl %eax, %eax
callq 0x2fdad
movl $0x0, 0x6ec(%r15)
movq 0x6a8(%r15), %rdi
addq %rbx, 0x690(%r15)
movzbl -0x1(%rdi), %eax
movl %eax, %ecx
andl $0x7, %ecx
cmpl $0x4, %ecx
ja 0x5d4a3
leaq 0x67aad(%rip), %rdx     # 0xc4e8c
movslq (%rdx,%rcx,4), %rsi
addq %rdx, %rsi
jmpq *%rsi
shrl $0x3, %eax
jmp 0x5d4a5
movq 0x978(%r15), %rdx
movq %rdx, %r14
subq 0xb8e27(%rip), %r14     # 0x116228
cmpq $0x1e, %r14
jg 0x5d41d
cmpq $-0x1, %rbx
jne 0x5d612
movq $-0x1, %rbx
jmp 0x5d679
movq %rdx, 0xb8e04(%rip)     # 0x116228
cmpq $-0x1, %rbx
je 0x5d459
cmpl $0x4, %eax
ja 0x5d5fa
movl %eax, %eax
leaq 0x67a3c(%rip), %rdx     # 0xc4e78
movslq (%rdx,%rax,4), %rax
addq %rdx, %rax
jmpq *%rax
shrl $0x3, %ecx
jmp 0x5d5fc
movq %rax, 0x6b8(%r15)
jmp 0x5d6b7
callq 0x26880
movq %rax, %rbx
movl (%rax), %edi
callq 0x26680
leaq 0x67da3(%rip), %rsi     # 0xc5212
movl $0x3, %edi
movq %rax, %rdx
xorl %eax, %eax
callq 0x2fdad
movl (%rbx), %ebp
movq $-0x1, %rbx
jmp 0x5d672
movq -0x11(%rdi), %rax
jmp 0x5d4a5
movzwl -0x5(%rdi), %eax
jmp 0x5d4a5
movl -0x9(%rdi), %eax
jmp 0x5d4a5
movzbl -0x3(%rdi), %eax
jmp 0x5d4a5
xorl %eax, %eax
decl %ecx
cmpl $0x3, %ecx
ja 0x5d4e5
leaq 0x679ed(%rip), %rdx     # 0xc4ea0
movslq (%rdx,%rcx,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
movzbl -0x2(%rdi), %ecx
movzbl -0x3(%rdi), %edx
jmp 0x5d4e0
movl -0x5(%rdi), %ecx
subl -0x9(%rdi), %ecx
jmp 0x5d4e7
movq -0x9(%rdi), %rcx
subq -0x11(%rdi), %rcx
jmp 0x5d4e7
movzwl -0x3(%rdi), %ecx
movzwl -0x5(%rdi), %edx
subq %rdx, %rcx
jmp 0x5d4e7
xorl %ecx, %ecx
addq %rax, %rcx
cmpq $0xf9f, %rcx            # imm = 0xF9F
ja 0x5d4fa
callq 0x3639d
jmp 0x5d50b
callq 0x36330
callq 0x3628d
movq %rax, 0x6a8(%r15)
cmpl $0x0, 0x678(%r15)
je 0x5d531
cmpl $-0x1, 0x69c(%r15)
jne 0x5d6b7
cmpl $-0x1, 0x730(%r15)
jne 0x5d6b7
movl 0x66c(%r15), %eax
cmpl $0x2, %eax
je 0x5d55a
cmpl $0x1, %eax
jne 0x5d6b7
cmpq $0x0, 0xa18(%r15)
je 0x5d58a
callq 0x2ffce
movq %rax, %rbx
jmp 0x5d58c
movq 0x978(%r15), %rax
cmpq 0x6c0(%r15), %rax
jle 0x5d6b7
testb %bpl, %bpl
jne 0x5d5d7
movslq 0x6b0(%r15), %rsi
movl $0x1, %edi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x72542
jmp 0x5d5d0
xorl %ebx, %ebx
movl 0x6b0(%r15), %edi
callq 0x26570
cmpq $0x0, 0xa18(%r15)
je 0x5d5d0
callq 0x2ffce
subq %rbx, %rax
movq 0xa18(%r15), %rcx
testq %rcx, %rcx
sete %dl
cmpq %rcx, %rax
setl %cl
orb %dl, %cl
jne 0x5d5d0
leaq 0x67dc2(%rip), %rdi     # 0xc538a
movq %rax, %rsi
callq 0x7eb75
movq 0x978(%r15), %rax
movq %rax, 0x6c0(%r15)
jmp 0x5d6b7
movq -0x11(%rdi), %rcx
jmp 0x5d5fc
movzwl -0x5(%rdi), %ecx
jmp 0x5d5fc
movl -0x9(%rdi), %ecx
jmp 0x5d5fc
movzbl -0x3(%rdi), %ecx
jmp 0x5d5fc
xorl %ecx, %ecx
leaq 0x67c31(%rip), %rsi     # 0xc5234
movl $0x3, %edi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x2fdad
cmpq $0x1f, %r14
setl %bpl
movl 0x6b0(%r15), %edi
movq 0x690(%r15), %rsi
callq 0x26730
cmpl $-0x1, %eax
setne %cl
movq $-0x1, %rdx
movq %rbx, %rax
cmovneq %rdx, %rax
orb %bpl, %cl
movl $0x1c, %ebp
je 0x5d650
movq %rax, %rbx
jmp 0x5d672
callq 0x26880
movl (%rax), %edi
callq 0x26680
leaq 0x67c1b(%rip), %rsi     # 0xc527e
movl $0x3, %edi
movq %rax, %rdx
xorl %eax, %eax
callq 0x2fdad
movl %ebp, 0x6f0(%r15)
cmpl $0x1, 0x66c(%r15)
je 0x5d6c2
movl $0xffffffff, 0x6ec(%r15) # imm = 0xFFFFFFFF
testq %rbx, %rbx
jle 0x5d6b7
addq %rbx, 0x690(%r15)
movq 0x6a8(%r15), %rdi
movl %ebx, %esi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
jmp 0x377b2
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
leaq 0x67c37(%rip), %rsi     # 0xc5300
movl $0x3, %edi
xorl %eax, %eax
callq 0x2fdad
movl $0x1, %edi
callq 0x262c0