// File: /workspace/github_cpp_makefile_star2_O2/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 %rbx
pushq %rax
movl %esi, %ebp
movq %rdi, %rbx
movl 0x1000(%rdi), %eax
leal -0x1000(%rax), %esi
cmpl $0x1000, %eax           # imm = 0x1000
cmovbl %eax, %esi
addq %rdi, %rsi
callq 0x4896
movl 0x1008(%rbx), %ecx
cmpl $-0x1, %ecx
je 0x4809
testl %ecx, %ecx
jne 0x4804
movq 0x1010(%rbx), %r14
movq %r14, %rdi
callq 0x30e0
movslq %eax, %r15
movl %ebp, %edi
movq %r14, %rsi
movq %r15, %rdx
callq 0x30b0
cmpl %r15d, %eax
jne 0x4881
leaq 0x6(%rsp), %r14
pushq $0x2
popq %rbp
movq %rbx, %rdi
movq %r14, %rsi
xorl %edx, %edx
movl %ebp, %ecx
callq 0x48d2
movswl (%r14), %eax
movl %eax, 0x1008(%rbx)
movq %rbx, %rdi
movl %ebp, %esi
callq 0x494e
cmpl $-0x1, 0x1008(%rbx)
jne 0x4869
orl $-0x1, 0x100c(%rbx)
jmp 0x4869
cmpl %eax, %ecx
cmovll %ecx, %eax
movl 0x1000(%rbx), %ecx
leal -0x1000(%rcx), %esi
cmpl $0x1000, %ecx           # imm = 0x1000
cmovbl %ecx, %esi
addq %rbx, %rsi
movslq %eax, %rdx
movl %ebp, %edi
callq 0x30b0
movq %rax, %r14
testl %r14d, %r14d
jle 0x4869
movq %rbx, %rdi
movl %r14d, %esi
callq 0x494e
movl 0x1008(%rbx), %eax
cmpl $-0x1, %eax
je 0x4869
subl %r14d, 0x100c(%rbx)
subl %r14d, %eax
movl %eax, 0x1008(%rbx)
jne 0x4869
leaq 0x37de(%rip), %rax      # 0x8040
movq %rax, 0x1010(%rbx)
cmpl $0x0, 0x1004(%rbx)
sete %al
movzbl %al, %eax
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
testl %eax, %eax
jle 0x4891
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
addq %rax, 0x1010(%rbx)
xorl %eax, %eax
jmp 0x4873
nop