// File: 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
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq -0x10(%rbp), %rdi
movq %rdi, -0x30(%rbp)
xorl %esi, %esi
callq 0x63d0
movq -0x30(%rbp), %rdi
movq %rax, %rsi
callq 0x6340
movl %eax, %ecx
movq -0x30(%rbp), %rax
movl %ecx, -0x18(%rbp)
cmpl $-0x1, 0x1008(%rax)
je 0x6286
movq -0x30(%rbp), %rax
cmpl $0x0, 0x1008(%rax)
jne 0x626d
movq -0x30(%rbp), %rax
movq 0x1010(%rax), %rdi
callq 0x30e0
movq %rax, %rcx
movq -0x30(%rbp), %rax
movl %ecx, -0x1c(%rbp)
movl -0x14(%rbp), %edi
movq 0x1010(%rax), %rsi
movslq -0x1c(%rbp), %rdx
callq 0x30b0
movl %eax, -0x20(%rbp)
movl -0x20(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jne 0x6240
movq -0x30(%rbp), %rdi
leaq -0x22(%rbp), %rsi
xorl %edx, %edx
movl $0x2, %ecx
callq 0x6410
movq -0x30(%rbp), %rdi
movswl -0x22(%rbp), %eax
movl %eax, 0x1008(%rdi)
movl $0x2, %esi
callq 0x64c0
movq -0x30(%rbp), %rax
cmpl $-0x1, 0x1008(%rax)
jne 0x6220
movq -0x30(%rbp), %rax
movl $0xffffffff, 0x100c(%rax) # imm = 0xFFFFFFFF
movq -0x30(%rbp), %rdi
callq 0x6520
movl %eax, %edx
xorl %eax, %eax
movl $0x1, %ecx
cmpl $0x0, %edx
cmovel %ecx, %eax
movl %eax, -0x4(%rbp)
jmp 0x6328
cmpl $0x0, -0x20(%rbp)
jle 0x6261
movq -0x30(%rbp), %rax
movl -0x20(%rbp), %edx
movq 0x1010(%rax), %rcx
movslq %edx, %rdx
addq %rdx, %rcx
movq %rcx, 0x1010(%rax)
movl $0x0, -0x4(%rbp)
jmp 0x6328
movq -0x30(%rbp), %rsi
addq $0x1008, %rsi           # imm = 0x1008
leaq -0x18(%rbp), %rdi
callq 0x6540
movl (%rax), %eax
movl %eax, -0x18(%rbp)
movq -0x30(%rbp), %rdi
movl -0x14(%rbp), %eax
movl %eax, -0x34(%rbp)
xorl %esi, %esi
callq 0x63d0
movl -0x34(%rbp), %edi
movq %rax, %rsi
movslq -0x18(%rbp), %rdx
callq 0x30b0
movl %eax, -0x28(%rbp)
cmpl $0x0, -0x28(%rbp)
jle 0x630d
movq -0x30(%rbp), %rdi
movl -0x28(%rbp), %esi
callq 0x64c0
movq -0x30(%rbp), %rax
cmpl $-0x1, 0x1008(%rax)
je 0x630b
movq -0x30(%rbp), %rax
movl -0x28(%rbp), %edx
movl 0x1008(%rax), %ecx
subl %edx, %ecx
movl %ecx, 0x1008(%rax)
movl -0x28(%rbp), %edx
movl 0x100c(%rax), %ecx
subl %edx, %ecx
movl %ecx, 0x100c(%rax)
cmpl $0x0, 0x1008(%rax)
jne 0x6309
movq -0x30(%rbp), %rax
leaq 0xad3e(%rip), %rcx      # 0x11040
movq %rcx, 0x1010(%rax)
jmp 0x630b
jmp 0x630d
movq -0x30(%rbp), %rdi
callq 0x6520
movl %eax, %edx
xorl %eax, %eax
movl $0x1, %ecx
cmpl $0x0, %edx
cmovel %ecx, %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)
nopl (%rax,%rax)