// File: /workspace/github_cpp_makefile_star2_O2/davmac314[P]dinit/src/dinit-iostream.cc
// Function: dio::ostream::put(char const*, unsigned long)
ssize_t ostream::put(const char *msg, size_t count) noexcept
{
    if (!good()) {
        return -1;
    }

    if (count == 0) {
        return 0;
    }

    size_t remaining_bytes = count;
    while (remaining_bytes > static_cast<unsigned>(buf->get_free())) {
        struct iovec bufv[3];
        char *ptr = buf->get_ptr(0);

        bufv[0].iov_base = ptr;
        bufv[0].iov_len = buf->get_contiguous_length(ptr);
        if (buf->get_length() != bufv[0].iov_len) {
            // buffer's data in streambuf is split into two parts in this case because of cpbuffer nature.
            bufv[1].iov_base = buf->get_buf_base();
            bufv[1].iov_len = buf->get_length() - bufv[0].iov_len;
        }
        else {
            bufv[1].iov_base = nullptr;
            bufv[1].iov_len = 0;
        }
        bufv[2].iov_base = const_cast<char *>(msg);
        bufv[2].iov_len = remaining_bytes;

        ssize_t cur_written_bytes = writev_unintr(fd, bufv, 3);
        if (cur_written_bytes < 0) {
            io_error = errno;
            // Some bytes may be written in previous iteration
            return (count - remaining_bytes > 0) ? count - remaining_bytes : -1;
        }
        if (cur_written_bytes >= buf->get_length()) {
            cur_written_bytes -= buf->get_length();
            buf->consume(buf->get_length());
            remaining_bytes -= cur_written_bytes;
            if (remaining_bytes == 0) {
                return count;
            }
            msg += cur_written_bytes;
        }
        else {
            buf->consume(cur_written_bytes);
        }
    }

    buf->append(msg, remaining_bytes);
    return count;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdi, %r14
movq 0x8(%rdi), %rdi
testq %rdi, %rdi
setne %al
cmpl $0x0, 0x10(%r14)
sete %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0xf1af
movq %rdx, %rbx
testq %rdx, %rdx
je 0xf1b4
movq %rsi, %r15
pushq $0x3
popq %rbp
movq %rbx, %r12
movl $0x4000, %eax           # imm = 0x4000
subl 0x4004(%rdi), %eax
cmpq %rax, %r12
jbe 0xf1b8
movl 0x4000(%rdi), %eax
leal -0x4000(%rax), %esi
cmpl $0x4000, %eax           # imm = 0x4000
cmovbl %eax, %esi
addq %rdi, %rsi
movq %rsi, (%rsp)
callq 0xffd4
movl %eax, %edx
movq %rdx, 0x8(%rsp)
movq 0x8(%r14), %rsi
movl 0x4004(%rsi), %ecx
cmpl %ecx, %eax
jne 0xf156
andq $0x0, 0x10(%rsp)
xorl %ecx, %ecx
jmp 0xf15e
movq %rsi, 0x10(%rsp)
subq %rdx, %rcx
movq %rcx, 0x18(%rsp)
movq %r15, 0x20(%rsp)
movq %r12, 0x28(%rsp)
movl (%r14), %edi
movq %rsp, %rsi
movl %ebp, %edx
callq 0x10010
testl %eax, %eax
js 0xf1c5
movslq %eax, %r13
movq 0x8(%r14), %rdi
movl 0x4004(%rdi), %esi
subq %rsi, %r13
jge 0xf199
movl %eax, %esi
callq 0x1004a
jmp 0xf1a6
callq 0x1004a
subq %r13, %r12
je 0xf1da
addq %r13, %r15
movq 0x8(%r14), %rdi
jmp 0xf103
pushq $-0x1
popq %rbx
jmp 0xf1da
xorl %ebx, %ebx
jmp 0xf1da
movq %r15, %rsi
movl %r12d, %edx
callq 0x10070
jmp 0xf1da
callq 0x4040
movl (%rax), %eax
movl %eax, 0x10(%r14)
subq %r12, %rbx
pushq $-0x1
popq %rax
cmoveq %rax, %rbx
movq %rbx, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rdi
callq 0xac53