// File: blakewford[P]kab/main.cpp
// Function: discover()
void discover()
{
    int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
    if(sock < 0) return;

    int32_t broadcast = 1;
    int ret = setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &broadcast, sizeof(broadcast));
    if(ret != 0) return;

    struct sockaddr_in server = buildServerType(INADDR_BROADCAST, DISCOVER_PORT1);

    broadcastParams params1;
    params1.socket = sock;
    params1.port = DISCOVER_PORT1;
    broadcastParams params2 = params1;
    params2.port = DISCOVER_PORT2;

    pthread_t watchDog;
    pthread_create(&gRecvP1,  nullptr, recvBroadcast, &params1);
    pthread_create(&gRecvP1,  nullptr, recvBroadcast, &params2);
    pthread_create(&watchDog, nullptr, timeoutThread, &sock);

    buildQuery();

    int32_t count = 0;
    ssize_t sent = sendto(sock, query, QUERY_SIZE, 0, (sockaddr*)&server, sizeof(server));
    while(sent == QUERY_SIZE && count < ATTTEMPTS && gKeepGoing)
    {
        count++;
        server.sin_port = server.sin_port == htons(DISCOVER_PORT1) ? htons(DISCOVER_PORT2): htons(DISCOVER_PORT1);
        usleep(1000*72);
        buildQuery();

        sent = sendto(sock, query, QUERY_SIZE, 0, (sockaddr*)&server, sizeof(server));
    }

    quit(sock);
}

// Assembly:
subq $0x58, %rsp
movl $0x2, %esi
movl $0x11, %edx
movl %esi, %edi
callq 0x2080
movl %eax, 0x54(%rsp)
cmpl $0x0, 0x54(%rsp)
jge 0x2ba5
jmp 0x2d69
movl $0x1, 0x50(%rsp)
movl 0x54(%rsp), %edi
movl $0x1, %esi
movl $0x6, %edx
leaq 0x50(%rsp), %rcx
movl $0x4, %r8d
callq 0x23c0
movl %eax, 0x4c(%rsp)
cmpl $0x0, 0x4c(%rsp)
je 0x2bdb
jmp 0x2d69
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
movl $0x19, %esi
callq 0x2570
movq %rax, 0x3c(%rsp)
movq %rdx, 0x44(%rsp)
movl 0x54(%rsp), %eax
movl %eax, 0x34(%rsp)
movw $0x19, 0x38(%rsp)
movq 0x34(%rsp), %rax
movq %rax, 0x2c(%rsp)
movw $0x1700, 0x30(%rsp)     # imm = 0x1700
leaq 0x96cd(%rip), %rdi      # 0xc2e8
xorl %eax, %eax
movl %eax, %esi
leaq -0x236(%rip), %rdx      # 0x29f0
leaq 0x34(%rsp), %rcx
callq 0x23d0
leaq 0x96b1(%rip), %rdi      # 0xc2e8
xorl %eax, %eax
movl %eax, %esi
leaq -0x252(%rip), %rdx      # 0x29f0
leaq 0x2c(%rsp), %rcx
callq 0x23d0
leaq 0x20(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
leaq -0x2ac(%rip), %rdx      # 0x29b0
leaq 0x54(%rsp), %rcx
callq 0x23d0
callq 0x25c0
movl $0x0, 0x1c(%rsp)
movl 0x54(%rsp), %edi
leaq 0x95c2(%rip), %rsi      # 0xc240
movl $0x80, %edx
xorl %ecx, %ecx
leaq 0x3c(%rsp), %r8
movl $0x10, %r9d
callq 0x2050
movq %rax, 0x10(%rsp)
xorl %eax, %eax
cmpq $0x80, 0x10(%rsp)
movb %al, 0xf(%rsp)
jne 0x2cc2
xorl %eax, %eax
cmpl $0xe, 0x1c(%rsp)
movb %al, 0xf(%rsp)
jge 0x2cc2
movb 0x9552(%rip), %al       # 0xc210
movb %al, 0xf(%rsp)
movb 0xf(%rsp), %al
testb $0x1, %al
jne 0x2ccf
jmp 0x2d60
movl 0x1c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1c(%rsp)
movzwl 0x3e(%rsp), %eax
movl %eax, 0x8(%rsp)
movl $0x19, %edi
callq 0x2040
movw %ax, %cx
movl 0x8(%rsp), %eax
movzwl %cx, %ecx
cmpl %ecx, %eax
jne 0x2d0c
movl $0x1700, %edi           # imm = 0x1700
callq 0x2040
movw %ax, 0x6(%rsp)
jmp 0x2d1b
movl $0x19, %edi
callq 0x2040
movw %ax, 0x6(%rsp)
movw 0x6(%rsp), %ax
movw %ax, 0x3e(%rsp)
movl $0x11940, %edi          # imm = 0x11940
callq 0x22e0
callq 0x25c0
movl 0x54(%rsp), %edi
leaq 0x9501(%rip), %rsi      # 0xc240
movl $0x80, %edx
xorl %ecx, %ecx
leaq 0x3c(%rsp), %r8
movl $0x10, %r9d
callq 0x2050
movq %rax, 0x10(%rsp)
jmp 0x2c9a
movl 0x54(%rsp), %edi
callq 0x2960
addq $0x58, %rsp
retq
nop