// File: /workspace/github_cpp_makefile_star2_O3/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 $0x88, %rsp
movq %r8, %r14
movl %esi, %r15d
movl %edi, %r12d
leaq -0x68(%rbp), %r13
leaq -0x58(%rbp), %rax
movq %rax, -0x10(%rax)
movq $0x0, -0x8(%rax)
movb $0x0, (%rax)
movl %edi, -0x28(%rax)
movl %esi, -0x24(%rax)
movq %rdx, -0x88(%rbp)
movq %rdx, -0x20(%rax)
movl %ecx, -0x18(%rax)
movq %r8, %rdi
callq 0x20f0
movq %r13, %rdi
xorl %esi, %esi
xorl %edx, %edx
movq %r14, %rcx
movq %rax, %r8
callq 0x22f0
movq $0x0, -0x48(%rbp)
movl $0x0, -0x40(%rbp)
movl %r12d, %edi
movl %r15d, %esi
callq 0x5f10
movl %eax, -0x3c(%rbp)
movq $0x0, -0x38(%rbp)
movq -0x78(%rbp), %rax
movb $0x0, (%rax)
decl -0x70(%rbp)
movq 0x9105(%rip), %rsi      # 0xeb28
movq 0x9106(%rip), %rdi      # 0xeb30
movq %rsi, %rax
cmpq %rdi, %rsi
je 0x5a54
addq $-0x20, %rdi
leaq 0x5c63(%rip), %rsi      # 0xb6a0
callq 0x2080
testl %eax, %eax
je 0x5ab0
movq 0x90e3(%rip), %rax      # 0xeb30
movq 0x90d4(%rip), %rsi      # 0xeb28
subq %rsi, %rax
cmpq $0xc80, %rax            # imm = 0xC80
jne 0x5a6b
leaq 0x90c2(%rip), %rdi      # 0xeb28
callq 0x7130
leaq -0x98(%rbp), %r15
movq %r15, -0x10(%r15)
leaq 0x5c23(%rip), %rdx      # 0xb6a0
leaq -0xa8(%rbp), %rdi
movq %rdx, %rsi
callq 0x7410
leaq 0x9095(%rip), %rdi      # 0xeb28
leaq -0xa8(%rbp), %rsi
callq 0x71a6
movq -0xa8(%rbp), %rdi
cmpq %r15, %rdi
je 0x5ab0
callq 0x2180
movl -0x7c(%rbp), %edi
movslq -0x60(%rbp), %rdx
movq %r14, %rsi
callq 0x20a0
cmpq $-0x1, %rax
je 0x5e23
leaq -0xac(%rbp), %r12
leaq -0xa8(%rbp), %r13
leaq -0x80(%rbp), %r15
leaq 0x59b6(%rip), %rbx      # 0xb498
movl -0x80(%rbp), %edi
movq %r12, %rsi
movq %r13, %rdx
callq 0x610f
testl %eax, %eax
jle 0x5e57
movl -0xa8(%rbp), %ecx
cmpl $0x9, %ecx
jne 0x5b2b
cmpq $0x0, 0x900d(%rip)      # 0xeb18
je 0x5b66
movq %r15, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x6242
movl -0xa8(%rbp), %ecx
testl %ecx, %ecx
js 0x5e57
je 0x5ae2
leal -0x1(%rcx), %edx
cmpl $0x1a, %edx
ja 0x5b54
movl $0x1, %r14d
movslq (%rbx,%rdx,4), %rcx
addq %rbx, %rcx
jmpq *%rcx
cmpl $0x0, -0x48(%rbp)
je 0x5ae2
movl $0x0, -0x48(%rbp)
jmp 0x5df1
cmpl $0x7f, %ecx
jne 0x5b66
movq %r15, %rdi
callq 0x64c1
jmp 0x5ae2
movq %r15, %rdi
movq %r12, %rsi
movl %eax, %edx
callq 0x6905
testl %eax, %eax
je 0x5ae2
jmp 0x5e23
movq -0x88(%rbp), %rax
movb $0x0, (%rax)
xorl %eax, %eax
movl %eax, -0x40(%rbp)
jmp 0x5dee
movl -0x80(%rbp), %edi
movl $0x1, %edx
leaq -0x2b(%rbp), %rsi
callq 0x22b0
cmpq $-0x1, %rax
je 0x5ae2
movl -0x80(%rbp), %edi
movl $0x1, %edx
leaq -0x2a(%rbp), %rsi
callq 0x22b0
cmpq $-0x1, %rax
je 0x5ae2
movzbl -0x2b(%rbp), %eax
cmpl $0x4f, %eax
je 0x5d9c
cmpl $0x5b, %eax
jne 0x5ae2
movzbl -0x2a(%rbp), %eax
leal -0x30(%rax), %ecx
cmpb $0x9, %cl
ja 0x5db3
movl -0x80(%rbp), %edi
movl $0x1, %edx
leaq -0x29(%rbp), %rsi
callq 0x22b0
cmpq $-0x1, %rax
je 0x5ae2
cmpb $0x7e, -0x29(%rbp)
jne 0x5ae2
cmpb $0x33, -0x2a(%rbp)
jne 0x5ae2
movl -0x40(%rbp), %esi
testl %esi, %esi
jle 0x5ae2
movl -0x48(%rbp), %edx
cmpl %esi, %edx
jge 0x5ae2
movq -0x78(%rbp), %rdi
callq 0x6a82
jmp 0x5c99
movl $0x7, %edx
movl $0x1, %edi
leaq 0x5d90(%rip), %rsi      # 0xb9e1
callq 0x20a0
jmp 0x5df1
movl -0x48(%rbp), %edx
movl -0x40(%rbp), %esi
cmpl %esi, %edx
je 0x5ae2
movq -0x78(%rbp), %rdi
callq 0x6a82
addl %eax, -0x48(%rbp)
jmp 0x5df1
movl -0x40(%rbp), %esi
testl %esi, %esi
jle 0x5e80
movl -0x48(%rbp), %edx
cmpl %esi, %edx
jge 0x5ae2
movq -0x78(%rbp), %rdi
callq 0x6a82
movslq -0x48(%rbp), %rdi
movslq %eax, %r14
leal (%r14,%rdi), %eax
addq -0x78(%rbp), %rdi
leaq (%rdi,%r14), %rsi
movl -0x40(%rbp), %ecx
subl %eax, %ecx
movslq %ecx, %rdx
callq 0x2350
movslq -0x40(%rbp), %rax
subq %r14, %rax
movl %eax, -0x40(%rbp)
movq -0x78(%rbp), %rcx
movb $0x0, (%rcx,%rax)
jmp 0x5df1
movslq -0x48(%rbp), %rax
movq -0x88(%rbp), %rcx
movb $0x0, (%rcx,%rax)
movl %eax, -0x40(%rbp)
jmp 0x5df1
movl -0x48(%rbp), %ecx
movl -0x40(%rbp), %eax
testl %ecx, %ecx
setg %dl
cmpl %eax, %ecx
setl %sil
andb %dl, %sil
cmpb $0x1, %sil
jne 0x5ae2
movl %ecx, %ecx
movq -0x88(%rbp), %rdx
rolw $0x8, -0x1(%rdx,%rcx)
decl %eax
cmpl %eax, %ecx
je 0x5d1f
incl %ecx
movl %ecx, -0x48(%rbp)
movq %r15, %rdi
callq 0x6be1
jmp 0x5ae2
movslq -0x48(%rbp), %rax
movq -0x78(%rbp), %rcx
movl %eax, %r14d
testq %rax, %rax
jle 0x5d78
movl %eax, %r14d
movl %r14d, %edx
cmpb $0x20, -0x1(%rcx,%rdx)
jne 0x5d5b
leal -0x1(%r14), %edx
movl %edx, -0x48(%rbp)
cmpl $0x1, %r14d
movl %edx, %r14d
jg 0x5d3f
jmp 0x5d75
movl %r14d, %edx
cmpb $0x20, -0x1(%rcx,%rdx)
je 0x5d78
leal -0x1(%r14), %edx
movl %edx, -0x48(%rbp)
cmpl $0x1, %r14d
movl %edx, %r14d
jg 0x5d5b
xorl %r14d, %r14d
movslq %r14d, %rdi
subl %eax, %r14d
addq %rcx, %rdi
movq %rax, %rsi
addq %rcx, %rsi
movl -0x40(%rbp), %ecx
subl %eax, %ecx
incl %ecx
movslq %ecx, %rdx
callq 0x2350
addl %r14d, -0x40(%rbp)
jmp 0x5df1
movzbl -0x2a(%rbp), %eax
cmpl $0x46, %eax
je 0x5de2
cmpl $0x48, %eax
je 0x5b42
jmp 0x5ae2
addl $-0x41, %eax
cmpl $0x7, %eax
ja 0x5ae2
leaq 0x573e(%rip), %rcx      # 0xb504
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
xorl %r14d, %r14d
movq %r15, %rdi
movl %r14d, %esi
callq 0x6802
jmp 0x5ae2
movl -0x40(%rbp), %eax
cmpl %eax, -0x48(%rbp)
je 0x5ae2
movl %eax, -0x48(%rbp)
movq %r15, %rdi
callq 0x6be1
jmp 0x5ae2
movq %r15, %rdi
callq 0x666d
jmp 0x5ae2
movq %r15, %rdi
callq 0x67d4
jmp 0x5ae2
callq 0x2030
movl $0xb, (%rax)
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
jmp 0x5e5a
movq 0x8cff(%rip), %rax      # 0xeb30
cmpq %rax, 0x8cf0(%rip)      # 0xeb28
je 0x5e57
leaq -0x20(%rax), %rcx
movq %rcx, 0x8ceb(%rip)      # 0xeb30
movq -0x20(%rax), %rdi
addq $-0x10, %rax
cmpq %rax, %rdi
je 0x5e57
callq 0x2180
movl -0x40(%rbp), %ebx
movq -0x68(%rbp), %rdi
leaq -0x58(%rbp), %rax
cmpq %rax, %rdi
je 0x5e6c
callq 0x2180
movl %ebx, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8ca9(%rip), %rax      # 0xeb30
leaq -0x20(%rax), %rcx
movq %rcx, 0x8c9e(%rip)      # 0xeb30
movq -0x20(%rax), %rdi
addq $-0x10, %rax
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
cmpq %rax, %rdi
je 0x5e5a
callq 0x2180
jmp 0x5e5a
jmp 0x5ec5
movq %rax, %rbx
movq -0xa8(%rbp), %rdi
cmpq %r15, %rdi
je 0x5ec8
callq 0x2180
jmp 0x5ec8
jmp 0x5ec5
movq %rax, %rbx
movq -0x68(%rbp), %rdi
leaq -0x58(%rbp), %rax
cmpq %rax, %rdi
je 0x5eda
callq 0x2180
movq %rbx, %rdi
callq 0x2370