// File: /workspace/github_cpp_makefile_star2_O2/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 $0x2e8, %rsp            # imm = 0x2E8
movq %rsi, %rdx
movl %edi, %esi
leaq -0x38(%rbp), %rbx
movq %rbx, %rdi
callq 0x58a2
cmpb $0x1, 0x9(%rbx)
jne 0x51d1
leaq 0xefd2(%rip), %rdi      # 0x1419e
callq 0x42c0
leaq -0x310(%rbp), %rdi
xorl %r15d, %r15d
movl $0x1f4, %edx            # imm = 0x1F4
xorl %esi, %esi
callq 0x4200
movzbl -0x2e(%rbp), %esi
leaq -0x68(%rbp), %rdi
callq 0x48c1
xorps %xmm0, %xmm0
leaq -0x50(%rbp), %rbx
andq $0x0, 0x10(%rbx)
movaps %xmm0, (%rbx)
pushq $0x28
popq %r12
leaq -0x90(%rbp), %r14
xorl %r13d, %r13d
movq -0x68(%rbp), %rcx
movq -0x60(%rbp), %rax
subq %rcx, %rax
cqto
idivq %r12
cmpq %r13, %rax
jbe 0x5244
addq %r15, %rcx
movq %rcx, -0x90(%rbp)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x13466
incq %r13
addq $0x28, %r15
jmp 0x5211
movq -0x38(%rbp), %rsi
testq %rsi, %rsi
jne 0x52b7
callq 0x4500
callq 0x40e0
callq 0x43c0
movq 0x19d45(%rip), %rax     # 0x1efa8
movq (%rax), %rdi
pushq $0x1
popq %rsi
callq 0x4210
xorl %edi, %edi
callq 0x42a0
movq 0x19d2c(%rip), %rax     # 0x1efa8
movq (%rax), %rdi
pushq $0x1
popq %rsi
callq 0x43d0
movq 0x19d1a(%rip), %rax     # 0x1efa8
movq (%rax), %rdi
pushq $0x1
popq %rsi
callq 0x4150
callq 0x4250
callq 0x4090
movq 0x19cfe(%rip), %rax     # 0x1efa8
movq (%rax), %rax
testq %rax, %rax
jne 0x5327
pushq $-0x6
popq %rax
jmp 0x532e
leaq -0x80(%rbp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq -0x68(%rbp), %rdi
leaq -0x90(%rbp), %rdx
callq 0x4fd4
testb %al, %al
je 0x52fe
leaq -0x110(%rbp), %rdi
leaq -0x90(%rbp), %rsi
callq 0x5952
movzbl -0x30(%rbp), %esi
leaq -0x110(%rbp), %rdi
callq 0x4e81
callq 0x43f0
movq 0x19cb6(%rip), %rax     # 0x1efc0
movq (%rax), %rdi
movq -0x38(%rbp), %rdx
leaq 0xee90(%rip), %rsi      # 0x141a8
xorl %eax, %eax
callq 0x44d0
pushq $0x1
popq %rdi
callq 0x4400
movswl 0x4(%rax), %eax
addl $-0x4, %eax
movl %eax, 0x19ffc(%rip)     # 0x1f330
leaq -0xa8(%rbp), %rdi
leaq -0x50(%rbp), %rsi
callq 0x5972
leaq -0x310(%rbp), %rdi
leaq -0xa8(%rbp), %rsi
xorl %ebx, %ebx
xorl %edx, %edx
callq 0x4c06
leaq -0xa8(%rbp), %rdi
callq 0x13620
leaq -0x310(%rbp), %r15
leaq -0x50(%rbp), %r12
leaq -0xc0(%rbp), %r13
xorl %r14d, %r14d
callq 0x473c
testl %eax, %eax
je 0x537c
movq 0x19c1c(%rip), %rax     # 0x1efa8
movq (%rax), %rdi
callq 0x4140
movslq %ebx, %rcx
cmpl $0xa, %eax
je 0x546f
cmpl $0x102, %eax            # imm = 0x102
je 0x53de
cmpl $0x103, %eax            # imm = 0x103
je 0x5455
cmpl $0x14a, %eax            # imm = 0x14A
je 0x5409
cmpl $0x107, %eax            # imm = 0x107
jne 0x5460
testl %r14d, %r14d
jle 0x5479
decl %r14d
movb $0x0, -0x310(%rbp,%r14)
jmp 0x5479
movq -0x48(%rbp), %rax
subq -0x50(%rbp), %rax
sarq $0x3, %rax
decq %rax
cmpq %rcx, %rax
jbe 0x5479
movl 0x19f34(%rip), %eax     # 0x1f330
decl %eax
xorl %ecx, %ecx
cmpl %eax, %ebx
setl %cl
addl %ecx, %ebx
jmp 0x5479
movq -0x50(%rbp), %rax
cmpq %rax, -0x48(%rbp)
je 0x5479
movq (%rax,%rcx,8), %rax
cmpb $0x1, 0x20(%rax)
jne 0x5479
movq %r12, %rdi
movl %ebx, %esi
callq 0x4d95
movzbl -0x2e(%rbp), %esi
leaq -0x90(%rbp), %rdi
callq 0x48c1
leaq -0x68(%rbp), %rdi
leaq -0x90(%rbp), %rsi
callq 0x13668
leaq -0x90(%rbp), %rdi
callq 0x57bc
jmp 0x5479
xorl %eax, %eax
testl %ebx, %ebx
setg %al
subl %eax, %ebx
jmp 0x5479
movslq %r14d, %rcx
movb %al, -0x310(%rbp,%rcx)
incl %r14d
jmp 0x5479
movq -0x50(%rbp), %rax
cmpq %rax, -0x48(%rbp)
jne 0x54ad
leaq -0x68(%rbp), %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x4ee1
movq %r13, %rdi
movq %r12, %rsi
callq 0x5972
movq %r15, %rdi
movq %r13, %rsi
movl %ebx, %edx
callq 0x4c06
movq %r13, %rdi
callq 0x13620
jmp 0x537c
movq (%rax,%rcx,8), %rsi
leaq -0xe8(%rbp), %rdi
callq 0x5952
movzbl -0x30(%rbp), %esi
leaq -0xe8(%rbp), %rdi
callq 0x4e81
jmp 0x5524
movq %rax, %rbx
leaq -0xe8(%rbp), %rdi
jmp 0x551b
jmp 0x5524
movq %rax, %rbx
leaq -0xc0(%rbp), %rdi
jmp 0x54f7
jmp 0x5524
jmp 0x5524
movq %rax, %rbx
leaq -0xa8(%rbp), %rdi
callq 0x13620
jmp 0x5527
jmp 0x5524
movq %rax, %rbx
leaq -0x110(%rbp), %rdi
callq 0x41a0
jmp 0x5514
movq %rax, %rbx
leaq -0x90(%rbp), %rdi
callq 0x41a0
jmp 0x5527
jmp 0x5524
movq %rax, %rbx
leaq -0x50(%rbp), %rdi
callq 0x13620
leaq -0x68(%rbp), %rdi
callq 0x57bc
movq %rbx, %rdi
callq 0x45a0
nop