// File: /workspace/github_cpp_makefile_star2_O1/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 0x24100
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 0x27186
movl $0x1, %r15d
testb %al, %al
je 0x24139
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 0x2415f
cmpl $-0x1, %eax
jne 0x24167
callq 0x8050
movl (%rax), %ecx
xorl %eax, %eax
cmpl $0x4, %ecx
je 0x24173
cmpl $0xb, %ecx
je 0x24173
movl $0x2, %r15d
jmp 0x24173
movq %rsp, %rsi
movl $0x400, %edx            # imm = 0x400
movl %ebp, %edi
callq 0x88f0
testl %eax, %eax
je 0x24140
cmpl $-0x1, %eax
jne 0x24147
callq 0x8050
movl (%rax), %eax
cmpl $0xb, %eax
sete %cl
cmpl $0x4, %eax
sete %al
orb %cl, %al
movzbl %al, %ecx
movl $0x2, %eax
subl %ecx, %eax
jmp 0x2414c
movl $0x1, %eax
jmp 0x24173
movl $0x3, %eax
jmp 0x2414c
movl $0x1, %eax
cmpl $0x1, %eax
je 0x24158
cmpl $0x2, %eax
je 0x24183
jmp 0x241c5
xorl %eax, %eax
jmp 0x241fd
movl $0x3, %r15d
jmp 0x24173
movq 0x28(%rbx), %rcx
addl %eax, 0x2d0(%rcx)
xorl %eax, %eax
cmpl $0x1, %r15d
je 0x241fd
cmpl $0x3, %r15d
je 0x241c5
movq 0x28(%rbx), %r12
callq 0x8050
movl (%rax), %edi
callq 0x81d0
movq %rax, %r15
leaq 0x118fb(%rip), %rsi     # 0x35a98
movl $0x2, %edi
callq 0x2e63a
movq 0x8(%r12), %rdi
callq 0x2e73d
leaq 0x1190d(%rip), %rdi     # 0x35ac5
callq 0x2e73d
movq %r15, %rdi
callq 0x2e759
movl 0x1c(%rbx), %edx
movq %r14, %rdi
movq %rbx, %rsi
callq 0xda6a
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, %eax
addq $0x400, %rsp            # imm = 0x400
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rdi
callq 0xb8be
nop