// File: /workspace/github_cpp_makefile_star2_O3/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
movq %rdi, %rbx
movb $0x1, 0x68(%rdi)
movq $0x5, 0x10(%rsp)
movq $0x0, 0x18(%rsp)
movl $0x10, 0x4(%rsp)
movl 0x8(%rdi), %eax
movl 0xc(%rdi), %r15d
cmpl %r15d, %eax
cmovgl %eax, %r15d
leaq 0x78(%rdi), %r14
leaq 0x30(%rsp), %rdi
xorl %r13d, %r13d
movl $0x1000, %edx           # imm = 0x1000
xorl %esi, %esi
callq 0x3140
movl $0x180, %edx            # imm = 0x180
movq %r14, %rdi
xorl %esi, %esi
callq 0x3140
movl 0x8(%rbx), %ecx
leaq 0x3b6f(%rip), %rdi      # 0x83c4
testl %ecx, %ecx
je 0x4ac4
leaq 0xf8(%rbx), %rax
movq %rax, 0x8(%rsp)
incl %r15d
xorl %r13d, %r13d
leaq 0x20(%rsp), %rbp
movl %r15d, (%rsp)
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 0x4ab4
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)
movq $0x5, 0x10(%rsp)
movq $0x0, 0x18(%rsp)
movl %r15d, %edi
movq 0x8(%rsp), %rsi
xorl %edx, %edx
movq %r14, %rcx
leaq 0x10(%rsp), %r8
callq 0x3230
movl %eax, %r13d
testl %eax, %eax
je 0x49c5
cmpl $-0x1, %r13d
je 0x4abd
movl 0x8(%rbx), %edi
leal 0x3f(%rdi), %edx
testl %edi, %edi
cmovnsl %edi, %edx
movl %edx, %eax
sarl $0x6, %eax
cltq
movq 0x78(%rbx,%rax,8), %rsi
andl $-0x40, %edx
movl %edi, %ecx
subl %edx, %ecx
movl $0x1, %edx
shlq %cl, %rdx
btq %rcx, %rsi
jae 0x497c
leaq 0x3a87(%rip), %rdi      # 0x83dd
callq 0x3380
movl 0x8(%rbx), %edi
leal 0x3f(%rdi), %edx
testl %edi, %edi
cmovnsl %edi, %edx
movl %edx, %eax
sarl $0x6, %eax
cltq
andl $-0x40, %edx
movl %edi, %ecx
subl %edx, %ecx
movl $0x1, %edx
shlq %cl, %rdx
movq 0x8(%rsp), %rcx
testq %rdx, (%rcx,%rax,8)
je 0x49d9
movl $0x1000, %edx           # imm = 0x1000
leaq 0x30(%rsp), %r12
movq %r12, %rsi
movl $0x100, %ecx            # imm = 0x100
movq %rbp, %r15
movq %rbp, %r8
leaq 0x4(%rsp), %r9
callq 0x31d0
movq %rax, %r13
cmpl $-0x1, %r13d
je 0x49cf
movq %rbx, %rdi
movq %r15, %rsi
movq %r12, %rdx
movl %r13d, %ecx
callq 0x4ade
jmp 0x49d5
leaq 0x30(%rsp), %r12
jmp 0x4a85
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
movl (%rsp), %r15d
movl 0xc(%rbx), %edi
leal 0x3f(%rdi), %edx
testl %edi, %edi
cmovnsl %edi, %edx
movl %edx, %eax
sarl $0x6, %eax
cltq
movq 0x78(%rbx,%rax,8), %rsi
andl $-0x40, %edx
movl %edi, %ecx
subl %edx, %ecx
movl $0x1, %edx
shlq %cl, %rdx
btq %rcx, %rsi
jae 0x4a32
leaq 0x39f5(%rip), %rdi      # 0x8401
callq 0x3380
movl 0xc(%rbx), %edi
leal 0x3f(%rdi), %edx
testl %edi, %edi
cmovnsl %edi, %edx
movl %edx, %eax
sarl $0x6, %eax
cltq
andl $-0x40, %edx
movl %edi, %ecx
subl %edx, %ecx
movl $0x1, %edx
shlq %cl, %rdx
movq 0x8(%rsp), %rcx
testq %rdx, (%rcx,%rax,8)
leaq 0x30(%rsp), %r12
je 0x4a85
movl $0x1000, %edx           # imm = 0x1000
movq %r12, %rsi
movl $0x100, %ecx            # imm = 0x100
movq %rbp, %r15
movq %rbp, %r8
leaq 0x4(%rsp), %r9
callq 0x31d0
movq %rax, %r13
cmpl $-0x1, %r13d
je 0x4a7b
movq %rbx, %rdi
movq %r15, %rsi
movq %r12, %rdx
movl %r13d, %ecx
callq 0x4ade
jmp 0x4a81
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
movl (%rsp), %r15d
cmpb $0x0, 0x68(%rbx)
je 0x4ac9
movl $0x1000, %edx           # imm = 0x1000
movq %r12, %rdi
xorl %esi, %esi
callq 0x3140
movl $0x180, %edx            # imm = 0x180
movq %r14, %rdi
xorl %esi, %esi
callq 0x3140
movl 0x8(%rbx), %ecx
testl %ecx, %ecx
jne 0x4878
leaq 0x3909(%rip), %rdi      # 0x83c4
jmp 0x4ac4
leaq 0x395f(%rip), %rdi      # 0x8423
callq 0x3380
movl %r13d, %eax
addq $0x1038, %rsp           # imm = 0x1038
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq