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

/* Variables and functions */
int /*<<< orphan*/ AF_UNSPEC;
int /*<<< orphan*/ EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int /*<<< orphan*/);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, ...);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
char *gai_strerror(int);
int getaddrinfo(char *, char *, struct addrinfo *, struct addrinfo **);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int write(int, char *, int);
int main(int argc, char *argv[]) {
  int sock;
  struct addrinfo hints;
  struct addrinfo *results, *rp;
  int count, retval, success;
  char buffer[1024];
  if (argc < 3) {
    printf("Usage: %s <server-name> <service-name/port>\n", argv[0]);
    exit(EXIT_FAILURE);
  }
  memset(&hints, 0, sizeof(struct addrinfo));
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  if ((retval = getaddrinfo(argv[1], argv[2], &hints, &results)) != 0) {
    fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(retval));
    exit(EXIT_FAILURE);
  }
  success = 0;
  rp = results;
  while (rp != NULL) {
    if ((sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol)) !=
        -1) {
      if (connect(sock, (struct sockaddr *)rp->ai_addr, rp->ai_addrlen) != -1) {
        printf("Successfully connected to %s:%s\n", argv[1], argv[2]);
        success = 1;
        rp = NULL;
      } else {
        printf("Unable to connect, move to the next\n");
        rp = rp->ai_next;
        close(sock);
      }
    } else {
      printf("Unable to create socket, move to the next\n");
      rp = rp->ai_next;
    }
  }
  if (!success) {
    fprintf(stderr, "Unable to connect to server %s:%s\n", argv[1], argv[2]);
    exit(EXIT_FAILURE);
  }
  freeaddrinfo(results);
  while (1) {
    if ((count = read(0, buffer, 1024)) == -1) {
      do {
        perror("read");
        exit(EXIT_FAILURE);
      } while (0);
    }
    if (write(sock, buffer, count) == -1) {
      do {
        perror("write");
        exit(EXIT_FAILURE);
      } while (0);
    }
    if ((count = read(sock, buffer, count)) == -1) {
      do {
        perror("read");
        exit(EXIT_FAILURE);
      } while (0);
    }
    if (write(1, buffer, count) == -1) {
      do {
        perror("write");
        exit(EXIT_FAILURE);
      } while (0);
    }
  }
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x470,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    1216 <main+0x46>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xdff(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2db9(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1060 <memset@plt>
mov    0x2dbb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2d97(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
lea    -0x40(%rbp),%rdx
lea    -0x48(%rbp),%rcx
call   10d0 <getaddrinfo@plt>
mov    %eax,-0x58(%rbp)
cmp    $0x0,%eax
je     12a3 <main+0xd3>
mov    0x2d6a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x464(%rbp)
mov    -0x58(%rbp),%edi
call   1050 <gai_strerror@plt>
mov    -0x464(%rbp),%edi
mov    %rax,%rdx
lea    0xd9f(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    0x2d2c(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
movl   $0x0,-0x5c(%rbp)
mov    -0x48(%rbp),%rax
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
je     1379 <main+0x1a9>
mov    -0x50(%rbp),%rax
mov    0x20(%rax),%edi
mov    -0x50(%rbp),%rax
mov    0x1c(%rax),%esi
mov    -0x50(%rbp),%rax
mov    0x18(%rax),%edx
call   10f0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
je     135b <main+0x18b>
mov    -0x14(%rbp),%edi
mov    -0x50(%rbp),%rax
mov    0x10(%rax),%rsi
mov    -0x50(%rbp),%rax
mov    0x8(%rax),%edx
call   10c0 <connect@plt>
cmp    $0xffffffff,%eax
je     1335 <main+0x165>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdx
lea    0xd24(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x5c(%rbp)
movq   $0x0,-0x50(%rbp)
jmp    1356 <main+0x186>
lea    0xd23(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x50(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x50(%rbp)
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
jmp    1374 <main+0x1a4>
lea    0xd22(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x50(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x50(%rbp)
jmp    12b2 <main+0xe2>
cmpl   $0x0,-0x5c(%rbp)
jne    13b8 <main+0x1e8>
mov    0x2c4e(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rcx
lea    0xd0c(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    0x2c17(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x48(%rbp),%rdi
call   10e0 <freeaddrinfo@plt>
lea    -0x460(%rbp),%rsi
xor    %edi,%edi
mov    $0x400,%edx
call   1080 <read@plt>
mov    %eax,-0x54(%rbp)
cmp    $0xffffffff,%eax
jne    13ff <main+0x22f>
jmp    13e5 <main+0x215>
lea    0xce6(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2bd0(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x14(%rbp),%edi
lea    -0x460(%rbp),%rsi
mov    -0x54(%rbp),%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    1439 <main+0x269>
jmp    141f <main+0x24f>
lea    0xcb1(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2b96(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x14(%rbp),%edi
lea    -0x460(%rbp),%rsi
mov    -0x54(%rbp),%edx
call   1080 <read@plt>
mov    %eax,-0x54(%rbp)
cmp    $0xffffffff,%eax
jne    1476 <main+0x2a6>
jmp    145c <main+0x28c>
lea    0xc6f(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2b59(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
lea    -0x460(%rbp),%rsi
mov    -0x54(%rbp),%edx
mov    $0x1,%edi
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    14b2 <main+0x2e2>
jmp    1498 <main+0x2c8>
lea    0xc38(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2b1d(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
jmp    13c1 <main+0x1f1>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x430,%rsp
mov    %rsi,%r14
cmp    $0x2,%edi
jg     120d <main+0x2d>
mov    (%r14),%rsi
lea    0xdff(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1303 <main+0x123>
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1070 <memset@plt>
mov    0x2dc0(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x28(%rsp)
mov    0x2d9b(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x24(%rsp)
mov    0x8(%r14),%rdi
mov    0x10(%r14),%rsi
mov    %rsp,%rcx
mov    %rbx,%rdx
call   10e0 <getaddrinfo@plt>
test   %eax,%eax
jne    12e0 <main+0x100>
mov    (%rsp),%rbx
test   %rbx,%rbx
je     12bf <main+0xdf>
lea    0xe27(%rip),%r15
lea    0xe4a(%rip),%r12
jmp    1280 <main+0xa0>
nop
mov    %r15,%rdi
call   1030 <puts@plt>
mov    (%rbx),%rbx
test   %rbx,%rbx
je     12bf <main+0xdf>
mov    0x20(%rbx),%edi
mov    0x18(%rbx),%edx
mov    0x1c(%rbx),%esi
call   1100 <socket@plt>
cmp    $0xffffffff,%eax
je     1270 <main+0x90>
mov    %eax,%ebp
mov    0x10(%rbx),%rsi
mov    0x8(%rbx),%edx
mov    %eax,%edi
call   10d0 <connect@plt>
cmp    $0xffffffff,%eax
jne    1311 <main+0x131>
mov    %r12,%rdi
call   1030 <puts@plt>
mov    (%rbx),%rbx
mov    %ebp,%edi
call   1080 <close@plt>
test   %rbx,%rbx
jne    1280 <main+0xa0>
mov    0x2d12(%rip),%rax
mov    (%rax),%edi
mov    0x8(%r14),%rdx
mov    0x10(%r14),%rcx
lea    0xd88(%rip),%rsi
xor    %eax,%eax
call   10a0 <fprintf@plt>
jmp    1303 <main+0x123>
mov    0x2cf1(%rip),%rcx
mov    (%rcx),%ebx
mov    %eax,%edi
call   1060 <gai_strerror@plt>
lea    0xd36(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   10a0 <fprintf@plt>
mov    0x2cbe(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    0x8(%r14),%rsi
mov    0x10(%r14),%rdx
lea    0xd1e(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    (%rsp),%rdi
call   10f0 <freeaddrinfo@plt>
lea    0x30(%rsp),%r14
cs nopw 0x0(%rax,%rax,1)
nop
xor    %edi,%edi
mov    %r14,%rsi
mov    $0x400,%edx
call   1090 <read@plt>
cmp    $0xffffffff,%eax
je     139d <main+0x1bd>
mov    %eax,%ebx
mov    %ebp,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1040 <write@plt>
cmp    $0xffffffff,%eax
je     138c <main+0x1ac>
mov    %ebp,%edi
mov    %r14,%rsi
mov    %ebx,%edx
call   1090 <read@plt>
cmp    $0xffffffff,%eax
je     139d <main+0x1bd>
mov    $0x1,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1040 <write@plt>
cmp    $0xffffffff,%eax
jne    1340 <main+0x160>
lea    0xcf4(%rip),%rdi
call   10b0 <perror@plt>
jmp    1303 <main+0x123>
lea    0xcde(%rip),%rdi
call   10b0 <perror@plt>
jmp    1303 <main+0x123>

LLVM IR (-O0):
%struct.addrinfo = type { %struct.addrinfo*, i32, i64, i32, i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [45 x i8] c"Usage: %s <server-name> <service-name/port>\0A\00"
@EXIT_FAILURE = global i32 0
@AF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@stderr = global i32 0
@.str.1 = [17 x i8] c"getaddrinfo: %s\0A\00"
@.str.2 = [33 x i8] c"Successfully connected to %s:%s\0A\00"
@.str.3 = [37 x i8] c"Unable to connect, move to the next\0A\00"
@.str.4 = [43 x i8] c"Unable to create socket, move to the next\0A\00"
@.str.5 = [35 x i8] c"Unable to connect to server %s:%s\0A\00"
@.str.6 = [5 x i8] c"read\00"
@.str.7 = [6 x i8] c"write\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca %struct.addrinfo
%8 = alloca %struct.addrinfo*
%9 = alloca %struct.addrinfo*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca [1024 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%14 = load i32, i32* %4
%15 = icmp slt i32 %14, 3
br i1 %15, label %16, label %23
16: ; preds = %2
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 0
%19 = load i8*, i8** %18
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0), i8* %19)
%21 = load i32, i32* @EXIT_FAILURE
%22 = call i32 @exit(i32 %21)
unreachable
23: ; preds = %2
%24 = call i32 @memset(%struct.addrinfo* %7, i32 0, i32 40)
%25 = load i32, i32* @AF_UNSPEC
%26 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %7, i32 0, i32 5
store i32 %25, i32* %26
%27 = load i32, i32* @SOCK_STREAM
%28 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %7, i32 0, i32 4
store i32 %27, i32* %28
%29 = load i8**, i8*** %5
%30 = getelementptr inbounds i8*, i8** %29, i64 1
%31 = load i8*, i8** %30
%32 = load i8**, i8*** %5
%33 = getelementptr inbounds i8*, i8** %32, i64 2
%34 = load i8*, i8** %33
%35 = call i32 @getaddrinfo(i8* %31, i8* %34, %struct.addrinfo* %7, %struct.addrinfo** %8)
store i32 %35, i32* %11
%36 = icmp ne i32 %35, 0
br i1 %36, label %37, label %44
37: ; preds = %23
%38 = load i32, i32* @stderr
%39 = load i32, i32* %11
%40 = call i8* @gai_strerror(i32 %39)
%41 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %38, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %40)
%42 = load i32, i32* @EXIT_FAILURE
%43 = call i32 @exit(i32 %42)
unreachable
44: ; preds = %23
store i32 0, i32* %12
%45 = load %struct.addrinfo*, %struct.addrinfo** %8
store %struct.addrinfo* %45, %struct.addrinfo** %9
br label %46
46: ; preds = %93, %44
%47 = load %struct.addrinfo*, %struct.addrinfo** %9
%48 = icmp ne %struct.addrinfo* %47, null
br i1 %48, label %49, label %94
49: ; preds = %46
%50 = load %struct.addrinfo*, %struct.addrinfo** %9
%51 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %50, i32 0, i32 5
%52 = load i32, i32* %51
%53 = load %struct.addrinfo*, %struct.addrinfo** %9
%54 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %53, i32 0, i32 4
%55 = load i32, i32* %54
%56 = load %struct.addrinfo*, %struct.addrinfo** %9
%57 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %56, i32 0, i32 3
%58 = load i32, i32* %57
%59 = call i32 @socket(i32 %52, i32 %55, i32 %58)
store i32 %59, i32* %6
%60 = icmp ne i32 %59, -1
br i1 %60, label %61, label %88
61: ; preds = %49
%62 = load i32, i32* %6
%63 = load %struct.addrinfo*, %struct.addrinfo** %9
%64 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %63, i32 0, i32 2
%65 = load i64, i64* %64
%66 = inttoptr i64 %65 to %struct.sockaddr*
%67 = load %struct.addrinfo*, %struct.addrinfo** %9
%68 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %67, i32 0, i32 1
%69 = load i32, i32* %68
%70 = call i32 @connect(i32 %62, %struct.sockaddr* %66, i32 %69)
%71 = icmp ne i32 %70, -1
br i1 %71, label %72, label %80
72: ; preds = %61
%73 = load i8**, i8*** %5
%74 = getelementptr inbounds i8*, i8** %73, i64 1
%75 = load i8*, i8** %74
%76 = load i8**, i8*** %5
%77 = getelementptr inbounds i8*, i8** %76, i64 2
%78 = load i8*, i8** %77
%79 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i8* %75, i8* %78)
store i32 1, i32* %12
store %struct.addrinfo* null, %struct.addrinfo** %9
br label %87
80: ; preds = %61
%81 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0))
%82 = load %struct.addrinfo*, %struct.addrinfo** %9
%83 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %82, i32 0, i32 0
%84 = load %struct.addrinfo*, %struct.addrinfo** %83
store %struct.addrinfo* %84, %struct.addrinfo** %9
%85 = load i32, i32* %6
%86 = call i32 @close(i32 %85)
br label %87
87: ; preds = %80, %72
br label %93
88: ; preds = %49
%89 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.4, i64 0, i64 0))
%90 = load %struct.addrinfo*, %struct.addrinfo** %9
%91 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %90, i32 0, i32 0
%92 = load %struct.addrinfo*, %struct.addrinfo** %91
store %struct.addrinfo* %92, %struct.addrinfo** %9
br label %93
93: ; preds = %88, %87
br label %46
94: ; preds = %46
%95 = load i32, i32* %12
%96 = icmp ne i32 %95, 0
br i1 %96, label %108, label %97
97: ; preds = %94
%98 = load i32, i32* @stderr
%99 = load i8**, i8*** %5
%100 = getelementptr inbounds i8*, i8** %99, i64 1
%101 = load i8*, i8** %100
%102 = load i8**, i8*** %5
%103 = getelementptr inbounds i8*, i8** %102, i64 2
%104 = load i8*, i8** %103
%105 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %98, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.5, i64 0, i64 0), i8* %101, i8* %104)
%106 = load i32, i32* @EXIT_FAILURE
%107 = call i32 @exit(i32 %106)
unreachable
108: ; preds = %94
%109 = load %struct.addrinfo*, %struct.addrinfo** %8
%110 = call i32 @freeaddrinfo(%struct.addrinfo* %109)
br label %111
111: ; preds = %108, %156
%112 = getelementptr inbounds [1024 x i8], [1024 x i8]* %13, i64 0, i64 0
%113 = call i32 @read(i32 0, i8* %112, i32 1024)
store i32 %113, i32* %10
%114 = icmp eq i32 %113, -1
br i1 %114, label %115, label %121
115: ; preds = %111
br label %116
116: ; preds = %115
%117 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0))
%118 = load i32, i32* @EXIT_FAILURE
%119 = call i32 @exit(i32 %118)
unreachable
120: ; No predecessors!
br label %121
121: ; preds = %120, %111
%122 = load i32, i32* %6
%123 = getelementptr inbounds [1024 x i8], [1024 x i8]* %13, i64 0, i64 0
%124 = load i32, i32* %10
%125 = call i32 @write(i32 %122, i8* %123, i32 %124)
%126 = icmp eq i32 %125, -1
br i1 %126, label %127, label %133
127: ; preds = %121
br label %128
128: ; preds = %127
%129 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0))
%130 = load i32, i32* @EXIT_FAILURE
%131 = call i32 @exit(i32 %130)
unreachable
132: ; No predecessors!
br label %133
133: ; preds = %132, %121
%134 = load i32, i32* %6
%135 = getelementptr inbounds [1024 x i8], [1024 x i8]* %13, i64 0, i64 0
%136 = load i32, i32* %10
%137 = call i32 @read(i32 %134, i8* %135, i32 %136)
store i32 %137, i32* %10
%138 = icmp eq i32 %137, -1
br i1 %138, label %139, label %145
139: ; preds = %133
br label %140
140: ; preds = %139
%141 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0))
%142 = load i32, i32* @EXIT_FAILURE
%143 = call i32 @exit(i32 %142)
unreachable
144: ; No predecessors!
br label %145
145: ; preds = %144, %133
%146 = getelementptr inbounds [1024 x i8], [1024 x i8]* %13, i64 0, i64 0
%147 = load i32, i32* %10
%148 = call i32 @write(i32 1, i8* %146, i32 %147)
%149 = icmp eq i32 %148, -1
br i1 %149, label %150, label %156
150: ; preds = %145
br label %151
151: ; preds = %150
%152 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0))
%153 = load i32, i32* @EXIT_FAILURE
%154 = call i32 @exit(i32 %153)
unreachable
155: ; No predecessors!
br label %156
156: ; preds = %155, %145
br label %111
}

LLVM IR (-O2):
%struct.addrinfo = type { %struct.addrinfo*, i32, i64, i32, i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [45 x i8] c"Usage: %s <server-name> <service-name/port>\0A\00"
@EXIT_FAILURE = global i32 0
@AF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@stderr = global i32 0
@.str.1 = [17 x i8] c"getaddrinfo: %s\0A\00"
@.str.2 = [33 x i8] c"Successfully connected to %s:%s\0A\00"
@.str.5 = [35 x i8] c"Unable to connect to server %s:%s\0A\00"
@.str.6 = [5 x i8] c"read\00"
@.str.7 = [6 x i8] c"write\00"
@EXIT_SUCCESS = global i32 0
@str = [42 x i8] c"Unable to create socket, move to the next\00"
@str.8 = [36 x i8] c"Unable to connect, move to the next\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.addrinfo
%4 = alloca %struct.addrinfo*
%5 = alloca [1024 x i8]
%6 = bitcast %struct.addrinfo* %3 to i8*
%7 = bitcast %struct.addrinfo** %4 to i8*
%8 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%9 = icmp slt i32 %0, 3
br i1 %9, label %10, label %15
10: ; preds = %2
%11 = load i8*, i8** %1
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0), i8* %11)
%13 = load i32, i32* @EXIT_FAILURE
%14 = tail call i32 @exit(i32 %13)
unreachable
15: ; preds = %2
%16 = call i32 @memset(%struct.addrinfo* %3, i32 0, i32 40)
%17 = load i32, i32* @AF_UNSPEC
%18 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 5
store i32 %17, i32* %18
%19 = load i32, i32* @SOCK_STREAM
%20 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 4
store i32 %19, i32* %20
%21 = getelementptr inbounds i8*, i8** %1, i64 1
%22 = load i8*, i8** %21
%23 = getelementptr inbounds i8*, i8** %1, i64 2
%24 = load i8*, i8** %23
%25 = call i32 @getaddrinfo(i8* %22, i8* %24, %struct.addrinfo* %3, %struct.addrinfo** %4)
%26 = icmp eq i32 %25, 0
br i1 %26, label %33, label %27
27: ; preds = %15
%28 = load i32, i32* @stderr
%29 = call i8* @gai_strerror(i32 %25)
%30 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %29)
%31 = load i32, i32* @EXIT_FAILURE
%32 = call i32 @exit(i32 %31)
unreachable
33: ; preds = %15
%34 = load %struct.addrinfo*, %struct.addrinfo** %4
%35 = icmp eq %struct.addrinfo* %34, null
br i1 %35, label %72, label %36
36: ; preds = %33, %69
%37 = phi %struct.addrinfo* [ %70, %69 ], [ %34, %33 ]
%38 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %37, i64 0, i32 5
%39 = load i32, i32* %38
%40 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %37, i64 0, i32 4
%41 = load i32, i32* %40
%42 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %37, i64 0, i32 3
%43 = load i32, i32* %42
%44 = call i32 @socket(i32 %39, i32 %41, i32 %43)
%45 = icmp eq i32 %44, -1
br i1 %45, label %65, label %46
46: ; preds = %36
%47 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %37, i64 0, i32 2
%48 = load i64, i64* %47
%49 = inttoptr i64 %48 to %struct.sockaddr*
%50 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %37, i64 0, i32 1
%51 = load i32, i32* %50
%52 = call i32 @connect(i32 %44, %struct.sockaddr* %49, i32 %51)
%53 = icmp eq i32 %52, -1
br i1 %53, label %60, label %54
54: ; preds = %46
%55 = load i8*, i8** %21
%56 = load i8*, i8** %23
%57 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i8* %55, i8* %56)
%58 = load %struct.addrinfo*, %struct.addrinfo** %4
%59 = call i32 @freeaddrinfo(%struct.addrinfo* %58)
br label %79
60: ; preds = %46
%61 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @str.8, i64 0, i64 0))
%62 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %37, i64 0, i32 0
%63 = load %struct.addrinfo*, %struct.addrinfo** %62
%64 = call i32 @close(i32 %44)
br label %69
65: ; preds = %36
%66 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @str, i64 0, i64 0))
%67 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %37, i64 0, i32 0
%68 = load %struct.addrinfo*, %struct.addrinfo** %67
br label %69
69: ; preds = %60, %65
%70 = phi %struct.addrinfo* [ %63, %60 ], [ %68, %65 ]
%71 = icmp eq %struct.addrinfo* %70, null
br i1 %71, label %72, label %36
72: ; preds = %69, %33
%73 = load i32, i32* @stderr
%74 = load i8*, i8** %21
%75 = load i8*, i8** %23
%76 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %73, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.5, i64 0, i64 0), i8* %74, i8* %75)
%77 = load i32, i32* @EXIT_FAILURE
%78 = call i32 @exit(i32 %77)
unreachable
79: ; preds = %100, %54
%80 = call i32 @read(i32 0, i8* %8, i32 1024)
%81 = icmp eq i32 %80, -1
br i1 %81, label %82, label %86
82: ; preds = %79
%83 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0))
%84 = load i32, i32* @EXIT_FAILURE
%85 = call i32 @exit(i32 %84)
unreachable
86: ; preds = %79
%87 = call i32 @write(i32 %44, i8* %8, i32 %80)
%88 = icmp eq i32 %87, -1
br i1 %88, label %89, label %93
89: ; preds = %86
%90 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0))
%91 = load i32, i32* @EXIT_FAILURE
%92 = call i32 @exit(i32 %91)
unreachable
93: ; preds = %86
%94 = call i32 @read(i32 %44, i8* %8, i32 %80)
%95 = icmp eq i32 %94, -1
br i1 %95, label %96, label %100
96: ; preds = %93
%97 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0))
%98 = load i32, i32* @EXIT_FAILURE
%99 = call i32 @exit(i32 %98)
unreachable
100: ; preds = %93
%101 = call i32 @write(i32 1, i8* %8, i32 %94)
%102 = icmp eq i32 %101, -1
br i1 %102, label %103, label %79
103: ; preds = %100
%104 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0))
%105 = load i32, i32* @EXIT_FAILURE
%106 = call i32 @exit(i32 %105)
unreachable
}