// File: /workspace/github_cpp_makefile_star2_O2/akojo[P]quickfind/qselect.cpp
// Function: main
int main(void)
{
    vector<wstr> input;
    string line;

    while (getline(cin, line)) input.emplace_back(line);

    Term tty("/dev/tty");
    Term::Size screen_size = tty.get_screen_size();

    char ch;
    int selection = 1;
    Prompt prompt(tty, "> ");

    tty.erase_display(Term::FORWARD);
    for (;;) {
        auto last = match(input.begin(), input.end(), prompt.query());
        auto it = input.cbegin();
        int i = 1;
        for (; it != last; ++i, ++it) {
            string str = (*it).str.substr(0, screen_size.columns);
            if (i == selection) {
                tty.puts_highlighted(str);
            } else {
                tty.puts(str);
            }
            if (i == MAXLINES)
                break;
            tty.putchar('\n');
        }
        tty.cursor_up(i);
        tty.move_to_col(prompt.current_column());

        ch = tty.getchar();
        if (ch == Term::ESC) {
            ch = handle_escape(tty);
        }

        if (ch == Term::CTRL_C || ch == Term::ENTER) {
            break;
        } else if (ch == Term::CTRL_N) {
            selection = min(selection + 1, i - 1);
        } else if (ch == Term::CTRL_P) {
            selection = max(selection - 1, 1);
        } else if (prompt.handle_key(static_cast<Term::Key>(ch))) {
            selection = 1;
        }
        tty.putchar('\n');
        tty.erase_display(Term::FORWARD);
    }

    tty.move_to_col(1);
    tty.erase_display(Term::FORWARD);

    if (ch == Term::ENTER) {
        cout << input[selection - 1].str.c_str() << endl;
        return 0;
    }
    return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x138, %rsp            # imm = 0x138
xorps %xmm0, %xmm0
leaq 0x10(%rsp), %rbx
andq $0x0, 0x10(%rbx)
leaq 0xa0(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movaps %xmm0, (%rbx)
movb $0x0, (%rax)
movq 0x4b10(%rip), %r14      # 0x6fd8
leaq 0x90(%rsp), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x2040
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x24f6
movq %rbx, %rdi
movq %r15, %rsi
callq 0x2812
jmp 0x24d0
leaq 0x1b07(%rip), %rsi      # 0x4004
leaq 0xb0(%rsp), %rdi
leaq 0x28(%rsp), %rdx
callq 0x284e
leaq 0x48(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
callq 0x34ac
leaq 0xb0(%rsp), %rdi
callq 0x20c0
leaq 0x48(%rsp), %rdi
callq 0x36e6
movq %rax, 0x8(%rsp)
leaq 0x1ac9(%rip), %rsi      # 0x400d
leaq 0x28(%rsp), %rdi
leaq 0x7(%rsp), %rdx
callq 0x284e
leaq 0xb0(%rsp), %rdi
leaq 0x48(%rsp), %rsi
leaq 0x28(%rsp), %rdx
callq 0x37c8
leaq 0x28(%rsp), %rdi
callq 0x20c0
leaq 0x48(%rsp), %rdi
xorl %esi, %esi
callq 0x3782
sarq $0x20, 0x8(%rsp)
pushq $0x1
popq %rbx
leaq 0x28(%rsp), %r15
leaq 0x48(%rsp), %r12
movq 0x10(%rsp), %r14
movq 0x18(%rsp), %r13
movq %r15, %rdi
leaq 0x118(%rsp), %rsi
callq 0x2230
movq %r14, %rdi
movq %r13, %rsi
movq %r15, %rdx
callq 0x2bf0
movq %rax, %r14
movq %r15, %rdi
callq 0x20c0
movq 0x10(%rsp), %rbp
pushq $0x1
popq %r13
cmpq %r14, %rbp
je 0x2635
addq $0x8, %rbp
movq %r15, %rdi
movq %rbp, %rsi
xorl %edx, %edx
movq 0x8(%rsp), %rcx
callq 0x2190
cmpl %r13d, %ebx
jne 0x25fc
movq %r12, %rdi
movq %r15, %rsi
callq 0x3666
jmp 0x2607
movq %r12, %rdi
movq %r15, %rsi
callq 0x3658
cmpl $0x14, %r13d
je 0x2629
movq %r12, %rdi
pushq $0xa
popq %rsi
callq 0x367a
movq %r15, %rdi
callq 0x20c0
incl %r13d
addq $0x20, %rbp
jmp 0x25cf
movq %r15, %rdi
callq 0x20c0
pushq $0x14
popq %r13
movq %r12, %rdi
movl %r13d, %esi
callq 0x371e
movl 0x100(%rsp), %eax
movl 0x120(%rsp), %ecx
leal (%rcx,%rax), %esi
incl %esi
movq %r12, %rdi
callq 0x376e
movq %r12, %rdi
callq 0x3686
movl %eax, %ebp
cmpb $0x1b, %bpl
jne 0x269d
movq %r12, %rdi
callq 0x3686
movl %eax, %ebp
cmpb $0x5b, %bpl
jne 0x269d
movq %r12, %rdi
callq 0x3686
cmpb $0x41, %al
je 0x26d1
movzbl %al, %ecx
cmpl $0x44, %ecx
je 0x26d1
andl $0xfe, %eax
xorl %esi, %esi
cmpl $0x42, %eax
jne 0x26ba
jmp 0x26de
cmpb $0xe, %bpl
je 0x26de
movzbl %bpl, %eax
cmpl $0x10, %eax
je 0x26d1
cmpl $0x3, %eax
je 0x2704
cmpl $0xa, %eax
je 0x2704
movsbl %bpl, %esi
leaq 0xb0(%rsp), %rdi
callq 0x389e
testb %al, %al
pushq $0x1
popq %rax
cmovnel %eax, %ebx
jmp 0x26ea
cmpl $0x3, %ebx
pushq $0x2
popq %rax
cmovll %eax, %ebx
decl %ebx
jmp 0x26ea
incl %ebx
decl %r13d
cmpl %ebx, %r13d
cmovll %r13d, %ebx
movq %r12, %rdi
pushq $0xa
popq %rsi
callq 0x367a
movq %r12, %rdi
xorl %esi, %esi
callq 0x3782
jmp 0x2593
leaq 0x48(%rsp), %rdi
pushq $0x1
popq %r14
movl %r14d, %esi
callq 0x376e
leaq 0x48(%rsp), %rdi
xorl %esi, %esi
callq 0x3782
cmpb $0xa, %bpl
jne 0x274f
movq 0x10(%rsp), %rax
movslq %ebx, %rcx
imulq $0x28, %rcx, %rcx
movq -0x20(%rax,%rcx), %rsi
movq 0x4879(%rip), %rdi      # 0x6fb8
callq 0x2160
xorl %r14d, %r14d
movq %rax, %rdi
callq 0x20d0
leaq 0xb0(%rsp), %rdi
callq 0x288a
leaq 0x48(%rsp), %rdi
callq 0x363a
leaq 0x90(%rsp), %rdi
callq 0x20c0
leaq 0x10(%rsp), %rdi
callq 0x28aa
movl %r14d, %eax
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x20c0
jmp 0x27e8
jmp 0x27a3
movq %rax, %rbx
jmp 0x27e8
movq %rax, %rbx
leaq 0xb0(%rsp), %rdi
callq 0x20c0
jmp 0x27f2
jmp 0x27be
jmp 0x27c9
movq %rax, %rbx
jmp 0x27f2
jmp 0x27ce
jmp 0x27c9
jmp 0x27c9
movq %rax, %rbx
jmp 0x27db
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x20c0
leaq 0xb0(%rsp), %rdi
callq 0x288a
leaq 0x48(%rsp), %rdi
callq 0x363a
leaq 0x90(%rsp), %rdi
callq 0x20c0
leaq 0x10(%rsp), %rdi
callq 0x28aa
movq %rbx, %rdi
callq 0x22d0
nop