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

    struct sockaddr_in server = buildServerType(INADDR_ANY, DISCOVER_WEMO);
    if(bind(sock, (struct sockaddr*)&server, sizeof(server)) < 0) return;

    struct ip_mreq group;
    group.imr_multiaddr.s_addr = inet_addr(SSDP);
    int ret = setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char*)&group, sizeof(group));
    if(ret != 0) return;

    pthread_t watchDog;
    pthread_create(&watchDog, nullptr, timeoutThread, &sock);

    const char* packet =
"HTTP/1.1 200 OK\r\n"\
"CACHE-CONTROL: max-age=86400\r\n"\
"DATE: %s, %02d %s %d %02d:%02d:%02d %s\r\n"\
"EXT:\r\n"\
"LOCATION: http://%s:49153/setup.xml\r\n"\
"OPT: \"http://schemas.upnp.org/upnp/1/0/\"; ns=01\r\n"\
"01-NLS: 11111111-2222-3333-4444-555555555555\r\n"\
"SERVER: Unspecified, UPnP/1.0, Unspecified\r\n"\
"X-User-Agent: redsonic\r\n"\
"ST: urn:Belkin:device:controllee:1\r\n"\
"USN: uuid:Socket-1_0-2GTbor5wqtq79E::urn:Belkin:device:controllee:1\r\n\r\n";

    struct timeval now;
    gettimeofday(&now, nullptr);
    struct tm* gmt = gmtime(&now.tv_sec);

    const char* DAYS[7] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
    const char* MONTHS[12] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};

    socklen_t length;
    char buffer[RESPONSE_SIZE];
    while(gKeepGoing)
    {
        ssize_t recv = recvfrom(sock, buffer, RESPONSE_SIZE, 0, (sockaddr*)&server, &length);
        std::string search(buffer);
//        if(search.find("Belkin") != std::string::npos)
        {
            inet_ntop(AF_INET, &server.sin_addr, buffer, INET_ADDRSTRLEN);
            printf("%s %d\n", buffer, server.sin_port);

            int reply = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
            sendto(reply, packet, strlen(packet), 0, (sockaddr*)&server, sizeof(server));
            close(reply);
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1f8, %rsp            # imm = 0x1F8
movl $0x2, %edi
movl $0x2, %esi
movl $0x11, %edx
callq 0x2060
movl %eax, 0x4(%rsp)
testl %eax, %eax
js 0x2eb8
leaq 0x8(%rsp), %rsi
movq $0x6c070002, (%rsi)     # imm = 0x6C070002
movq $0x0, 0x8(%rsi)
movl %eax, %edi
movl $0x10, %edx
callq 0x2050
testl %eax, %eax
js 0x2eb8
leaq 0x22e2(%rip), %rdi      # 0x505c
callq 0x2230
leaq 0x48(%rsp), %rcx
movl %eax, (%rcx)
movl 0x4(%rsp), %edi
xorl %esi, %esi
movl $0x23, %edx
movl $0x8, %r8d
callq 0x2370
testl %eax, %eax
jne 0x2eb8
leaq -0x54a(%rip), %rdx      # 0x2861
leaq 0x40(%rsp), %rdi
leaq 0x4(%rsp), %rcx
xorl %esi, %esi
callq 0x2390
leaq 0x50(%rsp), %rbx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x2320
movq %rbx, %rdi
callq 0x21a0
cmpb $0x1, 0x541e(%rip)      # 0x81f8
jne 0x2eb8
leaq 0x30(%rsp), %r13
leaq 0x60(%rsp), %r14
leaq 0x8(%rsp), %r15
leaq 0x247d(%rip), %rbp      # 0x5273
leaq 0x22e5(%rip), %rbx      # 0x50e2
movl 0x4(%rsp), %edi
movl $0x198, %edx            # imm = 0x198
movq %r14, %rsi
xorl %ecx, %ecx
movq %r15, %r8
leaq 0x1c(%rsp), %r9
callq 0x2170
movq %r13, 0x20(%rsp)
movq %r14, %rdi
callq 0x20e0
leaq (%rsp,%rax), %rdx
addq $0x60, %rdx
leaq 0x20(%rsp), %rdi
movq %r14, %rsi
callq 0x3616
movl $0x2, %edi
leaq 0xc(%rsp), %rsi
movq %r14, %rdx
movl $0x10, %ecx
callq 0x2240
movzwl 0xa(%rsp), %edx
movq %rbp, %rdi
movq %r14, %rsi
xorl %eax, %eax
callq 0x2030
movl $0x2, %edi
movl $0x2, %esi
movl $0x11, %edx
callq 0x2060
movl %eax, %r12d
movl $0x190, %edx            # imm = 0x190
movl %eax, %edi
movq %rbx, %rsi
xorl %ecx, %ecx
movq %r15, %r8
movl $0x10, %r9d
callq 0x2040
movl %r12d, %edi
callq 0x23c0
movq 0x20(%rsp), %rdi
cmpq %r13, %rdi
je 0x2eab
callq 0x21e0
cmpb $0x0, 0x5346(%rip)      # 0x81f8
jne 0x2dfd
addq $0x1f8, %rsp            # imm = 0x1F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq 0x20(%rsp), %rdi
cmpq %r13, %rdi
je 0x2edc
callq 0x21e0
movq %rbx, %rdi
callq 0x2360