// File: wrighteagle2d[P]wrighteaglebase/Debug/../src/UDPSocket.cpp
// Function: UDPSocket::Initial(char const*, int)
void UDPSocket::Initial(const char *host, int port) {
  struct hostent *host_ent;
  struct in_addr *addr_ptr;

#ifdef WIN32
  WORD wVersionRequested;
  WSADATA wsaData;
  wVersionRequested = MAKEWORD(2, 2);
  if (WSAStartup(wVersionRequested, &wsaData) != 0) {
    PRINT_ERROR("WSAStartup failed");
  }
  if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2) {
    WSACleanup();
    PRINT_ERROR("WSASTartup version mismatch");
  }
#endif

  if ((host_ent = (struct hostent *)gethostbyname(host)) == 0) {
    if (inet_addr(host) == INADDR_NONE)
      PRINT_ERROR("Invalid host name");
  } else {
    addr_ptr = (struct in_addr *)*host_ent->h_addr_list;
    host = inet_ntoa(*addr_ptr);
  }

  if ((mSockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
    PRINT_ERROR("Can't create socket");
  }

  memset(&mAddress, 0, sizeof(mAddress));
  mAddress.sin_family = AF_INET;
  mAddress.sin_addr.s_addr = htonl(INADDR_ANY);
  mAddress.sin_port = htons(0);

  if (bind(mSockfd, (sockaddr *)&mAddress, sizeof(mAddress)) < 0) {
#ifdef WIN32
    closesocket(mSockfd);
#else
    close(mSockfd);
#endif
    PRINT_ERROR("Can't bind client to any port");
  }

  memset(&mAddress, 0, sizeof(mAddress));
  mAddress.sin_family = AF_INET;
  mAddress.sin_addr.s_addr = inet_addr(host);
  mAddress.sin_port = htons(port);
  mIsInitialOK = true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rdi
callq 0xa6b0
movq %rax, -0x20(%rbp)
cmpq $0x0, %rax
jne 0x9e29e
movq -0x10(%rbp), %rdi
callq 0xa5d0
cmpl $-0x1, %eax
jne 0x9e29c
jmp 0x9e23e
movq 0x5fdab(%rip), %rdi     # 0xfdff0
leaq 0x270a6(%rip), %rsi     # 0xc52f2
callq 0xa5a0
movq %rax, %rdi
leaq 0x26ff6(%rip), %rsi     # 0xc5251
callq 0xa5a0
movq %rax, %rdi
movl $0x42, %esi
callq 0xaa40
movq %rax, %rdi
leaq 0x252e8(%rip), %rsi     # 0xc355f
callq 0xa5a0
movq %rax, %rdi
leaq 0x27081(%rip), %rsi     # 0xc5307
callq 0xa5a0
movq %rax, %rdi
movq 0x5fd1b(%rip), %rsi     # 0xfdfb0
callq 0xa630
jmp 0x9e29c
jmp 0x9e2c2
movq -0x20(%rbp), %rax
movq 0x18(%rax), %rax
movq (%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x2c(%rbp)
movl -0x2c(%rbp), %edi
callq 0xa280
movq %rax, -0x10(%rbp)
movl $0x2, %esi
xorl %edx, %edx
movl %esi, %edi
callq 0xa1a0
movq -0x38(%rbp), %rcx
movl %eax, 0x14(%rcx)
cmpl $0x0, %eax
jge 0x9e33c
jmp 0x9e2de
movq 0x5fd0b(%rip), %rdi     # 0xfdff0
leaq 0x27006(%rip), %rsi     # 0xc52f2
callq 0xa5a0
movq %rax, %rdi
leaq 0x26f56(%rip), %rsi     # 0xc5251
callq 0xa5a0
movq %rax, %rdi
movl $0x49, %esi
callq 0xaa40
movq %rax, %rdi
leaq 0x25248(%rip), %rsi     # 0xc355f
callq 0xa5a0
movq %rax, %rdi
leaq 0x26ff3(%rip), %rsi     # 0xc5319
callq 0xa5a0
movq %rax, %rdi
movq 0x5fc7b(%rip), %rsi     # 0xfdfb0
callq 0xa630
jmp 0x9e33c
movq -0x38(%rbp), %rdi
addq $0x4, %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0xa310
movq -0x38(%rbp), %rax
movw $0x2, 0x4(%rax)
xorl %edi, %edi
callq 0xa430
movl %eax, %ecx
movq -0x38(%rbp), %rax
movl %ecx, 0x8(%rax)
xorl %edi, %edi
callq 0xa0a0
movq -0x38(%rbp), %rsi
movw %ax, 0x6(%rsi)
movl 0x14(%rsi), %edi
addq $0x4, %rsi
movl $0x10, %edx
callq 0xa0f0
cmpl $0x0, %eax
jge 0x9e3f9
movq -0x38(%rbp), %rax
movl 0x14(%rax), %edi
callq 0xabc0
movq 0x5fc4e(%rip), %rdi     # 0xfdff0
leaq 0x26f49(%rip), %rsi     # 0xc52f2
callq 0xa5a0
movq %rax, %rdi
leaq 0x26e99(%rip), %rsi     # 0xc5251
callq 0xa5a0
movq %rax, %rdi
movl $0x57, %esi
callq 0xaa40
movq %rax, %rdi
leaq 0x2518b(%rip), %rsi     # 0xc355f
callq 0xa5a0
movq %rax, %rdi
leaq 0x26f4a(%rip), %rsi     # 0xc532d
callq 0xa5a0
movq %rax, %rdi
movq 0x5fbbe(%rip), %rsi     # 0xfdfb0
callq 0xa630
jmp 0x9e3f9
movq -0x38(%rbp), %rdi
addq $0x4, %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0xa310
movq -0x38(%rbp), %rax
movw $0x2, 0x4(%rax)
movq -0x10(%rbp), %rdi
callq 0xa5d0
movl %eax, %ecx
movq -0x38(%rbp), %rax
movl %ecx, 0x8(%rax)
movl -0x14(%rbp), %eax
movzwl %ax, %edi
callq 0xa0a0
movw %ax, %cx
movq -0x38(%rbp), %rax
movw %cx, 0x6(%rax)
movb $0x1, (%rax)
addq $0x40, %rsp
popq %rbp
retq
nopl (%rax,%rax)