// File: /workspace/github_cpp_makefile_star2_O1/brunokeymolen[P]ssdp/src/ssdp.cpp
// Function: SSDP::ReadLoop()
int SSDP::ReadLoop(){
    int ret = 0;
    mReadLoop = 1;

    struct timeval timeout;
    timeout.tv_sec = 5;
    timeout.tv_usec = 0;

    const int bufsize = 4096;
    u8 buf[bufsize];

    struct sockaddr_in sender;
    socklen_t senderlen = sizeof(struct sockaddr);

    int maxsock = mMulticastSocket > mUnicastSocket ? mMulticastSocket : mUnicastSocket;

    //Read UDP answers
    while (mReadLoop) {
        memset(buf, 0, bufsize);

        //(Re)set file descriptor
        FD_ZERO(&mReadFDS);
        FD_ZERO(&mWriteFDS);
        FD_ZERO(&mExceptionFDS);
        if (!mMulticastSocket) {
            printf("Multicast socket failed!\n");
            break;
        }

        FD_SET(mMulticastSocket, &mReadFDS);
        FD_SET(mMulticastSocket, &mWriteFDS);
        FD_SET(mMulticastSocket, &mExceptionFDS);

        if (!mUnicastSocket) {
            printf("Multicast socket failed!\n");
            break;
        }
        FD_SET(mUnicastSocket, &mReadFDS);
        FD_SET(mUnicastSocket, &mWriteFDS);
        FD_SET(mUnicastSocket, &mExceptionFDS);

        timeout.tv_sec = 5;
        timeout.tv_usec = 0;

        ret = select(maxsock+1, &mReadFDS, 0, &mExceptionFDS, &timeout);
        if (ret == SOCKET_ERROR) {
            printf("Socket error!\n");
            break;
        }
        else if (ret != 0) {
            //Multicast
            if (FD_ISSET(mMulticastSocket, &mExceptionFDS)) {
                printf("Error on Multicast socket, continue\n");
            }
            if (FD_ISSET(mMulticastSocket, &mReadFDS)) {
                //Data
                //printf("Data\n");
                ret = (int)recvfrom(mMulticastSocket, buf, bufsize, MSG_WAITALL, (struct sockaddr*)&sender, &senderlen);
                if(ret != SOCKET_ERROR){
                    //Be sure to only deliver full messages (!)
                    IncommingMessage((struct sockaddr*)&sender, buf, ret);
                }
            }
            //Unicast
            if (FD_ISSET(mUnicastSocket, &mExceptionFDS)) {
                printf("Error on Unicast socket, continue\n");
            }
            if (FD_ISSET(mUnicastSocket, &mReadFDS)) {
                //Data
                //printf("Data\n");
                ret = (int)recvfrom(mUnicastSocket, buf, bufsize, MSG_WAITALL, (struct sockaddr*)&sender, &senderlen);
                if(ret != SOCKET_ERROR){
                    //Be sure to only deliver full messages (!)
                    IncommingMessage((struct sockaddr*)&sender, buf, ret);
                }
            }

        }
    }
//EXIT:
    return ret;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1038, %rsp           # imm = 0x1038
movb $0x1, 0x68(%rdi)
movq $0x5, 0x10(%rsp)
movq $0x0, 0x18(%rsp)
movl $0x10, 0x8(%rsp)
cmpb $0x0, 0x68(%rdi)
je 0x4aa9
movq %rdi, %rbx
movl 0x8(%rdi), %eax
movl 0xc(%rdi), %ecx
cmpl %ecx, %eax
cmovgl %eax, %ecx
leaq 0xf8(%rdi), %r15
leaq 0x178(%rdi), %r13
leaq 0x78(%rdi), %r12
incl %ecx
movl %ecx, 0xc(%rsp)
xorl %ebp, %ebp
leaq 0x30(%rsp), %rdi
movl $0x1000, %edx           # imm = 0x1000
xorl %esi, %esi
callq 0x3140
xorps %xmm0, %xmm0
movups %xmm0, (%r15)
movups %xmm0, 0x10(%r15)
movups %xmm0, 0x20(%r15)
movups %xmm0, 0x30(%r15)
movups %xmm0, 0x40(%r15)
movups %xmm0, 0x50(%r15)
movups %xmm0, 0x60(%r15)
movups %xmm0, 0x70(%r15)
movups %xmm0, 0x70(%r13)
movups %xmm0, 0x60(%r13)
movups %xmm0, 0x50(%r13)
movups %xmm0, 0x40(%r13)
movups %xmm0, 0x30(%r13)
movups %xmm0, 0x20(%r13)
movups %xmm0, 0x10(%r13)
movups %xmm0, (%r13)
movups %xmm0, 0x70(%r12)
movups %xmm0, 0x60(%r12)
movups %xmm0, 0x50(%r12)
movups %xmm0, 0x40(%r12)
movups %xmm0, 0x30(%r12)
movups %xmm0, 0x20(%r12)
movups %xmm0, 0x10(%r12)
movups %xmm0, (%r12)
movl 0x8(%rbx), %ecx
testl %ecx, %ecx
je 0x4aad
leal 0x3f(%rcx), %eax
testl %ecx, %ecx
cmovnsl %ecx, %eax
movl %eax, %edx
andl $-0x40, %edx
subl %edx, %ecx
movl $0x1, %edx
shlq %cl, %rdx
sarl $0x6, %eax
cltq
orq %rdx, 0xf8(%rbx,%rax,8)
orq %rdx, 0x178(%rbx,%rax,8)
orq %rdx, 0x78(%rbx,%rax,8)
movl 0xc(%rbx), %ecx
testl %ecx, %ecx
je 0x4aad
leal 0x3f(%rcx), %eax
testl %ecx, %ecx
cmovnsl %ecx, %eax
movl %eax, %edx
andl $-0x40, %edx
subl %edx, %ecx
movl $0x1, %edx
shlq %cl, %rdx
sarl $0x6, %eax
cltq
orq %rdx, (%r15,%rax,8)
orq %rdx, (%r13,%rax,8)
orq %rdx, (%r12,%rax,8)
movq $0x5, 0x10(%rsp)
movq $0x0, 0x18(%rsp)
movl 0xc(%rsp), %edi
movq %r15, %rsi
xorl %edx, %edx
movq %r12, %rcx
leaq 0x10(%rsp), %r8
callq 0x3230
movl %eax, %ebp
testl %eax, %eax
je 0x4a98
cmpl $-0x1, %ebp
je 0x4acd
movl 0x8(%rbx), %eax
leal 0x3f(%rax), %ecx
testl %eax, %eax
cmovnsl %eax, %ecx
sarl $0x6, %ecx
movslq %ecx, %rcx
movq 0x78(%rbx,%rcx,8), %rcx
btq %rax, %rcx
jae 0x49b0
leaq 0x3a32(%rip), %rdi      # 0x83dd
callq 0x3380
movl 0x8(%rbx), %edi
leal 0x3f(%rdi), %eax
testl %edi, %edi
cmovnsl %edi, %eax
sarl $0x6, %eax
cltq
movq 0xf8(%rbx,%rax,8), %rax
btq %rdi, %rax
jae 0x4a10
movl $0x1000, %edx           # imm = 0x1000
leaq 0x30(%rsp), %rsi
movl $0x100, %ecx            # imm = 0x100
leaq 0x20(%rsp), %r14
movq %r14, %r8
leaq 0x8(%rsp), %r9
callq 0x31d0
movq %rax, %rbp
cmpl $-0x1, %ebp
je 0x4a0b
movq %rbx, %rdi
movq %r14, %rsi
leaq 0x30(%rsp), %rdx
movl %ebp, %ecx
callq 0x4ad6
jmp 0x4a10
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movl 0xc(%rbx), %eax
leal 0x3f(%rax), %ecx
testl %eax, %eax
cmovnsl %eax, %ecx
sarl $0x6, %ecx
movslq %ecx, %rcx
movq 0x78(%rbx,%rcx,8), %rcx
btq %rax, %rcx
jae 0x4a38
leaq 0x39ce(%rip), %rdi      # 0x8401
callq 0x3380
movl 0xc(%rbx), %edi
leal 0x3f(%rdi), %eax
testl %edi, %edi
cmovnsl %edi, %eax
sarl $0x6, %eax
cltq
movq 0xf8(%rbx,%rax,8), %rax
btq %rdi, %rax
leaq 0x20(%rsp), %r14
jae 0x4a98
movl $0x1000, %edx           # imm = 0x1000
leaq 0x30(%rsp), %rsi
movl $0x100, %ecx            # imm = 0x100
movq %r14, %r8
leaq 0x8(%rsp), %r9
callq 0x31d0
movq %rax, %rbp
cmpl $-0x1, %ebp
je 0x4a93
movq %rbx, %rdi
movq %r14, %rsi
leaq 0x30(%rsp), %rdx
movl %ebp, %ecx
callq 0x4ad6
jmp 0x4a98
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpb $0x0, 0x68(%rbx)
leaq 0x30(%rsp), %rdi
jne 0x4850
jmp 0x4ab9
xorl %ebp, %ebp
jmp 0x4ab9
leaq 0x3910(%rip), %rdi      # 0x83c4
callq 0x3380
movl %ebp, %eax
addq $0x1038, %rsp           # imm = 0x1038
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x394f(%rip), %rdi      # 0x8423
jmp 0x4ab4