// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x50, %rsp
movl %edi, -0x4(%rbp)
movl $0x0, -0x14(%rbp)
leaq 0x1075bf(%rip), %rax    # 0x1781d8
movq 0x6a8(%rax), %rdi
callq 0x71850
cmpq $0x0, %rax
jne 0x70c30
jmp 0x712b5
leaq 0x1075a1(%rip), %rax    # 0x1781d8
cmpl $0x2, 0x66c(%rax)
jne 0x70c59
movl $0x1, %edi
callq 0x8e4c0
cmpq $0x0, %rax
setne %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x14(%rbp)
leaq 0x107578(%rip), %rax    # 0x1781d8
cmpl $0x2, 0x66c(%rax)
jne 0x70d11
cmpl $0x0, -0x4(%rbp)
jne 0x70d11
cmpl $0x0, -0x14(%rbp)
je 0x70d0f
leaq 0x107550(%rip), %rax    # 0x1781d8
cmpq $0x0, 0x6b8(%rax)
jne 0x70cb3
leaq 0x10753f(%rip), %rax    # 0x1781d8
movq 0x978(%rax), %rcx
leaq 0x107531(%rip), %rax    # 0x1781d8
movq %rcx, 0x6b8(%rax)
jmp 0x712b5
leaq 0x10751e(%rip), %rax    # 0x1781d8
movq 0x978(%rax), %rax
leaq 0x107510(%rip), %rcx    # 0x1781d8
subq 0x6b8(%rcx), %rax
cmpq $0x2, %rax
jge 0x70cda
jmp 0x712b5
jmp 0x70cdc
leaq 0x1074f5(%rip), %rax    # 0x1781d8
movq 0x6e0(%rax), %rcx
addq $0x1, %rcx
leaq 0x1074e3(%rip), %rax    # 0x1781d8
movq %rcx, 0x6e0(%rax)
movl $0x2, %edi
leaq 0xce82a(%rip), %rsi     # 0x13f532
movb $0x0, %al
callq 0x2fc60
jmp 0x70d11
leaq 0x1074c0(%rip), %rax    # 0x1781d8
cmpq $0x0, 0xa18(%rax)
je 0x70d2d
callq 0x2ff70
movq %rax, -0x20(%rbp)
jmp 0x70d35
movq $0x0, -0x20(%rbp)
leaq 0x10749c(%rip), %rax    # 0x1781d8
movl 0x6b0(%rax), %eax
movl %eax, -0x34(%rbp)
leaq 0x10748c(%rip), %rax    # 0x1781d8
movq 0x6a8(%rax), %rax
movq %rax, -0x30(%rbp)
leaq 0x10747a(%rip), %rax    # 0x1781d8
movq 0x6a8(%rax), %rdi
callq 0x71850
movl -0x34(%rbp), %edi
movq -0x30(%rbp), %rsi
movq %rax, %rdx
callq 0x26bc0
movq %rax, -0x10(%rbp)
leaq 0x107454(%rip), %rax    # 0x1781d8
cmpq $0x0, 0xa18(%rax)
je 0x70d9b
callq 0x2ff70
subq -0x20(%rbp), %rax
movq %rax, -0x20(%rbp)
cmpl $0x0, -0x14(%rbp)
je 0x70ddb
leaq 0x107430(%rip), %rax    # 0x1781d8
cmpq $0x0, 0xa18(%rax)
je 0x70dd6
movq -0x20(%rbp), %rax
leaq 0x10741b(%rip), %rcx    # 0x1781d8
cmpq 0xa18(%rcx), %rax
jl 0x70dd6
movq -0x20(%rbp), %rsi
leaq 0xce7f4(%rip), %rdi     # 0x13f5c5
callq 0xa1210
jmp 0x70e6b
leaq 0x1073f6(%rip), %rax    # 0x1781d8
cmpl $-0x1, 0x69c(%rax)
jne 0x70dfb
leaq 0x1073e6(%rip), %rax    # 0x1781d8
cmpl $-0x1, 0x730(%rax)
je 0x70e32
leaq 0x1073d6(%rip), %rax    # 0x1781d8
cmpq $0x0, 0xa18(%rax)
je 0x70e30
movq -0x20(%rbp), %rax
leaq 0x1073c1(%rip), %rcx    # 0x1781d8
cmpq 0xa18(%rcx), %rax
jl 0x70e30
movq -0x20(%rbp), %rsi
leaq 0xce7b2(%rip), %rdi     # 0x13f5dd
callq 0xa1210
jmp 0x70e69
leaq 0x10739f(%rip), %rax    # 0x1781d8
cmpq $0x0, 0xa18(%rax)
je 0x70e67
movq -0x20(%rbp), %rax
leaq 0x10738a(%rip), %rcx    # 0x1781d8
cmpq 0xa18(%rcx), %rax
jl 0x70e67
movq -0x20(%rbp), %rsi
leaq 0xce792(%rip), %rdi     # 0x13f5f4
callq 0xa1210
jmp 0x70e69
jmp 0x70e6b
leaq 0x107366(%rip), %rax    # 0x1781d8
cmpq $0x0, 0xa18(%rax)
je 0x70ea0
movq -0x20(%rbp), %rax
leaq 0x107351(%rip), %rcx    # 0x1781d8
cmpq 0xa18(%rcx), %rax
jl 0x70ea0
movq -0x20(%rbp), %rsi
leaq 0xce769(%rip), %rdi     # 0x13f604
callq 0xa1210
leaq 0x107331(%rip), %rax    # 0x1781d8
movq $0x0, 0x6b8(%rax)
movq -0x10(%rbp), %rax
movq %rax, -0x40(%rbp)
leaq 0x107317(%rip), %rax    # 0x1781d8
movq 0x6a8(%rax), %rdi
callq 0x71850
movq %rax, %rcx
movq -0x40(%rbp), %rax
movslq %ecx, %rcx
cmpq %rcx, %rax
je 0x71091
movl $0x0, -0x24(%rbp)
leaq 0x1072ea(%rip), %rax    # 0x1781d8
movq 0x978(%rax), %rax
subq 0x11bc5c(%rip), %rax    # 0x18cb58
cmpq $0x1e, %rax
jle 0x70f1e
movl $0x1, -0x24(%rbp)
leaq 0x1072c8(%rip), %rax    # 0x1781d8
movq 0x978(%rax), %rax
movq %rax, 0x11bc3a(%rip)    # 0x18cb58
cmpq $-0x1, -0x10(%rbp)
jne 0x70f66
cmpl $0x0, -0x24(%rbp)
je 0x70f61
callq 0x26880
movl (%rax), %edi
callq 0x26690
movq %rax, %rdx
movl $0x3, %edi
leaq 0xce6c8(%rip), %rsi     # 0x13f60e
movb $0x0, %al
callq 0x2fc60
callq 0x26880
movl (%rax), %ecx
leaq 0x10727d(%rip), %rax    # 0x1781d8
movl %ecx, 0x6f0(%rax)
jmp 0x71009
cmpl $0x0, -0x24(%rbp)
je 0x70fa1
movq -0x10(%rbp), %rax
movq %rax, -0x48(%rbp)
leaq 0x10725d(%rip), %rax    # 0x1781d8
movq 0x6a8(%rax), %rdi
callq 0x71850
movq -0x48(%rbp), %rdx
movq %rax, %rcx
movl $0x3, %edi
leaq 0xce696(%rip), %rsi     # 0x13f630
movb $0x0, %al
callq 0x2fc60
leaq 0x107230(%rip), %rax    # 0x1781d8
movl 0x6b0(%rax), %edi
leaq 0x107223(%rip), %rax    # 0x1781d8
movq 0x690(%rax), %rsi
callq 0x26730
cmpl $-0x1, %eax
jne 0x70ff0
cmpl $0x0, -0x24(%rbp)
je 0x70fee
callq 0x26880
movl (%rax), %edi
callq 0x26690
movq %rax, %rdx
movl $0x3, %edi
leaq 0xce693(%rip), %rsi     # 0x13f67a
movb $0x0, %al
callq 0x2fc60
jmp 0x70ff8
movq $-0x1, -0x10(%rbp)
leaq 0x1071d9(%rip), %rax    # 0x1781d8
movl $0x1c, 0x6f0(%rax)
leaq 0x1071c8(%rip), %rax    # 0x1781d8
cmpl $0x1, 0x66c(%rax)
jne 0x71036
movl $0x3, %edi
leaq 0xce6d7(%rip), %rsi     # 0x13f6fc
movb $0x0, %al
callq 0x2fc60
movl $0x1, %edi
callq 0x262d0
leaq 0x10719b(%rip), %rax    # 0x1781d8
movl $0xffffffff, 0x6ec(%rax) # imm = 0xFFFFFFFF
cmpq $0x0, -0x10(%rbp)
jle 0x7108c
movq -0x10(%rbp), %rcx
leaq 0x10717f(%rip), %rax    # 0x1781d8
addq 0x690(%rax), %rcx
leaq 0x107171(%rip), %rax    # 0x1781d8
movq %rcx, 0x690(%rax)
leaq 0x107163(%rip), %rax    # 0x1781d8
movq 0x6a8(%rax), %rdi
movq -0x10(%rbp), %rax
movl %eax, %esi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x39db0
jmp 0x712b5
leaq 0x107140(%rip), %rax    # 0x1781d8
cmpl $-0x1, 0x6ec(%rax)
jne 0x710c5
movl $0x3, %edi
leaq 0xce6a4(%rip), %rsi     # 0x13f751
movb $0x0, %al
callq 0x2fc60
leaq 0x10711d(%rip), %rax    # 0x1781d8
movl $0x0, 0x6ec(%rax)
jmp 0x710c7
movq -0x10(%rbp), %rcx
leaq 0x107106(%rip), %rax    # 0x1781d8
addq 0x690(%rax), %rcx
leaq 0x1070f8(%rip), %rax    # 0x1781d8
movq %rcx, 0x690(%rax)
leaq 0x1070ea(%rip), %rax    # 0x1781d8
movq 0x6a8(%rax), %rdi
callq 0x71850
movq %rax, -0x50(%rbp)
leaq 0x1070d3(%rip), %rax    # 0x1781d8
movq 0x6a8(%rax), %rdi
callq 0x718e0
movq %rax, %rcx
movq -0x50(%rbp), %rax
addq %rcx, %rax
cmpq $0xfa0, %rax            # imm = 0xFA0
jae 0x71138
leaq 0x1070ae(%rip), %rax    # 0x1781d8
movq 0x6a8(%rax), %rdi
callq 0x38740
jmp 0x71161
leaq 0x107099(%rip), %rax    # 0x1781d8
movq 0x6a8(%rax), %rdi
callq 0x38620
callq 0x384f0
movq %rax, %rcx
leaq 0x10707e(%rip), %rax    # 0x1781d8
movq %rcx, 0x6a8(%rax)
leaq 0x107070(%rip), %rax    # 0x1781d8
cmpl $0x0, 0x678(%rax)
je 0x71196
leaq 0x107060(%rip), %rax    # 0x1781d8
cmpl $-0x1, 0x69c(%rax)
jne 0x71191
leaq 0x107050(%rip), %rax    # 0x1781d8
cmpl $-0x1, 0x730(%rax)
je 0x71196
jmp 0x712b5
leaq 0x10703b(%rip), %rax    # 0x1781d8
cmpl $0x1, 0x66c(%rax)
jne 0x71251
leaq 0x107027(%rip), %rax    # 0x1781d8
cmpq $0x0, 0xa18(%rax)
je 0x711c6
callq 0x2ff70
movq %rax, -0x20(%rbp)
jmp 0x711ce
movq $0x0, -0x20(%rbp)
leaq 0x107003(%rip), %rax    # 0x1781d8
movl 0x6b0(%rax), %edi
callq 0x26560
leaq 0x106ff1(%rip), %rax    # 0x1781d8
cmpq $0x0, 0xa18(%rax)
je 0x711fe
callq 0x2ff70
subq -0x20(%rbp), %rax
movq %rax, -0x20(%rbp)
leaq 0x106fd3(%rip), %rax    # 0x1781d8
cmpq $0x0, 0xa18(%rax)
je 0x71233
movq -0x20(%rbp), %rax
leaq 0x106fbe(%rip), %rcx    # 0x1781d8
cmpq 0xa18(%rcx), %rax
jl 0x71233
movq -0x20(%rbp), %rsi
leaq 0xce558(%rip), %rdi     # 0x13f786
callq 0xa1210
leaq 0x106f9e(%rip), %rax    # 0x1781d8
movq 0x978(%rax), %rcx
leaq 0x106f90(%rip), %rax    # 0x1781d8
movq %rcx, 0x6c0(%rax)
jmp 0x712b5
leaq 0x106f80(%rip), %rax    # 0x1781d8
cmpl $0x2, 0x66c(%rax)
jne 0x712b3
leaq 0x106f70(%rip), %rax    # 0x1781d8
movq 0x978(%rax), %rax
leaq 0x106f62(%rip), %rcx    # 0x1781d8
cmpq 0x6c0(%rcx), %rax
jle 0x712b3
cmpl $0x0, -0x14(%rbp)
jne 0x71297
leaq 0x106f4c(%rip), %rax    # 0x1781d8
movl 0x6b0(%rax), %edi
callq 0x70a70
leaq 0x106f3a(%rip), %rax    # 0x1781d8
movq 0x978(%rax), %rcx
leaq 0x106f2c(%rip), %rax    # 0x1781d8
movq %rcx, 0x6c0(%rax)
jmp 0x712b5
addq $0x50, %rsp
popq %rbp
retq
nopl (%rax,%rax)