#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;
  scalar_t__ ai_protocol;
  int /*<<< orphan*/ ai_socktype;
  int /*<<< orphan*/ ai_family;
  struct addrinfo *ai_next;
  scalar_t__ ai_flags;
};

/* Variables and functions */
int /*<<< orphan*/ AF_UNSPEC;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int connect(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int /*<<< orphan*/ getaddrinfo(char *, char *, struct addrinfo *,
                               struct addrinfo **);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int /*<<< orphan*/ scanf(char *, char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, scalar_t__);
int /*<<< orphan*/ stderr;
scalar_t__ strcmp(char const *, char *);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ write(int, char *, int /*<<< orphan*/);
int main(int args, char *argv[]) {
  struct addrinfo hints;
  struct addrinfo *result, *rp;
  char buf[81];
  int sfd;
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_flags = 0;
  hints.ai_protocol = 0;
  getaddrinfo(argv[1], argv[2], &hints, &result);
  for (rp = result; rp != NULL; rp = rp->ai_next) {
    sfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
    if (sfd == -1)
      continue;
    if (connect(sfd, rp->ai_addr, rp->ai_addrlen) != -1)
      break;
    close(sfd);
  }
  if (rp == NULL) {
    fprintf(stderr, "Could not connect\n");
    exit(EXIT_FAILURE);
  }
  freeaddrinfo(result);
  int nread;
  while (strcmp((const char *)buf, "quit")) {
    printf("TCP request to send:");
    scanf("%s", buf);
    write(sfd, buf, strlen(buf));
    nread = read(sfd, buf, 81);
    printf("Received %d bytes: %s\n", nread, buf);
  }
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2df8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2dd4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
lea    -0x38(%rbp),%rdx
lea    -0x40(%rbp),%rcx
call   10d0 <getaddrinfo@plt>
mov    -0x40(%rbp),%rax
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
je     12b6 <main+0xe6>
mov    -0x48(%rbp),%rax
mov    0x14(%rax),%edi
mov    -0x48(%rbp),%rax
mov    0x10(%rax),%esi
mov    -0x48(%rbp),%rax
mov    0x8(%rax),%rdx
call   10f0 <socket@plt>
mov    %eax,-0xa4(%rbp)
cmpl   $0xffffffff,-0xa4(%rbp)
jne    1274 <main+0xa4>
jmp    12a5 <main+0xd5>
mov    -0xa4(%rbp),%edi
mov    -0x48(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x48(%rbp),%rax
mov    (%rax),%edx
call   10c0 <connect@plt>
cmp    $0xffffffff,%eax
je     129a <main+0xca>
jmp    12b6 <main+0xe6>
mov    -0xa4(%rbp),%edi
call   1060 <close@plt>
mov    -0x48(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x48(%rbp)
jmp    1236 <main+0x66>
cmpq   $0x0,-0x48(%rbp)
jne    12e4 <main+0x114>
mov    0x2d10(%rip),%rax
mov    (%rax),%edi
lea    0xd2f(%rip),%rsi
call   1090 <fprintf@plt>
mov    0x2ceb(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x40(%rbp),%rdi
call   10e0 <freeaddrinfo@plt>
lea    -0xa0(%rbp),%rdi
lea    0xd18(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
je     13a2 <main+0x1d2>
lea    0xd07(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0xa0(%rbp),%rsi
lea    0xd07(%rip),%rdi
call   10a0 <scanf@plt>
mov    -0xa4(%rbp),%eax
mov    %eax,-0xb4(%rbp)
lea    -0xa0(%rbp),%rax
mov    %rax,-0xb0(%rbp)
lea    -0xa0(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0xb4(%rbp),%edi
mov    -0xb0(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    -0xa4(%rbp),%edi
lea    -0xa0(%rbp),%rsi
mov    $0x51,%edx
call   1070 <read@plt>
mov    %eax,-0xa8(%rbp)
mov    -0xa8(%rbp),%esi
lea    -0xa0(%rbp),%rdx
lea    0xc9a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    12ed <main+0x11d>
mov    0x2c17(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x88,%rsp
mov    0x2e00(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
mov    0x2ddb(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
movq   $0x0,0x28(%rsp)
movq   $0x0,0x10(%rsp)
mov    0x8(%rsi),%rdi
mov    0x10(%rsi),%rsi
lea    0x8(%rsp),%rdx
mov    %rsp,%rcx
call   10d0 <getaddrinfo@plt>
mov    (%rsp),%rbp
jmp    1234 <main+0x64>
nopl   0x0(%rax,%rax,1)
mov    0x18(%rbp),%rbp
test   %rbp,%rbp
je     126a <main+0x9a>
mov    0x14(%rbp),%edi
mov    0x10(%rbp),%esi
mov    0x8(%rbp),%rdx
call   10f0 <socket@plt>
cmp    $0xffffffff,%eax
je     1230 <main+0x60>
mov    %eax,%ebx
mov    0x0(%rbp),%edx
mov    0x4(%rbp),%esi
mov    %eax,%edi
call   10c0 <connect@plt>
cmp    $0xffffffff,%eax
jne    128d <main+0xbd>
mov    %ebx,%edi
call   1060 <close@plt>
jmp    1230 <main+0x60>
mov    0x2d67(%rip),%rax
mov    (%rax),%edi
lea    0xd86(%rip),%rsi
call   1090 <fprintf@plt>
mov    0x2d42(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    (%rsp),%rdi
call   10e0 <freeaddrinfo@plt>
lea    0xd76(%rip),%rsi
lea    0x30(%rsp),%rdi
call   1080 <strcmp@plt>
test   %rax,%rax
je     1327 <main+0x157>
lea    0xd65(%rip),%r14
lea    0xd73(%rip),%r15
lea    0x30(%rsp),%r12
lea    0xd6a(%rip),%r13
lea    0xd46(%rip),%rbp
nopl   (%rax)
mov    %r14,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r15,%rdi
mov    %r12,%rsi
call   10a0 <scanf@plt>
mov    %r12,%rdi
call   1040 <strlen@plt>
mov    %ebx,%edi
mov    %r12,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %ebx,%edi
mov    %r12,%rsi
mov    $0x51,%edx
call   1070 <read@plt>
mov    %r13,%rdi
mov    %eax,%esi
mov    %r12,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r12,%rdi
mov    %rbp,%rsi
call   1080 <strcmp@plt>
test   %rax,%rax
jne    12d0 <main+0x100>
mov    0x2c92(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i64, i32, i32, %struct.addrinfo*, i64 }
@AF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@stderr = global i32 0
@.str = [19 x i8] c"Could not connect\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [5 x i8] c"quit\00"
@.str.2 = [21 x i8] c"TCP request to send:\00"
@.str.3 = [3 x i8] c"%s\00"
@.str.4 = [23 x i8] c"Received %d bytes: %s\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.addrinfo
%7 = alloca %struct.addrinfo*
%8 = alloca %struct.addrinfo*
%9 = alloca [81 x i8]
%10 = alloca i32
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* @AF_UNSPEC
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 4
store i32 %12, i32* %13
%14 = load i32, i32* @SOCK_STREAM
%15 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 3
store i32 %14, i32* %15
%16 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 6
store i64 0, i64* %16
%17 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 2
store i64 0, i64* %17
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 2
%23 = load i8*, i8** %22
%24 = call i32 @getaddrinfo(i8* %20, i8* %23, %struct.addrinfo* %6, %struct.addrinfo** %7)
%25 = load %struct.addrinfo*, %struct.addrinfo** %7
store %struct.addrinfo* %25, %struct.addrinfo** %8
br label %26
26: ; preds = %57, %2
%27 = load %struct.addrinfo*, %struct.addrinfo** %8
%28 = icmp ne %struct.addrinfo* %27, null
br i1 %28, label %29, label %61
29: ; preds = %26
%30 = load %struct.addrinfo*, %struct.addrinfo** %8
%31 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %30, i32 0, i32 4
%32 = load i32, i32* %31
%33 = load %struct.addrinfo*, %struct.addrinfo** %8
%34 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %33, i32 0, i32 3
%35 = load i32, i32* %34
%36 = load %struct.addrinfo*, %struct.addrinfo** %8
%37 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %36, i32 0, i32 2
%38 = load i64, i64* %37
%39 = call i32 @socket(i32 %32, i32 %35, i64 %38)
store i32 %39, i32* %10
%40 = load i32, i32* %10
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %43
42: ; preds = %29
br label %57
43: ; preds = %29
%44 = load i32, i32* %10
%45 = load %struct.addrinfo*, %struct.addrinfo** %8
%46 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %45, i32 0, i32 1
%47 = load i32, i32* %46
%48 = load %struct.addrinfo*, %struct.addrinfo** %8
%49 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %48, i32 0, i32 0
%50 = load i32, i32* %49
%51 = call i32 @connect(i32 %44, i32 %47, i32 %50)
%52 = icmp ne i32 %51, -1
br i1 %52, label %53, label %54
53: ; preds = %43
br label %61
54: ; preds = %43
%55 = load i32, i32* %10
%56 = call i32 @close(i32 %55)
br label %57
57: ; preds = %54, %42
%58 = load %struct.addrinfo*, %struct.addrinfo** %8
%59 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %58, i32 0, i32 5
%60 = load %struct.addrinfo*, %struct.addrinfo** %59
store %struct.addrinfo* %60, %struct.addrinfo** %8
br label %26
61: ; preds = %53, %26
%62 = load %struct.addrinfo*, %struct.addrinfo** %8
%63 = icmp eq %struct.addrinfo* %62, null
br i1 %63, label %64, label %69
64: ; preds = %61
%65 = load i32, i32* @stderr
%66 = call i32 @fprintf(i32 %65, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%67 = load i32, i32* @EXIT_FAILURE
%68 = call i32 @exit(i32 %67)
unreachable
69: ; preds = %61
%70 = load %struct.addrinfo*, %struct.addrinfo** %7
%71 = call i32 @freeaddrinfo(%struct.addrinfo* %70)
br label %72
72: ; preds = %76, %69
%73 = getelementptr inbounds [81 x i8], [81 x i8]* %9, i64 0, i64 0
%74 = call i64 @strcmp(i8* %73, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%75 = icmp ne i64 %74, 0
br i1 %75, label %76, label %91
76: ; preds = %72
%77 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%78 = getelementptr inbounds [81 x i8], [81 x i8]* %9, i64 0, i64 0
%79 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %78)
%80 = load i32, i32* %10
%81 = getelementptr inbounds [81 x i8], [81 x i8]* %9, i64 0, i64 0
%82 = getelementptr inbounds [81 x i8], [81 x i8]* %9, i64 0, i64 0
%83 = call i32 @strlen(i8* %82)
%84 = call i32 @write(i32 %80, i8* %81, i32 %83)
%85 = load i32, i32* %10
%86 = getelementptr inbounds [81 x i8], [81 x i8]* %9, i64 0, i64 0
%87 = call i32 @read(i32 %85, i8* %86, i32 81)
store i32 %87, i32* %11
%88 = load i32, i32* %11
%89 = getelementptr inbounds [81 x i8], [81 x i8]* %9, i64 0, i64 0
%90 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i32 %88, i8* %89)
br label %72
91: ; preds = %72
%92 = load i32, i32* @EXIT_SUCCESS
%93 = call i32 @exit(i32 %92)
unreachable
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i64, i32, i32, %struct.addrinfo*, i64 }
@AF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@stderr = global i32 0
@.str = [19 x i8] c"Could not connect\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [5 x i8] c"quit\00"
@.str.2 = [21 x i8] c"TCP request to send:\00"
@.str.3 = [3 x i8] c"%s\00"
@.str.4 = [23 x i8] c"Received %d bytes: %s\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.addrinfo
%4 = alloca %struct.addrinfo*
%5 = alloca [81 x i8]
%6 = bitcast %struct.addrinfo* %3 to i8*
%7 = bitcast %struct.addrinfo** %4 to i8*
%8 = getelementptr inbounds [81 x i8], [81 x i8]* %5, i64 0, i64 0
%9 = load i32, i32* @AF_UNSPEC
%10 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 4
store i32 %9, i32* %10
%11 = load i32, i32* @SOCK_STREAM
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 3
store i32 %11, i32* %12
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 6
store i64 0, i64* %13
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 2
store i64 0, i64* %14
%15 = getelementptr inbounds i8*, i8** %1, i64 1
%16 = load i8*, i8** %15
%17 = getelementptr inbounds i8*, i8** %1, i64 2
%18 = load i8*, i8** %17
%19 = call i32 @getaddrinfo(i8* %16, i8* %18, %struct.addrinfo* %3, %struct.addrinfo** %4)
%20 = load %struct.addrinfo*, %struct.addrinfo** %4
%21 = icmp eq %struct.addrinfo* %20, null
br i1 %21, label %45, label %22
22: ; preds = %2, %41
%23 = phi %struct.addrinfo* [ %43, %41 ], [ %20, %2 ]
%24 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %23, i64 0, i32 4
%25 = load i32, i32* %24
%26 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %23, i64 0, i32 3
%27 = load i32, i32* %26
%28 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %23, i64 0, i32 2
%29 = load i64, i64* %28
%30 = call i32 @socket(i32 %25, i32 %27, i64 %29)
%31 = icmp eq i32 %30, -1
br i1 %31, label %41, label %32
32: ; preds = %22
%33 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %23, i64 0, i32 1
%34 = load i32, i32* %33
%35 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %23, i64 0, i32 0
%36 = load i32, i32* %35
%37 = call i32 @connect(i32 %30, i32 %34, i32 %36)
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %50
39: ; preds = %32
%40 = call i32 @close(i32 %30)
br label %41
41: ; preds = %22, %39
%42 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %23, i64 0, i32 5
%43 = load %struct.addrinfo*, %struct.addrinfo** %42
%44 = icmp eq %struct.addrinfo* %43, null
br i1 %44, label %45, label %22
45: ; preds = %41, %2
%46 = load i32, i32* @stderr
%47 = call i32 @fprintf(i32 %46, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%48 = load i32, i32* @EXIT_FAILURE
%49 = call i32 @exit(i32 %48)
unreachable
50: ; preds = %32
%51 = load %struct.addrinfo*, %struct.addrinfo** %4
%52 = call i32 @freeaddrinfo(%struct.addrinfo* %51)
%53 = call i64 @strcmp(i8* %8, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%54 = icmp eq i64 %53, 0
br i1 %54, label %64, label %55
55: ; preds = %50, %55
%56 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%57 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %8)
%58 = call i32 @strlen(i8* %8)
%59 = call i32 @write(i32 %30, i8* %8, i32 %58)
%60 = call i32 @read(i32 %30, i8* %8, i32 81)
%61 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i32 %60, i8* %8)
%62 = call i64 @strcmp(i8* %8, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%63 = icmp eq i64 %62, 0
br i1 %63, label %64, label %55
64: ; preds = %55, %50
%65 = load i32, i32* @EXIT_SUCCESS
%66 = call i32 @exit(i32 %65)
unreachable
}