// File: /workspace/github_cpp_makefile_star2_O2/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 0x1fd92
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 0x22866
testb %al, %al
je 0x1fe31
movq 0x28(%rbx), %rax
movq 0x2e0(%rax), %rdx
movl 0x2d0(%rax), %esi
addq 0x2d8(%rax), %rsi
subq %rsi, %rdx
movl %ebp, %edi
callq 0x88e0
testl %eax, %eax
je 0x1fe00
cmpl $-0x1, %eax
jne 0x1fe36
callq 0x8040
movl (%rax), %edx
xorl %ecx, %ecx
cmpl $0x4, %edx
je 0x1fe42
cmpl $0xb, %edx
je 0x1fe42
jmp 0x1fdc3
movq %rsp, %rsi
movl $0x400, %edx            # imm = 0x400
movl %ebp, %edi
callq 0x88e0
testl %eax, %eax
je 0x1fe00
cmpl $-0x1, %eax
jne 0x1fe40
callq 0x8040
movl (%rax), %ecx
cmpl $0x4, %ecx
je 0x1fe40
cmpl $0xb, %ecx
je 0x1fe40
movq 0x28(%rbx), %r15
addq $0x8, %r15
movl (%rax), %edi
callq 0x81c0
movq %rax, %r12
leaq 0xfc5b(%rip), %rsi      # 0x2fa37
pushq $0x2
popq %rdi
callq 0x28e0a
movq %r15, %rdi
callq 0x20e5b
leaq 0xfc71(%rip), %rdi      # 0x2fa64
callq 0x28f17
movq %r12, %rdi
callq 0x28f31
movq %rbx, %rdi
movq %r14, %rsi
callq 0xd002
movl %ebp, %edi
callq 0x8b50
movq 0x28(%rbx), %rax
movl 0x4ec(%rax), %edi
callq 0x8b50
movq 0x28(%rbx), %rax
orq $-0x1, 0x4ec(%rax)
pushq $0x4
jmp 0x1fe33
pushq $0x1
popq %rcx
jmp 0x1fe42
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 0xaebb