// File: /workspace/github_cpp_makefile_star2_O3/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 $0x308, %rsp            # imm = 0x308
movq %rsi, %rdx
movl %edi, %esi
leaq -0x50(%rbp), %rbx
movq %rbx, %rdi
callq 0x5fc8
cmpb $0x1, 0x9(%rbx)
jne 0x56d4
leaq 0x10acf(%rip), %rdi     # 0x1619e
callq 0x42b0
leaq -0x330(%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 0x575e
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 0x152b8
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 0x572a
movq -0x50(%rbp), %rsi
testq %rsi, %rsi
jne 0x59dc
callq 0x44e0
callq 0x40d0
callq 0x43b0
movq 0x1982f(%rip), %rax     # 0x1efb0
movq (%rax), %rdi
movl $0x1, %esi
callq 0x4200
xorl %edi, %edi
callq 0x4290
movq 0x19814(%rip), %rax     # 0x1efb0
movq (%rax), %rdi
movl $0x1, %esi
callq 0x43c0
movq 0x19800(%rip), %rax     # 0x1efb0
movq (%rax), %rdi
movl $0x1, %esi
callq 0x4160
callq 0x4240
callq 0x4090
movq 0x197e2(%rip), %rax     # 0x1efb0
movq (%rax), %rcx
movl $0xfffffffa, %eax       # imm = 0xFFFFFFFA
testq %rcx, %rcx
je 0x57e2
movswl 0x4(%rcx), %eax
addl $-0x4, %eax
movl %eax, 0x19b38(%rip)     # 0x1f320
leaq -0xc8(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0x60a0
leaq -0x330(%rbp), %rdi
leaq -0xc8(%rbp), %rsi
xorl %edx, %edx
callq 0x4eff
movq -0xc8(%rbp), %rdi
testq %rdi, %rdi
je 0x5828
movq -0xb8(%rbp), %rsi
subq %rdi, %rsi
callq 0x4300
xorl %ebx, %ebx
leaq -0x330(%rbp), %r15
leaq -0x40(%rbp), %r12
leaq -0xe0(%rbp), %r13
xorl %r14d, %r14d
callq 0x48ac
testl %eax, %eax
je 0x583f
movq 0x19761(%rip), %rax     # 0x1efb0
movq (%rax), %rdi
callq 0x4150
cmpl $0x102, %eax            # imm = 0x102
jle 0x5891
cmpl $0x103, %eax            # imm = 0x103
je 0x58d6
cmpl $0x14a, %eax            # imm = 0x14A
je 0x58e4
cmpl $0x107, %eax            # imm = 0x107
jne 0x5984
testl %r14d, %r14d
jle 0x5991
decl %r14d
movb $0x0, -0x330(%rbp,%r14)
jmp 0x5991
cmpl $0xa, %eax
je 0x5975
cmpl $0x102, %eax            # imm = 0x102
jne 0x5984
movq -0x38(%rbp), %rax
subq -0x40(%rbp), %rax
movslq %ebx, %rcx
sarq $0x3, %rax
decq %rax
cmpq %rcx, %rax
jbe 0x5991
movl 0x19a5a(%rip), %eax     # 0x1f320
decl %eax
xorl %ecx, %ecx
cmpl %eax, %ebx
setl %cl
addl %ecx, %ebx
jmp 0x5991
xorl %eax, %eax
testl %ebx, %ebx
setg %al
subl %eax, %ebx
jmp 0x5991
movq -0x40(%rbp), %rax
cmpq %rax, -0x38(%rbp)
je 0x5991
movslq %ebx, %rcx
movq (%rax,%rcx,8), %rax
cmpb $0x1, 0x20(%rax)
jne 0x5991
movq %r12, %rdi
movl %ebx, %esi
callq 0x50d1
movzbl -0x46(%rbp), %esi
leaq -0xb0(%rbp), %rdi
callq 0x4a7d
movups -0x68(%rbp), %xmm0
movq -0x58(%rbp), %rax
movaps -0xb0(%rbp), %xmm1
movups %xmm1, -0x68(%rbp)
movq -0xa0(%rbp), %rcx
movq %rcx, -0x58(%rbp)
movups %xmm0, -0x90(%rbp)
movq %rax, -0x80(%rbp)
xorps %xmm0, %xmm0
movaps %xmm0, -0xb0(%rbp)
movq $0x0, -0xa0(%rbp)
leaq -0x90(%rbp), %rdi
callq 0x5ec8
leaq -0xb0(%rbp), %rdi
callq 0x5ec8
jmp 0x5991
movq -0x40(%rbp), %rax
cmpq %rax, -0x38(%rbp)
je 0x5991
jmp 0x5a6f
movslq %r14d, %rcx
movb %al, -0x330(%rbp,%rcx)
incl %r14d
leaq -0x68(%rbp), %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x52d7
movq %r13, %rdi
movq %r12, %rsi
callq 0x60a0
movq %r15, %rdi
movq %r13, %rsi
movl %ebx, %edx
callq 0x4eff
movq -0xe0(%rbp), %rdi
testq %rdi, %rdi
je 0x583f
movq -0xd0(%rbp), %rsi
subq %rdi, %rsi
callq 0x4300
jmp 0x583f
leaq -0x80(%rbp), %r14
movq %r14, -0x10(%r14)
movq $0x0, -0x8(%r14)
movb $0x0, (%r14)
leaq -0x68(%rbp), %rdi
leaq -0x90(%rbp), %rdx
callq 0x5430
testb %al, %al
je 0x5a44
leaq -0x120(%rbp), %r15
movq %r15, -0x10(%r15)
movq -0x90(%rbp), %rsi
movq -0x88(%rbp), %rdx
addq %rsi, %rdx
leaq -0x130(%rbp), %rdi
callq 0x622a
movzwl -0x70(%rbp), %eax
leaq -0x130(%rbp), %rdi
movw %ax, 0x20(%rdi)
movzbl -0x48(%rbp), %esi
callq 0x5267
callq 0x43e0
movq 0x19578(%rip), %rax     # 0x1efc8
movq (%rax), %rdi
movq -0x50(%rbp), %rdx
leaq 0x1074a(%rip), %rsi     # 0x161a8
xorl %eax, %eax
callq 0x44b0
movl $0x1, %edi
callq 0x43f0
movslq %ebx, %rcx
movq (%rax,%rcx,8), %rbx
leaq -0xf8(%rbp), %r14
movq %r14, -0x10(%r14)
movq (%rbx), %rsi
movq 0x8(%rbx), %rdx
addq %rsi, %rdx
leaq -0x108(%rbp), %rdi
callq 0x622a
movzwl 0x20(%rbx), %eax
leaq -0x108(%rbp), %rdi
movw %ax, 0x20(%rdi)
movzbl -0x48(%rbp), %esi
callq 0x5267
jmp 0x5b4a
movq %rax, %rbx
movq -0x108(%rbp), %rdi
cmpq %r14, %rdi
je 0x5b4d
movq -0xf8(%rbp), %rsi
jmp 0x5b40
jmp 0x5b4a
movq %rax, %rbx
movq -0xe0(%rbp), %rdi
testq %rdi, %rdi
je 0x5b4d
movq -0xd0(%rbp), %rsi
jmp 0x5b04
jmp 0x5b4a
jmp 0x5b4a
movq %rax, %rbx
movq -0xc8(%rbp), %rdi
testq %rdi, %rdi
je 0x5b4d
movq -0xb8(%rbp), %rsi
subq %rdi, %rsi
jmp 0x5b43
jmp 0x5b4a
movq %rax, %rbx
movq -0x130(%rbp), %rdi
cmpq %r15, %rdi
je 0x5b30
movq -0x120(%rbp), %rsi
incq %rsi
callq 0x4300
jmp 0x5b30
jmp 0x5b4a
movq %rax, %rbx
movq -0x90(%rbp), %rdi
cmpq %r14, %rdi
je 0x5b4d
movq -0x80(%rbp), %rsi
incq %rsi
callq 0x4300
jmp 0x5b4d
movq %rax, %rbx
movq -0x40(%rbp), %rdi
testq %rdi, %rdi
je 0x5b62
movq -0x30(%rbp), %rsi
subq %rdi, %rsi
callq 0x4300
leaq -0x68(%rbp), %rdi
callq 0x5ec8
movq %rbx, %rdi
callq 0x4580
nop