// File: /workspace/github_cpp_makefile_star2_O3/dguglielmi[P]dlan-linux-software/common/packetinterface-linux.cpp
// Function: CPacketInterfaceLinux::ReceivePackets(IPacketReceiver&, unsigned long)
bool CPacketInterfaceLinux::ReceivePackets(IPacketReceiver& receiver, unsigned long ulTimeout)
{
  size_t buffersize = 3000;
  UnknownPacket packet(buffersize);

  bool infinite_timeout = (ulTimeout == (unsigned long)-1);
  struct timeval start, now, overall_timeout, select_timeout;

  if(!infinite_timeout)
  {
    overall_timeout.tv_sec = (long)(ulTimeout / 1000);
    overall_timeout.tv_usec = (long)(ulTimeout % 1000) * 1000;

    gettimeofday(&start, NULL);
    now = start;
  }
  bool stop_receiving = false;
  bool packet_indication = false;
  do
  {
    fd_set recset;
    int nfds;
    buildfdset(recset, nfds);

    if(!infinite_timeout)
      select_timeout = overall_timeout - (now - start);

    if(select(nfds, &recset, NULL, NULL, infinite_timeout ? 0 : &select_timeout) > 0)
    {
      std::map<CMACAddress,Interface>::const_iterator itIfc;
      if((itIfc = nextifcfromfdset(recset)) != m_ifcs.end())
      {
        struct sockaddr_ll source = { 0 };
        socklen_t sourcelen = sizeof(source);
        size_t recvbytes = (size_t) recvfrom(itIfc->second.fdsocket,
          (void*)packet.Data(), buffersize, 0, SOCKADDR_STAR(&source), &sourcelen);
        packet.SetSize(recvbytes);

        packet_indication = receiver.PacketIndication(itIfc->first, packet);
      }
      else if(FD_ISSET(m_interruptPipe[0], &recset))
      {
        char buffer[10];
        read(m_interruptPipe[0], buffer, sizeof(buffer));
        stop_receiving = true;
      }
    }

    if(!infinite_timeout)
      gettimeofday(&now, NULL);
  }
  while(!stop_receiving && !packet_indication && (infinite_timeout || ((now - start) < overall_timeout))); 

  return packet_indication;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x108, %rsp            # imm = 0x108
movq %rdx, %rbx
movq %rsi, 0x40(%rsp)
movq %rdi, %r15
leaq 0x9d2f(%rip), %rax      # 0x1dd80
movq %rax, 0x8(%rsp)
movl $0xbb8, %edi            # imm = 0xBB8
callq 0x4300
movq %rax, 0x10(%rsp)
movq $0x0, 0x18(%rsp)
cmpq $-0x1, %rbx
je 0x140ce
movq %rbx, %rax
shrq $0x3, %rax
movabsq $0x20c49ba5e353f7cf, %rcx # imm = 0x20C49BA5E353F7CF
mulq %rcx
movq %rdx, %r12
shrq $0x4, %r12
imulq $0x3e8, %r12, %rax      # imm = 0x3E8
movq %rbx, %rcx
subq %rax, %rcx
imulq $0x3e8, %rcx, %rax      # imm = 0x3E8
movq %rax, 0x30(%rsp)
leaq 0x78(%rsp), %r14
movq %r14, %rdi
xorl %esi, %esi
callq 0x43a0
movups (%r14), %xmm0
movaps %xmm0, 0x20(%rsp)
movq (%r14), %rax
movq %rax, 0x38(%rsp)
movq 0x8(%r14), %r13
jmp 0x140ce
leaq 0x70(%r15), %rax
movq %rax, 0x48(%rsp)
xorl %ebp, %ebp
leaq 0x88(%rsp), %r14
movq %r15, %rdi
movq %r14, %rsi
leaq 0x4(%rsp), %rdx
callq 0x142dc
cmpq $-0x1, %rbx
je 0x14155
movq 0x28(%rsp), %rax
movq %rax, %rcx
subq %r13, %rcx
movl $0xf4240, %esi          # imm = 0xF4240
addq %rsi, %rcx
xorl %edx, %edx
subq %r13, %rax
setl %dl
cmovlq %rcx, %rax
movq 0x30(%rsp), %rdi
movq %rdi, %rcx
subq %rax, %rcx
addq %rsi, %rcx
xorl %esi, %esi
subq %rax, %rdi
setl %sil
cmovlq %rcx, %rdi
movq %r12, %rax
subq 0x20(%rsp), %rax
addq 0x38(%rsp), %rax
addq %rdx, %rax
subq %rsi, %rax
movq %rax, 0x68(%rsp)
movq %rdi, 0x70(%rsp)
leaq 0x68(%rsp), %r8
jmp 0x14158
xorl %r8d, %r8d
movl 0x4(%rsp), %edi
movq %r14, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x4240
testl %eax, %eax
jle 0x141d5
movq %r15, %rdi
movq %r14, %rsi
callq 0x143a2
cmpq 0x48(%rsp), %rax
je 0x1424c
movq %rax, %r14
xorps %xmm0, %xmm0
movaps %xmm0, 0x50(%rsp)
movl $0x0, 0x60(%rsp)
movl $0x14, (%rsp)
movl 0x4c(%rax), %edi
movq 0x10(%rsp), %rsi
movl $0xbb8, %edx            # imm = 0xBB8
xorl %ecx, %ecx
leaq 0x50(%rsp), %r8
movq %rsp, %r9
callq 0x41f0
addq $0x20, %r14
movq %rax, 0x18(%rsp)
movq 0x40(%rsp), %rdi
movq (%rdi), %rax
movq %r14, %rsi
leaq 0x8(%rsp), %rdx
callq *(%rax)
movl %eax, %ebp
xorl %r14d, %r14d
cmpq $-0x1, %rbx
je 0x141ea
leaq 0x20(%rsp), %rdi
xorl %esi, %esi
callq 0x43a0
orb %bpl, %r14b
testb $0x1, %r14b
leaq 0x88(%rsp), %r14
jne 0x14289
cmpq $-0x1, %rbx
je 0x140e1
movq 0x20(%rsp), %rax
movq 0x28(%rsp), %rcx
xorl %edx, %edx
cmpq %r13, %rcx
setl %dl
subq %rdx, %rax
subq 0x38(%rsp), %rax
cmpq %r12, %rax
jl 0x140e1
subq %r13, %rcx
leaq 0xf4240(%rcx), %rdx
cmovgeq %rcx, %rdx
cmpq %r12, %rax
jne 0x14289
cmpq 0x30(%rsp), %rdx
jl 0x140e1
jmp 0x14289
movl 0x98(%r15), %edi
leal 0x3f(%rdi), %eax
testl %edi, %edi
cmovnsl %edi, %eax
sarl $0x6, %eax
cltq
movq 0x88(%rsp,%rax,8), %rax
btq %rdi, %rax
jae 0x141d5
movl $0xa, %edx
leaq 0x50(%rsp), %rsi
callq 0x4340
movb $0x1, %r14b
jmp 0x141d8
leaq 0x9af0(%rip), %rax      # 0x1dd80
movq %rax, 0x8(%rsp)
movq 0x10(%rsp), %rdi
callq 0x42e0
andb $0x1, %bpl
movl %ebp, %eax
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x142bb
jmp 0x142bb
movq %rax, %rbx
leaq 0x9abb(%rip), %rax      # 0x1dd80
movq %rax, 0x8(%rsp)
movq 0x10(%rsp), %rdi
callq 0x42e0
movq %rbx, %rdi
callq 0x43e0