// File: davmac314[P]dinit/src/shutdown.cc
// Function: subproc_buffer::mark_overflow()
void mark_overflow()
    {
        // Try to find the last newline in the buffer
        int begin = 0;
        if (last_overflow != -1) {
            begin = last_overflow + sizeof(int16_t);
        }
        int end = get_length() - 1 - sizeof(int16_t); // -1, then -2 for storage of marker

        int i;
        for (i = end; i >= begin; i--) {
            if ((*this)[i] == '\n') break;
        }

        if (last_overflow != -1 && i < begin) {
            // No new line after existing marker: trim all beyond that marker, don't
            // create a new marker:
            trim_to(last_overflow + sizeof(uint16_t));
            return;
        }

        if (i < begin) {
            // No newline in the whole buffer... we'll put the overflow marker at the end,
            // on the assumption that it is better to output a partial line than it is to
            // discard the entire buffer:
            last_overflow = get_length() - sizeof(int16_t);
            overflow_marker = last_overflow;
            int16_t overflow16 = -1;
            char * overflow16_ptr = reinterpret_cast<char *>(&overflow16);
            *get_ptr(last_overflow + 0) = overflow16_ptr[0];
            *get_ptr(last_overflow + 1) = overflow16_ptr[1];
            return;
        }

        // We found a newline, put the overflow marker just after it:
        int new_overflow = i + 1;
        if (last_overflow != -1) {
            int16_t new_overflow16 = new_overflow;
            char * new_overflow16_ptr = reinterpret_cast<char *>(&new_overflow16);
            *get_ptr(last_overflow + 0) = new_overflow16_ptr[0];
            *get_ptr(last_overflow + 1) = new_overflow16_ptr[1];
        }
        last_overflow = new_overflow;
        if (overflow_marker == -1) {
            overflow_marker = last_overflow;
        }

        int16_t overflow16 = -1;
        char * overflow16_ptr = reinterpret_cast<char *>(&overflow16);
        *get_ptr(last_overflow + 0) = overflow16_ptr[0];
        *get_ptr(last_overflow + 0) = overflow16_ptr[1];
        trim_to(last_overflow + sizeof(int16_t));
    }

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x48(%rbp)
movl $0x0, -0xc(%rbp)
cmpl $-0x1, 0x100c(%rax)
je 0x7af6
movq -0x48(%rbp), %rax
movslq 0x100c(%rax), %rax
addq $0x2, %rax
movl %eax, -0xc(%rbp)
movq -0x48(%rbp), %rdi
callq 0x6520
subl $0x1, %eax
movl %eax, %eax
subq $0x2, %rax
movl %eax, -0x10(%rbp)
movl -0x10(%rbp), %eax
movl %eax, -0x14(%rbp)
movl -0x14(%rbp), %eax
cmpl -0xc(%rbp), %eax
jl 0x7b3c
movq -0x48(%rbp), %rdi
movl -0x14(%rbp), %esi
callq 0x8150
movsbl %al, %eax
cmpl $0xa, %eax
jne 0x7b2f
jmp 0x7b3c
jmp 0x7b31
movl -0x14(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x7b11
movq -0x48(%rbp), %rax
cmpl $-0x1, 0x100c(%rax)
je 0x7b6c
movl -0x14(%rbp), %eax
cmpl -0xc(%rbp), %eax
jge 0x7b6c
movq -0x48(%rbp), %rdi
movslq 0x100c(%rdi), %rax
addq $0x2, %rax
movl %eax, %esi
callq 0x8190
jmp 0x7cda
movl -0x14(%rbp), %eax
cmpl -0xc(%rbp), %eax
jge 0x7be9
movq -0x48(%rbp), %rdi
callq 0x6520
movq -0x48(%rbp), %rdi
movl %eax, %eax
subq $0x2, %rax
movl %eax, 0x100c(%rdi)
movl 0x100c(%rdi), %eax
movl %eax, 0x1008(%rdi)
movw $0xffff, -0x16(%rbp)    # imm = 0xFFFF
leaq -0x16(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
movb (%rax), %al
movb %al, -0x4a(%rbp)
movl 0x100c(%rdi), %esi
addl $0x0, %esi
callq 0x63d0
movb -0x4a(%rbp), %cl
movq -0x48(%rbp), %rdi
movb %cl, (%rax)
movq -0x20(%rbp), %rax
movb 0x1(%rax), %al
movb %al, -0x49(%rbp)
movl 0x100c(%rdi), %esi
addl $0x1, %esi
callq 0x63d0
movb -0x49(%rbp), %cl
movb %cl, (%rax)
jmp 0x7cda
movq -0x48(%rbp), %rax
movl -0x14(%rbp), %ecx
addl $0x1, %ecx
movl %ecx, -0x24(%rbp)
cmpl $-0x1, 0x100c(%rax)
je 0x7c4f
movq -0x48(%rbp), %rdi
movl -0x24(%rbp), %eax
movw %ax, -0x26(%rbp)
leaq -0x26(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movb (%rax), %al
movb %al, -0x4c(%rbp)
movl 0x100c(%rdi), %esi
addl $0x0, %esi
callq 0x63d0
movb -0x4c(%rbp), %cl
movq -0x48(%rbp), %rdi
movb %cl, (%rax)
movq -0x30(%rbp), %rax
movb 0x1(%rax), %al
movb %al, -0x4b(%rbp)
movl 0x100c(%rdi), %esi
addl $0x1, %esi
callq 0x63d0
movb -0x4b(%rbp), %cl
movb %cl, (%rax)
movq -0x48(%rbp), %rax
movl -0x24(%rbp), %ecx
movl %ecx, 0x100c(%rax)
cmpl $-0x1, 0x1008(%rax)
jne 0x7c75
movq -0x48(%rbp), %rax
movl 0x100c(%rax), %ecx
movl %ecx, 0x1008(%rax)
movq -0x48(%rbp), %rdi
movw $0xffff, -0x32(%rbp)    # imm = 0xFFFF
leaq -0x32(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
movb (%rax), %al
movb %al, -0x4e(%rbp)
movl 0x100c(%rdi), %esi
addl $0x0, %esi
callq 0x63d0
movb -0x4e(%rbp), %cl
movq -0x48(%rbp), %rdi
movb %cl, (%rax)
movq -0x40(%rbp), %rax
movb 0x1(%rax), %al
movb %al, -0x4d(%rbp)
movl 0x100c(%rdi), %esi
addl $0x0, %esi
callq 0x63d0
movb -0x4d(%rbp), %cl
movq -0x48(%rbp), %rdi
movb %cl, (%rax)
movslq 0x100c(%rdi), %rax
addq $0x2, %rax
movl %eax, %esi
callq 0x8190
addq $0x50, %rsp
popq %rbp
retq