// File: blakewford[P]kab/main.cpp
// Function: main
int main(int argc, char **argv)
{
    FILE* cache = fopen("cache.json", "r");
    if(cache != nullptr)
    {
        fclose(cache);

        std::ifstream sample("cache.json");

        set_target t;
        std::string line;
        std::getline(sample, line);
        std::istringstream iss(line);
        parse(line.c_str(), &t);
        addCachedTarget(t);
        while(std::getline(sample, line))
        {
            std::istringstream iss(line);
            parse(line.c_str(), &t);
            addCachedTarget(t);
        }

        if(argc > 1)
        {
            loop();
            return 0;
        }

        int32_t selection = -1;
        printTargets(false);
        printf("[%zu] \u001b[31mCancel\u001b[0m\n", gFound.size());
        printf("\nChoose device:\n");
        std::cin >> selection;

        if(selection >=0 && selection < gFound.size())
        {
            size_t ndx = selection;
            printf("\nSwitch \u001b[34m%s\u001b[0m:\n", gFound.at(ndx).name);
            printf("[0] OFF\n");
            printf("[1] ON\n");
            printf("[2] Listen\n");
            printf("[3] \u001b[31mCancel\u001b[0m\n");
            std::cin >> selection;
            if(selection >=0 && selection < 3)
            {
                switch(selection)
                {
                    case 0:
                    case 1:
                        toggle(ndx, selection ? true: false);
                        break;
                    case 2:
                        loop();
                        break;
                    default:
                        break;
                }
            }
        }
    }
    else
    {
        printf("Starting discovery...\n");
        discover();
    }
 
    return 0;
}

