// File: /workspace/github_cpp_makefile_star2_O1/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
movl %esi, %r13d
movq %rdi, %rbx
movb 0x28(%rdi), %al
testb %al, %al
jne 0x2eab9
cmpb $0x0, 0x2b(%rbx)
jne 0x2ea49
cmpb $0x1, 0x29(%rbx)
jne 0x2ea49
leaq 0x7bfe(%rip), %rcx      # 0x36634
movq %rcx, 0x30(%rbx)
movb $0x1, 0x2b(%rbx)
movb $0x0, 0x29(%rbx)
movl $0x0, 0x38(%rbx)
testb %al, %al
jne 0x2eab9
cmpb $0x1, 0x2b(%rbx)
jne 0x2eab9
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 0x2ea68
movl %r13d, %edi
movq %r14, %rsi
callq 0x81e0
testl %eax, %eax
js 0x2ec3f
movl %eax, %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
addq %rcx, %r14
cmpq %r15, %r14
jbe 0x2ec6a
xorl %ebp, %ebp
movb %bpl, 0x2b(%rbx)
movb %bpl, 0x29(%rbx)
movl $0x0, 0x38(%rbx)
cmpb %bpl, 0x2a(%rbx)
jne 0x2eb1a
jmp 0x2ec59
cmpl $0x0, 0x1044(%rbx)
je 0x2eb1a
leaq 0x3c(%rbx), %rdi
movl 0x103c(%rbx), %ecx
movl 0x1040(%rbx), %edx
leal -0x1000(%rcx), %eax
cmpl $0x1000, %ecx           # imm = 0x1000
cmovbl %ecx, %eax
leaq (%rbx,%rax), %r12
addq $0x3c, %r12
testl %edx, %edx
movq %rdi, 0x8(%rsp)
je 0x2eb2c
leaq 0x103c(%rbx), %r15
leal (%rdx,%rcx), %esi
cmpl $0x1000, %esi           # imm = 0x1000
leal -0x1000(%rdx,%rcx), %ecx
cmovbl %esi, %ecx
leal (%rdi,%rcx), %edx
cmpl %eax, %ecx
cmoval %edx, %r15d
subl %r12d, %r15d
jmp 0x2eb2f
movl $0x1, %ebp
movq %rbx, %rdi
callq 0x2ec7a
jmp 0x2ec59
xorl %r15d, %r15d
addq %r12, %r15
leaq 0x10(%rsp), %rdx
movb $0xa, (%rdx)
movq %r12, %rdi
movq %r15, %rsi
callq 0x2ef47
movq %rax, %rbp
movl %r13d, 0x4(%rsp)
xorl %r13d, %r13d
cmpq %r15, %rax
setne %r13b
movq %r13, %r14
addq %rax, %r14
subq %r12, %r14
movq %r12, 0x10(%rsp)
movl %r14d, %eax
movq %rax, 0x18(%rsp)
movl $0x1, %edx
cmpq %r15, %rbp
jne 0x2ebce
movl 0x1040(%rbx), %r12d
cmpl %r14d, %r12d
je 0x2ebce
movq 0x8(%rsp), %rdi
addq %rdi, %r12
subq %rax, %r12
leaq 0x3(%rsp), %rdx
movb $0xa, (%rdx)
movq %r12, %rsi
callq 0x2ef47
cmpq %r15, %rbp
setne %cl
xorl %r13d, %r13d
cmpq %r12, %rax
setne %r13b
addq %r13, %rax
movq 0x8(%rsp), %rdx
movq %rdx, 0x20(%rsp)
subq %rdx, %rax
movq %rax, 0x28(%rsp)
addl %eax, %r14d
orb %cl, %r13b
movl $0x2, %edx
leaq 0x10(%rsp), %rsi
movl 0x4(%rsp), %edi
callq 0x8330
testq %rax, %rax
js 0x2ec3f
movl %r14d, %ecx
cmpq %rcx, %rax
setne %al
xorb $0x1, %r13b
movl 0x103c(%rbx), %ecx
leal (%rcx,%r14), %edx
cmpl $0x1000, %edx           # imm = 0x1000
leal -0x1000(%rcx,%r14), %ecx
cmovbl %edx, %ecx
movl %ecx, 0x103c(%rbx)
subl %r14d, 0x1040(%rbx)
xorl %ebp, %ebp
orb %al, %r13b
movb %r13b, 0x28(%rbx)
jne 0x2ec59
subl %r14d, 0x1044(%rbx)
je 0x2ec30
cmpb $0x1, 0x2a(%rbx)
jne 0x2ec59
movl $0x1, %ebp
movq %rbx, %rdi
callq 0x2ec7a
jmp 0x2ec59
callq 0x8050
movl (%rax), %eax
xorl %ecx, %ecx
cmpl $0x4, %eax
setne %cl
xorl %edx, %edx
cmpl $0xb, %eax
leal (%rcx,%rcx,2), %ebp
cmovel %edx, %ebp
movl %ebp, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
addl %eax, 0x38(%rbx)
xorl %ebp, %ebp
jmp 0x2ec59
movq %rax, %rdi
callq 0xb8be
nop