// File: /workspace/github_cpp_makefile_star2_O2/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
pushq $0x2
popq %rdi
pushq $0x11
popq %rdx
movl %edi, %esi
callq 0x2070
movl %eax, 0x4(%rsp)
testl %eax, %eax
js 0x2bb4
movl %eax, %ebx
leaq 0x8(%rsp), %rsi
movq $0x6c070002, (%rsi)     # imm = 0x6C070002
andq $0x0, 0x8(%rsi)
pushq $0x10
popq %rdx
movl %eax, %edi
callq 0x2050
testl %eax, %eax
js 0x2bb4
leaq 0x24c8(%rip), %rdi      # 0x505c
callq 0x2240
leaq 0x28(%rsp), %rcx
movl %eax, (%rcx)
pushq $0x23
popq %rdx
pushq $0x8
popq %r8
movl %ebx, %edi
xorl %esi, %esi
callq 0x2370
testl %eax, %eax
je 0x2bc6
addq $0x1f8, %rsp            # imm = 0x1F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq -0x3e8(%rip), %rdx      # 0x27e5
leaq 0x20(%rsp), %rdi
leaq 0x4(%rsp), %rcx
xorl %esi, %esi
callq 0x2380
leaq 0x50(%rsp), %rbx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x2320
movq %rbx, %rdi
callq 0x21c0
leaq 0x60(%rsp), %r14
leaq 0x8(%rsp), %r15
leaq 0x30(%rsp), %r13
pushq $0x2
popq %rbx
pushq $0x10
popq %r12
cmpb $0x1, 0x55d6(%rip)      # 0x81e8
jne 0x2bb4
movl 0x4(%rsp), %edi
movl $0x198, %edx            # imm = 0x198
movq %r14, %rsi
xorl %ecx, %ecx
movq %r15, %r8
leaq 0x1c(%rsp), %r9
callq 0x2190
movq %r13, %rdi
movq %r14, %rsi
leaq 0x3(%rsp), %rdx
callq 0x20d0
movl %ebx, %edi
leaq 0xc(%rsp), %rsi
movq %r14, %rdx
movl %r12d, %ecx
callq 0x2250
movzwl 0xa(%rsp), %edx
leaq 0x2616(%rip), %rdi      # 0x5273
movq %r14, %rsi
xorl %eax, %eax
callq 0x2030
movl %ebx, %edi
movl %ebx, %esi
pushq $0x11
popq %rdx
callq 0x2070
movl %eax, %ebp
movl $0x190, %edx            # imm = 0x190
movl %eax, %edi
leaq 0x245f(%rip), %rsi      # 0x50e2
xorl %ecx, %ecx
movq %r15, %r8
movl %r12d, %r9d
callq 0x2040
movl %ebp, %edi
callq 0x23a0
movq %r13, %rdi
callq 0x2100
jmp 0x2c0b
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x2100
movq %rbx, %rdi
callq 0x2360