// File: /workspace/github_cpp_makefile_star2_O3/davmac314[P]dinit/src/proc-service.cc
// Function: log_output_watcher::fd_event(dasynq::v2::event_loop<dasynq::null_mutex, dasynq::v2::default_traits<dasynq::null_mutex>>&, int, int)
rearm log_output_watcher::fd_event(eventloop_t &eloop, int fd, int flags) noexcept
{
    // In case buffer size has been decreased, check if we are already at the limit:
    if (service->log_buf_size >= service->log_buf_max) {
        // If so, read and discard.
        char buf[1024];
        int r = bp_sys::read(fd, buf, 1024);
        if (r == -1) {
            if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) {
                return rearm::REARM;
            }
            goto bad_read;
        }
        if (r == 0) goto eof_read;
        return rearm::REARM;
    }

    {
        size_t max_read = std::max(service->log_buf_max / 8, 256u);
        max_read = std::min((unsigned)max_read, service->log_buf_max - service->log_buf_size);

        // ensure vector has size sufficient to read
        unsigned new_size = service->log_buf_size + max_read;
        if (!service->ensure_log_buffer_backing(new_size)) {
            return rearm::DISARM;
        }

        max_read = service->log_buffer.size() - service->log_buf_size;

        int r = bp_sys::read(fd, service->log_buffer.data() + service->log_buf_size, max_read);
        if (r == -1) {
            if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) {
                return rearm::REARM;
            }
            goto bad_read;
        }

        if (r == 0) goto eof_read;

        service->log_buf_size += r;
        return rearm::REARM;
    }

    // error/end-of-stream handling:

    bad_read:
    log(loglevel_t::WARN, "Service ", service->get_name(), " output not readable: ", strerror(errno));

    eof_read:
    deregister(eloop);
    close(fd);
    close(service->log_output_fd);
    service->log_input_fd = -1;
    service->log_output_fd = -1;
    return rearm::REMOVED;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x400, %rsp            # imm = 0x400
movl %edx, %ebp
movq %rsi, %r14
movq %rdi, %rbx
movq 0x28(%rdi), %rdi
movl 0x2cc(%rdi), %edx
movl 0x2d0(%rdi), %eax
movl %edx, %ecx
subl %eax, %ecx
jbe 0x243fd
shrl $0x3, %edx
cmpl $0x101, %edx            # imm = 0x101
movl $0x100, %esi            # imm = 0x100
cmovael %edx, %esi
cmpl %esi, %ecx
cmovbl %ecx, %esi
addl %eax, %esi
callq 0x271e6
movl $0x1, %ecx
testb %al, %al
je 0x244b5
movq 0x28(%rbx), %rax
movq 0x2e0(%rax), %rdx
movl 0x2d0(%rax), %esi
addq 0x2d8(%rax), %rsi
subq %rsi, %rdx
movl %ebp, %edi
callq 0x88f0
testl %eax, %eax
je 0x2446f
cmpl $-0x1, %eax
jne 0x244a9
callq 0x8050
movl (%rax), %edx
xorl %ecx, %ecx
cmpl $0x4, %edx
je 0x244b5
cmpl $0xb, %edx
je 0x244b5
jmp 0x24432
movq %rsp, %rsi
movl $0x400, %edx            # imm = 0x400
movl %ebp, %edi
callq 0x88f0
testl %eax, %eax
je 0x2446f
cmpl $-0x1, %eax
jne 0x244b3
callq 0x8050
movl (%rax), %ecx
cmpl $0x4, %ecx
je 0x244b3
cmpl $0xb, %ecx
je 0x244b3
movq 0x28(%rbx), %r12
movl (%rax), %edi
callq 0x81d0
movq %rax, %r15
leaq 0x11608(%rip), %rsi     # 0x35a4f
movl $0x2, %edi
callq 0x2e412
movq 0x8(%r12), %rdi
callq 0x2e520
leaq 0x1161a(%rip), %rdi     # 0x35a7c
callq 0x2e520
movq %r15, %rdi
callq 0x2e53c
movl 0x1c(%rbx), %edx
movq %r14, %rdi
movq %rbx, %rsi
callq 0xd520
movl %ebp, %edi
callq 0x8b60
movq 0x28(%rbx), %rax
movl 0x4ec(%rax), %edi
callq 0x8b60
movq 0x28(%rbx), %rax
movq $-0x1, 0x4ec(%rax)
movl $0x4, %ecx
jmp 0x244b5
movq 0x28(%rbx), %rcx
addl %eax, 0x2d0(%rcx)
xorl %ecx, %ecx
movl %ecx, %eax
addq $0x400, %rsp            # imm = 0x400
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rdi
callq 0xb74e
nop