// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %rdx, %rbx
movq %rsi, -0x58(%rbp)
movq %rdi, %r15
xorps %xmm0, %xmm0
leaq -0x40(%rbp), %rdx
movaps %xmm0, (%rdx)
andq $0x0, 0x10(%rdx)
andl $0x0, (%rbx)
movq 0x8(%rdi), %rsi
leaq 0x8061(%rip), %rdi      # 0xdb18
callq 0x61be
movq -0x40(%rbp), %rax
cmpq -0x38(%rbp), %rax
je 0x5c2e
xorl %r13d, %r13d
xorl %r12d, %r12d
movq %r12, %r14
movslq %r12d, %rcx
movb $0x1, %r12b
movq %rcx, -0x48(%rbp)
shlq $0x5, %rcx
movq %rcx, -0x50(%rbp)
testb $0x1, %r12b
je 0x5c3b
movq -0x38(%rbp), %rax
subq -0x40(%rbp), %rax
shrq $0x5, %rax
movq %r14, %rcx
cmpl %eax, %ecx
jge 0x5b5c
leaq -0xa8(%rbp), %rdi
movq %r15, %rsi
callq 0x620a
movq -0x40(%rbp), %rax
movq -0x50(%rbp), %rdx
movl 0x8(%rax,%rdx), %ecx
movl %ecx, 0x38(%r15)
movl %ecx, 0x40(%r15)
movq (%rax,%rdx), %rax
movq %rax, 0x8(%r15)
movq %r15, %rdi
callq 0x63c7
movl -0x68(%rbp), %eax
movl %eax, 0x40(%r15)
movl -0x70(%rbp), %eax
movl %eax, 0x38(%r15)
movq -0xa0(%rbp), %rax
movq %rax, 0x8(%r15)
leaq -0x90(%rbp), %rdi
callq 0x2100
jmp 0x5b64
movq %r15, %rdi
callq 0x63c7
movl (%r15), %edi
movq -0x58(%rbp), %rsi
movq %rbx, %rdx
callq 0x59e8
movl %eax, %r13d
testl %eax, %eax
jle 0x5c38
movl (%rbx), %eax
cmpl $0x1b, %eax
je 0x5bd3
cmpl $0x9, %eax
je 0x5bfb
movq -0x40(%rbp), %rax
movq -0x38(%rbp), %rcx
subq %rax, %rcx
shrq $0x5, %rcx
xorl %r12d, %r12d
cmpl %ecx, -0x48(%rbp)
jge 0x5ae5
movq 0x8(%r15), %rdi
movslq 0x10(%r15), %rsi
movq -0x50(%rbp), %rcx
movq (%rax,%rcx), %rcx
xorl %r12d, %r12d
leaq 0x4b30(%rip), %rdx      # 0xa6ef
xorl %eax, %eax
callq 0x2350
movl %eax, 0x38(%r15)
movl %eax, 0x40(%r15)
jmp 0x5ae5
movq -0x38(%rbp), %rax
subq -0x40(%rbp), %rax
shrq $0x5, %rax
xorl %r12d, %r12d
cmpl %eax, %r14d
jge 0x5ae5
xorl %r12d, %r12d
movq %r15, %rdi
callq 0x63c7
jmp 0x5ae5
movq -0x48(%rbp), %rax
incl %eax
movq -0x38(%rbp), %rcx
subq -0x40(%rbp), %rcx
cltq
sarq $0x5, %rcx
leaq 0x1(%rcx), %rsi
xorl %edx, %edx
divq %rsi
movq %rdx, %r12
cmpl %ecx, %r12d
jne 0x5ad0
callq 0x61e3
jmp 0x5ad0
xorl %r13d, %r13d
callq 0x61e3
jmp 0x5c3b
orl $-0x1, (%rbx)
leaq -0x40(%rbp), %rdi
callq 0x52a6
movl %r13d, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x5c70
jmp 0x5c70
movq %rax, %rbx
leaq -0x90(%rbp), %rdi
callq 0x2100
jmp 0x5c73
jmp 0x5c70
movq %rax, %rbx
leaq -0x40(%rbp), %rdi
callq 0x52a6
movq %rbx, %rdi
callq 0x2380