#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 {
  scalar_t__ tv_usec;
  scalar_t__ tv_sec;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  int /*<<< orphan*/ h_length;
  int /*<<< orphan*/ h_addr;
};
typedef int /*<<< orphan*/ sin;
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
scalar_t__ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ SOCK_STREAM;
int STDIN_FILENO;
int STDOUT_FILENO;
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, int /*<<< orphan*/,
                          int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, unsigned char *, int);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/, int /*<<< orphan*/,
           int /*<<< orphan*/ *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int /*<<< orphan*/ write(int, unsigned char *, int);
void chatUser(char *hostname, int aPort) {
  unsigned char buffer[10000];
  struct sockaddr_in sin;
  struct hostent *hp;
  struct timeval stv;
  int i, nread;
  int fd_chat = -1;
  int iMaxFd, iNumInputs, nContinue = 1;
  fd_set fdCheck;
  hp = gethostbyname(hostname);
  if (hp == NULL) {
    printf("\n%s: unknown host.\n", hostname);
    nContinue = 0;
  } else {
    if ((fd_chat = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
      perror("client: socket()");
      nContinue = 0;
    } else {
      sin.sin_family = AF_INET;
      sin.sin_port = htons(aPort);
      memcpy(&sin.sin_addr, hp->h_addr, hp->h_length);
      if (connect(fd_chat, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
        perror("client: connect()");
        nContinue = 0;
      }
    }
  }
  for (; nContinue;) {
    stv.tv_sec = 0;
    stv.tv_usec = 0;
    FD_ZERO(&fdCheck);
    iMaxFd = STDIN_FILENO;
    if (fd_chat > STDIN_FILENO)
      iMaxFd = fd_chat;
    iMaxFd += 1;
    FD_SET(STDIN_FILENO, &fdCheck);
    FD_SET(fd_chat, &fdCheck);
    iNumInputs = select(iMaxFd, &fdCheck, 0, 0, NULL);
    if (iNumInputs == -1) {
      printf("\n Bad return from select ==> die\n");
      nContinue = 0;
    } else if (iNumInputs == 0) {
      printf("\n Timeout from select\n");
      nContinue = 0;
    } else if (FD_ISSET(fd_chat, &fdCheck)) {
      nread = read(fd_chat, buffer, 10000);
      if (nread > 0) {
        buffer[nread] = 0;
        write(STDOUT_FILENO, buffer, nread);
      } else {
        if (nread == 0)
          nContinue = 0;
        if (nread < 0)
          fprintf(stderr, "fd_chat err:%s\n", strerror(errno));
      }
    } else if (FD_ISSET(STDIN_FILENO, &fdCheck)) {
      nread = read(STDIN_FILENO, buffer, 10000);
      if (nread > 0) {
        buffer[nread] = 0;
        write(fd_chat, buffer, nread);
      }
    }
  }
  if (fd_chat >= 0)
    close(fd_chat);
  exit(0);
}


x86 assembly (clang, -O0):
<chatUser>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2770,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0xffffffff,-0x2754(%rbp)
ff ff ff
movl   $0x1,-0x2760(%rbp)
mov    -0x8(%rbp),%rdi
call   1090 <gethostbyname@plt>
mov    %rax,-0x2738(%rbp)
cmpq   $0x0,-0x2738(%rbp)
jne    1275 <chatUser+0x65>
mov    -0x8(%rbp),%rsi
lea    0xda1(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x0,-0x2760(%rbp)
jmp    1335 <chatUser+0x125>
mov    0x2d64(%rip),%rax
mov    (%rax),%edi
mov    0x2d43(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1130 <socket@plt>
mov    %eax,-0x2754(%rbp)
cmp    $0x0,%eax
jge    12b8 <chatUser+0xa8>
lea    0xd70(%rip),%rdi
call   10e0 <perror@plt>
movl   $0x0,-0x2760(%rbp)
jmp    1330 <chatUser+0x120>
mov    0x2d21(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2728(%rbp)
mov    -0xc(%rbp),%edi
call   1050 <htons@plt>
mov    %eax,-0x272c(%rbp)
lea    -0x2730(%rbp),%rdi
mov    -0x2738(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x2738(%rbp),%rax
mov    (%rax),%edx
call   10b0 <memcpy@plt>
mov    -0x2754(%rbp),%edi
lea    -0x2730(%rbp),%rsi
mov    $0xc,%edx
call   1100 <connect@plt>
cmp    $0x0,%rax
jge    132b <chatUser+0x11b>
lea    0xd09(%rip),%rdi
call   10e0 <perror@plt>
movl   $0x0,-0x2760(%rbp)
jmp    1330 <chatUser+0x120>
jmp    1335 <chatUser+0x125>
jmp    133a <chatUser+0x12a>
cmpl   $0x0,-0x2760(%rbp)
je     15a5 <chatUser+0x395>
movq   $0x0,-0x2740(%rbp)
movq   $0x0,-0x2748(%rbp)
lea    -0x2764(%rbp),%rdi
call   10c0 <FD_ZERO@plt>
mov    0x2c48(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2758(%rbp)
mov    -0x2754(%rbp),%eax
mov    0x2c33(%rip),%rcx
cmp    (%rcx),%eax
jle    1399 <chatUser+0x189>
mov    -0x2754(%rbp),%eax
mov    %eax,-0x2758(%rbp)
mov    -0x2758(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2758(%rbp)
mov    0x2c09(%rip),%rax
mov    (%rax),%edi
lea    -0x2764(%rbp),%rsi
call   1120 <FD_SET@plt>
mov    -0x2754(%rbp),%edi
lea    -0x2764(%rbp),%rsi
call   1120 <FD_SET@plt>
mov    -0x2758(%rbp),%edi
lea    -0x2764(%rbp),%rsi
xor    %ecx,%ecx
xor    %eax,%eax
mov    %eax,%r8d
mov    %ecx,%edx
call   10d0 <select@plt>
mov    %eax,-0x275c(%rbp)
cmpl   $0xffffffff,-0x275c(%rbp)
jne    141a <chatUser+0x20a>
lea    0xc33(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x0,-0x2760(%rbp)
jmp    15a0 <chatUser+0x390>
cmpl   $0x0,-0x275c(%rbp)
jne    1444 <chatUser+0x234>
lea    0xc2b(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x0,-0x2760(%rbp)
jmp    159b <chatUser+0x38b>
mov    -0x2754(%rbp),%edi
lea    -0x2764(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1519 <chatUser+0x309>
mov    -0x2754(%rbp),%edi
lea    -0x2720(%rbp),%rsi
mov    $0x2710,%edx
call   1080 <read@plt>
mov    %eax,-0x2750(%rbp)
cmpl   $0x0,-0x2750(%rbp)
jle    14b9 <chatUser+0x2a9>
movslq -0x2750(%rbp),%rax
movb   $0x0,-0x2720(%rbp,%rax,1)
mov    0x2b50(%rip),%rax
mov    (%rax),%edi
lea    -0x2720(%rbp),%rsi
mov    -0x2750(%rbp),%edx
call   1030 <write@plt>
jmp    1514 <chatUser+0x304>
cmpl   $0x0,-0x2750(%rbp)
jne    14d0 <chatUser+0x2c0>
movl   $0x0,-0x2760(%rbp)
cmpl   $0x0,-0x2750(%rbp)
jge    150f <chatUser+0x2ff>
mov    0x2aec(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2768(%rbp)
mov    0x2acd(%rip),%rax
mov    (%rax),%edi
call   1110 <strerror@plt>
mov    -0x2768(%rbp),%edi
mov    %rax,%rdx
lea    0xb66(%rip),%rsi
call   10a0 <fprintf@plt>
jmp    1514 <chatUser+0x304>
jmp    1596 <chatUser+0x386>
mov    0x2a98(%rip),%rax
mov    (%rax),%edi
lea    -0x2764(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1591 <chatUser+0x381>
mov    0x2a79(%rip),%rax
mov    (%rax),%edi
lea    -0x2720(%rbp),%rsi
mov    $0x2710,%edx
call   1080 <read@plt>
mov    %eax,-0x2750(%rbp)
cmpl   $0x0,-0x2750(%rbp)
jle    158c <chatUser+0x37c>
movslq -0x2750(%rbp),%rax
movb   $0x0,-0x2720(%rbp,%rax,1)
mov    -0x2754(%rbp),%edi
lea    -0x2720(%rbp),%rsi
mov    -0x2750(%rbp),%edx
call   1030 <write@plt>
jmp    1591 <chatUser+0x381>
jmp    1596 <chatUser+0x386>
jmp    159b <chatUser+0x38b>
jmp    15a0 <chatUser+0x390>
jmp    133a <chatUser+0x12a>
cmpl   $0x0,-0x2754(%rbp)
jl     15bd <chatUser+0x3ad>
mov    -0x2754(%rbp),%edi
call   1070 <close@plt>
xor    %edi,%edi
call   10f0 <exit@plt>

x86 assembly (clang, -O2):
<chatUser>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x2728,%rsp
mov    %esi,%r14d
mov    %rdi,%rbx
call   10a0 <gethostbyname@plt>
test   %rax,%rax
je     12cd <chatUser+0xad>
mov    %rax,%rbp
mov    0x2d91(%rip),%r15
mov    (%r15),%edi
mov    0x2d6f(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1140 <socket@plt>
test   %eax,%eax
jns    1279 <chatUser+0x59>
lea    0xda7(%rip),%rdi
call   10f0 <perror@plt>
xor    %edi,%edi
call   1100 <exit@plt>
mov    %eax,%ebx
mov    (%r15),%eax
mov    %eax,0x8(%rsp)
mov    %r14d,%edi
call   1060 <htons@plt>
mov    %eax,0x4(%rsp)
mov    0x0(%rbp),%edx
mov    0x4(%rbp),%esi
mov    %rsp,%rbp
mov    %rbp,%rdi
call   10c0 <memcpy@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0xc,%edx
call   1110 <connect@plt>
test   %rax,%rax
jns    12e5 <chatUser+0xc5>
lea    0xd6b(%rip),%rdi
call   10f0 <perror@plt>
mov    %ebx,%edi
call   1080 <close@plt>
xor    %edi,%edi
call   1100 <exit@plt>
lea    0xd2c(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
xor    %edi,%edi
call   1100 <exit@plt>
lea    0xc(%rsp),%r14
mov    0x2cc7(%rip),%r13
lea    0x10(%rsp),%r15
mov    0x2cf3(%rip),%r12
jmp    1315 <chatUser+0xf5>
nop
mov    %eax,%ecx
movb   $0x0,0x10(%rsp,%rcx,1)
mov    (%r12),%edi
mov    %r15,%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    %r14,%rdi
call   10d0 <FD_ZERO@plt>
mov    0x0(%r13),%edi
cmp    %edi,%ebx
mov    %edi,%ebp
cmovg  %ebx,%ebp
add    $0x1,%ebp
mov    %r14,%rsi
call   1130 <FD_SET@plt>
mov    %ebx,%edi
mov    %r14,%rsi
call   1130 <FD_SET@plt>
mov    %ebp,%edi
mov    %r14,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   10e0 <select@plt>
cmp    $0xffffffff,%eax
je     13f9 <chatUser+0x1d9>
test   %eax,%eax
je     1402 <chatUser+0x1e2>
mov    %ebx,%edi
mov    %r14,%rsi
call   1050 <FD_ISSET@plt>
test   %rax,%rax
je     1390 <chatUser+0x170>
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x2710,%edx
call   1090 <read@plt>
test   %eax,%eax
jg     1300 <chatUser+0xe0>
js     13cc <chatUser+0x1ac>
jne    1315 <chatUser+0xf5>
jmp    140e <chatUser+0x1ee>
xchg   %ax,%ax
mov    0x0(%r13),%edi
mov    %r14,%rsi
call   1050 <FD_ISSET@plt>
test   %rax,%rax
je     1315 <chatUser+0xf5>
mov    0x0(%r13),%edi
mov    %r15,%rsi
mov    $0x2710,%edx
call   1090 <read@plt>
test   %eax,%eax
jle    1315 <chatUser+0xf5>
mov    %eax,%ecx
movb   $0x0,0x10(%rsp,%rcx,1)
mov    %ebx,%edi
jmp    130b <chatUser+0xeb>
mov    0x2bfd(%rip),%rax
mov    (%rax),%ebp
mov    0x2be4(%rip),%rax
mov    (%rax),%edi
call   1120 <strerror@plt>
mov    %ebp,%edi
lea    0xc4b(%rip),%rsi
mov    %rax,%rdx
call   10b0 <fprintf@plt>
jmp    1315 <chatUser+0xf5>
lea    0xc5d(%rip),%rdi
jmp    1409 <chatUser+0x1e9>
lea    0xc3e(%rip),%rdi
call   1030 <puts@plt>
test   %ebx,%ebx
jns    12bf <chatUser+0x9f>
jmp    12c6 <chatUser+0xa6>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.hostent = type { i32, i32 }
%struct.timeval = type { i64, i64 }
%struct.sockaddr = type { i32 }
@.str = [20 x i8] c"\0A%s: unknown host.\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [17 x i8] c"client: socket()\00"
@.str.2 = [18 x i8] c"client: connect()\00"
@STDIN_FILENO = global i32 0
@.str.3 = [34 x i8] c"\0A Bad return from select ==> die\0A\00"
@.str.4 = [23 x i8] c"\0A Timeout from select\0A\00"
@STDOUT_FILENO = global i32 0
@stderr = global i32 0
@.str.5 = [16 x i8] c"fd_chat err:%s\0A\00"
@errno = global i32 0
define void @chatUser(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca [10000 x i8]
%6 = alloca %struct.sockaddr_in
%7 = alloca %struct.hostent*
%8 = alloca %struct.timeval
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
store i32 -1, i32* %11
store i32 1, i32* %14
%16 = load i8*, i8** %3
%17 = call %struct.hostent* @gethostbyname(i8* %16)
store %struct.hostent* %17, %struct.hostent** %7
%18 = load %struct.hostent*, %struct.hostent** %7
%19 = icmp eq %struct.hostent* %18, null
br i1 %19, label %20, label %23
20: ; preds = %2
%21 = load i8*, i8** %3
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %21)
store i32 0, i32* %14
br label %52
23: ; preds = %2
%24 = load i32, i32* @AF_INET
%25 = load i32, i32* @SOCK_STREAM
%26 = call i32 @socket(i32 %24, i32 %25, i32 0)
store i32 %26, i32* %11
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %23
%29 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %14
br label %51
30: ; preds = %23
%31 = load i32, i32* @AF_INET
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
store i32 %31, i32* %32
%33 = load i32, i32* %4
%34 = call i32 @htons(i32 %33)
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
store i32 %34, i32* %35
%36 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
%37 = load %struct.hostent*, %struct.hostent** %7
%38 = getelementptr inbounds %struct.hostent, %struct.hostent* %37, i32 0, i32 1
%39 = load i32, i32* %38
%40 = load %struct.hostent*, %struct.hostent** %7
%41 = getelementptr inbounds %struct.hostent, %struct.hostent* %40, i32 0, i32 0
%42 = load i32, i32* %41
%43 = call i32 @memcpy(i32* %36, i32 %39, i32 %42)
%44 = load i32, i32* %11
%45 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%46 = call i64 @connect(i32 %44, %struct.sockaddr* %45, i32 12)
%47 = icmp slt i64 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %30
%49 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %14
br label %50
50: ; preds = %48, %30
br label %51
51: ; preds = %50, %28
br label %52
52: ; preds = %51, %20
br label %53
53: ; preds = %138, %52
%54 = load i32, i32* %14
%55 = icmp ne i32 %54, 0
br i1 %55, label %56, label %139
56: ; preds = %53
%57 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i32 0, i32 1
store i64 0, i64* %57
%58 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i32 0, i32 0
store i64 0, i64* %58
%59 = call i32 @FD_ZERO(i32* %15)
%60 = load i32, i32* @STDIN_FILENO
store i32 %60, i32* %12
%61 = load i32, i32* %11
%62 = load i32, i32* @STDIN_FILENO
%63 = icmp sgt i32 %61, %62
br i1 %63, label %64, label %66
64: ; preds = %56
%65 = load i32, i32* %11
store i32 %65, i32* %12
br label %66
66: ; preds = %64, %56
%67 = load i32, i32* %12
%68 = add nsw i32 %67, 1
store i32 %68, i32* %12
%69 = load i32, i32* @STDIN_FILENO
%70 = call i32 @FD_SET(i32 %69, i32* %15)
%71 = load i32, i32* %11
%72 = call i32 @FD_SET(i32 %71, i32* %15)
%73 = load i32, i32* %12
%74 = call i32 @select(i32 %73, i32* %15, i32 0, i32 0, i32* null)
store i32 %74, i32* %13
%75 = load i32, i32* %13
%76 = icmp eq i32 %75, -1
br i1 %76, label %77, label %79
77: ; preds = %66
%78 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %14
br label %138
79: ; preds = %66
%80 = load i32, i32* %13
%81 = icmp eq i32 %80, 0
br i1 %81, label %82, label %84
82: ; preds = %79
%83 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0))
store i32 0, i32* %14
br label %137
84: ; preds = %79
%85 = load i32, i32* %11
%86 = call i64 @FD_ISSET(i32 %85, i32* %15)
%87 = icmp ne i64 %86, 0
br i1 %87, label %88, label %116
88: ; preds = %84
%89 = load i32, i32* %11
%90 = getelementptr inbounds [10000 x i8], [10000 x i8]* %5, i64 0, i64 0
%91 = call i32 @read(i32 %89, i8* %90, i32 10000)
store i32 %91, i32* %10
%92 = load i32, i32* %10
%93 = icmp sgt i32 %92, 0
br i1 %93, label %94, label %102
94: ; preds = %88
%95 = load i32, i32* %10
%96 = sext i32 %95 to i64
%97 = getelementptr inbounds [10000 x i8], [10000 x i8]* %5, i64 0, i64 %96
store i8 0, i8* %97
%98 = load i32, i32* @STDOUT_FILENO
%99 = getelementptr inbounds [10000 x i8], [10000 x i8]* %5, i64 0, i64 0
%100 = load i32, i32* %10
%101 = call i32 @write(i32 %98, i8* %99, i32 %100)
br label %115
102: ; preds = %88
%103 = load i32, i32* %10
%104 = icmp eq i32 %103, 0
br i1 %104, label %105, label %106
105: ; preds = %102
store i32 0, i32* %14
br label %106
106: ; preds = %105, %102
%107 = load i32, i32* %10
%108 = icmp slt i32 %107, 0
br i1 %108, label %109, label %114
109: ; preds = %106
%110 = load i32, i32* @stderr
%111 = load i32, i32* @errno
%112 = call i8* @strerror(i32 %111)
%113 = call i32 @fprintf(i32 %110, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0), i8* %112)
br label %114
114: ; preds = %109, %106
br label %115
115: ; preds = %114, %94
br label %136
116: ; preds = %84
%117 = load i32, i32* @STDIN_FILENO
%118 = call i64 @FD_ISSET(i32 %117, i32* %15)
%119 = icmp ne i64 %118, 0
br i1 %119, label %120, label %135
120: ; preds = %116
%121 = load i32, i32* @STDIN_FILENO
%122 = getelementptr inbounds [10000 x i8], [10000 x i8]* %5, i64 0, i64 0
%123 = call i32 @read(i32 %121, i8* %122, i32 10000)
store i32 %123, i32* %10
%124 = load i32, i32* %10
%125 = icmp sgt i32 %124, 0
br i1 %125, label %126, label %134
126: ; preds = %120
%127 = load i32, i32* %10
%128 = sext i32 %127 to i64
%129 = getelementptr inbounds [10000 x i8], [10000 x i8]* %5, i64 0, i64 %128
store i8 0, i8* %129
%130 = load i32, i32* %11
%131 = getelementptr inbounds [10000 x i8], [10000 x i8]* %5, i64 0, i64 0
%132 = load i32, i32* %10
%133 = call i32 @write(i32 %130, i8* %131, i32 %132)
br label %134
134: ; preds = %126, %120
br label %135
135: ; preds = %134, %116
br label %136
136: ; preds = %135, %115
br label %137
137: ; preds = %136, %82
br label %138
138: ; preds = %137, %77
br label %53
139: ; preds = %53
%140 = load i32, i32* %11
%141 = icmp sge i32 %140, 0
br i1 %141, label %142, label %145
142: ; preds = %139
%143 = load i32, i32* %11
%144 = call i32 @close(i32 %143)
br label %145
145: ; preds = %142, %139
%146 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.hostent = type { i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [20 x i8] c"\0A%s: unknown host.\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [17 x i8] c"client: socket()\00"
@.str.2 = [18 x i8] c"client: connect()\00"
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@stderr = global i32 0
@.str.5 = [16 x i8] c"fd_chat err:%s\0A\00"
@errno = global i32 0
@str = [22 x i8] c"\0A Timeout from select\00"
@str.6 = [33 x i8] c"\0A Bad return from select ==> die\00"
define void @chatUser(i8* %0, i32 %1) {
%3 = alloca [10000 x i8]
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = getelementptr inbounds [10000 x i8], [10000 x i8]* %3, i64 0, i64 0
%7 = bitcast %struct.sockaddr_in* %4 to i8*
%8 = bitcast i32* %5 to i8*
%9 = tail call %struct.hostent* @gethostbyname(i8* %0)
%10 = icmp eq %struct.hostent* %9, null
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %86
13: ; preds = %2
%14 = load i32, i32* @AF_INET
%15 = load i32, i32* @SOCK_STREAM
%16 = tail call i32 @socket(i32 %14, i32 %15, i32 0)
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %13
%19 = tail call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
br label %86
20: ; preds = %13
%21 = load i32, i32* @AF_INET
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %21, i32* %22
%23 = tail call i32 @htons(i32 %1)
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
store i32 %23, i32* %24
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
%26 = getelementptr inbounds %struct.hostent, %struct.hostent* %9, i64 0, i32 1
%27 = load i32, i32* %26
%28 = getelementptr inbounds %struct.hostent, %struct.hostent* %9, i64 0, i32 0
%29 = load i32, i32* %28
%30 = call i32 @memcpy(i32* %25, i32 %27, i32 %29)
%31 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%32 = call i64 @connect(i32 %16, %struct.sockaddr* %31, i32 12)
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %20
%35 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
br label %84
36: ; preds = %20, %67
%37 = call i32 @FD_ZERO(i32* %5)
%38 = load i32, i32* @STDIN_FILENO
%39 = icmp sgt i32 %16, %38
%40 = select i1 %39, i32 %16, i32 %38
%41 = add nsw i32 %40, 1
%42 = call i32 @FD_SET(i32 %38, i32* %5)
%43 = call i32 @FD_SET(i32 %16, i32* %5)
%44 = call i32 @select(i32 %41, i32* %5, i32 0, i32 0, i32* null)
switch i32 %44, label %45 [
i32 -1, label %79
i32 0, label %78
]
45: ; preds = %36
%46 = call i64 @FD_ISSET(i32 %16, i32* %5)
%47 = icmp eq i64 %46, 0
br i1 %47, label %63, label %48
48: ; preds = %45
%49 = call i32 @read(i32 %16, i8* %6, i32 10000)
%50 = icmp sgt i32 %49, 0
br i1 %50, label %51, label %56
51: ; preds = %48
%52 = zext i32 %49 to i64
%53 = getelementptr inbounds [10000 x i8], [10000 x i8]* %3, i64 0, i64 %52
store i8 0, i8* %53
%54 = load i32, i32* @STDOUT_FILENO
%55 = call i32 @write(i32 %54, i8* %6, i32 %49)
br label %67
56: ; preds = %48
%57 = icmp slt i32 %49, 0
br i1 %57, label %58, label %76
58: ; preds = %56
%59 = load i32, i32* @stderr
%60 = load i32, i32* @errno
%61 = call i8* @strerror(i32 %60)
%62 = call i32 @fprintf(i32 %59, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0), i8* %61)
br label %67
63: ; preds = %45
%64 = load i32, i32* @STDIN_FILENO
%65 = call i64 @FD_ISSET(i32 %64, i32* %5)
%66 = icmp eq i64 %65, 0
br i1 %66, label %67, label %68
67: ; preds = %63, %68, %76, %51, %58, %72
br label %36
68: ; preds = %63
%69 = load i32, i32* @STDIN_FILENO
%70 = call i32 @read(i32 %69, i8* %6, i32 10000)
%71 = icmp sgt i32 %70, 0
br i1 %71, label %72, label %67
72: ; preds = %68
%73 = zext i32 %70 to i64
%74 = getelementptr inbounds [10000 x i8], [10000 x i8]* %3, i64 0, i64 %73
store i8 0, i8* %74
%75 = call i32 @write(i32 %16, i8* %6, i32 %70)
br label %67
76: ; preds = %56
%77 = icmp eq i32 %49, 0
br i1 %77, label %82, label %67
78: ; preds = %36
br label %79
79: ; preds = %36, %78
%80 = phi i8* [ getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0), %78 ], [ getelementptr inbounds ([33 x i8], [33 x i8]* @str.6, i64 0, i64 0), %36 ]
%81 = call i32 @puts(i8* dereferenceable(1) %80)
br label %82
82: ; preds = %76, %79
%83 = icmp sgt i32 %16, -1
br i1 %83, label %84, label %86
84: ; preds = %34, %82
%85 = call i32 @close(i32 %16)
br label %86
86: ; preds = %11, %18, %84, %82
%87 = call i32 @exit(i32 0)
unreachable
}