// File: /workspace/github_cpp_makefile_star2_O1/npazosmendez[P]branchsearch/branchsearch.cpp
// Function: main
int main(int argc, char** argv){
    args_t args(argc, argv);

    if (args.fetch_before) system("git fetch");

    // variables
    char regex_value[MAX_BRANCH_NAME_LENGTH] = {0};
    int index = 0;
    int selected_branch = 0;
    vector<branch_t> all_branches = get_branches(args.locals_only);
    vector<branch_t*> filtered_branches;
    for (int i = 0; i < all_branches.size(); ++i){
        filtered_branches.push_back(&all_branches[i]);
    }

    if (args.fast_switch != NULL){
        branch_t target_branch;
        if (best_match(all_branches, args.fast_switch, target_branch)){
            switch_to_branch(target_branch, args.pull_after);
        } else {
            endwin();
            fprintf(stderr, "No branches matching '%s'\n", args.fast_switch);
            exit(1);
        }
    }

    // setup curses
    initscr();
    cbreak();
    noecho();
    nodelay(stdscr, TRUE);
    curs_set(0);
    scrollok(stdscr, TRUE);
    keypad(stdscr, TRUE);
    use_default_colors();
    start_color();

    int max_height, max_width;
    getmaxyx(stdscr, max_height, max_width);
    max_branch_shown = max_height - 5;

    print_window(regex_value, filtered_branches, selected_branch);

    // handle keyboard input and loop until a branch is selected
    while (1) {
        if (kbhit()) {
            int c = getch();
            if(c == KEY_BACKSPACE){
                if(index > 0){
                    index--;
                    regex_value[index] = 0;
                }
            }else if (c == NEW_LINE){
                if(filtered_branches.size()) switch_to_branch(*filtered_branches[selected_branch], args.pull_after);
            }else if (c == KEY_UP){
                if(selected_branch > 0) selected_branch--;
            }else if (c == KEY_DOWN){
                if(selected_branch < filtered_branches.size()-1 && selected_branch < max_branch_shown - 1) selected_branch++;
            }else if (c == KEY_DC){
                if(filtered_branches.size() and filtered_branches[selected_branch]->local){
                    delete_branch(filtered_branches, selected_branch);
                    all_branches = get_branches(args.locals_only);
                }
            }else{
                regex_value[index] = c;
                index++;
            }
            filter_branches(all_branches, regex_value, filtered_branches);
            print_window(regex_value, filtered_branches, selected_branch);
        }
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2f8, %rsp            # imm = 0x2F8
movq %rsi, %rdx
movl %edi, %esi
leaq -0x50(%rbp), %rbx
movq %rbx, %rdi
callq 0x5fb2
cmpb $0x1, 0x9(%rbx)
jne 0x56d8
leaq 0x10acb(%rip), %rdi     # 0x1619e
callq 0x42b0
leaq -0x320(%rbp), %rdi
movl $0x1f4, %edx            # imm = 0x1F4
xorl %esi, %esi
callq 0x41f0
movzbl -0x46(%rbp), %esi
leaq -0x68(%rbp), %rbx
movq %rbx, %rdi
callq 0x4a7d
xorps %xmm0, %xmm0
movaps %xmm0, -0x40(%rbp)
movq $0x0, -0x30(%rbp)
movq (%rbx), %rax
cmpq %rax, 0x8(%rbx)
je 0x5762
xorl %r15d, %r15d
leaq -0x40(%rbp), %rbx
leaq -0x90(%rbp), %r14
movabsq $-0x3333333333333333, %r12 # imm = 0xCCCCCCCCCCCCCCCD
xorl %r13d, %r13d
addq %r15, %rax
movq %rax, -0x90(%rbp)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x15270
incq %r13
movq -0x68(%rbp), %rax
movq -0x60(%rbp), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
imulq %r12, %rcx
addq $0x28, %r15
cmpq %r13, %rcx
ja 0x572e
movq -0x50(%rbp), %rsi
testq %rsi, %rsi
jne 0x59ee
callq 0x44e0
callq 0x40d0
callq 0x43b0
movq 0x1982b(%rip), %rax     # 0x1efb0
movq (%rax), %rdi
movl $0x1, %esi
callq 0x4200
xorl %edi, %edi
callq 0x4290
movq 0x19810(%rip), %rax     # 0x1efb0
movq (%rax), %rdi
movl $0x1, %esi
callq 0x43c0
movq 0x197fc(%rip), %rax     # 0x1efb0
movq (%rax), %rdi
movl $0x1, %esi
callq 0x4160
callq 0x4240
callq 0x4090
movq 0x197de(%rip), %rax     # 0x1efb0
movq (%rax), %rcx
movl $0xfffffffa, %eax       # imm = 0xFFFFFFFA
testq %rcx, %rcx
je 0x57e6
movswl 0x4(%rcx), %eax
addl $-0x4, %eax
movl %eax, 0x19b34(%rip)     # 0x1f320
leaq -0xc0(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0x608a
leaq -0x320(%rbp), %rdi
leaq -0xc0(%rbp), %rsi
xorl %edx, %edx
callq 0x4efc
movq -0xc0(%rbp), %rdi
testq %rdi, %rdi
je 0x582c
movq -0xb0(%rbp), %rsi
subq %rdi, %rsi
callq 0x4300
xorl %ebx, %ebx
leaq -0x320(%rbp), %r15
leaq -0x40(%rbp), %r12
leaq -0xd8(%rbp), %r13
xorl %r14d, %r14d
callq 0x48ac
testl %eax, %eax
je 0x5843
movq 0x1975d(%rip), %rax     # 0x1efb0
movq (%rax), %rdi
callq 0x4150
cmpl $0x102, %eax            # imm = 0x102
jle 0x5895
cmpl $0x103, %eax            # imm = 0x103
je 0x58da
cmpl $0x14a, %eax            # imm = 0x14A
je 0x58e8
cmpl $0x107, %eax            # imm = 0x107
jne 0x5996
testl %r14d, %r14d
jle 0x59a3
decl %r14d
movb $0x0, -0x320(%rbp,%r14)
jmp 0x59a3
cmpl $0xa, %eax
je 0x5987
cmpl $0x102, %eax            # imm = 0x102
jne 0x5996
movq -0x38(%rbp), %rax
subq -0x40(%rbp), %rax
movslq %ebx, %rcx
sarq $0x3, %rax
decq %rax
cmpq %rcx, %rax
jbe 0x59a3
movl 0x19a56(%rip), %eax     # 0x1f320
decl %eax
xorl %ecx, %ecx
cmpl %eax, %ebx
setl %cl
addl %ecx, %ebx
jmp 0x59a3
xorl %eax, %eax
testl %ebx, %ebx
setg %al
subl %eax, %ebx
jmp 0x59a3
movq -0x40(%rbp), %rax
cmpq %rax, -0x38(%rbp)
je 0x59a3
movslq %ebx, %rcx
movq (%rax,%rcx,8), %rax
cmpb $0x1, 0x20(%rax)
jne 0x59a3
movq %r12, %rdi
movl %ebx, %esi
callq 0x50ce
movzbl -0x46(%rbp), %esi
leaq -0xa8(%rbp), %rdi
callq 0x4a7d
xorps %xmm0, %xmm0
movaps %xmm0, -0x90(%rbp)
movups -0x68(%rbp), %xmm0
movq -0x58(%rbp), %rax
movups -0xa8(%rbp), %xmm1
movups %xmm1, -0x68(%rbp)
movq -0x98(%rbp), %rcx
movq %rcx, -0x58(%rbp)
movaps -0x90(%rbp), %xmm1
movaps %xmm0, -0x90(%rbp)
movq %rax, -0x80(%rbp)
movups %xmm1, -0xa8(%rbp)
movq $0x0, -0x98(%rbp)
leaq -0x90(%rbp), %rdi
callq 0x5eb6
leaq -0xa8(%rbp), %rdi
callq 0x5eb6
jmp 0x59a3
movq -0x40(%rbp), %rax
cmpq %rax, -0x38(%rbp)
je 0x59a3
jmp 0x5a81
movslq %r14d, %rcx
movb %al, -0x320(%rbp,%rcx)
incl %r14d
leaq -0x68(%rbp), %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x52d4
movq %r13, %rdi
movq %r12, %rsi
callq 0x608a
movq %r15, %rdi
movq %r13, %rsi
movl %ebx, %edx
callq 0x4efc
movq -0xd8(%rbp), %rdi
testq %rdi, %rdi
je 0x5843
movq -0xc8(%rbp), %rsi
subq %rdi, %rsi
callq 0x4300
jmp 0x5843
leaq -0x80(%rbp), %r14
movq %r14, -0x10(%r14)
movq $0x0, -0x8(%r14)
movb $0x0, (%r14)
leaq -0x68(%rbp), %rdi
leaq -0x90(%rbp), %rdx
callq 0x542d
testb %al, %al
je 0x5a56
leaq -0x118(%rbp), %r15
movq %r15, -0x10(%r15)
movq -0x90(%rbp), %rsi
movq -0x88(%rbp), %rdx
addq %rsi, %rdx
leaq -0x128(%rbp), %rdi
callq 0x6214
movzwl -0x70(%rbp), %eax
leaq -0x128(%rbp), %rdi
movw %ax, 0x20(%rdi)
movzbl -0x48(%rbp), %esi
callq 0x5264
callq 0x43e0
movq 0x19566(%rip), %rax     # 0x1efc8
movq (%rax), %rdi
movq -0x50(%rbp), %rdx
leaq 0x10738(%rip), %rsi     # 0x161a8
xorl %eax, %eax
callq 0x44b0
movl $0x1, %edi
callq 0x43f0
movslq %ebx, %rcx
movq (%rax,%rcx,8), %rbx
leaq -0xf0(%rbp), %r14
movq %r14, -0x10(%r14)
movq (%rbx), %rsi
movq 0x8(%rbx), %rdx
addq %rsi, %rdx
leaq -0x100(%rbp), %rdi
callq 0x6214
movzwl 0x20(%rbx), %eax
leaq -0x100(%rbp), %rdi
movw %ax, 0x20(%rdi)
movzbl -0x48(%rbp), %esi
callq 0x5264
jmp 0x5b5c
movq %rax, %rbx
movq -0x100(%rbp), %rdi
cmpq %r14, %rdi
je 0x5b5f
movq -0xf0(%rbp), %rsi
jmp 0x5b52
jmp 0x5b5c
movq %rax, %rbx
movq -0xd8(%rbp), %rdi
testq %rdi, %rdi
je 0x5b5f
movq -0xc8(%rbp), %rsi
jmp 0x5b16
jmp 0x5b5c
jmp 0x5b5c
movq %rax, %rbx
movq -0xc0(%rbp), %rdi
testq %rdi, %rdi
je 0x5b5f
movq -0xb0(%rbp), %rsi
subq %rdi, %rsi
jmp 0x5b55
jmp 0x5b5c
movq %rax, %rbx
movq -0x128(%rbp), %rdi
cmpq %r15, %rdi
je 0x5b42
movq -0x118(%rbp), %rsi
incq %rsi
callq 0x4300
jmp 0x5b42
jmp 0x5b5c
movq %rax, %rbx
movq -0x90(%rbp), %rdi
cmpq %r14, %rdi
je 0x5b5f
movq -0x80(%rbp), %rsi
incq %rsi
callq 0x4300
jmp 0x5b5f
movq %rax, %rbx
movq -0x40(%rbp), %rdi
testq %rdi, %rdi
je 0x5b74
movq -0x30(%rbp), %rsi
subq %rdi, %rsi
callq 0x4300
leaq -0x68(%rbp), %rdi
callq 0x5eb6
movq %rbx, %rdi
callq 0x4580
nop