// File: Distrotech[P]stund/udp.cxx
// Function: openPort(unsigned short, unsigned int, bool)
Socket
openPort( unsigned short port, unsigned int interfaceIp, bool verbose )
{
   Socket fd;
    
   fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
   if ( fd == INVALID_SOCKET )
   {
      int err = getErrno();
      cerr << "Could not create a UDP socket:" << err << endl;
      return INVALID_SOCKET;
   }
    
   struct sockaddr_in addr;
   memset((char*) &(addr),0, sizeof((addr)));
   addr.sin_family = AF_INET;
   addr.sin_addr.s_addr = htonl(INADDR_ANY);
   addr.sin_port = htons(port);
    
   if ( (interfaceIp != 0) && 
        ( interfaceIp != 0x100007f ) )
   {
      addr.sin_addr.s_addr = htonl(interfaceIp);
      if (verbose )
      {
         clog << "Binding to interface " 
              << hex << "0x" << htonl(interfaceIp) << dec << endl;
      }
   }
	
   if ( bind( fd,(struct sockaddr*)&addr, sizeof(addr)) != 0 )
   {
      int e = getErrno();
        
      switch (e)
      {
         case 0:
         {
            cerr << "Could not bind socket" << endl;
            return INVALID_SOCKET;
         }
         case EADDRINUSE:
         {
            cerr << "Port " << port << " for receiving UDP is in use" << endl;
            return INVALID_SOCKET;
         }
         break;
         case EADDRNOTAVAIL:
         {
            if ( verbose ) 
            {
               cerr << "Cannot assign requested address" << endl;
            }
            return INVALID_SOCKET;
         }
         break;
         default:
         {
            cerr << "Could not bind UDP receive port"
                 << "Error=" << e << " " << strerror(e) << endl;
            return INVALID_SOCKET;
         }
         break;
      }
   }
   if ( verbose )
   {
      clog << "Opened port " << port << " with fd " << fd << endl;
   }
   
   assert( fd != INVALID_SOCKET  );
	
   return fd;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movb %dl, %al
movw %di, %cx
movw %cx, -0x6(%rbp)
movl %esi, -0xc(%rbp)
andb $0x1, %al
movb %al, -0xd(%rbp)
movl $0x2, %esi
movl $0x11, %edx
movl %esi, %edi
callq 0x20a0
movl %eax, -0x14(%rbp)
cmpl $-0x1, -0x14(%rbp)
jne 0x8f04
callq 0x8e80
movl %eax, -0x18(%rbp)
movq 0x511e(%rip), %rdi      # 0xdff0
leaq 0x2527(%rip), %rsi      # 0xb400
callq 0x21f0
movq %rax, %rdi
movl -0x18(%rbp), %esi
callq 0x2300
movq %rax, %rdi
movq 0x50cd(%rip), %rsi      # 0xdfc0
callq 0x2200
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x9194
leaq -0x28(%rbp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x2130
movw $0x2, -0x28(%rbp)
xorl %edi, %edi
callq 0x2170
movl %eax, -0x24(%rbp)
movzwl -0x6(%rbp), %edi
callq 0x2040
movw %ax, -0x26(%rbp)
cmpl $0x0, -0xc(%rbp)
je 0x8fbd
cmpl $0x100007f, -0xc(%rbp)  # imm = 0x100007F
je 0x8fbd
movl -0xc(%rbp), %edi
callq 0x2170
movl %eax, -0x24(%rbp)
testb $0x1, -0xd(%rbp)
je 0x8fbb
movq 0x5054(%rip), %rdi      # 0xdfb0
leaq 0x24bc(%rip), %rsi      # 0xb41f
callq 0x21f0
movq %rax, %rdi
leaq -0x5d92(%rip), %rsi     # 0x31e0
callq 0x20c0
movq %rax, %rdi
leaq 0x1456(%rip), %rsi      # 0xa3d7
callq 0x21f0
movq %rax, -0x38(%rbp)
movl -0xc(%rbp), %edi
callq 0x2170
movq -0x38(%rbp), %rdi
movl %eax, %esi
callq 0x2260
movq %rax, %rdi
leaq -0x5d67(%rip), %rsi     # 0x3240
callq 0x20c0
movq %rax, %rdi
movq 0x500a(%rip), %rsi      # 0xdfc0
callq 0x2200
jmp 0x8fbd
movl -0x14(%rbp), %edi
leaq -0x28(%rbp), %rsi
movl $0x10, %edx
callq 0x2060
cmpl $0x0, %eax
je 0x9119
callq 0x8e80
movl %eax, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
movl %eax, -0x3c(%rbp)
testl %eax, %eax
je 0x9002
jmp 0x8feb
movl -0x3c(%rbp), %eax
subl $0x62, %eax
je 0x9030
jmp 0x8ff5
movl -0x3c(%rbp), %eax
subl $0x63, %eax
je 0x9079
jmp 0x90ad
movq 0x4fe7(%rip), %rdi      # 0xdff0
leaq 0x2425(%rip), %rsi      # 0xb435
callq 0x21f0
movq %rax, %rdi
movq 0x4fa1(%rip), %rsi      # 0xdfc0
callq 0x2200
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x9194
movq 0x4fb9(%rip), %rdi      # 0xdff0
leaq 0x240d(%rip), %rsi      # 0xb44b
callq 0x21f0
movq %rax, %rdi
movzwl -0x6(%rbp), %esi
callq 0x22f0
movq %rax, %rdi
leaq 0x23f8(%rip), %rsi      # 0xb451
callq 0x21f0
movq %rax, %rdi
movq 0x4f58(%rip), %rsi      # 0xdfc0
callq 0x2200
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x9194
testb $0x1, -0xd(%rbp)
je 0x90a1
movq 0x4f6a(%rip), %rdi      # 0xdff0
leaq 0x23e1(%rip), %rsi      # 0xb46e
callq 0x21f0
movq %rax, %rdi
movq 0x4f24(%rip), %rsi      # 0xdfc0
callq 0x2200
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x9194
movq 0x4f3c(%rip), %rdi      # 0xdff0
leaq 0x23d3(%rip), %rsi      # 0xb48e
callq 0x21f0
movq %rax, %rdi
leaq 0x24f5(%rip), %rsi      # 0xb5bf
callq 0x21f0
movq %rax, %rdi
movl -0x2c(%rbp), %esi
callq 0x2300
movq %rax, %rdi
leaq 0x13a9(%rip), %rsi      # 0xa48d
callq 0x21f0
movq %rax, -0x48(%rbp)
movl -0x2c(%rbp), %edi
callq 0x20b0
movq -0x48(%rbp), %rdi
movq %rax, %rsi
callq 0x21f0
movq %rax, %rdi
movq 0x4eb5(%rip), %rsi      # 0xdfc0
callq 0x2200
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x9194
testb $0x1, -0xd(%rbp)
je 0x9167
movq 0x4e8a(%rip), %rdi      # 0xdfb0
leaq 0x2381(%rip), %rsi      # 0xb4ae
callq 0x21f0
movq %rax, %rdi
movzwl -0x6(%rbp), %esi
callq 0x22f0
movq %rax, %rdi
leaq 0x2373(%rip), %rsi      # 0xb4bb
callq 0x21f0
movq %rax, %rdi
movl -0x14(%rbp), %esi
callq 0x2300
movq %rax, %rdi
movq 0x4e5e(%rip), %rsi      # 0xdfc0
callq 0x2200
cmpl $-0x1, -0x14(%rbp)
je 0x916f
jmp 0x918e
leaq 0x234f(%rip), %rdi      # 0xb4c5
leaq 0x235d(%rip), %rsi      # 0xb4da
movl $0x6b, %edx
leaq 0x2359(%rip), %rcx      # 0xb4e2
callq 0x2140
movl -0x14(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nopl (%rax)