// File: /workspace/github_cpp_makefile_star2_O3/davmac314[P]dinit/src/shutdown.cc
// Function: subproc_buffer::flush(int)
fill_status flush(int fd)
    {
        int to_write = get_contiguous_length(get_ptr(0));

        if (overflow_marker != -1) {
            if (overflow_marker == 0) {
                // output (remainder of) overflow message
                int l = std::strlen(overflow_msg_ptr);
                int r = write(fd, overflow_msg_ptr, l);
                if (r == l) {
                    // entire message has been written; next marker is in buffer
                    int16_t overflow_marker16;
                    extract(reinterpret_cast<char *>(&overflow_marker16), 0, sizeof(overflow_marker16));
                    overflow_marker = overflow_marker16;
                    consume(sizeof(overflow_marker16));

                    // no more overflow markers?
                    if (overflow_marker == -1) {
                        last_overflow = -1;
                    }
                    return get_length() == 0 ? fill_status::ENDFILE : fill_status::OK;
                }
                if (r > 0) {
                    overflow_msg_ptr += r;
                }
                return fill_status::OK;
            }

            to_write = std::min(to_write, overflow_marker);
        }

        int r = write(fd, get_ptr(0), to_write);
        if (r > 0) {
            consume(r);
            if (overflow_marker != -1) {
                overflow_marker -= r;
                last_overflow -= r;
                if (overflow_marker == 0) {
                    overflow_msg_ptr = output_lost_msg;
                }
            }
        }

        return get_length() == 0 ? fill_status::ENDFILE : fill_status::OK;
    }

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl %esi, %ebp
movq %rdi, %rbx
leaq 0x1000(%rdi), %r12
movl 0x1000(%rdi), %eax
movl 0x1004(%rdi), %edx
leal -0x1000(%rax), %ecx
cmpl $0x1000, %eax           # imm = 0x1000
cmovbl %eax, %ecx
leaq (%rcx,%rdi), %rsi
testl %edx, %edx
je 0x4b73
leal (%rdx,%rax), %edi
cmpl $0x1000, %edi           # imm = 0x1000
leal -0x1000(%rdx,%rax), %edx
cmovbl %edi, %edx
leal (%rbx,%rdx), %eax
cmpl %ecx, %edx
cmovbel %r12d, %eax
subl %esi, %eax
jmp 0x4b75
xorl %eax, %eax
movl 0x1008(%rbx), %ecx
cmpl $-0x1, %ecx
je 0x4c1f
testl %ecx, %ecx
jne 0x4c1a
movq 0x1010(%rbx), %r14
movq %r14, %rdi
callq 0x30f0
movslq %eax, %r15
movl %ebp, %edi
movq %r14, %rsi
movq %r15, %rdx
callq 0x30c0
cmpl %r15d, %eax
jne 0x4c9a
movl (%r12), %r13d
leal -0x1000(%r13), %r14d
cmpl $0x1000, %r13d          # imm = 0x1000
cmovbl %r13d, %r14d
leal -0xfff(%r14), %eax
cmpl $0xffffeffe, %eax       # imm = 0xFFFFEFFE
ja 0x4cae
movl $0x1000, %r12d          # imm = 0x1000
subl %r14d, %r12d
movl %r14d, %esi
addq %rbx, %rsi
leaq 0x6(%rsp), %r15
movq %r15, %rdi
movq %r12, %rdx
callq 0x31f0
leaq (%rsp,%r12), %rdi
addq $0x6, %rdi
addl $0xfffff002, %r14d      # imm = 0xFFFFF002
movq %rbx, %rsi
movq %r14, %rdx
callq 0x31f0
jmp 0x4cb4
cmpl %eax, %ecx
cmovll %ecx, %eax
movslq %eax, %rdx
movl %ebp, %edi
callq 0x30c0
movl 0x1004(%rbx), %ecx
testl %eax, %eax
jle 0x4c83
movl 0x1000(%rbx), %esi
movl 0x1008(%rbx), %edx
leal (%rsi,%rax), %edi
cmpl $0x1000, %edi           # imm = 0x1000
leal -0x1000(%rsi,%rax), %esi
cmovbl %edi, %esi
movl %esi, 0x1000(%rbx)
subl %eax, %ecx
movl %ecx, 0x1004(%rbx)
cmpl $-0x1, %edx
je 0x4c83
subl %eax, 0x100c(%rbx)
subl %eax, %edx
movl %edx, 0x1008(%rbx)
jne 0x4c83
leaq 0x33c4(%rip), %rax      # 0x8040
movq %rax, 0x1010(%rbx)
testl %ecx, %ecx
sete %al
movzbl %al, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
testl %eax, %eax
jle 0x4caa
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
addq %rax, 0x1010(%rbx)
xorl %eax, %eax
jmp 0x4c88
movl %r14d, %r15d
addq %rbx, %r15
movswl (%r15), %ecx
leal 0x2(%r13), %eax
addl $0xfffff002, %r13d      # imm = 0xFFFFF002
cmpl $0x1000, %eax           # imm = 0x1000
cmovbl %eax, %r13d
movl 0x1004(%rbx), %eax
addl $-0x2, %eax
movl %ecx, 0x1008(%rbx)
movl %r13d, 0x1000(%rbx)
movl %eax, 0x1004(%rbx)
cmpl $-0x1, %ecx
jne 0x4cf7
movl $0xffffffff, 0x100c(%rbx) # imm = 0xFFFFFFFF
testl %eax, %eax
jmp 0x4c85
nop