// File: brunokeymolen[P]ssdp/src/ssdp.cpp
// Function: SSDP::SendSearchRequest(char const*)
int SSDP::SendSearchRequest(const char *target) {
    std::stringstream str;
    str << "M-SEARCH * HTTP/1.1\r\n";
    str << "Mx: " << 5 << "\r\n";
    str << "St: " << target << "\r\n";
    str << "Man: \"ssdp:discover\"\r\n";
    str << "User-Agent: UPnP/1.1 " << mOS << " " << mProduct << "\r\n";
    str << "Connection: close\r\n";
    str << "Host: 239.255.255.250:1900\r\n\r\n";

    str.seekp(0, ios::end);
    stringstream::pos_type length = str.tellp();
    str.seekp(0, ios::beg);

    if(mMulticastSocket != INVALID_SOCKET)
        sendto(mMulticastSocket, str.str().c_str(), (unsigned long)length, 0, (struct sockaddr*)&mDstaddr , sizeof(struct sockaddr));
    else
        printf("invalid socket\n");

    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x250, %rsp            # imm = 0x250
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x1e0(%rbp)
leaq -0x198(%rbp), %rdi
callq 0x31a0
leaq -0x188(%rbp), %rdi
leaq 0xb88b(%rip), %rsi      # 0x10373
callq 0x32b0
jmp 0x4aef
leaq -0x188(%rbp), %rdi
leaq 0xb88c(%rip), %rsi      # 0x10389
callq 0x32b0
movq %rax, -0x1e8(%rbp)
jmp 0x4b0b
movq -0x1e8(%rbp), %rdi
movl $0x5, %esi
callq 0x34a0
movq %rax, -0x1f0(%rbp)
jmp 0x4b25
movq -0x1f0(%rbp), %rdi
leaq 0xb8bd(%rip), %rsi      # 0x103f0
callq 0x32b0
jmp 0x4b3a
leaq -0x188(%rbp), %rdi
leaq 0xb846(%rip), %rsi      # 0x1038e
callq 0x32b0
movq %rax, -0x1f8(%rbp)
jmp 0x4b56
movq -0x1f8(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x32b0
movq %rax, -0x200(%rbp)
jmp 0x4b6f
movq -0x200(%rbp), %rdi
leaq 0xb873(%rip), %rsi      # 0x103f0
callq 0x32b0
jmp 0x4b84
leaq -0x188(%rbp), %rdi
leaq 0xb801(%rip), %rsi      # 0x10393
callq 0x32b0
jmp 0x4b99
leaq -0x188(%rbp), %rdi
leaq 0xb803(%rip), %rsi      # 0x103aa
callq 0x32b0
movq %rax, -0x208(%rbp)
jmp 0x4bb5
movq -0x208(%rbp), %rdi
movq -0x1e0(%rbp), %rsi
addq $0x200, %rsi            # imm = 0x200
callq 0x3270
movq %rax, -0x210(%rbp)
jmp 0x4bd8
movq -0x210(%rbp), %rdi
leaq 0xb7d8(%rip), %rsi      # 0x103be
callq 0x32b0
movq %rax, -0x218(%rbp)
jmp 0x4bf4
movq -0x218(%rbp), %rdi
movq -0x1e0(%rbp), %rsi
addq $0x220, %rsi            # imm = 0x220
callq 0x3270
movq %rax, -0x220(%rbp)
jmp 0x4c17
movq -0x220(%rbp), %rdi
leaq 0xb7cb(%rip), %rsi      # 0x103f0
callq 0x32b0
jmp 0x4c2c
leaq -0x188(%rbp), %rdi
leaq 0xb786(%rip), %rsi      # 0x103c0
callq 0x32b0
jmp 0x4c41
leaq -0x188(%rbp), %rdi
leaq 0xb785(%rip), %rsi      # 0x103d4
callq 0x32b0
jmp 0x4c56
leaq -0x188(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0x2, %edx
callq 0x32c0
jmp 0x4c6d
leaq -0x188(%rbp), %rdi
callq 0x3250
movq %rdx, -0x230(%rbp)
movq %rax, -0x228(%rbp)
jmp 0x4c89
movq -0x230(%rbp), %rax
movq -0x228(%rbp), %rcx
movq %rcx, -0x1b8(%rbp)
movq %rax, -0x1b0(%rbp)
leaq -0x188(%rbp), %rdi
xorl %edx, %edx
movl %edx, %esi
callq 0x32c0
jmp 0x4cb7
movq -0x1e0(%rbp), %rax
cmpl $-0x1, 0x8(%rax)
je 0x4d85
movq -0x1e0(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0x234(%rbp)
leaq -0x1d8(%rbp), %rdi
leaq -0x198(%rbp), %rsi
callq 0x32a0
jmp 0x4ced
leaq -0x1d8(%rbp), %rdi
callq 0x3100
movq %rax, -0x248(%rbp)
leaq -0x1b8(%rbp), %rdi
callq 0x5c00
movq %rax, -0x240(%rbp)
jmp 0x4d15
movq -0x240(%rbp), %rdx
movq -0x248(%rbp), %rsi
movl -0x234(%rbp), %edi
movq -0x1e0(%rbp), %r8
addq $0x20, %r8
xorl %ecx, %ecx
movl $0x10, %r9d
callq 0x3070
jmp 0x4d43
leaq -0x1d8(%rbp), %rdi
callq 0x3548
jmp 0x4d97
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1a0(%rbp)
movl %eax, -0x1a4(%rbp)
jmp 0x4dae
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1a0(%rbp)
movl %eax, -0x1a4(%rbp)
leaq -0x1d8(%rbp), %rdi
callq 0x3548
jmp 0x4dae
leaq 0xb42b(%rip), %rdi      # 0x101b7
xorl %eax, %eax
callq 0x3040
jmp 0x4d95
jmp 0x4d97
leaq -0x198(%rbp), %rdi
callq 0x31c0
xorl %eax, %eax
addq $0x250, %rsp            # imm = 0x250
popq %rbp
retq
leaq -0x198(%rbp), %rdi
callq 0x31c0
movq -0x1a0(%rbp), %rdi
callq 0x34b0
nopw %cs:(%rax,%rax)