#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */
struct timeval {
  int tv_sec;
  int tv_usec;
};
struct in_addr {
  int dummy;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  struct in_addr sin_addr;
  int /*<<< orphan*/ sin_zero;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  scalar_t__ h_addr;
};
typedef int /*<<< orphan*/ recieve_data;
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ FD_CLR(int, int /*<<< orphan*/ *);
scalar_t__ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
int O_NONBLOCK;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ bzero(int /*<<< orphan*/ *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ error(char *);
int fcntl(int, int /*<<< orphan*/, ...);
scalar_t__ gethostbyname(char *);
int /*<<< orphan*/ htons(int);
char *inet_ntoa(struct in_addr);
int ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int recvfrom(int, char *, int, int /*<<< orphan*/, struct sockaddr *,
             unsigned int *);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int /*<<< orphan*/ sendto(int, char *, int, int /*<<< orphan*/,
                          struct sockaddr *, int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ strcmp(char *, char *);
int strlen(char *);
int main() {
  fd_set original_socket;
  fd_set original_stdin;
  fd_set readfds;
  fd_set writefds;
  struct timeval tv;
  int numfd, flags;
  int socket_fd, bytes_recieved;
  unsigned int address_length;
  struct sockaddr_in server_address;
  struct hostent *host;
  char send_data[1024], recieve_data[1024];
  host = (struct hostent *)gethostbyname((char *)"localhost");
  if ((socket_fd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
    error("socket()");
  }
  flags = fcntl(socket_fd, F_GETFL);
  flags |= O_NONBLOCK;
  fcntl(socket_fd, F_SETFL, flags);
  FD_ZERO(&original_socket);
  FD_ZERO(&original_stdin);
  FD_ZERO(&readfds);
  FD_ZERO(&writefds);
  FD_SET(socket_fd, &original_socket);
  FD_SET(socket_fd, &readfds);
  FD_SET(socket_fd, &original_stdin);
  FD_SET(socket_fd, &writefds);
  numfd = socket_fd + 1;
  server_address.sin_family = AF_INET;
  server_address.sin_port = htons(5000);
  server_address.sin_addr = *((struct in_addr *)host->h_addr);
  bzero(&(server_address.sin_zero), 8);
  address_length = sizeof(struct sockaddr);
  printf("Type (q or Q) at anytime to quit\n");
  int index = 0;
  while (1) {
    tv.tv_sec = 2;
    tv.tv_usec = 500000;
    readfds = original_socket;
    writefds = original_stdin;
    int recieve = select(numfd, &readfds, &writefds, NULL, &tv);
    if (recieve == -1) {
      perror("select");
    } else if (recieve == 0) {
      printf("Timeout occurred!    "
             "No data after 10.5 seconds.\n");
    } else {
      if (FD_ISSET(socket_fd, &readfds)) {
        FD_CLR(socket_fd, &readfds);
        bytes_recieved =
            recvfrom(socket_fd, recieve_data, sizeof(recieve_data), 0,
                     (struct sockaddr *)&server_address, &address_length);
        recieve_data[bytes_recieved] = '\0';
        if ((strcmp(recieve_data, "q") == 0) ||
            (strcmp(recieve_data, "Q") == 0)) {
          printf("\nServer has exited the chat.\n");
          break;
        }
        printf("\n(%s , %d) said: %s\n", inet_ntoa(server_address.sin_addr),
               ntohs(server_address.sin_port), recieve_data);
      } else if (FD_ISSET(socket_fd, &writefds)) {
        index++;
        FD_CLR(socket_fd, &writefds);
        snprintf(send_data, 1024, "This is %d test.", index);
        if ((strlen(send_data) > 0) &&
            (send_data[strlen(send_data) - 1] == '\n')) {
          send_data[strlen(send_data) - 1] = '\0';
        }
        if ((strcmp(send_data, "q") == 0) || strcmp(send_data, "Q") == 0) {
          sendto(socket_fd, send_data, strlen(send_data), 0,
                 (struct sockaddr *)&server_address, sizeof(struct sockaddr));
          break;
        } else {
          sendto(socket_fd, send_data, strlen(send_data), 0,
                 (struct sockaddr *)&server_address, sizeof(struct sockaddr));
        }
        sleep(1);
      } else {
        printf("\nOOPS! What happened? CLIENT");
      }
    }
  }
  close(socket_fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x880,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xd87(%rip),%rdi
call   10d0 <gethostbyname@plt>
mov    %rax,-0x50(%rbp)
mov    0x2d5f(%rip),%rax
mov    (%rax),%edi
mov    0x2d46(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1180 <socket@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0xffffffff,%eax
jne    12b3 <main+0x53>
lea    0xd5c(%rip),%rdi
call   1120 <error@plt>
mov    -0x2c(%rbp),%edi
mov    0x2d03(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0x28(%rbp)
mov    0x2d10(%rip),%rax
mov    (%rax),%eax
or     -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x2c(%rbp),%edi
mov    0x2ce6(%rip),%rax
mov    (%rax),%esi
mov    -0x28(%rbp),%edx
mov    $0x0,%al
call   1030 <fcntl@plt>
lea    -0x8(%rbp),%rdi
call   10e0 <FD_ZERO@plt>
lea    -0xc(%rbp),%rdi
call   10e0 <FD_ZERO@plt>
lea    -0x10(%rbp),%rdi
call   10e0 <FD_ZERO@plt>
lea    -0x14(%rbp),%rdi
call   10e0 <FD_ZERO@plt>
mov    -0x2c(%rbp),%edi
lea    -0x8(%rbp),%rsi
call   1160 <FD_SET@plt>
mov    -0x2c(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   1160 <FD_SET@plt>
mov    -0x2c(%rbp),%edi
lea    -0xc(%rbp),%rsi
call   1160 <FD_SET@plt>
mov    -0x2c(%rbp),%edi
lea    -0x14(%rbp),%rsi
call   1160 <FD_SET@plt>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    0x2c96(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    $0x1388,%edi
call   1070 <htons@plt>
mov    %eax,-0x48(%rbp)
mov    -0x50(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
lea    -0x48(%rbp),%rdi
add    $0x8,%rdi
mov    $0x8,%esi
call   1140 <bzero@plt>
movl   $0x4,-0x34(%rbp)
lea    0xc83(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
movl   $0x0,-0x854(%rbp)
movl   $0x2,-0x20(%rbp)
movl   $0x7a120,-0x1c(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x24(%rbp),%edi
lea    -0x10(%rbp),%rsi
lea    -0x14(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x20(%rbp),%r8
call   10f0 <select@plt>
mov    %eax,-0x858(%rbp)
cmpl   $0xffffffff,-0x858(%rbp)
jne    13f7 <main+0x197>
lea    0xc48(%rip),%rdi
call   1130 <perror@plt>
jmp    167d <main+0x41d>
cmpl   $0x0,-0x858(%rbp)
jne    1417 <main+0x1b7>
lea    0xc31(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
jmp    1678 <main+0x418>
mov    -0x2c(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     14ed <main+0x28d>
mov    -0x2c(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   1150 <FD_CLR@plt>
mov    -0x2c(%rbp),%edi
lea    -0x850(%rbp),%rsi
lea    -0x48(%rbp),%r8
mov    $0x400,%edx
xor    %ecx,%ecx
lea    -0x34(%rbp),%r9
call   1100 <recvfrom@plt>
mov    %eax,-0x30(%rbp)
movslq -0x30(%rbp),%rax
movb   $0x0,-0x850(%rbp,%rax,1)
lea    -0x850(%rbp),%rdi
lea    0xbfa(%rip),%rsi
call   10c0 <strcmp@plt>
cmp    $0x0,%rax
je     14a0 <main+0x240>
lea    -0x850(%rbp),%rdi
lea    0xbdf(%rip),%rsi
call   10c0 <strcmp@plt>
cmp    $0x0,%rax
jne    14b3 <main+0x253>
lea    0xbcb(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
jmp    1682 <main+0x422>
mov    -0x44(%rbp),%edi
call   1050 <inet_ntoa@plt>
mov    %rax,-0x860(%rbp)
mov    -0x48(%rbp),%edi
call   1110 <ntohs@plt>
mov    -0x860(%rbp),%rsi
mov    %eax,%edx
lea    -0x850(%rbp),%rcx
lea    0xbaf(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
jmp    1673 <main+0x413>
mov    -0x2c(%rbp),%edi
lea    -0x14(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1660 <main+0x400>
mov    -0x854(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x854(%rbp)
mov    -0x2c(%rbp),%edi
lea    -0x14(%rbp),%rsi
call   1150 <FD_CLR@plt>
lea    -0x450(%rbp),%rdi
mov    -0x854(%rbp),%ecx
mov    $0x400,%esi
lea    0xb6e(%rip),%rdx
call   1090 <snprintf@plt>
lea    -0x450(%rbp),%rdi
call   1060 <strlen@plt>
cmp    $0x0,%eax
jle    158c <main+0x32c>
lea    -0x450(%rbp),%rdi
call   1060 <strlen@plt>
sub    $0x1,%eax
cltq
movsbl -0x450(%rbp,%rax,1),%eax
ff
cmp    $0xa,%eax
jne    158c <main+0x32c>
lea    -0x450(%rbp),%rdi
call   1060 <strlen@plt>
sub    $0x1,%eax
cltq
movb   $0x0,-0x450(%rbp,%rax,1)
lea    -0x450(%rbp),%rdi
lea    0xad4(%rip),%rsi
call   10c0 <strcmp@plt>
cmp    $0x0,%rax
je     15c6 <main+0x366>
lea    -0x450(%rbp),%rdi
lea    0xab9(%rip),%rsi
call   10c0 <strcmp@plt>
cmp    $0x0,%rax
jne    160e <main+0x3ae>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x86c(%rbp)
lea    -0x450(%rbp),%rax
mov    %rax,-0x868(%rbp)
lea    -0x450(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x86c(%rbp),%edi
mov    -0x868(%rbp),%rsi
mov    %eax,%edx
lea    -0x48(%rbp),%r8
xor    %ecx,%ecx
mov    $0x4,%r9d
call   10a0 <sendto@plt>
jmp    1682 <main+0x422>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x87c(%rbp)
lea    -0x450(%rbp),%rax
mov    %rax,-0x878(%rbp)
lea    -0x450(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x87c(%rbp),%edi
mov    -0x878(%rbp),%rsi
mov    %eax,%edx
lea    -0x48(%rbp),%r8
xor    %ecx,%ecx
mov    $0x4,%r9d
call   10a0 <sendto@plt>
mov    $0x1,%edi
call   1170 <sleep@plt>
jmp    166e <main+0x40e>
lea    0xa4f(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
jmp    1673 <main+0x413>
jmp    1678 <main+0x418>
jmp    167d <main+0x41d>
jmp    13a1 <main+0x141>
mov    -0x2c(%rbp),%edi
call   10b0 <close@plt>
xor    %eax,%eax
add    $0x880,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x848,%rsp
lea    0xd78(%rip),%rdi
call   10e0 <gethostbyname@plt>
mov    %rax,0x8(%rsp)
mov    0x2d4f(%rip),%rax
mov    (%rax),%edi
mov    0x2d36(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1190 <socket@plt>
mov    %eax,%r12d
mov    %eax,0x4(%rsp)
cmp    $0xffffffff,%eax
jne    12c3 <main+0x53>
lea    0xd4c(%rip),%rdi
call   1130 <error@plt>
mov    0x2cf6(%rip),%rax
mov    (%rax),%esi
mov    %r12d,%edi
xor    %eax,%eax
call   1040 <fcntl@plt>
mov    0x2d03(%rip),%rcx
or     (%rcx),%eax
mov    0x2ce2(%rip),%rcx
mov    (%rcx),%esi
mov    %r12d,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <fcntl@plt>
lea    0x34(%rsp),%rbx
mov    %rbx,%rdi
call   10f0 <FD_ZERO@plt>
lea    0x30(%rsp),%r15
mov    %r15,%rdi
call   10f0 <FD_ZERO@plt>
lea    0x2c(%rsp),%r14
mov    %r14,%rdi
call   10f0 <FD_ZERO@plt>
lea    0x28(%rsp),%r13
mov    %r13,%rdi
call   10f0 <FD_ZERO@plt>
mov    %r12d,%edi
mov    %rbx,%rsi
call   1170 <FD_SET@plt>
mov    %r12d,%edi
mov    %r14,%rsi
call   1170 <FD_SET@plt>
mov    %r12d,%edi
mov    %r15,%rsi
call   1170 <FD_SET@plt>
mov    %r12d,0x20(%rsp)
mov    %r12d,%edi
mov    %r13,%rsi
call   1170 <FD_SET@plt>
addl   $0x1,0x4(%rsp)
mov    0x2c83(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
mov    $0x1388,%edi
call   1080 <htons@plt>
mov    %eax,0x10(%rsp)
mov    0x8(%rsp),%rax
mov    (%rax),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
lea    0x18(%rsp),%rdi
mov    $0x8,%esi
call   1150 <bzero@plt>
movl   $0x4,0x24(%rsp)
lea    0xcbc(%rip),%rdi
call   1030 <puts@plt>
movabs $0x7a12000000002,%rbp
lea    0x38(%rsp),%r12
lea    0x440(%rsp),%r15
movl   $0x0,0x8(%rsp)
jmp    13dc <main+0x16c>
nopl   0x0(%rax,%rax,1)
lea    0xcc8(%rip),%rdi
call   1030 <puts@plt>
mov    %rbp,0x38(%rsp)
mov    0x34(%rsp),%eax
mov    %eax,0x2c(%rsp)
mov    0x30(%rsp),%eax
mov    %eax,0x28(%rsp)
mov    0x4(%rsp),%edi
mov    %r14,%rsi
mov    %r13,%rdx
xor    %ecx,%ecx
mov    %r12,%r8
call   1100 <select@plt>
test   %eax,%eax
je     13d0 <main+0x160>
cmp    $0xffffffff,%eax
je     14ea <main+0x27a>
mov    0x20(%rsp),%ebx
mov    %ebx,%edi
mov    %r14,%rsi
call   1050 <FD_ISSET@plt>
mov    %ebx,%edi
test   %rax,%rax
je     14ca <main+0x25a>
mov    %r14,%rsi
call   1160 <FD_CLR@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
lea    0x10(%rsp),%r8
lea    0x24(%rsp),%r9
call   1110 <recvfrom@plt>
cltq
movb   $0x0,0x440(%rsp,%rax,1)
mov    %r15,%rdi
lea    0xbb8(%rip),%rsi
call   10d0 <strcmp@plt>
test   %rax,%rax
je     15c3 <main+0x353>
mov    %r15,%rdi
lea    0xba2(%rip),%rsi
call   10d0 <strcmp@plt>
test   %rax,%rax
je     15c3 <main+0x353>
mov    0x14(%rsp),%edi
call   1060 <inet_ntoa@plt>
mov    %r13,%rbx
mov    %r14,%r13
mov    %r12,%r14
mov    %rax,%r12
mov    0x10(%rsp),%edi
call   1120 <ntohs@plt>
lea    0xb71(%rip),%rdi
mov    %r12,%rsi
mov    %r14,%r12
mov    %r13,%r14
mov    %rbx,%r13
mov    %eax,%edx
mov    %r15,%rcx
xor    %eax,%eax
call   1090 <printf@plt>
jmp    13dc <main+0x16c>
mov    %r13,%rsi
call   1050 <FD_ISSET@plt>
test   %rax,%rax
jne    14fb <main+0x28b>
lea    0xb66(%rip),%rdi
xor    %eax,%eax
call   1090 <printf@plt>
jmp    13dc <main+0x16c>
lea    0xb22(%rip),%rdi
call   1140 <perror@plt>
jmp    13dc <main+0x16c>
addl   $0x1,0x8(%rsp)
mov    %ebx,%edi
mov    %r13,%rsi
call   1160 <FD_CLR@plt>
lea    0x40(%rsp),%rdi
mov    $0x400,%esi
lea    0xb18(%rip),%rdx
mov    0x8(%rsp),%ecx
call   10a0 <snprintf@plt>
lea    0x40(%rsp),%rdi
call   1070 <strlen@plt>
lea    0x40(%rsp),%rcx
test   %eax,%eax
jle    1565 <main+0x2f5>
lea    0x40(%rsp),%rdi
call   1070 <strlen@plt>
lea    0x40(%rsp),%rcx
cltq
cmpb   $0xa,-0x1(%rax,%rcx,1)
jne    1565 <main+0x2f5>
lea    0x40(%rsp),%rdi
call   1070 <strlen@plt>
lea    0x40(%rsp),%rcx
cltq
movb   $0x0,-0x1(%rax,%rcx,1)
mov    %rcx,%rdi
lea    0xaab(%rip),%rsi
call   10d0 <strcmp@plt>
test   %rax,%rax
je     15ea <main+0x37a>
lea    0x40(%rsp),%rdi
lea    0xa97(%rip),%rsi
call   10d0 <strcmp@plt>
test   %rax,%rax
je     15ea <main+0x37a>
lea    0x40(%rsp),%rdi
call   1070 <strlen@plt>
mov    %ebx,%edi
lea    0x40(%rsp),%rsi
mov    %eax,%edx
xor    %ecx,%ecx
lea    0x10(%rsp),%r8
mov    $0x4,%r9d
call   10b0 <sendto@plt>
mov    $0x1,%edi
call   1180 <sleep@plt>
jmp    13dc <main+0x16c>
lea    0xab8(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
call   10c0 <close@plt>
xor    %eax,%eax
add    $0x848,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0x40(%rsp),%rbp
mov    %rbp,%rdi
call   1070 <strlen@plt>
lea    0x10(%rsp),%r8
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
mov    $0x4,%r9d
call   10b0 <sendto@plt>
jmp    15cf <main+0x35f>

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
%struct.sockaddr_in = type { i32, %struct.in_addr, i32, i32 }
%struct.in_addr = type { i32 }
%struct.hostent = type { i64 }
%struct.sockaddr = type { i32 }
@.str = [10 x i8] c"localhost\00"
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str.1 = [9 x i8] c"socket()\00"
@F_GETFL = global i32 0
@O_NONBLOCK = global i32 0
@F_SETFL = global i32 0
@.str.2 = [34 x i8] c"Type (q or Q) at anytime to quit\0A\00"
@.str.3 = [7 x i8] c"select\00"
@.str.4 = [50 x i8] c"Timeout occurred! No data after 10.5 seconds.\0A\00"
@.str.5 = [2 x i8] c"q\00"
@.str.6 = [2 x i8] c"Q\00"
@.str.7 = [30 x i8] c"\0AServer has exited the chat.\0A\00"
@.str.8 = [21 x i8] c"\0A(%s , %d) said: %s\0A\00"
@.str.9 = [17 x i8] c"This is %d test.\00"
@.str.10 = [29 x i8] c"\0AOOPS! What happened? CLIENT\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.timeval
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca %struct.sockaddr_in
%13 = alloca %struct.hostent*
%14 = alloca [1024 x i8]
%15 = alloca [1024 x i8]
%16 = alloca i32
%17 = alloca i32
store i32 0, i32* %1
%18 = call i64 @gethostbyname(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%19 = inttoptr i64 %18 to %struct.hostent*
store %struct.hostent* %19, %struct.hostent** %13
%20 = load i32, i32* @AF_INET
%21 = load i32, i32* @SOCK_DGRAM
%22 = call i32 @socket(i32 %20, i32 %21, i32 0)
store i32 %22, i32* %9
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %26
24: ; preds = %0
%25 = call i32 @error(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
br label %26
26: ; preds = %24, %0
%27 = load i32, i32* %9
%28 = load i32, i32* @F_GETFL
%29 = call i32 (i32, i32, ...) @fcntl(i32 %27, i32 %28)
store i32 %29, i32* %8
%30 = load i32, i32* @O_NONBLOCK
%31 = load i32, i32* %8
%32 = or i32 %31, %30
store i32 %32, i32* %8
%33 = load i32, i32* %9
%34 = load i32, i32* @F_SETFL
%35 = load i32, i32* %8
%36 = call i32 (i32, i32, ...) @fcntl(i32 %33, i32 %34, i32 %35)
%37 = call i32 @FD_ZERO(i32* %2)
%38 = call i32 @FD_ZERO(i32* %3)
%39 = call i32 @FD_ZERO(i32* %4)
%40 = call i32 @FD_ZERO(i32* %5)
%41 = load i32, i32* %9
%42 = call i32 @FD_SET(i32 %41, i32* %2)
%43 = load i32, i32* %9
%44 = call i32 @FD_SET(i32 %43, i32* %4)
%45 = load i32, i32* %9
%46 = call i32 @FD_SET(i32 %45, i32* %3)
%47 = load i32, i32* %9
%48 = call i32 @FD_SET(i32 %47, i32* %5)
%49 = load i32, i32* %9
%50 = add nsw i32 %49, 1
store i32 %50, i32* %7
%51 = load i32, i32* @AF_INET
%52 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %12, i32 0, i32 3
store i32 %51, i32* %52
%53 = call i32 @htons(i32 5000)
%54 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %12, i32 0, i32 0
store i32 %53, i32* %54
%55 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %12, i32 0, i32 1
%56 = load %struct.hostent*, %struct.hostent** %13
%57 = getelementptr inbounds %struct.hostent, %struct.hostent* %56, i32 0, i32 0
%58 = load i64, i64* %57
%59 = inttoptr i64 %58 to %struct.in_addr*
%60 = bitcast %struct.in_addr* %55 to i8*
%61 = bitcast %struct.in_addr* %59 to i8*
%62 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %12, i32 0, i32 2
%63 = call i32 @bzero(i32* %62, i32 8)
store i32 4, i32* %11
%64 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %16
br label %65
65: ; preds = %26, %173
%66 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i32 0, i32 0
store i32 2, i32* %66
%67 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i32 0, i32 1
store i32 500000, i32* %67
%68 = load i32, i32* %2
store i32 %68, i32* %4
%69 = load i32, i32* %3
store i32 %69, i32* %5
%70 = load i32, i32* %7
%71 = call i32 @select(i32 %70, i32* %4, i32* %5, i32* null, %struct.timeval* %6)
store i32 %71, i32* %17
%72 = load i32, i32* %17
%73 = icmp eq i32 %72, -1
br i1 %73, label %74, label %76
74: ; preds = %65
%75 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
br label %173
76: ; preds = %65
%77 = load i32, i32* %17
%78 = icmp eq i32 %77, 0
br i1 %78, label %79, label %81
79: ; preds = %76
%80 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.4, i64 0, i64 0))
br label %172
81: ; preds = %76
%82 = load i32, i32* %9
%83 = call i64 @FD_ISSET(i32 %82, i32* %4)
%84 = icmp ne i64 %83, 0
br i1 %84, label %85, label %114
85: ; preds = %81
%86 = load i32, i32* %9
%87 = call i32 @FD_CLR(i32 %86, i32* %4)
%88 = load i32, i32* %9
%89 = getelementptr inbounds [1024 x i8], [1024 x i8]* %15, i64 0, i64 0
%90 = bitcast %struct.sockaddr_in* %12 to %struct.sockaddr*
%91 = call i32 @recvfrom(i32 %88, i8* %89, i32 1024, i32 0, %struct.sockaddr* %90, i32* %11)
store i32 %91, i32* %10
%92 = load i32, i32* %10
%93 = sext i32 %92 to i64
%94 = getelementptr inbounds [1024 x i8], [1024 x i8]* %15, i64 0, i64 %93
store i8 0, i8* %94
%95 = getelementptr inbounds [1024 x i8], [1024 x i8]* %15, i64 0, i64 0
%96 = call i64 @strcmp(i8* %95, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%97 = icmp eq i64 %96, 0
br i1 %97, label %102, label %98
98: ; preds = %85
%99 = getelementptr inbounds [1024 x i8], [1024 x i8]* %15, i64 0, i64 0
%100 = call i64 @strcmp(i8* %99, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
%101 = icmp eq i64 %100, 0
br i1 %101, label %102, label %104
102: ; preds = %98, %85
%103 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.7, i64 0, i64 0))
br label %174
104: ; preds = %98
%105 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %12, i32 0, i32 1
%106 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %105, i32 0, i32 0
%107 = load i32, i32* %106
%108 = call i8* @inet_ntoa(i32 %107)
%109 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %12, i32 0, i32 0
%110 = load i32, i32* %109
%111 = call i32 @ntohs(i32 %110)
%112 = getelementptr inbounds [1024 x i8], [1024 x i8]* %15, i64 0, i64 0
%113 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.8, i64 0, i64 0), i8* %108, i32 %111, i8* %112)
br label %171
114: ; preds = %81
%115 = load i32, i32* %9
%116 = call i64 @FD_ISSET(i32 %115, i32* %5)
%117 = icmp ne i64 %116, 0
br i1 %117, label %118, label %168
118: ; preds = %114
%119 = load i32, i32* %16
%120 = add nsw i32 %119, 1
store i32 %120, i32* %16
%121 = load i32, i32* %9
%122 = call i32 @FD_CLR(i32 %121, i32* %5)
%123 = getelementptr inbounds [1024 x i8], [1024 x i8]* %14, i64 0, i64 0
%124 = load i32, i32* %16
%125 = call i32 @snprintf(i8* %123, i32 1024, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.9, i64 0, i64 0), i32 %124)
%126 = getelementptr inbounds [1024 x i8], [1024 x i8]* %14, i64 0, i64 0
%127 = call i32 @strlen(i8* %126)
%128 = icmp sgt i32 %127, 0
br i1 %128, label %129, label %144
129: ; preds = %118
%130 = getelementptr inbounds [1024 x i8], [1024 x i8]* %14, i64 0, i64 0
%131 = call i32 @strlen(i8* %130)
%132 = sub nsw i32 %131, 1
%133 = sext i32 %132 to i64
%134 = getelementptr inbounds [1024 x i8], [1024 x i8]* %14, i64 0, i64 %133
%135 = load i8, i8* %134
%136 = sext i8 %135 to i32
%137 = icmp eq i32 %136, 10
br i1 %137, label %138, label %144
138: ; preds = %129
%139 = getelementptr inbounds [1024 x i8], [1024 x i8]* %14, i64 0, i64 0
%140 = call i32 @strlen(i8* %139)
%141 = sub nsw i32 %140, 1
%142 = sext i32 %141 to i64
%143 = getelementptr inbounds [1024 x i8], [1024 x i8]* %14, i64 0, i64 %142
store i8 0, i8* %143
br label %144
144: ; preds = %138, %129, %118
%145 = getelementptr inbounds [1024 x i8], [1024 x i8]* %14, i64 0, i64 0
%146 = call i64 @strcmp(i8* %145, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%147 = icmp eq i64 %146, 0
br i1 %147, label %152, label %148
148: ; preds = %144
%149 = getelementptr inbounds [1024 x i8], [1024 x i8]* %14, i64 0, i64 0
%150 = call i64 @strcmp(i8* %149, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
%151 = icmp eq i64 %150, 0
br i1 %151, label %152, label %159
152: ; preds = %148, %144
%153 = load i32, i32* %9
%154 = getelementptr inbounds [1024 x i8], [1024 x i8]* %14, i64 0, i64 0
%155 = getelementptr inbounds [1024 x i8], [1024 x i8]* %14, i64 0, i64 0
%156 = call i32 @strlen(i8* %155)
%157 = bitcast %struct.sockaddr_in* %12 to %struct.sockaddr*
%158 = call i32 @sendto(i32 %153, i8* %154, i32 %156, i32 0, %struct.sockaddr* %157, i32 4)
br label %174
159: ; preds = %148
%160 = load i32, i32* %9
%161 = getelementptr inbounds [1024 x i8], [1024 x i8]* %14, i64 0, i64 0
%162 = getelementptr inbounds [1024 x i8], [1024 x i8]* %14, i64 0, i64 0
%163 = call i32 @strlen(i8* %162)
%164 = bitcast %struct.sockaddr_in* %12 to %struct.sockaddr*
%165 = call i32 @sendto(i32 %160, i8* %161, i32 %163, i32 0, %struct.sockaddr* %164, i32 4)
br label %166
166: ; preds = %159
%167 = call i32 @sleep(i32 1)
br label %170
168: ; preds = %114
%169 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.10, i64 0, i64 0))
br label %170
170: ; preds = %168, %166
br label %171
171: ; preds = %170, %104
br label %172
172: ; preds = %171, %79
br label %173
173: ; preds = %172, %74
br label %65
174: ; preds = %152, %102
%175 = load i32, i32* %9
%176 = call i32 @close(i32 %175)
ret i32 0
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
%struct.sockaddr_in = type { i32, %struct.in_addr, i32, i32 }
%struct.in_addr = type { i32 }
%struct.hostent = type { i64 }
%struct.sockaddr = type { i32 }
@.str = [10 x i8] c"localhost\00"
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str.1 = [9 x i8] c"socket()\00"
@F_GETFL = global i32 0
@O_NONBLOCK = global i32 0
@F_SETFL = global i32 0
@.str.3 = [7 x i8] c"select\00"
@.str.5 = [2 x i8] c"q\00"
@.str.6 = [2 x i8] c"Q\00"
@.str.8 = [21 x i8] c"\0A(%s , %d) said: %s\0A\00"
@.str.9 = [17 x i8] c"This is %d test.\00"
@.str.10 = [29 x i8] c"\0AOOPS! What happened? CLIENT\00"
@str = [33 x i8] c"Type (q or Q) at anytime to quit\00"
@str.11 = [29 x i8] c"\0AServer has exited the chat.\00"
@str.12 = [49 x i8] c"Timeout occurred! No data after 10.5 seconds.\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.timeval
%6 = alloca i32
%7 = alloca %struct.sockaddr_in
%8 = alloca [1024 x i8]
%9 = alloca [1024 x i8]
%10 = bitcast i32* %1 to i8*
%11 = bitcast i32* %2 to i8*
%12 = bitcast i32* %3 to i8*
%13 = bitcast i32* %4 to i8*
%14 = bitcast %struct.timeval* %5 to i8*
%15 = bitcast i32* %6 to i8*
%16 = bitcast %struct.sockaddr_in* %7 to i8*
%17 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%18 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%19 = tail call i64 @gethostbyname(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%20 = inttoptr i64 %19 to %struct.hostent*
%21 = load i32, i32* @AF_INET
%22 = load i32, i32* @SOCK_DGRAM
%23 = tail call i32 @socket(i32 %21, i32 %22, i32 0)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %27
25: ; preds = %0
%26 = tail call i32 @error(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
br label %27
27: ; preds = %25, %0
%28 = load i32, i32* @F_GETFL
%29 = tail call i32 (i32, i32, ...) @fcntl(i32 %23, i32 %28)
%30 = load i32, i32* @O_NONBLOCK
%31 = or i32 %30, %29
%32 = load i32, i32* @F_SETFL
%33 = tail call i32 (i32, i32, ...) @fcntl(i32 %23, i32 %32, i32 %31)
%34 = call i32 @FD_ZERO(i32* %1)
%35 = call i32 @FD_ZERO(i32* %2)
%36 = call i32 @FD_ZERO(i32* %3)
%37 = call i32 @FD_ZERO(i32* %4)
%38 = call i32 @FD_SET(i32 %23, i32* %1)
%39 = call i32 @FD_SET(i32 %23, i32* %3)
%40 = call i32 @FD_SET(i32 %23, i32* %2)
%41 = call i32 @FD_SET(i32 %23, i32* %4)
%42 = add nsw i32 %23, 1
%43 = load i32, i32* @AF_INET
%44 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i64 0, i32 3
store i32 %43, i32* %44
%45 = call i32 @htons(i32 5000)
%46 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i64 0, i32 0
store i32 %45, i32* %46
%47 = getelementptr inbounds %struct.hostent, %struct.hostent* %20, i64 0, i32 0
%48 = load i64, i64* %47
%49 = inttoptr i64 %48 to i32*
%50 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i64 0, i32 1, i32 0
%51 = load i32, i32* %49
store i32 %51, i32* %50
%52 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i64 0, i32 2
%53 = call i32 @bzero(i32* %52, i32 8)
store i32 4, i32* %6
%54 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str, i64 0, i64 0))
%55 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 0
%56 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 1
%57 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
br label %58
58: ; preds = %120, %27
%59 = phi i32 [ %94, %120 ], [ 0, %27 ]
br label %60
60: ; preds = %66, %58
store i32 2, i32* %55
store i32 500000, i32* %56
%61 = load i32, i32* %1
store i32 %61, i32* %3
%62 = load i32, i32* %2
store i32 %62, i32* %4
%63 = call i32 @select(i32 %42, i32* %3, i32* %4, i32* null, %struct.timeval* %5)
switch i32 %63, label %69 [
i32 -1, label %64
i32 0, label %67
]
64: ; preds = %60
%65 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
br label %66
66: ; preds = %64, %84, %124, %67
br label %60
67: ; preds = %60
%68 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([49 x i8], [49 x i8]* @str.12, i64 0, i64 0))
br label %66
69: ; preds = %60
%70 = call i64 @FD_ISSET(i32 %23, i32* %3)
%71 = icmp eq i64 %70, 0
br i1 %71, label %90, label %72
72: ; preds = %69
%73 = call i32 @FD_CLR(i32 %23, i32* %3)
%74 = call i32 @recvfrom(i32 %23, i8* %18, i32 1024, i32 0, %struct.sockaddr* %57, i32* %6)
%75 = sext i32 %74 to i64
%76 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 %75
store i8 0, i8* %76
%77 = call i64 @strcmp(i8* %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%78 = icmp eq i64 %77, 0
br i1 %78, label %82, label %79
79: ; preds = %72
%80 = call i64 @strcmp(i8* %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
%81 = icmp eq i64 %80, 0
br i1 %81, label %82, label %84
82: ; preds = %79, %72
%83 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str.11, i64 0, i64 0))
br label %126
84: ; preds = %79
%85 = load i32, i32* %50
%86 = call i8* @inet_ntoa(i32 %85)
%87 = load i32, i32* %46
%88 = call i32 @ntohs(i32 %87)
%89 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.8, i64 0, i64 0), i8* %86, i32 %88, i8* %18)
br label %66
90: ; preds = %69
%91 = call i64 @FD_ISSET(i32 %23, i32* %4)
%92 = icmp eq i64 %91, 0
br i1 %92, label %124, label %93
93: ; preds = %90
%94 = add nuw nsw i32 %59, 1
%95 = call i32 @FD_CLR(i32 %23, i32* %4)
%96 = call i32 @snprintf(i8* %17, i32 1024, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.9, i64 0, i64 0), i32 %94)
%97 = call i32 @strlen(i8* %17)
%98 = icmp sgt i32 %97, 0
br i1 %98, label %99, label %111
99: ; preds = %93
%100 = call i32 @strlen(i8* %17)
%101 = add nsw i32 %100, -1
%102 = sext i32 %101 to i64
%103 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 %102
%104 = load i8, i8* %103
%105 = icmp eq i8 %104, 10
br i1 %105, label %106, label %111
106: ; preds = %99
%107 = call i32 @strlen(i8* %17)
%108 = add nsw i32 %107, -1
%109 = sext i32 %108 to i64
%110 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 %109
store i8 0, i8* %110
br label %111
111: ; preds = %106, %99, %93
%112 = call i64 @strcmp(i8* %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%113 = icmp eq i64 %112, 0
br i1 %113, label %117, label %114
114: ; preds = %111
%115 = call i64 @strcmp(i8* %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
%116 = icmp eq i64 %115, 0
br i1 %116, label %117, label %120
117: ; preds = %114, %111
%118 = call i32 @strlen(i8* %17)
%119 = call i32 @sendto(i32 %23, i8* %17, i32 %118, i32 0, %struct.sockaddr* %57, i32 4)
br label %126
120: ; preds = %114
%121 = call i32 @strlen(i8* %17)
%122 = call i32 @sendto(i32 %23, i8* %17, i32 %121, i32 0, %struct.sockaddr* %57, i32 4)
%123 = call i32 @sleep(i32 1)
br label %58
124: ; preds = %90
%125 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.10, i64 0, i64 0))
br label %66
126: ; preds = %82, %117
%127 = call i32 @close(i32 %23)
ret i32 0
}