// File: /workspace/github_cpp_makefile_star2_O3/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, %ebp
movq %rdi, %rbx
cmpb $0x0, 0x28(%rdi)
jne 0x2e8a1
movb 0x2b(%rbx), %al
testb %al, %al
jne 0x2e838
cmpb $0x1, 0x29(%rbx)
jne 0x2e838
leaq 0x7de1(%rip), %rax      # 0x36604
movq %rax, 0x30(%rbx)
movb $0x1, 0x2b(%rbx)
movb $0x0, 0x29(%rbx)
movl $0x0, 0x38(%rbx)
jmp 0x2e83c
testb %al, %al
je 0x2e8a1
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 0x2e851
movl %ebp, %edi
movq %r14, %rsi
callq 0x81e0
testl %eax, %eax
js 0x2e964
movl %eax, %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
addq %rcx, %r14
cmpq %r15, %r14
jbe 0x2e983
xorl %ebp, %ebp
movb %bpl, 0x2b(%rbx)
movb %bpl, 0x29(%rbx)
movl $0x0, 0x38(%rbx)
cmpb %bpl, 0x2a(%rbx)
jne 0x2e8fe
jmp 0x2ea4e
cmpl $0x0, 0x1044(%rbx)
je 0x2e8fe
leaq 0x3c(%rbx), %r14
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
je 0x2e910
leaq 0x103c(%rbx), %r15
leal (%rdx,%rcx), %esi
cmpl $0x1000, %esi           # imm = 0x1000
leal -0x1000(%rdx,%rcx), %ecx
cmovbl %esi, %ecx
leal (%r14,%rcx), %edx
cmpl %eax, %ecx
cmoval %edx, %r15d
subl %r12d, %r15d
jmp 0x2e913
movl $0x1, %ebp
movq %rbx, %rdi
callq 0x2ea68
jmp 0x2ea4e
xorl %r15d, %r15d
addq %r12, %r15
leaq 0xf(%rsp), %rdx
movb $0xa, (%rdx)
movq %r12, %rdi
movq %r15, %rsi
callq 0x2ed35
xorl %r13d, %r13d
cmpq %r15, %rax
setne %r13b
addq %rax, %r13
subq %r12, %r13
movq %r12, 0x10(%rsp)
movl %r13d, %ecx
movq %rcx, 0x18(%rsp)
movl $0x1, %edx
movb $0x1, %r12b
cmpq %r15, %rax
jne 0x2e9c9
movl 0x1040(%rbx), %r15d
cmpl %r13d, %r15d
jne 0x2e98b
xorl %r12d, %r12d
jmp 0x2e9c9
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
jmp 0x2ea4e
addl %eax, 0x38(%rbx)
jmp 0x2ea4c
addq %r14, %r15
subq %rcx, %r15
leaq 0xf(%rsp), %rdx
movb $0xa, (%rdx)
movq %r14, %rdi
movq %r15, %rsi
callq 0x2ed35
xorl %r12d, %r12d
cmpq %r15, %rax
setne %r12b
movq %r12, %rcx
addq %rax, %rcx
movq %r14, 0x20(%rsp)
subq %r14, %rcx
movq %rcx, 0x28(%rsp)
addl %ecx, %r13d
movl $0x2, %edx
leaq 0x10(%rsp), %rsi
movl %ebp, %edi
callq 0x8330
testq %rax, %rax
js 0x2ea36
movl %r13d, %ecx
cmpq %rcx, %rax
setne %al
xorb $0x1, %r12b
movl 0x103c(%rbx), %ecx
leal (%rcx,%r13), %edx
cmpl $0x1000, %edx           # imm = 0x1000
leal -0x1000(%rcx,%r13), %ecx
cmovbl %edx, %ecx
movl %ecx, 0x103c(%rbx)
subl %r13d, 0x1040(%rbx)
orb %al, %r12b
movb %r12b, 0x28(%rbx)
jne 0x2ea4c
subl %r13d, 0x1044(%rbx)
je 0x2ea27
cmpb $0x1, 0x2a(%rbx)
jne 0x2ea4c
movl $0x1, %ebp
movq %rbx, %rdi
callq 0x2ea68
jmp 0x2ea4e
callq 0x8050
movl (%rax), %eax
cmpl $0x4, %eax
je 0x2ea4c
movl $0x3, %ebp
cmpl $0xb, %eax
jne 0x2ea4e
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rdi
callq 0xb74e
nop