// File: /workspace/github_cpp_makefile_star2_O1/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 %r12
pushq %rbx
subq $0x10, %rsp
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 0x4ca0
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 0x4ca2
xorl %eax, %eax
movl 0x1008(%rbx), %ecx
cmpl $-0x1, %ecx
je 0x4d48
testl %ecx, %ecx
jne 0x4d43
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 0x4dbe
movl (%r12), %eax
leal -0x1000(%rax), %r14d
cmpl $0x1000, %eax           # imm = 0x1000
cmovbl %eax, %r14d
leal -0xfff(%r14), %eax
cmpl $0xffffeffe, %eax       # imm = 0xFFFFEFFE
ja 0x4dd2
movl $0x1000, %r15d          # imm = 0x1000
subl %r14d, %r15d
movl %r14d, %esi
addq %rbx, %rsi
leaq 0xe(%rsp), %r12
movq %r12, %rdi
movq %r15, %rdx
callq 0x31f0
addq %r12, %r15
addl $0xfffff002, %r14d      # imm = 0xFFFFF002
movq %r15, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x31f0
jmp 0x4dde
cmpl %eax, %ecx
cmovll %ecx, %eax
movslq %eax, %rdx
movl %ebp, %edi
callq 0x30c0
testl %eax, %eax
jle 0x4da4
movl 0x1000(%rbx), %edx
movl 0x1008(%rbx), %ecx
leal (%rdx,%rax), %esi
cmpl $0x1000, %esi           # imm = 0x1000
leal -0x1000(%rdx,%rax), %edx
cmovbl %esi, %edx
movl %edx, 0x1000(%rbx)
subl %eax, 0x1004(%rbx)
cmpl $-0x1, %ecx
je 0x4da4
subl %eax, 0x100c(%rbx)
subl %eax, %ecx
movl %ecx, 0x1008(%rbx)
jne 0x4da4
leaq 0x42b3(%rip), %rax      # 0x9050
movq %rax, 0x1010(%rbx)
cmpl $0x0, 0x1004(%rbx)
sete %al
movzbl %al, %eax
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
testl %eax, %eax
jle 0x4dce
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
addq %rax, 0x1010(%rbx)
xorl %eax, %eax
jmp 0x4dae
movl %r14d, %eax
movzwl (%rbx,%rax), %eax
movw %ax, 0xe(%rsp)
movswl 0xe(%rsp), %ecx
movl 0x1000(%rbx), %edx
movl 0x1004(%rbx), %eax
leal 0x2(%rdx), %esi
addl $0xfffff002, %edx       # imm = 0xFFFFF002
cmpl $0x1000, %esi           # imm = 0x1000
cmovbl %esi, %edx
addl $-0x2, %eax
movl %ecx, 0x1008(%rbx)
movl %edx, 0x1000(%rbx)
movl %eax, 0x1004(%rbx)
cmpl $-0x1, %ecx
jne 0x4e25
movl $0xffffffff, 0x100c(%rbx) # imm = 0xFFFFFFFF
testl %eax, %eax
jmp 0x4dab
nop