#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;
  scalar_t__ tv_usec;
};
struct termio {
  int c_lflag;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
typedef int /*<<< orphan*/ servaddr;
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int ECHO;
scalar_t__ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int ICANON;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ TCGETA;
int /*<<< orphan*/ TCSETAF;
int /*<<< orphan*/ atoi(char *);
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
scalar_t__ connect(int, struct sockaddr_in *, int);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
scalar_t__ inet_pton(int /*<<< orphan*/, char *, int /*<<< orphan*/ *);
int ioctl(int /*<<< orphan*/, int /*<<< orphan*/, struct termio *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int read(int, char *, int);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ write(int, char *, int);
int main(int argc, char **argv) {
  struct sockaddr_in servaddr;
  struct termio tbuf, tbufsav;
  struct timeval tv;
  char buf[1024];
  int sockfd, rc, c;
  fd_set rfds;
  if (argc != 3) {
    printf("Usage: %s <IP-Address> <TCP-Port>\n", argv[0]);
    return 1;
  }
  if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
    perror("socket");
    return 1;
  }
  bzero(&servaddr, sizeof(servaddr));
  servaddr.sin_family = AF_INET;
  servaddr.sin_port = htons(atoi(argv[2]));
  if (inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0) {
    printf("Not an IP address: %s\n", argv[1]);
    return 1;
  }
  if (connect(sockfd, &servaddr, sizeof(servaddr)) < 0) {
    perror("connect");
    return 1;
  }
  if (ioctl(0, TCGETA, &tbuf) == -1) {
    perror("ioctl1");
    return 1;
  }
  tbufsav = tbuf;
  tbuf.c_lflag &= ~(ICANON | ECHO);
  if (ioctl(0, TCSETAF, &tbuf) == -1) {
    perror("ioctl2");
    return 1;
  }
  do {
    FD_ZERO(&rfds);
    FD_SET(sockfd, &rfds);
    FD_SET(0, &rfds);
    tv.tv_sec = 1;
    tv.tv_usec = 0;
    rc = select(sockfd + 1, &rfds, NULL, NULL, NULL);
    if (rc == -1) {
      perror("select");
      return 1;
    }
    if (FD_ISSET(sockfd, &rfds)) {
      rc = read(sockfd, buf, 1024);
      for (c = 0; c < rc; c++)
        if (buf[c] != '\r')
          write(1, buf + c, 1);
    }
    if (FD_ISSET(0, &rfds)) {
      rc = read(0, buf, 1024);
      for (c = 0; c < rc; c += write(sockfd, buf, rc))
        ;
    }
  } while (rc > 0);
  if (ioctl(0, TCSETAF, &tbufsav) == -1) {
    perror("ioctl3");
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x450,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     1232 <main+0x42>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xddf(%rip),%rdi
call   1060 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    15b0 <main+0x3c0>
mov    0x2daf(%rip),%rax
mov    (%rax),%edi
mov    0x2d86(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1110 <socket@plt>
mov    %eax,-0x444(%rbp)
cmp    $0x0,%eax
jge    1272 <main+0x82>
lea    0xdc2(%rip),%rdi
call   10c0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    15b0 <main+0x3c0>
lea    -0x20(%rbp),%rdi
mov    $0xc,%esi
call   10d0 <bzero@plt>
mov    0x2d61(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10e0 <atoi@plt>
mov    %eax,%edi
call   1050 <htons@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2d3e(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    -0x20(%rbp),%rdx
call   1090 <inet_pton@plt>
cmp    $0x0,%rax
jg     12e7 <main+0xf7>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xd54(%rip),%rdi
call   1060 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    15b0 <main+0x3c0>
mov    -0x444(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   10f0 <connect@plt>
cmp    $0x0,%rax
jge    131d <main+0x12d>
lea    0xd35(%rip),%rdi
call   10c0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    15b0 <main+0x3c0>
mov    0x2cbc(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    -0x28(%rbp),%rdx
call   1070 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    1352 <main+0x162>
lea    0xd08(%rip),%rdi
call   10c0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    15b0 <main+0x3c0>
mov    -0x28(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    0x2c59(%rip),%rax
mov    (%rax),%eax
mov    0x2c70(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    0x2c46(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    -0x28(%rbp),%rdx
call   1070 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    13a8 <main+0x1b8>
lea    0xcb9(%rip),%rdi
call   10c0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    15b0 <main+0x3c0>
jmp    13ad <main+0x1bd>
lea    -0x450(%rbp),%rdi
call   10a0 <FD_ZERO@plt>
mov    -0x444(%rbp),%edi
lea    -0x450(%rbp),%rsi
call   1100 <FD_SET@plt>
xor    %edi,%edi
lea    -0x450(%rbp),%rsi
call   1100 <FD_SET@plt>
movl   $0x1,-0x40(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x444(%rbp),%edi
add    $0x1,%edi
lea    -0x450(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rdx
mov    %r8,%rcx
call   10b0 <select@plt>
mov    %eax,-0x448(%rbp)
cmpl   $0xffffffff,-0x448(%rbp)
jne    1433 <main+0x243>
lea    0xc35(%rip),%rdi
call   10c0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    15b0 <main+0x3c0>
mov    -0x444(%rbp),%edi
lea    -0x450(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     14db <main+0x2eb>
mov    -0x444(%rbp),%edi
lea    -0x440(%rbp),%rsi
mov    $0x400,%edx
call   1080 <read@plt>
mov    %eax,-0x448(%rbp)
movl   $0x0,-0x44c(%rbp)
mov    -0x44c(%rbp),%eax
cmp    -0x448(%rbp),%eax
jge    14d6 <main+0x2e6>
movslq -0x44c(%rbp),%rax
movsbl -0x440(%rbp,%rax,1),%eax
ff
cmp    $0xd,%eax
je     14bd <main+0x2cd>
lea    -0x440(%rbp),%rsi
movslq -0x44c(%rbp),%rax
add    %rax,%rsi
mov    $0x1,%edx
mov    %edx,%edi
call   1030 <write@plt>
jmp    14c2 <main+0x2d2>
mov    -0x44c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44c(%rbp)
jmp    1476 <main+0x286>
jmp    14db <main+0x2eb>
xor    %edi,%edi
lea    -0x450(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1562 <main+0x372>
lea    -0x440(%rbp),%rsi
xor    %edi,%edi
mov    $0x400,%edx
call   1080 <read@plt>
mov    %eax,-0x448(%rbp)
movl   $0x0,-0x44c(%rbp)
mov    -0x44c(%rbp),%eax
cmp    -0x448(%rbp),%eax
jge    155d <main+0x36d>
jmp    152d <main+0x33d>
mov    -0x444(%rbp),%edi
lea    -0x440(%rbp),%rsi
mov    -0x448(%rbp),%edx
call   1030 <write@plt>
mov    %rax,%rcx
movslq -0x44c(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x44c(%rbp)
jmp    1516 <main+0x326>
jmp    1562 <main+0x372>
jmp    1567 <main+0x377>
cmpl   $0x0,-0x448(%rbp)
jg     13ad <main+0x1bd>
mov    0x2a45(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    -0x30(%rbp),%rdx
call   1070 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    15a9 <main+0x3b9>
lea    0xac6(%rip),%rdi
call   10c0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    15b0 <main+0x3c0>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x450,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x428,%rsp
mov    %rsi,%rbp
cmp    $0x3,%edi
jne    13e8 <main+0x1f8>
mov    0x2dd4(%rip),%rbx
mov    (%rbx),%edi
mov    0x2dab(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1110 <socket@plt>
test   %eax,%eax
js     1431 <main+0x241>
mov    %eax,%r13d
lea    0x10(%rsp),%r14
mov    %r14,%rdi
mov    $0xc,%esi
call   10d0 <bzero@plt>
mov    (%rbx),%eax
mov    %eax,0x18(%rsp)
mov    0x10(%rbp),%rdi
call   10e0 <atoi@plt>
mov    %eax,%edi
call   1050 <htons@plt>
mov    %eax,0x14(%rsp)
mov    (%rbx),%edi
mov    0x8(%rbp),%rsi
mov    %r14,%rdx
call   1090 <inet_pton@plt>
test   %rax,%rax
jle    141f <main+0x22f>
lea    0x10(%rsp),%rsi
mov    %r13d,%edi
mov    $0xc,%edx
call   10f0 <connect@plt>
test   %rax,%rax
js     143a <main+0x24a>
mov    0x2d4a(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
xor    %edi,%edi
call   1070 <ioctl@plt>
cmp    $0xffffffff,%eax
je     1443 <main+0x253>
mov    (%rsp),%eax
mov    %eax,0x8(%rsp)
mov    0x2cff(%rip),%rcx
mov    0x2d18(%rip),%rdx
mov    (%rdx),%edx
or     (%rcx),%edx
not    %edx
and    %eax,%edx
mov    %edx,(%rsp)
mov    0x2cee(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
xor    %edi,%edi
call   1070 <ioctl@plt>
cmp    $0xffffffff,%eax
je     144c <main+0x25c>
mov    %r13d,%r14d
add    $0x1,%r14d
lea    0x1c(%rsp),%r15
lea    0x20(%rsp),%r12
mov    %r14d,0xc(%rsp)
jmp    1308 <main+0x118>
nop
test   %ebp,%ebp
jle    13fa <main+0x20a>
mov    %r15,%rdi
call   10a0 <FD_ZERO@plt>
mov    %r13d,%edi
mov    %r15,%rsi
call   1100 <FD_SET@plt>
xor    %edi,%edi
mov    %r15,%rsi
call   1100 <FD_SET@plt>
mov    %r14d,%edi
mov    %r15,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   10b0 <select@plt>
cmp    $0xffffffff,%eax
je     1455 <main+0x265>
mov    %eax,%ebp
mov    %r13d,%edi
mov    %r15,%rsi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     13a5 <main+0x1b5>
mov    %r13d,%edi
mov    %r12,%rsi
mov    $0x400,%edx
call   1080 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
jle    13a5 <main+0x1b5>
mov    %ebp,%r14d
mov    %r12,%rbx
jmp    137a <main+0x18a>
add    $0x1,%rbx
add    $0xffffffffffffffff,%r14
je     13a0 <main+0x1b0>
cmpb   $0xd,(%rbx)
je     1370 <main+0x180>
mov    $0x1,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
jmp    1370 <main+0x180>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    0xc(%rsp),%r14d
xor    %edi,%edi
mov    %r15,%rsi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     1300 <main+0x110>
xor    %edi,%edi
mov    %r12,%rsi
mov    $0x400,%edx
call   1080 <read@plt>
test   %eax,%eax
jle    13fa <main+0x20a>
mov    %eax,%ebp
xor    %ebx,%ebx
nop
mov    %r13d,%edi
mov    %r12,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
add    %eax,%ebx
cmp    %ebp,%ebx
jl     13d0 <main+0x1e0>
jmp    1300 <main+0x110>
mov    0x0(%rbp),%rsi
lea    0xc0d(%rip),%rdi
call   1060 <printf@plt>
jmp    1461 <main+0x271>
mov    0x2bbf(%rip),%rax
mov    (%rax),%esi
xor    %ebx,%ebx
lea    0x8(%rsp),%rdx
xor    %edi,%edi
call   1070 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    1466 <main+0x276>
lea    0xc41(%rip),%rdi
jmp    145c <main+0x26c>
mov    0x8(%rbp),%rsi
lea    0xc00(%rip),%rdi
call   1060 <printf@plt>
jmp    1461 <main+0x271>
lea    0xbeb(%rip),%rdi
jmp    145c <main+0x26c>
lea    0xc00(%rip),%rdi
jmp    145c <main+0x26c>
lea    0xbff(%rip),%rdi
jmp    145c <main+0x26c>
lea    0xbfd(%rip),%rdi
jmp    145c <main+0x26c>
lea    0xbfb(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x428,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.termio = type { i32 }
%struct.timeval = type { i32, i64 }
@.str = [35 x i8] c"Usage: %s <IP-Address> <TCP-Port>\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [7 x i8] c"socket\00"
@.str.2 = [23 x i8] c"Not an IP address: %s\0A\00"
@.str.3 = [8 x i8] c"connect\00"
@TCGETA = global i32 0
@.str.4 = [7 x i8] c"ioctl1\00"
@ICANON = global i32 0
@ECHO = global i32 0
@TCSETAF = global i32 0
@.str.5 = [7 x i8] c"ioctl2\00"
@.str.6 = [7 x i8] c"select\00"
@.str.7 = [7 x i8] c"ioctl3\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.sockaddr_in
%7 = alloca %struct.termio
%8 = alloca %struct.termio
%9 = alloca %struct.timeval
%10 = alloca [1024 x i8]
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%15 = load i32, i32* %4
%16 = icmp ne i32 %15, 3
br i1 %16, label %17, label %22
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 0
%20 = load i8*, i8** %19
%21 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i8* %20)
store i32 1, i32* %3
br label %155
22: ; preds = %2
%23 = load i32, i32* @AF_INET
%24 = load i32, i32* @SOCK_STREAM
%25 = call i32 @socket(i32 %23, i32 %24, i32 0)
store i32 %25, i32* %11
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %22
%28 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %155
29: ; preds = %22
%30 = call i32 @bzero(%struct.sockaddr_in* %6, i32 12)
%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 i8**, i8*** %5
%34 = getelementptr inbounds i8*, i8** %33, i64 2
%35 = load i8*, i8** %34
%36 = call i32 @atoi(i8* %35)
%37 = call i32 @htons(i32 %36)
%38 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
store i32 %37, i32* %38
%39 = load i32, i32* @AF_INET
%40 = load i8**, i8*** %5
%41 = getelementptr inbounds i8*, i8** %40, i64 1
%42 = load i8*, i8** %41
%43 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
%44 = call i64 @inet_pton(i32 %39, i8* %42, i32* %43)
%45 = icmp sle i64 %44, 0
br i1 %45, label %46, label %51
46: ; preds = %29
%47 = load i8**, i8*** %5
%48 = getelementptr inbounds i8*, i8** %47, i64 1
%49 = load i8*, i8** %48
%50 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %49)
store i32 1, i32* %3
br label %155
51: ; preds = %29
%52 = load i32, i32* %11
%53 = call i64 @connect(i32 %52, %struct.sockaddr_in* %6, i32 12)
%54 = icmp slt i64 %53, 0
br i1 %54, label %55, label %57
55: ; preds = %51
%56 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %3
br label %155
57: ; preds = %51
%58 = load i32, i32* @TCGETA
%59 = call i32 @ioctl(i32 0, i32 %58, %struct.termio* %7)
%60 = icmp eq i32 %59, -1
br i1 %60, label %61, label %63
61: ; preds = %57
%62 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %3
br label %155
63: ; preds = %57
%64 = bitcast %struct.termio* %8 to i8*
%65 = bitcast %struct.termio* %7 to i8*
%66 = load i32, i32* @ICANON
%67 = load i32, i32* @ECHO
%68 = or i32 %66, %67
%69 = xor i32 %68, -1
%70 = getelementptr inbounds %struct.termio, %struct.termio* %7, i32 0, i32 0
%71 = load i32, i32* %70
%72 = and i32 %71, %69
store i32 %72, i32* %70
%73 = load i32, i32* @TCSETAF
%74 = call i32 @ioctl(i32 0, i32 %73, %struct.termio* %7)
%75 = icmp eq i32 %74, -1
br i1 %75, label %76, label %78
76: ; preds = %63
%77 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
store i32 1, i32* %3
br label %155
78: ; preds = %63
br label %79
79: ; preds = %145, %78
%80 = call i32 @FD_ZERO(i32* %14)
%81 = load i32, i32* %11
%82 = call i32 @FD_SET(i32 %81, i32* %14)
%83 = call i32 @FD_SET(i32 0, i32* %14)
%84 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 0
store i32 1, i32* %84
%85 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 1
store i64 0, i64* %85
%86 = load i32, i32* %11
%87 = add nsw i32 %86, 1
%88 = call i32 @select(i32 %87, i32* %14, i32* null, i32* null, i32* null)
store i32 %88, i32* %12
%89 = load i32, i32* %12
%90 = icmp eq i32 %89, -1
br i1 %90, label %91, label %93
91: ; preds = %79
%92 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0))
store i32 1, i32* %3
br label %155
93: ; preds = %79
%94 = load i32, i32* %11
%95 = call i64 @FD_ISSET(i32 %94, i32* %14)
%96 = icmp ne i64 %95, 0
br i1 %96, label %97, label %123
97: ; preds = %93
%98 = load i32, i32* %11
%99 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%100 = call i32 @read(i32 %98, i8* %99, i32 1024)
store i32 %100, i32* %12
store i32 0, i32* %13
br label %101
101: ; preds = %119, %97
%102 = load i32, i32* %13
%103 = load i32, i32* %12
%104 = icmp slt i32 %102, %103
br i1 %104, label %105, label %122
105: ; preds = %101
%106 = load i32, i32* %13
%107 = sext i32 %106 to i64
%108 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 %107
%109 = load i8, i8* %108
%110 = sext i8 %109 to i32
%111 = icmp ne i32 %110, 13
br i1 %111, label %112, label %118
112: ; preds = %105
%113 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%114 = load i32, i32* %13
%115 = sext i32 %114 to i64
%116 = getelementptr inbounds i8, i8* %113, i64 %115
%117 = call i64 @write(i32 1, i8* %116, i32 1)
br label %118
118: ; preds = %112, %105
br label %119
119: ; preds = %118
%120 = load i32, i32* %13
%121 = add nsw i32 %120, 1
store i32 %121, i32* %13
br label %101
122: ; preds = %101
br label %123
123: ; preds = %122, %93
%124 = call i64 @FD_ISSET(i32 0, i32* %14)
%125 = icmp ne i64 %124, 0
br i1 %125, label %126, label %144
126: ; preds = %123
%127 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%128 = call i32 @read(i32 0, i8* %127, i32 1024)
store i32 %128, i32* %12
store i32 0, i32* %13
br label %129
129: ; preds = %134, %126
%130 = load i32, i32* %13
%131 = load i32, i32* %12
%132 = icmp slt i32 %130, %131
br i1 %132, label %133, label %143
133: ; preds = %129
br label %134
134: ; preds = %133
%135 = load i32, i32* %11
%136 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%137 = load i32, i32* %12
%138 = call i64 @write(i32 %135, i8* %136, i32 %137)
%139 = load i32, i32* %13
%140 = sext i32 %139 to i64
%141 = add nsw i64 %140, %138
%142 = trunc i64 %141 to i32
store i32 %142, i32* %13
br label %129
143: ; preds = %129
br label %144
144: ; preds = %143, %123
br label %145
145: ; preds = %144
%146 = load i32, i32* %12
%147 = icmp sgt i32 %146, 0
br i1 %147, label %79, label %148
148: ; preds = %145
%149 = load i32, i32* @TCSETAF
%150 = call i32 @ioctl(i32 0, i32 %149, %struct.termio* %8)
%151 = icmp eq i32 %150, -1
br i1 %151, label %152, label %154
152: ; preds = %148
%153 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.7, i64 0, i64 0))
store i32 1, i32* %3
br label %155
154: ; preds = %148
store i32 0, i32* %3
br label %155
155: ; preds = %154, %152, %91, %76, %61, %55, %46, %27, %17
%156 = load i32, i32* %3
ret i32 %156
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.termio = type { i32 }
@.str = [35 x i8] c"Usage: %s <IP-Address> <TCP-Port>\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [7 x i8] c"socket\00"
@.str.2 = [23 x i8] c"Not an IP address: %s\0A\00"
@.str.3 = [8 x i8] c"connect\00"
@TCGETA = global i32 0
@.str.4 = [7 x i8] c"ioctl1\00"
@ICANON = global i32 0
@ECHO = global i32 0
@TCSETAF = global i32 0
@.str.5 = [7 x i8] c"ioctl2\00"
@.str.6 = [7 x i8] c"select\00"
@.str.7 = [7 x i8] c"ioctl3\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca %struct.termio
%5 = alloca %struct.termio
%6 = alloca [1024 x i8]
%7 = alloca i32
%8 = bitcast %struct.sockaddr_in* %3 to i8*
%9 = bitcast %struct.termio* %4 to i8*
%10 = bitcast %struct.termio* %5 to i8*
%11 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%12 = bitcast i32* %7 to i8*
%13 = icmp eq i32 %0, 3
br i1 %13, label %17, label %14
14: ; preds = %2
%15 = load i8*, i8** %1
%16 = tail call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i8* %15)
br label %117
17: ; preds = %2
%18 = load i32, i32* @AF_INET
%19 = load i32, i32* @SOCK_STREAM
%20 = tail call i32 @socket(i32 %18, i32 %19, i32 0)
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %17
%23 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %117
24: ; preds = %17
%25 = call i32 @bzero(%struct.sockaddr_in* %3, i32 12)
%26 = load i32, i32* @AF_INET
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %26, i32* %27
%28 = getelementptr inbounds i8*, i8** %1, i64 2
%29 = load i8*, i8** %28
%30 = tail call i32 @atoi(i8* %29)
%31 = tail call i32 @htons(i32 %30)
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %31, i32* %32
%33 = load i32, i32* @AF_INET
%34 = getelementptr inbounds i8*, i8** %1, i64 1
%35 = load i8*, i8** %34
%36 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
%37 = call i64 @inet_pton(i32 %33, i8* %35, i32* %36)
%38 = icmp slt i64 %37, 1
br i1 %38, label %39, label %42
39: ; preds = %24
%40 = load i8*, i8** %34
%41 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %40)
br label %117
42: ; preds = %24
%43 = call i64 @connect(i32 %20, %struct.sockaddr_in* %3, i32 12)
%44 = icmp slt i64 %43, 0
br i1 %44, label %45, label %47
45: ; preds = %42
%46 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
br label %117
47: ; preds = %42
%48 = load i32, i32* @TCGETA
%49 = call i32 @ioctl(i32 0, i32 %48, %struct.termio* %4)
%50 = icmp eq i32 %49, -1
br i1 %50, label %51, label %53
51: ; preds = %47
%52 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
br label %117
53: ; preds = %47
%54 = getelementptr inbounds %struct.termio, %struct.termio* %4, i64 0, i32 0
%55 = getelementptr inbounds %struct.termio, %struct.termio* %5, i64 0, i32 0
%56 = load i32, i32* %54
store i32 %56, i32* %55
%57 = load i32, i32* @ICANON
%58 = load i32, i32* @ECHO
%59 = or i32 %58, %57
%60 = xor i32 %59, -1
%61 = and i32 %56, %60
store i32 %61, i32* %54
%62 = load i32, i32* @TCSETAF
%63 = call i32 @ioctl(i32 0, i32 %62, %struct.termio* %4)
%64 = icmp eq i32 %63, -1
br i1 %64, label %67, label %65
65: ; preds = %53
%66 = add nuw nsw i32 %20, 1
br label %69
67: ; preds = %53
%68 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
br label %117
69: ; preds = %65, %108
%70 = call i32 @FD_ZERO(i32* %7)
%71 = call i32 @FD_SET(i32 %20, i32* %7)
%72 = call i32 @FD_SET(i32 0, i32* %7)
%73 = call i32 @select(i32 %66, i32* %7, i32* null, i32* null, i32* null)
%74 = icmp eq i32 %73, -1
br i1 %74, label %75, label %77
75: ; preds = %69
%76 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0))
br label %117
77: ; preds = %69
%78 = call i64 @FD_ISSET(i32 %20, i32* %7)
%79 = icmp eq i64 %78, 0
br i1 %79, label %95, label %80
80: ; preds = %77
%81 = call i32 @read(i32 %20, i8* %11, i32 1024)
%82 = icmp sgt i32 %81, 0
br i1 %82, label %83, label %95
83: ; preds = %80
%84 = zext i32 %81 to i64
br label %85
85: ; preds = %83, %92
%86 = phi i64 [ 0, %83 ], [ %93, %92 ]
%87 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 %86
%88 = load i8, i8* %87
%89 = icmp eq i8 %88, 13
br i1 %89, label %92, label %90
90: ; preds = %85
%91 = call i64 @write(i32 1, i8* %87, i32 1)
br label %92
92: ; preds = %85, %90
%93 = add nuw nsw i64 %86, 1
%94 = icmp eq i64 %93, %84
br i1 %94, label %95, label %85
95: ; preds = %92, %80, %77
%96 = phi i32 [ %73, %77 ], [ %81, %80 ], [ %81, %92 ]
%97 = call i64 @FD_ISSET(i32 0, i32* %7)
%98 = icmp eq i64 %97, 0
br i1 %98, label %108, label %99
99: ; preds = %95
%100 = call i32 @read(i32 0, i8* %11, i32 1024)
%101 = icmp sgt i32 %100, 0
br i1 %101, label %102, label %111
102: ; preds = %99, %102
%103 = phi i32 [ %106, %102 ], [ 0, %99 ]
%104 = call i64 @write(i32 %20, i8* %11, i32 %100)
%105 = trunc i64 %104 to i32
%106 = add i32 %103, %105
%107 = icmp slt i32 %106, %100
br i1 %107, label %102, label %108
108: ; preds = %102, %95
%109 = phi i32 [ %96, %95 ], [ %100, %102 ]
%110 = icmp sgt i32 %109, 0
br i1 %110, label %69, label %111
111: ; preds = %99, %108
%112 = load i32, i32* @TCSETAF
%113 = call i32 @ioctl(i32 0, i32 %112, %struct.termio* %5)
%114 = icmp eq i32 %113, -1
br i1 %114, label %115, label %117
115: ; preds = %111
%116 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.7, i64 0, i64 0))
br label %117
117: ; preds = %111, %115, %75, %67, %51, %45, %39, %22, %14
%118 = phi i32 [ 1, %14 ], [ 1, %22 ], [ 1, %39 ], [ 1, %45 ], [ 1, %51 ], [ 1, %67 ], [ 1, %75 ], [ 1, %115 ], [ 0, %111 ]
ret i32 %118
}