// File: /workspace/github_c_makefile_star2_O1/zhou-yuxin[P]redis-aof-no-cow/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
movl %edi, %ebx
leaq 0xa5adc(%rip), %r15     # 0x106858
movq 0x698(%r15), %rcx
movzbl -0x1(%rcx), %eax
movl %eax, %edx
andl $0x7, %edx
cmpl $0x4, %edx
ja 0x60dbd
leaq 0x67038(%rip), %rsi     # 0xc7dd0
movslq (%rsi,%rdx,4), %rdx
addq %rsi, %rdx
jmpq *%rdx
shrl $0x3, %eax
jmp 0x60dbf
movq -0x11(%rcx), %rax
jmp 0x60dbf
movzwl -0x5(%rcx), %eax
jmp 0x60dbf
movl -0x9(%rcx), %eax
jmp 0x60dbf
movzbl -0x3(%rcx), %eax
jmp 0x60dbf
xorl %eax, %eax
testq %rax, %rax
je 0x612cb
cmpl $0x2, 0x65c(%r15)
jne 0x60de5
movl $0x1, %edi
callq 0x7608f
testq %rax, %rax
setne %bpl
jmp 0x60de7
xorl %ebp, %ebp
movl 0x65c(%r15), %eax
xorl $0x2, %eax
orl %eax, %ebx
sete %al
andb %bpl, %al
cmpb $0x1, %al
jne 0x60e37
movq 0x6a8(%r15), %rcx
movq 0x968(%r15), %rax
testq %rcx, %rcx
je 0x60e7f
subq %rcx, %rax
cmpq $0x2, %rax
jl 0x612cb
incq 0x6d0(%r15)
leaq 0x6729f(%rip), %rsi     # 0xc80ca
movl $0x2, %edi
xorl %eax, %eax
callq 0x32ea6
cmpq $0x0, 0xa08(%r15)
je 0x60e4b
callq 0x330c8
movq %rax, %r14
jmp 0x60e4e
xorl %r14d, %r14d
movl 0x6a0(%r15), %edi
movq 0x698(%r15), %rsi
movzbl -0x1(%rsi), %edx
movl %edx, %eax
andl $0x7, %eax
cmpl $0x4, %eax
ja 0x60ea2
leaq 0x66f73(%rip), %rcx     # 0xc7de4
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
shrl $0x3, %edx
jmp 0x60ea4
movq %rax, 0x6a8(%r15)
jmp 0x612cb
movq -0x11(%rsi), %rdx
jmp 0x60ea4
movzwl -0x5(%rsi), %edx
jmp 0x60ea4
movl -0x9(%rsi), %edx
jmp 0x60ea4
movzbl -0x3(%rsi), %edx
jmp 0x60ea4
xorl %edx, %edx
callq 0x26bc0
movq %rax, %rbx
cmpq $0x0, 0xa08(%r15)
je 0x60ec1
callq 0x330c8
subq %r14, %rax
movq %rax, %r14
testb %bpl, %bpl
je 0x60ee6
movq 0xa08(%r15), %rax
testq %rax, %rax
sete %cl
cmpq %rax, %r14
setl %al
orb %cl, %al
jne 0x60f2a
leaq 0x67279(%rip), %rdi     # 0xc815d
jmp 0x60f22
movl 0x720(%r15), %eax
andl 0x68c(%r15), %eax
movq 0xa08(%r15), %rcx
testq %rcx, %rcx
sete %dl
cmpq %rcx, %r14
setl %cl
orb %dl, %cl
cmpl $-0x1, %eax
leaq 0x67262(%rip), %rax     # 0xc8175
leaq 0x67272(%rip), %rdi     # 0xc818c
cmovneq %rax, %rdi
testb %cl, %cl
jne 0x60f2a
movq %r14, %rsi
callq 0x825dd
movq 0xa08(%r15), %rax
testq %rax, %rax
sete %cl
cmpq %rax, %r14
setl %al
orb %cl, %al
jne 0x60f50
leaq 0x67254(%rip), %rdi     # 0xc819c
movq %r14, %rsi
callq 0x825dd
movq $0x0, 0x6a8(%r15)
movq 0x698(%r15), %rax
movzbl -0x1(%rax), %ecx
movl %ecx, %edx
andl $0x7, %edx
cmpl $0x4, %edx
ja 0x60f9e
leaq 0x66e81(%rip), %rsi     # 0xc7df8
movslq (%rsi,%rdx,4), %rdi
addq %rsi, %rdi
jmpq *%rdi
movl %ecx, %esi
shrl $0x3, %esi
jmp 0x60fa0
movq -0x11(%rax), %rsi
jmp 0x60fa0
movzwl -0x5(%rax), %esi
jmp 0x60fa0
movl -0x9(%rax), %esi
jmp 0x60fa0
movzbl -0x3(%rax), %esi
jmp 0x60fa0
xorl %esi, %esi
movslq %esi, %rsi
cmpq %rsi, %rbx
jne 0x61008
cmpl $-0x1, 0x6dc(%r15)
jne 0x60fd0
leaq 0x67330(%rip), %rsi     # 0xc82e9
movl $0x3, %edi
xorl %eax, %eax
callq 0x32ea6
movl $0x0, 0x6dc(%r15)
addq %rbx, 0x680(%r15)
movq 0x698(%r15), %rdi
movzbl -0x1(%rdi), %eax
movl %eax, %ecx
andl $0x7, %ecx
cmpl $0x4, %ecx
ja 0x610b4
leaq 0x66e29(%rip), %rdx     # 0xc7e20
movslq (%rdx,%rcx,4), %rsi
addq %rdx, %rsi
jmpq *%rsi
shrl $0x3, %eax
jmp 0x610b6
movq 0x968(%r15), %rsi
movq %rsi, %r14
subq 0xba1af(%rip), %r14     # 0x11b1c8
cmpq $0x1f, %r14
jl 0x61026
movq %rsi, 0xba1a2(%rip)     # 0x11b1c8
cmpq $-0x1, %rbx
je 0x61059
cmpq $0x1f, %r14
jl 0x61226
cmpl $0x4, %edx
ja 0x6120e
movl %edx, %edx
leaq 0x66dc4(%rip), %rsi     # 0xc7e0c
movslq (%rsi,%rdx,4), %rdx
addq %rsi, %rdx
jmpq *%rdx
shrl $0x3, %ecx
jmp 0x61210
movq $-0x1, %rbx
cmpq $0x1f, %r14
jl 0x6128d
callq 0x26890
movq %rax, %rbx
movl (%rax), %edi
callq 0x26690
leaq 0x67126(%rip), %rsi     # 0xc81a6
movl $0x3, %edi
movq %rax, %rdx
xorl %eax, %eax
callq 0x32ea6
movl (%rbx), %ebp
movq $-0x1, %rbx
jmp 0x61286
movq -0x11(%rdi), %rax
jmp 0x610b6
movzwl -0x5(%rdi), %eax
jmp 0x610b6
movl -0x9(%rdi), %eax
jmp 0x610b6
movzbl -0x3(%rdi), %eax
jmp 0x610b6
xorl %eax, %eax
decl %ecx
cmpl $0x3, %ecx
ja 0x610f6
leaq 0x66d70(%rip), %rdx     # 0xc7e34
movslq (%rdx,%rcx,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
movzbl -0x2(%rdi), %ecx
movzbl -0x3(%rdi), %edx
jmp 0x610f1
movl -0x5(%rdi), %ecx
subl -0x9(%rdi), %ecx
jmp 0x610f8
movq -0x9(%rdi), %rcx
subq -0x11(%rdi), %rcx
jmp 0x610f8
movzwl -0x3(%rdi), %ecx
movzwl -0x5(%rdi), %edx
subq %rdx, %rcx
jmp 0x610f8
xorl %ecx, %ecx
addq %rax, %rcx
cmpq $0xf9f, %rcx            # imm = 0xF9F
ja 0x6110b
callq 0x39323
jmp 0x6111c
callq 0x392b6
callq 0x39222
movq %rax, 0x698(%r15)
cmpl $0x0, 0x668(%r15)
je 0x61142
cmpl $-0x1, 0x68c(%r15)
jne 0x612cb
cmpl $-0x1, 0x720(%r15)
jne 0x612cb
movl 0x65c(%r15), %eax
cmpl $0x2, %eax
je 0x6116b
cmpl $0x1, %eax
jne 0x612cb
cmpq $0x0, 0xa08(%r15)
je 0x6119b
callq 0x330c8
movq %rax, %rbx
jmp 0x6119d
movq 0x968(%r15), %rax
cmpq 0x6b0(%r15), %rax
jle 0x612cb
testb %bpl, %bpl
jne 0x611e4
movslq 0x6a0(%r15), %rsi
movl $0x1, %edi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x75ff6
jmp 0x611e4
xorl %ebx, %ebx
movl 0x6a0(%r15), %edi
callq 0x26580
cmpq $0x0, 0xa08(%r15)
je 0x611be
callq 0x330c8
subq %rbx, %rax
movq %rax, %rbx
movq 0xa08(%r15), %rax
testq %rax, %rax
sete %cl
cmpq %rax, %rbx
setl %al
orb %cl, %al
jne 0x611e4
leaq 0x67142(%rip), %rdi     # 0xc831e
movq %rbx, %rsi
callq 0x825dd
movq 0x968(%r15), %rax
movq %rax, 0x6b0(%r15)
jmp 0x612cb
movq -0x11(%rax), %rcx
jmp 0x61210
movzwl -0x5(%rax), %ecx
jmp 0x61210
movl -0x9(%rax), %ecx
jmp 0x61210
movzbl -0x3(%rax), %ecx
jmp 0x61210
xorl %ecx, %ecx
leaq 0x66fb1(%rip), %rsi     # 0xc81c8
movl $0x3, %edi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x32ea6
cmpq $0x1f, %r14
setl %bpl
movl 0x6a0(%r15), %edi
movq 0x680(%r15), %rsi
callq 0x26740
cmpl $-0x1, %eax
setne %cl
movq $-0x1, %rdx
movq %rbx, %rax
cmovneq %rdx, %rax
orb %bpl, %cl
movl $0x1c, %ebp
je 0x61264
movq %rax, %rbx
jmp 0x61286
callq 0x26890
movl (%rax), %edi
callq 0x26690
leaq 0x66f9b(%rip), %rsi     # 0xc8212
movl $0x3, %edi
movq %rax, %rdx
xorl %eax, %eax
callq 0x32ea6
movl %ebp, 0x6e0(%r15)
cmpl $0x1, 0x65c(%r15)
je 0x612d6
movl $0xffffffff, 0x6dc(%r15) # imm = 0xFFFFFFFF
testq %rbx, %rbx
jle 0x612cb
addq %rbx, 0x680(%r15)
movq 0x698(%r15), %rdi
movl %ebx, %esi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
jmp 0x3a73b
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
leaq 0x66fb7(%rip), %rsi     # 0xc8294
movl $0x3, %edi
xorl %eax, %eax
callq 0x32ea6
movl $0x1, %edi
callq 0x262d0