// File: /workspace/github_cpp_makefile_star2_O1/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 0x30(%rsp), %rbx
movaps %xmm0, (%rbx)
xorl %eax, %eax
movq %rax, 0x10(%rbx)
leaq 0x58(%rsp), %rcx
movq %rcx, -0x10(%rcx)
movq %rax, -0x8(%rcx)
movb $0x0, (%rcx)
movq 0x4ac3(%rip), %r14      # 0x6fd8
leaq 0x48(%rsp), %r15
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x2160
movsbl %al, %edx
movq %r14, %rdi
movq %r15, %rsi
callq 0x2300
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x2557
movq %rbx, %rdi
movq %r15, %rsi
callq 0x29fa
jmp 0x251a
leaq 0x78(%rsp), %r14
movq %r14, -0x10(%r14)
leaq 0x2a9d(%rip), %rsi      # 0x5004
leaq 0x2a9e(%rip), %rdx      # 0x500c
leaq 0x68(%rsp), %rdi
callq 0x2dfe
leaq 0xf0(%rsp), %rdi
leaq 0x68(%rsp), %rsi
callq 0x3d14
movq 0x68(%rsp), %rdi
cmpq %r14, %rdi
je 0x25a1
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x2180
leaq 0xf0(%rsp), %rdi
callq 0x3fcc
movq %rax, 0x28(%rsp)
leaq 0x18(%rsp), %rax
movq %rax, -0x10(%rax)
leaq 0x2a4a(%rip), %rsi      # 0x500d
leaq 0x2a45(%rip), %rdx      # 0x500f
leaq 0x8(%rsp), %rdi
callq 0x2dfe
leaq 0x68(%rsp), %rdi
leaq 0xf0(%rsp), %rsi
leaq 0x8(%rsp), %rdx
callq 0x4090
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x2607
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x2180
leaq 0xf0(%rsp), %rdi
xorl %esi, %esi
callq 0x4068
sarq $0x20, 0x28(%rsp)
leaq 0x8(%rsp), %r14
leaq 0xf0(%rsp), %r15
movl $0x1, %r12d
movq 0x30(%rsp), %r13
movq 0x38(%rsp), %rbp
leaq 0x18(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0xd0(%rsp), %rsi
movq 0xd8(%rsp), %rdx
addq %rsi, %rdx
movq %r14, %rdi
callq 0x2ae0
movq %r13, %rdi
movq %rbp, %rsi
movq %r14, %rdx
callq 0x2e90
movq %rax, %r13
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x268b
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x2180
movq 0x30(%rsp), %rbx
movl $0x1, %ebp
cmpq %r13, %rbx
je 0x270a
leaq 0x8(%rbx), %rsi
movq %r14, %rdi
xorl %edx, %edx
movq 0x28(%rsp), %rcx
callq 0x2190
cmpl %ebp, %r12d
jne 0x26bf
movq %r15, %rdi
movq %r14, %rsi
callq 0x3f48
jmp 0x26ca
movq %r15, %rdi
movq %r14, %rsi
callq 0x3f3a
cmpl $0x14, %ebp
je 0x26dc
movq %r15, %rdi
movl $0xa, %esi
callq 0x3f5c
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x26f8
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x2180
cmpl $0x14, %ebp
je 0x2705
incl %ebp
addq $0x28, %rbx
jmp 0x2695
movl $0x14, %ebp
movq %r15, %rdi
movl %ebp, %esi
callq 0x4004
movl 0xb8(%rsp), %eax
movl 0xd8(%rsp), %ecx
leal (%rcx,%rax), %esi
incl %esi
movq %r15, %rdi
callq 0x4054
movq %r15, %rdi
callq 0x3f68
movl %eax, %r13d
cmpb $0x1b, %r13b
jne 0x277f
movq %r15, %rdi
callq 0x3f68
movl %eax, %r13d
cmpb $0x5b, %r13b
jne 0x277f
movq %r15, %rdi
callq 0x3f68
movb $0x10, %r13b
cmpb $0x41, %al
je 0x277f
movzbl %al, %ecx
cmpl $0x44, %ecx
je 0x277f
andl $0xfe, %eax
cmpl $0x42, %eax
movl $0x0, %r13d
movl $0xe, %eax
cmovel %eax, %r13d
movzbl %r13b, %eax
cmpl $0xd, %eax
jg 0x2796
xorl %ebx, %ebx
cmpl $0x3, %eax
je 0x27f2
cmpl $0xa, %eax
je 0x27f2
jmp 0x27ae
cmpl $0x10, %eax
je 0x27c9
cmpl $0xe, %eax
jne 0x27ae
incl %r12d
decl %ebp
cmpl %r12d, %ebp
cmovll %ebp, %r12d
jmp 0x27d9
movsbl %r13b, %esi
leaq 0x68(%rsp), %rdi
callq 0x41fe
testb %al, %al
movl $0x1, %eax
cmovnel %eax, %r12d
jmp 0x27d9
cmpl $0x3, %r12d
movl $0x2, %eax
cmovll %eax, %r12d
decl %r12d
movq %r15, %rdi
movl $0xa, %esi
callq 0x3f5c
movb $0x1, %bl
movq %r15, %rdi
xorl %esi, %esi
callq 0x4068
testb %bl, %bl
jne 0x262f
leaq 0xf0(%rsp), %rdi
movl $0x1, %esi
callq 0x4054
leaq 0xf0(%rsp), %rdi
xorl %esi, %esi
callq 0x4068
movl $0x1, %ebx
cmpb $0xa, %r13b
jne 0x28ab
movq 0x30(%rsp), %rax
movslq %r12d, %rcx
leaq (%rcx,%rcx,4), %rcx
movq -0x20(%rax,%rcx,8), %rbx
testq %rbx, %rbx
je 0x285c
movq %rbx, %rdi
callq 0x20c0
movq 0x4769(%rip), %rdi      # 0x6fb8
movq %rbx, %rsi
movq %rax, %rdx
callq 0x21e0
jmp 0x287a
movq 0x4755(%rip), %rax      # 0x6fb8
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
leaq (%rax,%rcx), %rdi
movl 0x20(%rax,%rcx), %esi
orl $0x1, %esi
callq 0x22b0
movq 0x4737(%rip), %rdi      # 0x6fb8
movq (%rdi), %rax
addq -0x18(%rax), %rdi
movl $0xa, %esi
callq 0x2160
movsbl %al, %esi
movq 0x471c(%rip), %rdi      # 0x6fb8
callq 0x2030
xorl %ebx, %ebx
movq %rax, %rdi
callq 0x20f0
leaq 0xe0(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x28cc
movq 0xe0(%rsp), %rsi
incq %rsi
callq 0x2180
leaq 0xc0(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x28ed
movq 0xc0(%rsp), %rsi
incq %rsi
callq 0x2180
leaq 0x68(%rsp), %rdi
callq 0x3f1a
leaq 0xf0(%rsp), %rdi
callq 0x3f1a
movq 0x48(%rsp), %rdi
leaq 0x58(%rsp), %rax
cmpq %rax, %rdi
je 0x2920
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x2180
leaq 0x30(%rsp), %rdi
callq 0x2a84
movl %ebx, %eax
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x29bf
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x2180
jmp 0x29bf
jmp 0x2961
movq %rax, %rbx
jmp 0x29bf
movq %rax, %rbx
movq 0x68(%rsp), %rdi
cmpq %r14, %rdi
je 0x29cc
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x2180
jmp 0x29cc
jmp 0x298a
jmp 0x2991
jmp 0x2991
jmp 0x2996
movq %rax, %rbx
jmp 0x29cc
jmp 0x2991
movq %rax, %rbx
jmp 0x29b5
movq %rax, %rbx
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x29b5
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x2180
leaq 0x68(%rsp), %rdi
callq 0x2a46
leaq 0xf0(%rsp), %rdi
callq 0x3f1a
movq 0x48(%rsp), %rdi
leaq 0x58(%rsp), %rax
cmpq %rax, %rdi
je 0x29e8
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x2180
leaq 0x30(%rsp), %rdi
callq 0x2a84
movq %rbx, %rdi
callq 0x22d0