// File: /workspace/github_cpp_makefile_star2_O2/davmac314[P]dinit/src/dinit-log.cc
// Function: (anonymous namespace)::buffered_log_stream::fd_event(dasynq::v2::event_loop<dasynq::null_mutex, dasynq::v2::default_traits<dasynq::null_mutex>>&, int, int)
rearm buffered_log_stream::fd_event(eventloop_t &loop, int fd, int flags) noexcept
{
    if ((! partway) && (! special) && discarded) {
        special_buf = "dinit: *** log message discarded due to full buffer ***\n";
        special = true;
        discarded = false;
        msg_index = 0;
    }

    if ((! partway) && special) {
        const char * start = special_buf + msg_index;
        const char * end = start;
        while (*end != '\n') end++;
        int r = bp_sys::write(fd, start, end - start + 1);
        if (r >= 0) {
            if (start + r > end) {
                // All written: go on to next message in queue
                special = false;
                discarded = false;
                msg_index = 0;
                
                if (release) {
                    release_console();
                    return rearm::DISARM;
                }
            }
            else {
                msg_index += r;
                return rearm::REARM;
            }
        }
        else if (errno != EAGAIN && errno != EINTR && errno != EWOULDBLOCK) {
            return rearm::REMOVE;
        }
        return rearm::REARM;
    }
    else {
        // Writing from the regular circular buffer
        
        if (current_index == 0) {
            release_console();
            return rearm::DISARM;
        }
        
        // We try to find a complete line (terminated by '\n') in the buffer, and write it
        // out. Since it may span the circular buffer end, it may consist of two distinct spans,
        // and so we use writev to write them atomically.
        
        struct iovec logiov[2];
        
        char *ptr = log_buffer.get_ptr(0);
        unsigned len = log_buffer.get_contiguous_length(ptr);
        char *creptr = ptr + len;  // contiguous region end
        char *eptr = std::find(ptr, creptr, '\n');
        
        bool will_complete = false;  // will complete this message?
        if (eptr != creptr) {
            eptr++;  // include '\n'
            will_complete = true;
        }

        len = eptr - ptr;
        
        logiov[0].iov_base = ptr;
        logiov[0].iov_len = len;
        int iovs_to_write = 1;
        
        // Do we need the second span?
        if (!will_complete && len != log_buffer.get_length()) {
            ptr = log_buffer.get_buf_base();
            creptr = ptr + log_buffer.get_length() - len;
            eptr = std::find(ptr, creptr, '\n');
            if (eptr != creptr) {
                eptr++; // include '\n'
                // It should not ever be the case that we do not now have a complete message
                will_complete = true;
            }
            logiov[1].iov_base = ptr;
            logiov[1].iov_len = eptr - ptr;
            len += logiov[1].iov_len;
            iovs_to_write = 2;
        }
        
        ssize_t r = bp_sys::writev(fd, logiov, iovs_to_write);

        if (r >= 0) {
            bool complete = (r == len) && will_complete;
            log_buffer.consume(len);
            partway = ! complete;
            if (complete) {
                current_index -= len;
                if (current_index == 0 || release) {
                    // No more messages buffered / stop logging to console:
                    release_console();
                    return rearm::DISARM;
                }
            }
        }
        else if (errno != EAGAIN && errno != EINTR && errno != EWOULDBLOCK) {
            return rearm::REMOVE;
        }
    }
    
    // We've written something by the time we get here. We could fall through to below, but
    // let's give other events a chance to be processed by returning now.
    return rearm::REARM;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdi, %rbx
cmpb $0x0, 0x28(%rdi)
jne 0x29210
movb 0x2b(%rbx), %al
testb %al, %al
jne 0x291a9
cmpb $0x1, 0x29(%rbx)
jne 0x291a9
leaq 0x747d(%rip), %rax      # 0x30614
movq %rax, 0x30(%rbx)
movb $0x1, 0x2b(%rbx)
movb $0x0, 0x29(%rbx)
andl $0x0, 0x38(%rbx)
jmp 0x291ad
testb %al, %al
je 0x29210
movq 0x30(%rbx), %rax
movslq 0x38(%rbx), %rcx
leaq (%rax,%rcx), %r14
leaq (%rax,%rcx), %r15
decq %r15
xorl %edx, %edx
incq %rdx
cmpb $0xa, 0x1(%r15)
leaq 0x1(%r15), %r15
jne 0x291c2
movl %esi, %edi
movq %r14, %rsi
callq 0x81d0
testl %eax, %eax
js 0x292aa
movl %eax, %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
addq %rcx, %r14
cmpq %r15, %r14
jbe 0x292c9
xorl %eax, %eax
movb %al, 0x2b(%rbx)
movb %al, 0x29(%rbx)
andl $0x0, 0x38(%rbx)
cmpb %al, 0x2a(%rbx)
je 0x29370
jmp 0x2929d
cmpl $0x0, 0x1044(%rbx)
je 0x2929d
movl %esi, 0xc(%rsp)
leaq 0x3c(%rbx), %r14
movl 0x103c(%rbx), %eax
leal -0x1000(%rax), %ecx
cmpl $0x1000, %eax           # imm = 0x1000
cmovbl %eax, %ecx
leaq (%rbx,%rcx), %r12
addq $0x3c, %r12
movq %r14, %rdi
movq %r12, %rsi
callq 0x295b2
movl %eax, %r13d
addq %r12, %r13
leaq 0xb(%rsp), %rdx
movb $0xa, (%rdx)
movq %r12, %rdi
movq %r13, %rsi
callq 0x29614
xorl %r15d, %r15d
cmpq %r13, %rax
setne %r15b
addq %rax, %r15
subq %r12, %r15
movq %r12, 0x10(%rsp)
movl %r15d, %ecx
movq %rcx, 0x18(%rsp)
pushq $0x1
popq %rdx
movb $0x1, %bpl
cmpq %r13, %rax
jne 0x2930c
movl 0x1040(%rbx), %r12d
cmpl %r15d, %r12d
jne 0x292d1
xorl %ebp, %ebp
jmp 0x2930c
movq %rbx, %rdi
callq 0x2938c
jmp 0x29358
callq 0x8040
movl (%rax), %eax
xorl %ecx, %ecx
cmpl $0x4, %eax
setne %cl
xorl %edx, %edx
cmpl $0xb, %eax
leal (%rcx,%rcx,2), %eax
cmovel %edx, %eax
jmp 0x29370
addl %eax, 0x38(%rbx)
jmp 0x2936e
addq %r14, %r12
subq %rcx, %r12
leaq 0xb(%rsp), %rdx
movb $0xa, (%rdx)
movq %r14, %rdi
movq %r12, %rsi
callq 0x29614
xorl %ebp, %ebp
cmpq %r12, %rax
setne %bpl
movq %rbp, %rcx
addq %rax, %rcx
movq %r14, 0x20(%rsp)
subq %r14, %rcx
movq %rcx, 0x28(%rsp)
addl %ecx, %r15d
pushq $0x2
popq %rdx
leaq 0x10(%rsp), %rsi
movl 0xc(%rsp), %edi
callq 0x8330
testq %rax, %rax
js 0x2935d
movl %r15d, %ecx
cmpq %rcx, %rax
setne %r12b
xorb $0x1, %bpl
movq %r14, %rdi
movl %r15d, %esi
callq 0x295ee
orb %r12b, %bpl
movb %bpl, 0x28(%rbx)
jne 0x2936e
subl %r15d, 0x1044(%rbx)
je 0x29350
cmpb $0x1, 0x2a(%rbx)
jne 0x2936e
movq %rbx, %rdi
callq 0x2938c
pushq $0x1
popq %rax
jmp 0x29370
callq 0x8040
movl (%rax), %eax
cmpl $0x4, %eax
je 0x2936e
cmpl $0xb, %eax
jne 0x2937f
xorl %eax, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x3
jmp 0x2935a
movq %rax, %rdi
callq 0xaebb
nop