// File: /workspace/github_cpp_makefile_star2_O2/sangwoojun[P]rv32emulator/./linenoise.hpp
// Function: linenoise::linenoiseEdit(int, int, char*, int, char const*)
inline int linenoiseEdit(int stdin_fd, int stdout_fd, char *buf, int buflen, const char *prompt)
{
    struct linenoiseState l;

    /* Populate the linenoise state that we pass to functions implementing
     * specific editing functionalities. */
    l.ifd = stdin_fd;
    l.ofd = stdout_fd;
    l.buf = buf;
    l.buflen = buflen;
    l.prompt = prompt;
    l.oldcolpos = l.pos = 0;
    l.len = 0;
    l.cols = getColumns(stdin_fd, stdout_fd);
    l.maxrows = 0;
    l.history_index = 0;

    /* Buffer starts empty. */
    l.buf[0] = '\0';
    l.buflen--; /* Make sure there is always space for the nulterm */

    /* The latest history entry is always our current buffer, that
     * initially is just an empty string. */
    AddHistory("");

    if (write(l.ofd,prompt, static_cast<int>(l.prompt.length())) == -1) return -1;
    while(1) {
        int c;
        char cbuf[4];
        int nread;
        char seq[3];

#ifdef _WIN32
        nread = win32read(&c);
        if (nread == 1) {
            cbuf[0] = c;
        }
#else
        nread = unicodeReadUTF8Char(l.ifd,cbuf,&c);
#endif
        if (nread <= 0) return (int)l.len;

        /* Only autocomplete when the callback is set. It returns < 0 when
         * there was an error reading from fd. Otherwise it will return the
         * character that should be handled next. */
        if (c == 9 && completionCallback != NULL) {
            nread = completeLine(&l,cbuf,&c);
            /* Return on errors */
            if (c < 0) return l.len;
            /* Read next character when 0 */
            if (c == 0) continue;
        }

        switch(c) {
        case ENTER:    /* enter */
            if (!history.empty()) history.pop_back();
            if (mlmode) linenoiseEditMoveEnd(&l);
            return (int)l.len;
        case CTRL_C:     /* ctrl-c */
            errno = EAGAIN;
            return -1;
        case BACKSPACE:   /* backspace */
        case 8:     /* ctrl-h */
            linenoiseEditBackspace(&l);
            break;
        case CTRL_D:     /* ctrl-d, remove char at right of cursor, or if the
                            line is empty, act as end-of-file. */
            if (l.len > 0) {
                linenoiseEditDelete(&l);
            } else {
                history.pop_back();
                return -1;
            }
            break;
        case CTRL_T:    /* ctrl-t, swaps current character with previous. */
            if (l.pos > 0 && l.pos < l.len) {
                char aux = buf[l.pos-1];
                buf[l.pos-1] = buf[l.pos];
                buf[l.pos] = aux;
                if (l.pos != l.len-1) l.pos++;
                refreshLine(&l);
            }
            break;
        case CTRL_B:     /* ctrl-b */
            linenoiseEditMoveLeft(&l);
            break;
        case CTRL_F:     /* ctrl-f */
            linenoiseEditMoveRight(&l);
            break;
        case CTRL_P:    /* ctrl-p */
            linenoiseEditHistoryNext(&l, LINENOISE_HISTORY_PREV);
            break;
        case CTRL_N:    /* ctrl-n */
            linenoiseEditHistoryNext(&l, LINENOISE_HISTORY_NEXT);
            break;
        case ESC:    /* escape sequence */
            /* Read the next two bytes representing the escape sequence.
             * Use two calls to handle slow terminals returning the two
             * chars at different times. */
            if (read(l.ifd,seq,1) == -1) break;
            if (read(l.ifd,seq+1,1) == -1) break;

            /* ESC [ sequences. */
            if (seq[0] == '[') {
                if (seq[1] >= '0' && seq[1] <= '9') {
                    /* Extended escape, read additional byte. */
                    if (read(l.ifd,seq+2,1) == -1) break;
                    if (seq[2] == '~') {
                        switch(seq[1]) {
                        case '3': /* Delete key. */
                            linenoiseEditDelete(&l);
                            break;
                        }
                    }
                } else {
                    switch(seq[1]) {
                    case 'A': /* Up */
                        linenoiseEditHistoryNext(&l, LINENOISE_HISTORY_PREV);
                        break;
                    case 'B': /* Down */
                        linenoiseEditHistoryNext(&l, LINENOISE_HISTORY_NEXT);
                        break;
                    case 'C': /* Right */
                        linenoiseEditMoveRight(&l);
                        break;
                    case 'D': /* Left */
                        linenoiseEditMoveLeft(&l);
                        break;
                    case 'H': /* Home */
                        linenoiseEditMoveHome(&l);
                        break;
                    case 'F': /* End*/
                        linenoiseEditMoveEnd(&l);
                        break;
                    }
                }
            }

            /* ESC O sequences. */
            else if (seq[0] == 'O') {
                switch(seq[1]) {
                case 'H': /* Home */
                    linenoiseEditMoveHome(&l);
                    break;
                case 'F': /* End*/
                    linenoiseEditMoveEnd(&l);
                    break;
                }
            }
            break;
        default:
            if (linenoiseEditInsert(&l,cbuf,nread)) return -1;
            break;
        case CTRL_U: /* Ctrl+u, delete the whole line. */
            buf[0] = '\0';
            l.pos = l.len = 0;
            refreshLine(&l);
            break;
        case CTRL_K: /* Ctrl+k, delete from current to end of line. */
            buf[l.pos] = '\0';
            l.len = l.pos;
            refreshLine(&l);
            break;
        case CTRL_A: /* Ctrl+a, go to the start of the line */
            linenoiseEditMoveHome(&l);
            break;
        case CTRL_E: /* ctrl+e, go to the end of the line */
            linenoiseEditMoveEnd(&l);
            break;
        case CTRL_L: /* ctrl+l, clear screen */
            linenoiseClearScreen();
            refreshLine(&l);
            break;
        case CTRL_W: /* ctrl+w, delete previous word */
            linenoiseEditDeletePrevWord(&l);
            break;
        }
    }
    return l.len;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %r8, %rbx
movl %esi, %r15d
movl %edi, %r12d
leaq -0x78(%rbp), %rdi
leaq -0x68(%rbp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
movl %r12d, -0x28(%rax)
movl %esi, -0x24(%rax)
movq %rdx, -0x40(%rbp)
movq %rdx, -0x20(%rax)
movl %ecx, -0x18(%rax)
movq %r8, %rsi
callq 0x23a0
andq $0x0, -0x58(%rbp)
andl $0x0, -0x50(%rbp)
movl %r12d, %edi
movl %r15d, %esi
callq 0x591a
movl %eax, -0x4c(%rbp)
andq $0x0, -0x48(%rbp)
movq -0x88(%rbp), %rax
movb $0x0, (%rax)
decl -0x80(%rbp)
leaq 0x5102(%rip), %rdi      # 0xa706
callq 0x532c
movl -0x8c(%rbp), %edi
movslq -0x70(%rbp), %rdx
movq %rbx, %rsi
callq 0x20a0
cmpq $-0x1, %rax
je 0x586d
leaq -0x94(%rbp), %r13
leaq -0x34(%rbp), %r12
leaq -0x90(%rbp), %rbx
pushq $-0x1
popq %rax
movl %eax, -0x30(%rbp)
pushq $0x1
popq %r15
leaq 0x4e50(%rip), %r14      # 0xa498
movl -0x90(%rbp), %edi
movq %r13, %rsi
movq %r12, %rdx
callq 0x59e8
testl %eax, %eax
jle 0x589b
movl -0x34(%rbp), %ecx
cmpl $0x9, %ecx
jne 0x568e
cmpq $0x0, 0x84b7(%rip)      # 0xdb28
je 0x56bb
movq %rbx, %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0x5a7c
movl -0x34(%rbp), %ecx
testl %ecx, %ecx
js 0x589b
je 0x5648
leal -0x1(%rcx), %edx
cmpl $0x1a, %edx
ja 0x56ac
movslq (%r14,%rdx,4), %rcx
addq %r14, %rcx
movl %r15d, %esi
jmpq *%rcx
movq %rbx, %rdi
callq 0x5ea1
jmp 0x5648
cmpl $0x7f, %ecx
jne 0x56bb
movq %rbx, %rdi
callq 0x5cb4
jmp 0x5648
movq %rbx, %rdi
movq %r13, %rsi
movl %eax, %edx
callq 0x5eb7
testl %eax, %eax
je 0x5648
jmp 0x58a1
movq -0x40(%rbp), %rax
movb $0x0, (%rax)
andl $0x0, -0x50(%rbp)
andl $0x0, -0x58(%rbp)
jmp 0x57a5
movl -0x90(%rbp), %edi
leaq -0x2b(%rbp), %rsi
pushq $0x1
popq %rdx
callq 0x22d0
cmpq $-0x1, %rax
je 0x5648
movl -0x90(%rbp), %edi
leaq -0x2a(%rbp), %rsi
pushq $0x1
popq %rdx
callq 0x22d0
cmpq $-0x1, %rax
je 0x5648
movzbl -0x2b(%rbp), %eax
cmpl $0x4f, %eax
je 0x5801
cmpl $0x5b, %eax
jne 0x5648
movzbl -0x2a(%rbp), %eax
leal -0x30(%rax), %ecx
cmpb $0x9, %cl
ja 0x5818
movl -0x90(%rbp), %edi
leaq -0x29(%rbp), %rsi
pushq $0x1
popq %rdx
callq 0x22d0
cmpq $-0x1, %rax
je 0x5648
cmpb $0x7e, -0x29(%rbp)
jne 0x5648
cmpb $0x33, -0x2a(%rbp)
je 0x5789
jmp 0x5648
callq 0x5f94
jmp 0x57a5
cmpl $0x0, -0x50(%rbp)
jle 0x58be
movq %rbx, %rdi
callq 0x5d1b
jmp 0x5648
movslq -0x58(%rbp), %rax
movq -0x40(%rbp), %rcx
movb $0x0, (%rcx,%rax)
movl %eax, -0x50(%rbp)
movq %rbx, %rdi
callq 0x63c7
jmp 0x5648
movl -0x58(%rbp), %ecx
movl -0x50(%rbp), %eax
testl %ecx, %ecx
setg %dl
cmpl %eax, %ecx
setl %sil
andb %dl, %sil
cmpb $0x1, %sil
jne 0x5648
movl %ecx, %ecx
movq -0x40(%rbp), %rdx
rolw $0x8, -0x1(%rdx,%rcx)
decl %eax
cmpl %eax, %ecx
je 0x57e7
incl %ecx
movl %ecx, -0x58(%rbp)
movq %rbx, %rdi
callq 0x63c7
jmp 0x5648
movq %rbx, %rdi
callq 0x5fb4
jmp 0x5648
movzbl -0x2a(%rbp), %eax
cmpl $0x46, %eax
je 0x5846
cmpl $0x48, %eax
je 0x56a2
jmp 0x5648
addl $-0x41, %eax
cmpl $0x7, %eax
ja 0x5648
leaq 0x4cd9(%rip), %rcx      # 0xa504
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
movl %r15d, %esi
jmpq *%rax
xorl %esi, %esi
movq %rbx, %rdi
callq 0x5dd8
jmp 0x5648
movq %rbx, %rdi
callq 0x5c9d
jmp 0x5648
movq %rbx, %rdi
callq 0x5d7f
jmp 0x5648
movq %rbx, %rdi
callq 0x5daa
jmp 0x5648
pushq $-0x1
popq %rbx
jmp 0x58a4
callq 0x2030
movl $0xb, (%rax)
jmp 0x58a1
movq 0x82b2(%rip), %rax      # 0xdb38
cmpq 0x82b3(%rip), %rax      # 0xdb40
je 0x589b
leaq 0x82a2(%rip), %rdi      # 0xdb38
callq 0x5c84
movl -0x50(%rbp), %eax
movl %eax, -0x30(%rbp)
movl -0x30(%rbp), %ebx
leaq -0x78(%rbp), %rdi
callq 0x2100
movl %ebx, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x8273(%rip), %rdi      # 0xdb38
callq 0x5c84
jmp 0x58a1
jmp 0x58d0
jmp 0x58d0
movq %rax, %rbx
leaq -0x78(%rbp), %rdi
callq 0x2100
movq %rbx, %rdi
callq 0x2380