// File: sangwoojun[P]rv32emulator/./linenoise.hpp
// Function: linenoise::completeLine(linenoise::linenoiseState*, char*, int*)
inline int completeLine(struct linenoiseState *ls, char *cbuf, int *c) {
    std::vector<std::string> lc;
    int nread = 0, nwritten;
    *c = 0;

    completionCallback(ls->buf,lc);
    if (lc.empty()) {
        linenoiseBeep();
    } else {
        int stop = 0, i = 0;

        while(!stop) {
            /* Show completion or original buffer */
            if (i < static_cast<int>(lc.size())) {
                struct linenoiseState saved = *ls;

                ls->len = ls->pos = static_cast<int>(lc[i].size());
                ls->buf = &lc[i][0];
                refreshLine(ls);
                ls->len = saved.len;
                ls->pos = saved.pos;
                ls->buf = saved.buf;
            } else {
                refreshLine(ls);
            }

            //nread = read(ls->ifd,&c,1);
#ifdef _WIN32
            nread = win32read(c);
            if (nread == 1) {
                cbuf[0] = *c;
            }
#else
            nread = unicodeReadUTF8Char(ls->ifd,cbuf,c);
#endif
            if (nread <= 0) {
                *c = -1;
                return nread;
            }

            switch(*c) {
                case 9: /* tab */
                    i = (i+1) % (lc.size()+1);
                    if (i == static_cast<int>(lc.size())) linenoiseBeep();
                    break;
                case 27: /* escape */
                    /* Re-show original buffer */
                    if (i < static_cast<int>(lc.size())) refreshLine(ls);
                    stop = 1;
                    break;
                default:
                    /* Update buffer and return */
                    if (i < static_cast<int>(lc.size())) {
                        nwritten = snprintf(ls->buf,ls->buflen,"%s",&lc[i][0]);
                        ls->len = ls->pos = nwritten;
                    }
                    stop = 1;
                    break;
            }
        }
    }

    return nread;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x110, %rsp            # imm = 0x110
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
leaq -0x38(%rbp), %rdi
movq %rdi, -0xb8(%rbp)
callq 0x6e70
movq -0xb8(%rbp), %rdx
movl $0x0, -0x3c(%rbp)
movq -0x20(%rbp), %rax
movl $0x0, (%rax)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
leaq 0xb17a(%rip), %rdi      # 0x12e78
callq 0x8ae0
jmp 0x7d05
leaq -0x38(%rbp), %rdi
callq 0x8030
testb $0x1, %al
jne 0x7d14
jmp 0x7d31
callq 0x8b30
jmp 0x7d1b
jmp 0x7fed
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x8012
movl $0x0, -0x50(%rbp)
movl $0x0, -0x54(%rbp)
cmpl $0x0, -0x50(%rbp)
setne %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x7d51
jmp 0x7feb
movl -0x54(%rbp), %eax
movl %eax, -0xbc(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x8b60
movq %rax, %rcx
movl -0xbc(%rbp), %eax
cmpl %ecx, %eax
jge 0x7e3f
movq -0x10(%rbp), %rsi
leaq -0xa8(%rbp), %rdi
callq 0x8b80
jmp 0x7d86
movslq -0x54(%rbp), %rsi
leaq -0x38(%rbp), %rdi
movq %rdi, -0xd0(%rbp)
callq 0x8bf0
movq %rax, %rdi
callq 0x2170
movq -0xd0(%rbp), %rdi
movl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x38(%rax)
movq -0x10(%rbp), %rax
movl %ecx, 0x40(%rax)
movslq -0x54(%rbp), %rsi
callq 0x8bf0
movq %rax, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x2400
movq %rax, -0xc8(%rbp)
jmp 0x7dd7
movq -0xc8(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x10(%rbp), %rdi
callq 0x8280
jmp 0x7df1
movl -0x68(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x40(%rax)
movl -0x70(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x38(%rax)
movq -0xa0(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x8(%rax)
leaq -0xa8(%rbp), %rdi
callq 0x8810
jmp 0x7e4c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0xa8(%rbp), %rdi
callq 0x8810
jmp 0x8012
movq -0x10(%rbp), %rdi
callq 0x8280
jmp 0x7e4a
jmp 0x7e4c
movq -0x10(%rbp), %rax
movl (%rax), %edi
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
callq 0x7b50
movl %eax, -0xd4(%rbp)
jmp 0x7e67
movl -0xd4(%rbp), %eax
movl %eax, -0x3c(%rbp)
cmpl $0x0, -0x3c(%rbp)
jg 0x7e95
movq -0x20(%rbp), %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
movl -0x3c(%rbp), %eax
movl %eax, -0x4(%rbp)
movl $0x1, -0xac(%rbp)
jmp 0x7ffd
movq -0x20(%rbp), %rax
movl (%rax), %eax
movl %eax, -0xd8(%rbp)
subl $0x9, %eax
je 0x7eb8
jmp 0x7ea8
movl -0xd8(%rbp), %eax
subl $0x1b, %eax
je 0x7f15
jmp 0x7f4d
movl -0x54(%rbp), %eax
addl $0x1, %eax
cltq
movq %rax, -0xe8(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x8b60
movq %rax, %rcx
movq -0xe8(%rbp), %rax
addq $0x1, %rcx
xorl %edx, %edx
divq %rcx
movl %edx, %eax
movl %eax, -0x54(%rbp)
movl -0x54(%rbp), %eax
movl %eax, -0xdc(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x8b60
movq %rax, %rcx
movl -0xdc(%rbp), %eax
cmpl %ecx, %eax
jne 0x7f10
callq 0x8b30
jmp 0x7f0e
jmp 0x7f10
jmp 0x7fe6
movl -0x54(%rbp), %eax
movl %eax, -0xec(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x8b60
movq %rax, %rcx
movl -0xec(%rbp), %eax
cmpl %ecx, %eax
jge 0x7f41
movq -0x10(%rbp), %rdi
callq 0x8280
jmp 0x7f3f
jmp 0x7f41
movl $0x1, -0x50(%rbp)
jmp 0x7fe6
movl -0x54(%rbp), %eax
movl %eax, -0xf0(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x8b60
movq %rax, %rcx
movl -0xf0(%rbp), %eax
cmpl %ecx, %eax
jge 0x7fdf
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rcx
movq %rcx, -0x108(%rbp)
movslq 0x10(%rax), %rax
movq %rax, -0x100(%rbp)
movslq -0x54(%rbp), %rsi
leaq -0x38(%rbp), %rdi
callq 0x8bf0
movq %rax, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x2400
movq %rax, -0xf8(%rbp)
jmp 0x7fa8
movq -0xf8(%rbp), %rcx
movq -0x100(%rbp), %rsi
movq -0x108(%rbp), %rdi
leaq 0x357f(%rip), %rdx      # 0xb543
movb $0x0, %al
callq 0x23b0
movl %eax, -0x40(%rbp)
movl -0x40(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x38(%rax)
movq -0x10(%rbp), %rax
movl %ecx, 0x40(%rax)
movl $0x1, -0x50(%rbp)
jmp 0x7d3f
jmp 0x7fed
movl -0x3c(%rbp), %eax
movl %eax, -0x4(%rbp)
movl $0x1, -0xac(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x6e90
movl -0x4(%rbp), %eax
addq $0x110, %rsp            # imm = 0x110
popq %rbp
retq
leaq -0x38(%rbp), %rdi
callq 0x6e90
movq -0x48(%rbp), %rdi
callq 0x23e0
nopw %cs:(%rax,%rax)
nop