#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 addrinfo {
  int /*<<< orphan*/ ai_addrlen;
  int /*<<< orphan*/ ai_addr;
  int /*<<< orphan*/ ai_protocol;
  int /*<<< orphan*/ ai_socktype;
  int /*<<< orphan*/ ai_family;
  struct addrinfo *ai_next;
  int /*<<< orphan*/ ai_flags;
};
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
int /*<<< orphan*/ AF_UNSPEC;
int /*<<< orphan*/ AI_PASSIVE;
int EXIT_FAILURE;
int EXIT_SUCCESS;
scalar_t__ FD_ISSET(int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int connect(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
scalar_t__ getaddrinfo(int /*<<< orphan*/ *, char *, struct addrinfo *,
                       struct addrinfo **);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int read(int /*<<< orphan*/, char *, int);
int recv(int, char *, int, int /*<<< orphan*/);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int send(int, char *, int, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int main(void) {
  int server;
  struct addrinfo hints;
  memset(&hints, 0, sizeof hints);
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_flags = AI_PASSIVE;
  struct addrinfo *servinfo;
  if (getaddrinfo(NULL, "42000", &hints, &servinfo) != 0) {
    perror("getaddrinfo");
    exit(EXIT_FAILURE);
  }
  struct addrinfo *p;
  for (p = servinfo; p != NULL; p = p->ai_next) {
    server = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
    if (server == -1) {
      perror("socket");
      continue;
    }
    if (connect(server, p->ai_addr, p->ai_addrlen) == -1) {
      close(server);
      perror("connect");
      continue;
    }
    break;
  }
  if (p == NULL) {
    fprintf(stderr, "failed to bind\n");
    exit(EXIT_FAILURE);
  }
  freeaddrinfo(servinfo);
  fd_set pool_fds;
  fd_set read_fds;
  FD_ZERO(&pool_fds);
  FD_ZERO(&read_fds);
  FD_SET(server, &pool_fds);
  FD_SET(0, &pool_fds);
  int max_descriptor = server;
  while (1) {
    read_fds = pool_fds;
    if (select(max_descriptor + 1, &read_fds, NULL, NULL, NULL) == -1) {
      perror("select");
      exit(EXIT_FAILURE);
    }
    if (FD_ISSET(0, &read_fds)) {
      char buffer[1024];
      int n = read(0, buffer, 1024);
      buffer[n] = '\0';
      if (send(server, buffer, n + 1, 0) == -1) {
        perror("send");
        return EXIT_FAILURE;
      }
    } else {
      char buffer[1024];
      int n = recv(server, buffer, sizeof buffer, 0);
      if (n == 0) {
        fprintf(stdout, "socket disconnected\n");
        close(server);
        return EXIT_FAILURE;
      }
      if (n < 0) {
        perror("recv");
        close(server);
        return EXIT_FAILURE;
      }
      fprintf(stdout, buffer);
    }
  }
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x870,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1060 <memset@plt>
mov    0x2db7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2d93(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2da7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
xor    %eax,%eax
mov    %eax,%edi
lea    0xdaf(%rip),%rsi
lea    -0x30(%rbp),%rdx
lea    -0x38(%rbp),%rcx
call   10f0 <getaddrinfo@plt>
cmp    $0x0,%rax
je     1282 <main+0x82>
lea    0xd97(%rip),%rdi
call   10c0 <perror@plt>
mov    0x2d45(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
je     131a <main+0x11a>
mov    -0x40(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x40(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x40(%rbp),%rax
mov    0x8(%rax),%edx
call   1120 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    12cd <main+0xcd>
lea    0xd4f(%rip),%rdi
call   10c0 <perror@plt>
jmp    1309 <main+0x109>
mov    -0x8(%rbp),%edi
mov    -0x40(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x40(%rbp),%rax
mov    (%rax),%edx
call   10e0 <connect@plt>
cmp    $0xffffffff,%eax
jne    1304 <main+0x104>
mov    -0x8(%rbp),%edi
call   1070 <close@plt>
lea    0xd1f(%rip),%rdi
call   10c0 <perror@plt>
jmp    1309 <main+0x109>
jmp    131a <main+0x11a>
mov    -0x40(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x40(%rbp)
jmp    128a <main+0x8a>
cmpq   $0x0,-0x40(%rbp)
jne    1348 <main+0x148>
mov    0x2ca4(%rip),%rax
mov    (%rax),%edi
lea    0xcec(%rip),%rsi
call   1090 <fprintf@plt>
mov    0x2c7f(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
mov    -0x38(%rbp),%rdi
call   1110 <freeaddrinfo@plt>
lea    -0x44(%rbp),%rdi
call   10a0 <FD_ZERO@plt>
lea    -0x48(%rbp),%rdi
call   10a0 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x44(%rbp),%rsi
call   1100 <FD_SET@plt>
xor    %edi,%edi
lea    -0x44(%rbp),%rsi
call   1100 <FD_SET@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x44(%rbp),%eax
mov    %eax,-0x48(%rbp)
mov    -0x4c(%rbp),%edi
add    $0x1,%edi
lea    -0x48(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rdx
mov    %r8,%rcx
call   10b0 <select@plt>
cmp    $0xffffffff,%eax
jne    13c3 <main+0x1c3>
lea    0xc81(%rip),%rdi
call   10c0 <perror@plt>
mov    0x2c04(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
xor    %edi,%edi
lea    -0x48(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1445 <main+0x245>
lea    -0x450(%rbp),%rsi
xor    %edi,%edi
mov    $0x400,%edx
call   1080 <read@plt>
mov    %eax,-0x454(%rbp)
movslq -0x454(%rbp),%rax
movb   $0x0,-0x450(%rbp,%rax,1)
mov    -0x8(%rbp),%edi
lea    -0x450(%rbp),%rsi
mov    -0x454(%rbp),%edx
add    $0x1,%edx
xor    %ecx,%ecx
call   1050 <send@plt>
cmp    $0xffffffff,%eax
jne    1440 <main+0x240>
lea    0xc0e(%rip),%rdi
call   10c0 <perror@plt>
mov    0x2b8a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    14e8 <main+0x2e8>
jmp    14e3 <main+0x2e3>
mov    -0x8(%rbp),%edi
lea    -0x860(%rbp),%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x864(%rbp)
cmpl   $0x0,-0x864(%rbp)
jne    149c <main+0x29c>
mov    0x2b43(%rip),%rax
mov    (%rax),%edi
lea    0xbbf(%rip),%rsi
call   1090 <fprintf@plt>
mov    -0x8(%rbp),%edi
call   1070 <close@plt>
mov    0x2b2e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    14e8 <main+0x2e8>
cmpl   $0x0,-0x864(%rbp)
jge    14ce <main+0x2ce>
lea    0xba2(%rip),%rdi
call   10c0 <perror@plt>
mov    -0x8(%rbp),%edi
call   1070 <close@plt>
mov    0x2afc(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    14e8 <main+0x2e8>
mov    0x2ae3(%rip),%rax
mov    (%rax),%edi
lea    -0x860(%rbp),%rsi
call   1090 <fprintf@plt>
jmp    1380 <main+0x180>
mov    -0x4(%rbp),%eax
add    $0x870,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x440,%rsp
lea    0x18(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1060 <memset@plt>
mov    0x2db6(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x28(%rsp)
mov    0x2d91(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x24(%rsp)
mov    0x2da4(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x38(%rsp)
lea    0xdaf(%rip),%rsi
lea    0x10(%rsp),%rcx
xor    %edi,%edi
mov    %rbx,%rdx
call   10f0 <getaddrinfo@plt>
test   %rax,%rax
jne    144f <main+0x24f>
mov    0x10(%rsp),%rbx
test   %rbx,%rbx
je     12c4 <main+0xc4>
lea    0xd98(%rip),%rbp
lea    0xd98(%rip),%r15
mov    0x10(%rbx),%edi
mov    0x8(%rbx),%edx
mov    0xc(%rbx),%esi
call   1120 <socket@plt>
mov    %rbp,%rdi
cmp    $0xffffffff,%eax
je     12b6 <main+0xb6>
mov    %eax,%r14d
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
mov    %eax,%edi
call   10e0 <connect@plt>
cmp    $0xffffffff,%eax
jne    12de <main+0xde>
mov    %r14d,%edi
call   1070 <close@plt>
mov    %r15,%rdi
call   10c0 <perror@plt>
mov    0x18(%rbx),%rbx
test   %rbx,%rbx
jne    1281 <main+0x81>
mov    0x2d05(%rip),%rax
mov    (%rax),%edi
lea    0xd4d(%rip),%rsi
call   1090 <fprintf@plt>
jmp    1441 <main+0x241>
mov    0x10(%rsp),%rdi
call   1110 <freeaddrinfo@plt>
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
call   10a0 <FD_ZERO@plt>
lea    0x8(%rsp),%rbp
mov    %rbp,%rdi
call   10a0 <FD_ZERO@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
call   1100 <FD_SET@plt>
xor    %edi,%edi
mov    %rbx,%rsi
call   1100 <FD_SET@plt>
mov    %r14d,%r15d
add    $0x1,%r15d
mov    0xc(%rsp),%eax
mov    %eax,0x8(%rsp)
mov    %r15d,%edi
mov    %rbp,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   10b0 <select@plt>
cmp    $0xffffffff,%eax
je     1435 <main+0x235>
lea    0x8(%rsp),%rbx
lea    0x40(%rsp),%rbp
mov    0x2c66(%rip),%r12
jmp    13b3 <main+0x1b3>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
xor    %edi,%edi
mov    %rbp,%rsi
mov    $0x400,%edx
call   1080 <read@plt>
movslq %eax,%rdx
movb   $0x0,0x40(%rsp,%rdx,1)
add    $0x1,%edx
mov    %r14d,%edi
mov    %rbp,%rsi
xor    %ecx,%ecx
call   1050 <send@plt>
cmp    $0xffffffff,%eax
je     1419 <main+0x219>
mov    0xc(%rsp),%eax
mov    %eax,0x8(%rsp)
mov    %r15d,%edi
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   10b0 <select@plt>
cmp    $0xffffffff,%eax
je     1435 <main+0x235>
xor    %edi,%edi
mov    %rbx,%rsi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
jne    1360 <main+0x160>
mov    %r14d,%edi
mov    %rbp,%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
je     13e8 <main+0x1e8>
js     1427 <main+0x227>
mov    (%r12),%edi
mov    %rbp,%rsi
call   1090 <fprintf@plt>
jmp    1390 <main+0x190>
mov    (%r12),%edi
lea    0xc4a(%rip),%rsi
call   1090 <fprintf@plt>
mov    %r14d,%edi
call   1070 <close@plt>
mov    0x2bb9(%rip),%rax
mov    (%rax),%eax
add    $0x440,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xc18(%rip),%rdi
call   10c0 <perror@plt>
jmp    1400 <main+0x200>
lea    0xc24(%rip),%rdi
call   10c0 <perror@plt>
jmp    13f8 <main+0x1f8>
lea    0xbf5(%rip),%rdi
call   10c0 <perror@plt>
mov    0x2b78(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
lea    0xbb0(%rip),%rdi
jmp    143c <main+0x23c>

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i32, i32, i32, %struct.addrinfo*, i32 }
@AF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@AI_PASSIVE = global i32 0
@.str = [6 x i8] c"42000\00"
@.str.1 = [12 x i8] c"getaddrinfo\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [7 x i8] c"socket\00"
@.str.3 = [8 x i8] c"connect\00"
@stderr = global i32 0
@.str.4 = [16 x i8] c"failed to bind\0A\00"
@.str.5 = [7 x i8] c"select\00"
@.str.6 = [5 x i8] c"send\00"
@stdout = global i32 0
@.str.7 = [21 x i8] c"socket disconnected\0A\00"
@.str.8 = [5 x i8] c"recv\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.addrinfo
%4 = alloca %struct.addrinfo*
%5 = alloca %struct.addrinfo*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca [1024 x i8]
%10 = alloca i32
%11 = alloca [1024 x i8]
%12 = alloca i32
store i32 0, i32* %1
%13 = call i32 @memset(%struct.addrinfo* %3, i32 0, i32 40)
%14 = load i32, i32* @AF_UNSPEC
%15 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i32 0, i32 4
store i32 %14, i32* %15
%16 = load i32, i32* @SOCK_STREAM
%17 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i32 0, i32 3
store i32 %16, i32* %17
%18 = load i32, i32* @AI_PASSIVE
%19 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i32 0, i32 6
store i32 %18, i32* %19
%20 = call i64 @getaddrinfo(i32* null, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), %struct.addrinfo* %3, %struct.addrinfo** %4)
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %26
22: ; preds = %0
%23 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%24 = load i32, i32* @EXIT_FAILURE
%25 = call i32 @exit(i32 %24)
unreachable
26: ; preds = %0
%27 = load %struct.addrinfo*, %struct.addrinfo** %4
store %struct.addrinfo* %27, %struct.addrinfo** %5
br label %28
28: ; preds = %61, %26
%29 = load %struct.addrinfo*, %struct.addrinfo** %5
%30 = icmp ne %struct.addrinfo* %29, null
br i1 %30, label %31, label %65
31: ; preds = %28
%32 = load %struct.addrinfo*, %struct.addrinfo** %5
%33 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %32, i32 0, i32 4
%34 = load i32, i32* %33
%35 = load %struct.addrinfo*, %struct.addrinfo** %5
%36 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %35, i32 0, i32 3
%37 = load i32, i32* %36
%38 = load %struct.addrinfo*, %struct.addrinfo** %5
%39 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %38, i32 0, i32 2
%40 = load i32, i32* %39
%41 = call i32 @socket(i32 %34, i32 %37, i32 %40)
store i32 %41, i32* %2
%42 = load i32, i32* %2
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %46
44: ; preds = %31
%45 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %61
46: ; preds = %31
%47 = load i32, i32* %2
%48 = load %struct.addrinfo*, %struct.addrinfo** %5
%49 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %48, i32 0, i32 1
%50 = load i32, i32* %49
%51 = load %struct.addrinfo*, %struct.addrinfo** %5
%52 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %51, i32 0, i32 0
%53 = load i32, i32* %52
%54 = call i32 @connect(i32 %47, i32 %50, i32 %53)
%55 = icmp eq i32 %54, -1
br i1 %55, label %56, label %60
56: ; preds = %46
%57 = load i32, i32* %2
%58 = call i32 @close(i32 %57)
%59 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
br label %61
60: ; preds = %46
br label %65
61: ; preds = %56, %44
%62 = load %struct.addrinfo*, %struct.addrinfo** %5
%63 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %62, i32 0, i32 5
%64 = load %struct.addrinfo*, %struct.addrinfo** %63
store %struct.addrinfo* %64, %struct.addrinfo** %5
br label %28
65: ; preds = %60, %28
%66 = load %struct.addrinfo*, %struct.addrinfo** %5
%67 = icmp eq %struct.addrinfo* %66, null
br i1 %67, label %68, label %73
68: ; preds = %65
%69 = load i32, i32* @stderr
%70 = call i32 @fprintf(i32 %69, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
%71 = load i32, i32* @EXIT_FAILURE
%72 = call i32 @exit(i32 %71)
unreachable
73: ; preds = %65
%74 = load %struct.addrinfo*, %struct.addrinfo** %4
%75 = call i32 @freeaddrinfo(%struct.addrinfo* %74)
%76 = call i32 @FD_ZERO(i32* %6)
%77 = call i32 @FD_ZERO(i32* %7)
%78 = load i32, i32* %2
%79 = call i32 @FD_SET(i32 %78, i32* %6)
%80 = call i32 @FD_SET(i32 0, i32* %6)
%81 = load i32, i32* %2
store i32 %81, i32* %8
br label %82
82: ; preds = %73, %135
%83 = load i32, i32* %6
store i32 %83, i32* %7
%84 = load i32, i32* %8
%85 = add nsw i32 %84, 1
%86 = call i32 @select(i32 %85, i32* %7, i32* null, i32* null, i32* null)
%87 = icmp eq i32 %86, -1
br i1 %87, label %88, label %92
88: ; preds = %82
%89 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
%90 = load i32, i32* @EXIT_FAILURE
%91 = call i32 @exit(i32 %90)
unreachable
92: ; preds = %82
%93 = call i64 @FD_ISSET(i32 0, i32* %7)
%94 = icmp ne i64 %93, 0
br i1 %94, label %95, label %111
95: ; preds = %92
%96 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%97 = call i32 @read(i32 0, i8* %96, i32 1024)
store i32 %97, i32* %10
%98 = load i32, i32* %10
%99 = sext i32 %98 to i64
%100 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 %99
store i8 0, i8* %100
%101 = load i32, i32* %2
%102 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%103 = load i32, i32* %10
%104 = add nsw i32 %103, 1
%105 = call i32 @send(i32 %101, i8* %102, i32 %104, i32 0)
%106 = icmp eq i32 %105, -1
br i1 %106, label %107, label %110
107: ; preds = %95
%108 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0))
%109 = load i32, i32* @EXIT_FAILURE
store i32 %109, i32* %1
br label %136
110: ; preds = %95
br label %135
111: ; preds = %92
%112 = load i32, i32* %2
%113 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%114 = call i32 @recv(i32 %112, i8* %113, i32 1024, i32 0)
store i32 %114, i32* %12
%115 = load i32, i32* %12
%116 = icmp eq i32 %115, 0
br i1 %116, label %117, label %123
117: ; preds = %111
%118 = load i32, i32* @stdout
%119 = call i32 @fprintf(i32 %118, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.7, i64 0, i64 0))
%120 = load i32, i32* %2
%121 = call i32 @close(i32 %120)
%122 = load i32, i32* @EXIT_FAILURE
store i32 %122, i32* %1
br label %136
123: ; preds = %111
%124 = load i32, i32* %12
%125 = icmp slt i32 %124, 0
br i1 %125, label %126, label %131
126: ; preds = %123
%127 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.8, i64 0, i64 0))
%128 = load i32, i32* %2
%129 = call i32 @close(i32 %128)
%130 = load i32, i32* @EXIT_FAILURE
store i32 %130, i32* %1
br label %136
131: ; preds = %123
%132 = load i32, i32* @stdout
%133 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%134 = call i32 @fprintf(i32 %132, i8* %133)
br label %135
135: ; preds = %131, %110
br label %82
136: ; preds = %126, %117, %107
%137 = load i32, i32* %1
ret i32 %137
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32, i32, %struct.addrinfo*, i32 }
@AF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@AI_PASSIVE = global i32 0
@.str = [6 x i8] c"42000\00"
@.str.1 = [12 x i8] c"getaddrinfo\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [7 x i8] c"socket\00"
@.str.3 = [8 x i8] c"connect\00"
@stderr = global i32 0
@.str.4 = [16 x i8] c"failed to bind\0A\00"
@.str.5 = [7 x i8] c"select\00"
@.str.6 = [5 x i8] c"send\00"
@stdout = global i32 0
@.str.7 = [21 x i8] c"socket disconnected\0A\00"
@.str.8 = [5 x i8] c"recv\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca %struct.addrinfo
%2 = alloca %struct.addrinfo*
%3 = alloca i32
%4 = alloca i32
%5 = alloca [1024 x i8]
%6 = alloca [1024 x i8]
%7 = bitcast %struct.addrinfo* %1 to i8*
%8 = call i32 @memset(%struct.addrinfo* %1, i32 0, i32 40)
%9 = load i32, i32* @AF_UNSPEC
%10 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %1, i64 0, i32 4
store i32 %9, i32* %10
%11 = load i32, i32* @SOCK_STREAM
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %1, i64 0, i32 3
store i32 %11, i32* %12
%13 = load i32, i32* @AI_PASSIVE
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %1, i64 0, i32 6
store i32 %13, i32* %14
%15 = bitcast %struct.addrinfo** %2 to i8*
%16 = call i64 @getaddrinfo(i32* null, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), %struct.addrinfo* %1, %struct.addrinfo** %2)
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %0
%19 = load %struct.addrinfo*, %struct.addrinfo** %2
%20 = icmp eq %struct.addrinfo* %19, null
br i1 %20, label %50, label %25
21: ; preds = %0
%22 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%23 = load i32, i32* @EXIT_FAILURE
%24 = call i32 @exit(i32 %23)
unreachable
25: ; preds = %18, %44
%26 = phi %struct.addrinfo* [ %48, %44 ], [ %19, %18 ]
%27 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %26, i64 0, i32 4
%28 = load i32, i32* %27
%29 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %26, i64 0, i32 3
%30 = load i32, i32* %29
%31 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %26, i64 0, i32 2
%32 = load i32, i32* %31
%33 = call i32 @socket(i32 %28, i32 %30, i32 %32)
%34 = icmp eq i32 %33, -1
br i1 %34, label %44, label %35
35: ; preds = %25
%36 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %26, i64 0, i32 1
%37 = load i32, i32* %36
%38 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %26, i64 0, i32 0
%39 = load i32, i32* %38
%40 = call i32 @connect(i32 %33, i32 %37, i32 %39)
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %55
42: ; preds = %35
%43 = call i32 @close(i32 %33)
br label %44
44: ; preds = %25, %42
%45 = phi i8* [ getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), %42 ], [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), %25 ]
%46 = call i32 @perror(i8* %45)
%47 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %26, i64 0, i32 5
%48 = load %struct.addrinfo*, %struct.addrinfo** %47
%49 = icmp eq %struct.addrinfo* %48, null
br i1 %49, label %50, label %25
50: ; preds = %44, %18
%51 = load i32, i32* @stderr
%52 = call i32 @fprintf(i32 %51, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
%53 = load i32, i32* @EXIT_FAILURE
%54 = call i32 @exit(i32 %53)
unreachable
55: ; preds = %35
%56 = load %struct.addrinfo*, %struct.addrinfo** %2
%57 = call i32 @freeaddrinfo(%struct.addrinfo* %56)
%58 = bitcast i32* %3 to i8*
%59 = bitcast i32* %4 to i8*
%60 = call i32 @FD_ZERO(i32* %3)
%61 = call i32 @FD_ZERO(i32* %4)
%62 = call i32 @FD_SET(i32 %33, i32* %3)
%63 = call i32 @FD_SET(i32 0, i32* %3)
%64 = add nuw nsw i32 %33, 1
%65 = load i32, i32* %3
store i32 %65, i32* %4
%66 = call i32 @select(i32 %64, i32* %4, i32* null, i32* null, i32* null)
%67 = icmp eq i32 %66, -1
br i1 %67, label %71, label %68
68: ; preds = %55
%69 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%70 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
br label %75
71: ; preds = %105, %55
%72 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
%73 = load i32, i32* @EXIT_FAILURE
%74 = call i32 @exit(i32 %73)
unreachable
75: ; preds = %68, %105
%76 = call i64 @FD_ISSET(i32 0, i32* %4)
%77 = icmp eq i64 %76, 0
br i1 %77, label %89, label %78
78: ; preds = %75
%79 = call i32 @read(i32 0, i8* %69, i32 1024)
%80 = sext i32 %79 to i64
%81 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 %80
store i8 0, i8* %81
%82 = add nsw i32 %79, 1
%83 = call i32 @send(i32 %33, i8* %69, i32 %82, i32 0)
%84 = icmp eq i32 %83, -1
br i1 %84, label %85, label %88
85: ; preds = %78
%86 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0))
%87 = load i32, i32* @EXIT_FAILURE
br label %109
88: ; preds = %78
br label %105
89: ; preds = %75
%90 = call i32 @recv(i32 %33, i8* %70, i32 1024, i32 0)
%91 = icmp eq i32 %90, 0
br i1 %91, label %92, label %95
92: ; preds = %89
%93 = load i32, i32* @stdout
%94 = call i32 @fprintf(i32 %93, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.7, i64 0, i64 0))
br label %99
95: ; preds = %89
%96 = icmp slt i32 %90, 0
br i1 %96, label %97, label %102
97: ; preds = %95
%98 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.8, i64 0, i64 0))
br label %99
99: ; preds = %97, %92
%100 = call i32 @close(i32 %33)
%101 = load i32, i32* @EXIT_FAILURE
br label %109
102: ; preds = %95
%103 = load i32, i32* @stdout
%104 = call i32 @fprintf(i32 %103, i8* %70)
br label %105
105: ; preds = %102, %88
%106 = load i32, i32* %3
store i32 %106, i32* %4
%107 = call i32 @select(i32 %64, i32* %4, i32* null, i32* null, i32* null)
%108 = icmp eq i32 %107, -1
br i1 %108, label %71, label %75
109: ; preds = %99, %85
%110 = phi i32 [ %87, %85 ], [ %101, %99 ]
ret i32 %110
}