// File: 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
subq $0xf0, %rsp
movl %edi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq %rdx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movq %r8, -0x28(%rbp)
leaq -0x78(%rbp), %rdi
callq 0x7a30
movl -0x8(%rbp), %eax
movl %eax, -0x78(%rbp)
movl -0xc(%rbp), %eax
movl %eax, -0x74(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x70(%rbp)
movl -0x1c(%rbp), %eax
movl %eax, -0x68(%rbp)
movq -0x28(%rbp), %rsi
leaq -0x60(%rbp), %rdi
callq 0x22c0
jmp 0x73fe
movl $0x0, -0x40(%rbp)
movl $0x0, -0x3c(%rbp)
movl $0x0, -0x38(%rbp)
movl -0x8(%rbp), %edi
movl -0xc(%rbp), %esi
callq 0x7a50
movl %eax, -0x9c(%rbp)
jmp 0x7426
movl -0x9c(%rbp), %eax
movl %eax, -0x34(%rbp)
movl $0x0, -0x30(%rbp)
movl $0x0, -0x2c(%rbp)
movq -0x70(%rbp), %rax
movb $0x0, (%rax)
movl -0x68(%rbp), %eax
decl %eax
movl %eax, -0x68(%rbp)
leaq 0x4564(%rip), %rdi      # 0xb9b7
callq 0x6f60
jmp 0x745a
movl -0x74(%rbp), %eax
movl %eax, -0xb4(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0xb0(%rbp)
leaq -0x60(%rbp), %rdi
callq 0x2350
movl -0xb4(%rbp), %edi
movq -0xb0(%rbp), %rsi
movslq %eax, %rdx
callq 0x20a0
movq %rax, -0xa8(%rbp)
jmp 0x7495
movq -0xa8(%rbp), %rax
cmpq $-0x1, %rax
jne 0x74d5
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0x88(%rbp)
jmp 0x79b6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
leaq -0x78(%rbp), %rdi
callq 0x8810
jmp 0x79cb
jmp 0x74d7
jmp 0x74d9
movl -0x78(%rbp), %edi
leaq -0x90(%rbp), %rsi
leaq -0x8c(%rbp), %rdx
callq 0x7b50
movl %eax, -0xb8(%rbp)
jmp 0x74f7
movl -0xb8(%rbp), %eax
movl %eax, -0x94(%rbp)
cmpl $0x0, -0x94(%rbp)
jg 0x7521
movl -0x38(%rbp), %eax
movl %eax, -0x4(%rbp)
movl $0x1, -0x88(%rbp)
jmp 0x79b6
cmpl $0x9, -0x8c(%rbp)
jne 0x7599
leaq 0xb947(%rip), %rdi      # 0x12e78
xorl %eax, %eax
movl %eax, %esi
callq 0x7c80
testb $0x1, %al
jne 0x7540
jmp 0x7599
leaq -0x78(%rbp), %rdi
leaq -0x90(%rbp), %rsi
leaq -0x8c(%rbp), %rdx
callq 0x7cb0
movl %eax, -0xbc(%rbp)
jmp 0x755f
movl -0xbc(%rbp), %eax
movl %eax, -0x94(%rbp)
cmpl $0x0, -0x8c(%rbp)
jge 0x7589
movl -0x38(%rbp), %eax
movl %eax, -0x4(%rbp)
movl $0x1, -0x88(%rbp)
jmp 0x79b6
cmpl $0x0, -0x8c(%rbp)
jne 0x7597
jmp 0x74d7
jmp 0x7599
movl -0x8c(%rbp), %eax
decl %eax
movl %eax, %ecx
movq %rcx, -0xc8(%rbp)
subl $0x7e, %eax
ja 0x78f4
movq -0xc8(%rbp), %rax
leaq 0x3bb7(%rip), %rcx      # 0xb178
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq 0xb8c7(%rip), %rdi      # 0x12e98
callq 0x8030
testb $0x1, %al
jne 0x75e6
leaq 0xb8b7(%rip), %rdi      # 0x12e98
callq 0x8070
testb $0x1, 0xb918(%rip)      # 0x12f05
je 0x75fc
leaq -0x78(%rbp), %rdi
callq 0x80a0
jmp 0x75fa
jmp 0x75fc
movl -0x38(%rbp), %eax
movl %eax, -0x4(%rbp)
movl $0x1, -0x88(%rbp)
jmp 0x79b6
callq 0x2030
movl $0xb, (%rax)
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0x88(%rbp)
jmp 0x79b6
leaq -0x78(%rbp), %rdi
callq 0x80e0
jmp 0x763d
jmp 0x79b1
cmpl $0x0, -0x38(%rbp)
jle 0x7655
leaq -0x78(%rbp), %rdi
callq 0x81b0
jmp 0x7653
jmp 0x7677
leaq 0xb83c(%rip), %rdi      # 0x12e98
callq 0x8070
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0x88(%rbp)
jmp 0x79b6
jmp 0x79b1
cmpl $0x0, -0x40(%rbp)
jle 0x76ef
movl -0x40(%rbp), %eax
cmpl -0x38(%rbp), %eax
jge 0x76ef
movq -0x18(%rbp), %rax
movl -0x40(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movb (%rax,%rcx), %al
movb %al, -0x98(%rbp)
movq -0x18(%rbp), %rax
movslq -0x40(%rbp), %rcx
movb (%rax,%rcx), %dl
movq -0x18(%rbp), %rax
movl -0x40(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
movb -0x98(%rbp), %dl
movq -0x18(%rbp), %rax
movslq -0x40(%rbp), %rcx
movb %dl, (%rax,%rcx)
movl -0x40(%rbp), %eax
movl -0x38(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
je 0x76e2
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
leaq -0x78(%rbp), %rdi
callq 0x8280
jmp 0x76ed
jmp 0x76ef
jmp 0x79b1
leaq -0x78(%rbp), %rdi
callq 0x82b0
jmp 0x76ff
jmp 0x79b1
leaq -0x78(%rbp), %rdi
callq 0x8300
jmp 0x770f
jmp 0x79b1
leaq -0x78(%rbp), %rdi
movl $0x1, %esi
callq 0x8360
jmp 0x7724
jmp 0x79b1
leaq -0x78(%rbp), %rdi
xorl %esi, %esi
callq 0x8360
jmp 0x7736
jmp 0x79b1
movl -0x78(%rbp), %edi
leaq -0x97(%rbp), %rsi
movl $0x1, %edx
callq 0x2330
movq %rax, -0xd0(%rbp)
jmp 0x7758
movq -0xd0(%rbp), %rax
cmpq $-0x1, %rax
jne 0x776a
jmp 0x79b1
movl -0x78(%rbp), %edi
leaq -0x96(%rbp), %rsi
movl $0x1, %edx
callq 0x2330
movq %rax, -0xd8(%rbp)
jmp 0x7787
movq -0xd8(%rbp), %rax
cmpq $-0x1, %rax
jne 0x7799
jmp 0x79b1
movsbl -0x97(%rbp), %eax
cmpl $0x5b, %eax
jne 0x78a4
movsbl -0x96(%rbp), %eax
cmpl $0x30, %eax
jl 0x781c
movsbl -0x96(%rbp), %eax
cmpl $0x39, %eax
jg 0x781c
movl -0x78(%rbp), %edi
leaq -0x95(%rbp), %rsi
movl $0x1, %edx
callq 0x2330
movq %rax, -0xe0(%rbp)
jmp 0x77de
movq -0xe0(%rbp), %rax
cmpq $-0x1, %rax
jne 0x77f0
jmp 0x79b1
movsbl -0x95(%rbp), %eax
cmpl $0x7e, %eax
jne 0x7817
movb -0x96(%rbp), %al
subb $0x33, %al
jne 0x7815
jmp 0x7808
leaq -0x78(%rbp), %rdi
callq 0x81b0
jmp 0x7813
jmp 0x7815
jmp 0x7817
jmp 0x78a2
movsbl -0x96(%rbp), %eax
addl $-0x41, %eax
movl %eax, %ecx
movq %rcx, -0xe8(%rbp)
subl $0x7, %eax
ja 0x78a0
movq -0xe8(%rbp), %rax
leaq 0x3b32(%rip), %rcx      # 0xb374
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq -0x78(%rbp), %rdi
movl $0x1, %esi
callq 0x8360
jmp 0x785b
jmp 0x78a0
leaq -0x78(%rbp), %rdi
xorl %esi, %esi
callq 0x8360
jmp 0x786a
jmp 0x78a0
leaq -0x78(%rbp), %rdi
callq 0x8300
jmp 0x7877
jmp 0x78a0
leaq -0x78(%rbp), %rdi
callq 0x82b0
jmp 0x7884
jmp 0x78a0
leaq -0x78(%rbp), %rdi
callq 0x84d0
jmp 0x7891
jmp 0x78a0
leaq -0x78(%rbp), %rdi
callq 0x80a0
jmp 0x789e
jmp 0x78a0
jmp 0x78a2
jmp 0x78ef
movsbl -0x97(%rbp), %eax
cmpl $0x4f, %eax
jne 0x78ed
movsbl -0x96(%rbp), %eax
movl %eax, -0xec(%rbp)
subl $0x46, %eax
je 0x78de
jmp 0x78c4
movl -0xec(%rbp), %eax
subl $0x48, %eax
jne 0x78eb
jmp 0x78d1
leaq -0x78(%rbp), %rdi
callq 0x84d0
jmp 0x78dc
jmp 0x78eb
leaq -0x78(%rbp), %rdi
callq 0x80a0
jmp 0x78e9
jmp 0x78eb
jmp 0x78ed
jmp 0x78ef
jmp 0x79b1
movl -0x94(%rbp), %edx
leaq -0x78(%rbp), %rdi
leaq -0x90(%rbp), %rsi
callq 0x8500
movl %eax, -0xf0(%rbp)
jmp 0x7912
movl -0xf0(%rbp), %eax
cmpl $0x0, %eax
je 0x7933
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0x88(%rbp)
jmp 0x79b6
jmp 0x79b1
movq -0x18(%rbp), %rax
movb $0x0, (%rax)
movl $0x0, -0x38(%rbp)
movl $0x0, -0x40(%rbp)
leaq -0x78(%rbp), %rdi
callq 0x8280
jmp 0x7955
jmp 0x79b1
movq -0x18(%rbp), %rax
movslq -0x40(%rbp), %rcx
movb $0x0, (%rax,%rcx)
movl -0x40(%rbp), %eax
movl %eax, -0x38(%rbp)
leaq -0x78(%rbp), %rdi
callq 0x8280
jmp 0x7974
jmp 0x79b1
leaq -0x78(%rbp), %rdi
callq 0x84d0
jmp 0x7981
jmp 0x79b1
leaq -0x78(%rbp), %rdi
callq 0x80a0
jmp 0x798e
jmp 0x79b1
callq 0x86d0
jmp 0x7997
leaq -0x78(%rbp), %rdi
callq 0x8280
jmp 0x79a2
jmp 0x79b1
leaq -0x78(%rbp), %rdi
callq 0x8700
jmp 0x79af
jmp 0x79b1
jmp 0x74d7
leaq -0x78(%rbp), %rdi
callq 0x8810
movl -0x4(%rbp), %eax
addq $0xf0, %rsp
popq %rbp
retq
movq -0x80(%rbp), %rdi
callq 0x23e0
nopw %cs:(%rax,%rax)
nop