#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*/, ...);
int /*<<< orphan*/ fgets(char *, 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 socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stdin;
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(0, &original_stdin);
  FD_SET(0, &writefds);
  numfd = socket_fd + 1;
  tv.tv_sec = 10;
  tv.tv_usec = 500000;
  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");
  while (1) {
    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(0, &writefds)) {
        FD_CLR(0, &writefds);
        printf("ClIENT: ");
        fgets(send_data, 1024, stdin);
        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));
        }
      } 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    0xd97(%rip),%rdi
call   10d0 <gethostbyname@plt>
mov    %rax,-0x50(%rbp)
mov    0x2d6f(%rip),%rax
mov    (%rax),%edi
mov    0x2d4e(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1170 <socket@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0xffffffff,%eax
jne    12a3 <main+0x53>
lea    0xd6c(%rip),%rdi
call   1120 <error@plt>
mov    -0x2c(%rbp),%edi
mov    0x2d0b(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0x28(%rbp)
mov    0x2d20(%rip),%rax
mov    (%rax),%eax
or     -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x2c(%rbp),%edi
mov    0x2cee(%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>
xor    %edi,%edi
lea    -0xc(%rbp),%rsi
call   1160 <FD_SET@plt>
xor    %edi,%edi
lea    -0x14(%rbp),%rsi
call   1160 <FD_SET@plt>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
movl   $0xa,-0x20(%rbp)
movl   $0x7a120,-0x1c(%rbp)
mov    0x2c9a(%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    0xc87(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
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,-0x854(%rbp)
cmpl   $0xffffffff,-0x854(%rbp)
jne    13db <main+0x18b>
lea    0xc64(%rip),%rdi
call   1130 <perror@plt>
jmp    1650 <main+0x400>
cmpl   $0x0,-0x854(%rbp)
jne    13fb <main+0x1ab>
lea    0xc4d(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
jmp    164b <main+0x3fb>
mov    -0x2c(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     14d1 <main+0x281>
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    0xc16(%rip),%rsi
call   10c0 <strcmp@plt>
cmp    $0x0,%rax
je     1484 <main+0x234>
lea    -0x850(%rbp),%rdi
lea    0xbfb(%rip),%rsi
call   10c0 <strcmp@plt>
cmp    $0x0,%rax
jne    1497 <main+0x247>
lea    0xbe7(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
jmp    1655 <main+0x405>
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    0xbcb(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
jmp    1646 <main+0x3f6>
xor    %edi,%edi
lea    -0x14(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1633 <main+0x3e3>
xor    %edi,%edi
lea    -0x14(%rbp),%rsi
call   1150 <FD_CLR@plt>
lea    0xbad(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
lea    -0x450(%rbp),%rdi
mov    0x2acb(%rip),%rax
mov    (%rax),%edx
mov    $0x400,%esi
call   10b0 <fgets@plt>
lea    -0x450(%rbp),%rdi
call   1060 <strlen@plt>
cmp    $0x0,%eax
jle    1569 <main+0x319>
lea    -0x450(%rbp),%rdi
call   1060 <strlen@plt>
sub    $0x1,%eax
cltq
movsbl -0x450(%rbp,%rax,1),%eax
ff
cmp    $0xa,%eax
jne    1569 <main+0x319>
lea    -0x450(%rbp),%rdi
call   1060 <strlen@plt>
sub    $0x1,%eax
cltq
movb   $0x0,-0x450(%rbp,%rax,1)
lea    -0x450(%rbp),%rdi
lea    0xaf7(%rip),%rsi
call   10c0 <strcmp@plt>
cmp    $0x0,%rax
je     15a3 <main+0x353>
lea    -0x450(%rbp),%rdi
lea    0xadc(%rip),%rsi
call   10c0 <strcmp@plt>
cmp    $0x0,%rax
jne    15eb <main+0x39b>
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   1090 <sendto@plt>
jmp    1655 <main+0x405>
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   1090 <sendto@plt>
jmp    1641 <main+0x3f1>
lea    0xa74(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
jmp    1646 <main+0x3f6>
jmp    164b <main+0x3fb>
jmp    1650 <main+0x400>
jmp    1393 <main+0x143>
mov    -0x2c(%rbp),%edi
call   10a0 <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    0xd88(%rip),%rdi
call   10e0 <gethostbyname@plt>
mov    %rax,0x30(%rsp)
mov    0x2d5f(%rip),%rax
mov    (%rax),%edi
mov    0x2d3e(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1180 <socket@plt>
mov    %eax,%r15d
mov    %eax,0x4(%rsp)
cmp    $0xffffffff,%eax
jne    12b3 <main+0x53>
lea    0xd5c(%rip),%rdi
call   1130 <error@plt>
mov    0x2cfe(%rip),%rax
mov    (%rax),%esi
mov    %r15d,%edi
xor    %eax,%eax
call   1040 <fcntl@plt>
mov    0x2d13(%rip),%rcx
or     (%rcx),%eax
mov    0x2cea(%rip),%rcx
mov    (%rcx),%esi
mov    %r15d,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <fcntl@plt>
lea    0x2c(%rsp),%rbx
mov    %rbx,%rdi
call   10f0 <FD_ZERO@plt>
lea    0x28(%rsp),%r12
mov    %r12,%rdi
call   10f0 <FD_ZERO@plt>
lea    0x24(%rsp),%r14
mov    %r14,%rdi
call   10f0 <FD_ZERO@plt>
lea    0x20(%rsp),%rbp
mov    %rbp,%rdi
call   10f0 <FD_ZERO@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
call   1170 <FD_SET@plt>
mov    %r15d,%edi
mov    %r14,%rsi
call   1170 <FD_SET@plt>
xor    %edi,%edi
mov    %r12,%rsi
mov    %rbp,%rbx
call   1170 <FD_SET@plt>
xor    %edi,%edi
mov    %rbp,%rsi
call   1170 <FD_SET@plt>
addl   $0x1,0x4(%rsp)
movabs $0x7a1200000000a,%rax
mov    %rax,0x38(%rsp)
mov    0x2c88(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    $0x1388,%edi
call   1080 <htons@plt>
mov    %eax,0x8(%rsp)
mov    0x30(%rsp),%rax
mov    (%rax),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
lea    0x10(%rsp),%rdi
mov    $0x8,%esi
call   1150 <bzero@plt>
movl   $0x4,0x1c(%rsp)
lea    0xcb9(%rip),%rdi
call   1030 <puts@plt>
lea    0x38(%rsp),%rbp
lea    0x440(%rsp),%r12
mov    %r15d,0x18(%rsp)
jmp    13cc <main+0x16c>
nopl   0x0(%rax)
lea    0xcd0(%rip),%rdi
call   1030 <puts@plt>
mov    0x2c(%rsp),%eax
mov    %eax,0x24(%rsp)
mov    0x28(%rsp),%eax
mov    %eax,0x20(%rsp)
mov    0x4(%rsp),%edi
mov    %r14,%rsi
mov    %rbx,%rdx
xor    %ecx,%ecx
mov    %rbp,%r8
call   1100 <select@plt>
test   %eax,%eax
je     13c0 <main+0x160>
cmp    $0xffffffff,%eax
je     158a <main+0x32a>
mov    %r15d,%edi
mov    %r14,%rsi
call   1050 <FD_ISSET@plt>
test   %rax,%rax
je     14b9 <main+0x259>
mov    %r15d,%edi
mov    %r14,%rsi
call   1160 <FD_CLR@plt>
mov    %r15d,%edi
lea    0x40(%rsp),%r13
mov    %r13,%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
lea    0x8(%rsp),%r8
lea    0x1c(%rsp),%r9
call   1110 <recvfrom@plt>
cltq
movb   $0x0,0x40(%rsp,%rax,1)
mov    %r13,%rdi
lea    0xbcc(%rip),%rsi
call   10d0 <strcmp@plt>
test   %rax,%rax
je     159b <main+0x33b>
lea    0x40(%rsp),%rdi
lea    0xbb4(%rip),%rsi
call   10d0 <strcmp@plt>
test   %rax,%rax
je     159b <main+0x33b>
mov    0xc(%rsp),%edi
call   1060 <inet_ntoa@plt>
mov    %r14,%r13
mov    %r12,%r14
mov    %rax,%r12
mov    0x8(%rsp),%edi
call   1120 <ntohs@plt>
lea    0xb86(%rip),%rdi
mov    %r12,%rsi
mov    %r14,%r12
mov    %r13,%r14
mov    0x18(%rsp),%r15d
mov    %eax,%edx
lea    0x40(%rsp),%rcx
xor    %eax,%eax
call   1090 <printf@plt>
jmp    13cc <main+0x16c>
xor    %edi,%edi
mov    %rbx,%rsi
call   1050 <FD_ISSET@plt>
test   %rax,%rax
je     1577 <main+0x317>
xor    %edi,%edi
mov    %rbx,%rsi
call   1160 <FD_CLR@plt>
lea    0xb56(%rip),%rdi
xor    %eax,%eax
call   1090 <printf@plt>
mov    0x2aed(%rip),%rax
mov    (%rax),%edx
mov    %r12,%rdi
mov    $0x400,%esi
call   10c0 <fgets@plt>
mov    %r12,%rdi
call   1070 <strlen@plt>
test   %eax,%eax
jle    1528 <main+0x2c8>
mov    %r12,%rdi
call   1070 <strlen@plt>
cltq
cmpb   $0xa,-0x1(%rax,%r12,1)
jne    1528 <main+0x2c8>
mov    %r12,%rdi
call   1070 <strlen@plt>
cltq
movb   $0x0,-0x1(%rax,%r12,1)
mov    %r12,%rdi
lea    0xae8(%rip),%rsi
call   10d0 <strcmp@plt>
test   %rax,%rax
je     15a9 <main+0x349>
mov    %r12,%rdi
lea    0xad6(%rip),%rsi
call   10d0 <strcmp@plt>
test   %rax,%rax
je     15a9 <main+0x349>
mov    %r12,%rdi
call   1070 <strlen@plt>
mov    %r15d,%edi
mov    %r12,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
lea    0x8(%rsp),%r8
mov    $0x4,%r9d
call   10a0 <sendto@plt>
jmp    13cc <main+0x16c>
lea    0xabe(%rip),%rdi
xor    %eax,%eax
call   1090 <printf@plt>
jmp    13cc <main+0x16c>
lea    0xa82(%rip),%rdi
call   1140 <perror@plt>
jmp    13cc <main+0x16c>
lea    0xad8(%rip),%rdi
call   1030 <puts@plt>
jmp    15d3 <main+0x373>
lea    0x440(%rsp),%rbp
mov    %rbp,%rdi
call   1070 <strlen@plt>
lea    0x8(%rsp),%r8
mov    %r15d,%edi
mov    %rbp,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
mov    $0x4,%r9d
call   10a0 <sendto@plt>
mov    %r15d,%edi
call   10b0 <close@plt>
xor    %eax,%eax
add    $0x848,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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