// Assembly:
subq $0x658, %rsp            # imm = 0x658
movl $0x0, 0x654(%rsp)
movl %edi, 0x650(%rsp)
movq %rsi, 0x648(%rsp)
leaq 0x4c52(%rip), %rdi      # 0x801a
leaq 0x4eca(%rip), %rsi      # 0x8299
callq 0x2280
movq %rax, 0x640(%rsp)
cmpq $0x0, 0x640(%rsp)
je 0x378c
movq 0x640(%rsp), %rdi
callq 0x2230
leaq 0x4c1b(%rip), %rsi      # 0x801a
leaq 0x438(%rsp), %rdi
movl $0x8, %edx
callq 0x23e0
leaq 0x370(%rsp), %rdi
callq 0x3b30
jmp 0x3420
leaq 0x340(%rsp), %rdi
movq %rdi, 0x28(%rsp)
callq 0x22c0
movq 0x28(%rsp), %rsi
leaq 0x438(%rsp), %rdi
callq 0x2070
jmp 0x3446
leaq 0x1c0(%rsp), %rdi
leaq 0x340(%rsp), %rsi
movl $0x8, %edx
callq 0x2390
jmp 0x3462
leaq 0x340(%rsp), %rdi
callq 0x20f0
movq %rax, %rdi
leaq 0x370(%rsp), %rsi
callq 0x4910
jmp 0x3481
leaq 0x370(%rsp), %rdi
callq 0x3150
jmp 0x3490
jmp 0x3492
leaq 0x438(%rsp), %rdi
leaq 0x340(%rsp), %rsi
callq 0x2070
movq %rax, 0x20(%rsp)
jmp 0x34ae
movq 0x20(%rsp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
addq %rax, %rdi
callq 0x22a0
movb %al, 0x1f(%rsp)
jmp 0x34c8
movb 0x1f(%rsp), %al
testb $0x1, %al
jne 0x34d5
jmp 0x3599
leaq 0x40(%rsp), %rdi
leaq 0x340(%rsp), %rsi
movl $0x8, %edx
callq 0x2390
jmp 0x34ee
leaq 0x340(%rsp), %rdi
callq 0x20f0
movq %rax, %rdi
leaq 0x370(%rsp), %rsi
callq 0x4910
jmp 0x350d
leaq 0x370(%rsp), %rdi
callq 0x3150
jmp 0x351c
leaq 0x40(%rsp), %rdi
callq 0x2110
jmp 0x3492
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x368(%rsp)
movl %eax, 0x364(%rsp)
jmp 0x377d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x368(%rsp)
movl %eax, 0x364(%rsp)
jmp 0x3770
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x368(%rsp)
movl %eax, 0x364(%rsp)
jmp 0x3763
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x368(%rsp)
movl %eax, 0x364(%rsp)
leaq 0x40(%rsp), %rdi
callq 0x2110
jmp 0x3763
cmpl $0x1, 0x650(%rsp)
jle 0x35c2
callq 0x3260
jmp 0x35aa
movl $0x0, 0x654(%rsp)
movl $0x1, 0x3c(%rsp)
jmp 0x372e
movl $0xffffffff, 0x38(%rsp) # imm = 0xFFFFFFFF
xorl %edi, %edi
callq 0x2810
jmp 0x35d3
leaq 0x8d1e(%rip), %rdi      # 0xc2f8
callq 0x3910
movq %rax, %rsi
leaq 0x4d38(%rip), %rdi      # 0x8321
xorl %eax, %eax
callq 0x2030
jmp 0x35f2
leaq 0x4d3f(%rip), %rdi      # 0x8338
xorl %eax, %eax
callq 0x2030
jmp 0x3602
movq 0x89cf(%rip), %rdi      # 0xbfd8
leaq 0x38(%rsp), %rsi
callq 0x2140
jmp 0x3615
cmpl $0x0, 0x38(%rsp)
jl 0x3726
movslq 0x38(%rsp), %rax
movq %rax, 0x10(%rsp)
leaq 0x8cc7(%rip), %rdi      # 0xc2f8
callq 0x3910
movq %rax, %rcx
movq 0x10(%rsp), %rax
cmpq %rcx, %rax
jae 0x3726
movslq 0x38(%rsp), %rax
movq %rax, 0x30(%rsp)
movq 0x30(%rsp), %rsi
leaq 0x8c9b(%rip), %rdi      # 0xc2f8
callq 0x38d0
movq %rax, 0x8(%rsp)
jmp 0x3669
movq 0x8(%rsp), %rsi
addq $0x2a, %rsi
leaq 0x4cd0(%rip), %rdi      # 0x8349
xorl %eax, %eax
callq 0x2030
jmp 0x3682
leaq 0x4cd6(%rip), %rdi      # 0x835f
xorl %eax, %eax
callq 0x2030
jmp 0x3692
leaq 0x4ccf(%rip), %rdi      # 0x8368
xorl %eax, %eax
callq 0x2030
jmp 0x36a2
leaq 0x4cc7(%rip), %rdi      # 0x8370
xorl %eax, %eax
callq 0x2030
jmp 0x36b2
leaq 0x4cc3(%rip), %rdi      # 0x837c
xorl %eax, %eax
callq 0x2030
jmp 0x36c2
movq 0x890f(%rip), %rdi      # 0xbfd8
leaq 0x38(%rsp), %rsi
callq 0x2140
jmp 0x36d5
cmpl $0x0, 0x38(%rsp)
jl 0x3724
cmpl $0x3, 0x38(%rsp)
jge 0x3724
movl 0x38(%rsp), %eax
movl %eax, 0x4(%rsp)
subl $0x2, %eax
jb 0x36fd
jmp 0x36f2
movl 0x4(%rsp), %eax
subl $0x2, %eax
je 0x3717
jmp 0x3720
movq 0x30(%rsp), %rdi
movl 0x38(%rsp), %eax
testl %eax, %eax
setne %al
movzbl %al, %esi
callq 0x26d0
jmp 0x3715
jmp 0x3722
callq 0x3260
jmp 0x371e
jmp 0x3722
jmp 0x3722
jmp 0x3724
jmp 0x3726
movl $0x0, 0x3c(%rsp)
leaq 0x1c0(%rsp), %rdi
callq 0x2110
leaq 0x340(%rsp), %rdi
callq 0x2150
leaq 0x438(%rsp), %rdi
callq 0x2090
movl 0x3c(%rsp), %eax
testl %eax, %eax
je 0x3761
jmp 0x375f
jmp 0x37aa
jmp 0x379f
leaq 0x1c0(%rsp), %rdi
callq 0x2110
leaq 0x340(%rsp), %rdi
callq 0x2150
leaq 0x438(%rsp), %rdi
callq 0x2090
jmp 0x37b9
leaq 0x4bfe(%rip), %rdi      # 0x8391
movb $0x0, %al
callq 0x2030
callq 0x2b80
movl $0x0, 0x654(%rsp)
movl 0x654(%rsp), %eax
addq $0x658, %rsp            # imm = 0x658
retq
movq 0x368(%rsp), %rdi
callq 0x23a0
nopw %cs:(%rax,%rax)