// File: 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
movq %rsp, %rbp
subq $0x490, %rsp            # imm = 0x490
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movl %ecx, -0x20(%rbp)
movq -0x10(%rbp), %rcx
movq %rcx, -0x458(%rbp)
movq 0x28(%rcx), %rax
movl 0x2d0(%rax), %eax
movq 0x28(%rcx), %rcx
cmpl 0x2cc(%rcx), %eax
jb 0x4fd66
movl -0x1c(%rbp), %edi
leaq -0x420(%rbp), %rsi
movl $0x400, %edx            # imm = 0x400
callq 0xaac0
movq %rax, -0x460(%rbp)
jmp 0x4fd07
movq -0x460(%rbp), %rax
movl %eax, -0x424(%rbp)
cmpl $-0x1, -0x424(%rbp)
jne 0x4fd4c
callq 0xa060
cmpl $0xb, (%rax)
je 0x4fd3b
callq 0xa060
cmpl $0xb, (%rax)
je 0x4fd3b
callq 0xa060
cmpl $0x4, (%rax)
jne 0x4fd47
movl $0x0, -0x4(%rbp)
jmp 0x4ffec
jmp 0x4ff40
cmpl $0x0, -0x424(%rbp)
jne 0x4fd5a
jmp 0x4ff90
movl $0x0, -0x4(%rbp)
jmp 0x4ffec
movq -0x458(%rbp), %rax
movq 0x28(%rax), %rax
movl 0x2cc(%rax), %eax
shrl $0x3, %eax
movl %eax, -0x434(%rbp)
movl $0x100, -0x438(%rbp)    # imm = 0x100
leaq -0x434(%rbp), %rdi
leaq -0x438(%rbp), %rsi
callq 0x52bc0
movq %rax, -0x468(%rbp)
jmp 0x4fda6
movq -0x458(%rbp), %rax
movq -0x468(%rbp), %rcx
movl (%rcx), %ecx
movq %rcx, -0x430(%rbp)
movl -0x430(%rbp), %ecx
movl %ecx, -0x43c(%rbp)
movq 0x28(%rax), %rcx
movl 0x2cc(%rcx), %eax
movl 0x2d0(%rcx), %ecx
subl %ecx, %eax
movl %eax, -0x440(%rbp)
leaq -0x43c(%rbp), %rdi
leaq -0x440(%rbp), %rsi
callq 0x52c00
movq %rax, -0x470(%rbp)
jmp 0x4fdfd
movq -0x458(%rbp), %rax
movq -0x470(%rbp), %rcx
movl (%rcx), %ecx
movq %rcx, -0x430(%rbp)
movq 0x28(%rax), %rcx
movl 0x2d0(%rcx), %ecx
addq -0x430(%rbp), %rcx
movl %ecx, -0x444(%rbp)
movq 0x28(%rax), %rdi
movl -0x444(%rbp), %esi
callq 0x562b0
testb $0x1, %al
jne 0x4fe4a
movl $0x1, -0x4(%rbp)
jmp 0x4ffec
movq -0x458(%rbp), %rax
movq 0x28(%rax), %rdi
addq $0x2d8, %rdi            # imm = 0x2D8
callq 0x196b0
movq %rax, %rcx
movq -0x458(%rbp), %rax
movq 0x28(%rax), %rdx
movl 0x2d0(%rdx), %edx
subq %rdx, %rcx
movq %rcx, -0x430(%rbp)
movl -0x1c(%rbp), %ecx
movl %ecx, -0x47c(%rbp)
movq 0x28(%rax), %rdi
addq $0x2d8, %rdi            # imm = 0x2D8
callq 0x18f20
movl -0x47c(%rbp), %edi
movq %rax, %rsi
movq -0x458(%rbp), %rax
movq 0x28(%rax), %rax
movl 0x2d0(%rax), %eax
addq %rax, %rsi
movq -0x430(%rbp), %rdx
callq 0xaac0
movq %rax, -0x478(%rbp)
jmp 0x4feca
movq -0x478(%rbp), %rax
movl %eax, -0x448(%rbp)
cmpl $-0x1, -0x448(%rbp)
jne 0x4ff0c
callq 0xa060
cmpl $0xb, (%rax)
je 0x4fefe
callq 0xa060
cmpl $0xb, (%rax)
je 0x4fefe
callq 0xa060
cmpl $0x4, (%rax)
jne 0x4ff0a
movl $0x0, -0x4(%rbp)
jmp 0x4ffec
jmp 0x4ff40
cmpl $0x0, -0x448(%rbp)
jne 0x4ff17
jmp 0x4ff90
movq -0x458(%rbp), %rax
movl -0x448(%rbp), %ecx
movq 0x28(%rax), %rax
addl 0x2d0(%rax), %ecx
movl %ecx, 0x2d0(%rax)
movl $0x0, -0x4(%rbp)
jmp 0x4ffec
movq -0x458(%rbp), %rax
movq 0x28(%rax), %rdi
callq 0x2d420
movq %rax, -0x488(%rbp)
callq 0xa060
movl (%rax), %edi
callq 0xa1f0
movq -0x488(%rbp), %rdx
movq %rax, -0x450(%rbp)
movl $0x2, %edi
leaq 0x28a92(%rip), %rsi     # 0x78a0f
leaq 0x28ab8(%rip), %rcx     # 0x78a3c
leaq -0x450(%rbp), %r8
callq 0x50000
movq -0x458(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x17d20
movl -0x1c(%rbp), %edi
callq 0xad80
jmp 0x4ffaa
movq -0x458(%rbp), %rax
movq 0x28(%rax), %rax
movl 0x4ec(%rax), %edi
callq 0xad80
jmp 0x4ffc2
movq -0x458(%rbp), %rax
movq 0x28(%rax), %rcx
movl $0xffffffff, 0x4f0(%rcx) # imm = 0xFFFFFFFF
movq 0x28(%rax), %rax
movl $0xffffffff, 0x4ec(%rax) # imm = 0xFFFFFFFF
movl $0x4, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x490, %rsp            # imm = 0x490
popq %rbp
retq
movq %rax, %rdi
callq 0xfa30