#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_in {
  int /*<<< orphan*/ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef size_t ssize_t;
typedef int /*<<< orphan*/ servaddr;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
scalar_t__ EOF;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
scalar_t__ fputs(char *, int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int inet_pton(int /*<<< orphan*/, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
size_t read(int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
char *strerror(int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  int sockfd;
  struct sockaddr_in servaddr;
  ssize_t n;
  char buf[128];
  if (argc != 2) {
    fprintf(stderr, "Usage: %s <IP address>\n", argv[0]);
    return -1;
  }
  if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
    perror("socket() failed: ");
    return -1;
  }
  memset(&servaddr, 0, sizeof(servaddr));
  servaddr.sin_family = AF_INET;
  servaddr.sin_port = htons(13);
  if (inet_pton(AF_INET, argv[1], &servaddr.sin_addr) != 1) {
    fprintf(stderr, "inet_pton() for %s failed: %s\n", argv[1],
            strerror(errno));
    close(sockfd);
    return -1;
  }
  if (connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) == -1) {
    fprintf(stderr, "connect() failed: %s\n", strerror(errno));
    close(sockfd);
    return -1;
  }
  while ((n = read(sockfd, buf, sizeof(buf))) > 0) {
    buf[n] = '\0';
    if (fputs(buf, stdout) == EOF) {
      fprintf(stderr, "fputs() error\n");
      close(sockfd);
      return -1;
    }
  }
  if (n < 0) {
    fprintf(stderr, "read() failed: %s\n", strerror(errno));
    close(sockfd);
    return -1;
  }
  close(sockfd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xd0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11fd <main+0x4d>
mov    0x2df6(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe16(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1430 <main+0x280>
mov    0x2de4(%rip),%rax
mov    (%rax),%edi
mov    0x2dbb(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    123a <main+0x8a>
lea    0xdef(%rip),%rdi
call   10a0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1430 <main+0x280>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2d97(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    $0xd,%edi
call   1030 <htons@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2d7e(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    -0x20(%rbp),%rdx
call   1090 <inet_pton@plt>
cmp    $0x1,%eax
je     12e4 <main+0x134>
mov    0x2d43(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xbc(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0xb8(%rbp)
mov    0x2d15(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0xbc(%rbp),%edi
mov    -0xb8(%rbp),%rdx
mov    %rax,%rcx
lea    0xd61(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1430 <main+0x280>
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   10b0 <connect@plt>
cmp    $0xffffffff,%eax
jne    1346 <main+0x196>
mov    0x2ccb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc0(%rbp)
mov    0x2cac(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0xc0(%rbp),%edi
mov    %rax,%rdx
lea    0xd1e(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1430 <main+0x280>
jmp    134b <main+0x19b>
mov    -0x14(%rbp),%edi
lea    -0xb0(%rbp),%rsi
mov    $0x80,%edx
call   1070 <read@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jbe    13ce <main+0x21e>
mov    -0x28(%rbp),%rax
movb   $0x0,-0xb0(%rbp,%rax,1)
lea    -0xb0(%rbp),%rdi
mov    0x2c31(%rip),%rax
mov    (%rax),%esi
call   1040 <fputs@plt>
mov    0x2c4b(%rip),%rcx
cmp    (%rcx),%rax
jne    13c9 <main+0x219>
mov    0x2c2b(%rip),%rax
mov    (%rax),%edi
lea    0xcb1(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1430 <main+0x280>
jmp    134b <main+0x19b>
cmpq   $0x0,-0x28(%rbp)
jae    1421 <main+0x271>
mov    0x2bf0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc4(%rbp)
mov    0x2bd1(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0xc4(%rbp),%edi
mov    %rax,%rdx
lea    0xc68(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1430 <main+0x280>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xd0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x98,%rsp
mov    %rsi,%rbx
cmp    $0x2,%edi
jne    12a6 <main+0xf6>
mov    0x2e18(%rip),%r15
mov    (%r15),%edi
mov    0x2dee(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
cmp    $0xffffffff,%eax
je     1346 <main+0x196>
mov    %eax,%ebp
mov    %rsp,%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    (%r15),%eax
mov    %eax,0x8(%rsp)
mov    $0xd,%edi
call   1030 <htons@plt>
mov    %eax,0x4(%rsp)
mov    (%r15),%edi
mov    0x8(%rbx),%rsi
mov    %r14,%rdx
call   1090 <inet_pton@plt>
cmp    $0x1,%eax
jne    12c2 <main+0x112>
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0xc,%edx
call   10b0 <connect@plt>
cmp    $0xffffffff,%eax
je     12f7 <main+0x147>
lea    0x10(%rsp),%rbx
mov    0x2d67(%rip),%r14
mov    0x2d88(%rip),%r15
nopl   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x80,%edx
call   1070 <read@plt>
test   %rax,%rax
je     133b <main+0x18b>
movb   $0x0,0x10(%rsp,%rax,1)
mov    (%r14),%esi
mov    %rbx,%rdi
call   1040 <fputs@plt>
cmp    (%r15),%rax
jne    1260 <main+0xb0>
mov    0x2d3c(%rip),%rax
mov    (%rax),%edi
lea    0xdc2(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
jmp    1321 <main+0x171>
mov    0x2d23(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xd47(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
jmp    1328 <main+0x178>
mov    0x2d07(%rip),%rax
mov    (%rax),%r14d
mov    0x8(%rbx),%rbx
mov    0x2ce9(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xd45(%rip),%rsi
mov    %r14d,%edi
mov    %rbx,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1080 <fprintf@plt>
jmp    1321 <main+0x171>
mov    0x2cd2(%rip),%rax
mov    (%rax),%ebx
mov    0x2cb9(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xd34(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    %ebp,%edi
call   1060 <close@plt>
mov    $0xffffffff,%eax
add    $0x98,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %ebp,%edi
call   1060 <close@plt>
xor    %eax,%eax
jmp    132d <main+0x17d>
lea    0xccb(%rip),%rdi
call   10a0 <perror@plt>
jmp    1328 <main+0x178>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [24 x i8] c"Usage: %s <IP address>\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [18 x i8] c"socket() failed: \00"
@.str.2 = [31 x i8] c"inet_pton() for %s failed: %s\0A\00"
@errno = global i32 0
@.str.3 = [22 x i8] c"connect() failed: %s\0A\00"
@stdout = global i32 0
@EOF = global i64 0
@.str.4 = [15 x i8] c"fputs() error\0A\00"
@.str.5 = [19 x i8] c"read() failed: %s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca %struct.sockaddr_in
%8 = alloca i64
%9 = alloca [128 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 2
br i1 %11, label %12, label %18
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 0
%16 = load i8*, i8** %15
%17 = call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %16)
store i32 -1, i32* %3
br label %93
18: ; preds = %2
%19 = load i32, i32* @AF_INET
%20 = load i32, i32* @SOCK_STREAM
%21 = call i32 @socket(i32 %19, i32 %20, i32 0)
store i32 %21, i32* %6
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %25
23: ; preds = %18
%24 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %93
25: ; preds = %18
%26 = call i32 @memset(%struct.sockaddr_in* %7, i32 0, i32 12)
%27 = load i32, i32* @AF_INET
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i32 %27, i32* %28
%29 = call i32 @htons(i32 13)
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
store i32 %29, i32* %30
%31 = load i32, i32* @AF_INET
%32 = load i8**, i8*** %5
%33 = getelementptr inbounds i8*, i8** %32, i64 1
%34 = load i8*, i8** %33
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
%36 = call i32 @inet_pton(i32 %31, i8* %34, i32* %35)
%37 = icmp ne i32 %36, 1
br i1 %37, label %38, label %48
38: ; preds = %25
%39 = load i32, i32* @stderr
%40 = load i8**, i8*** %5
%41 = getelementptr inbounds i8*, i8** %40, i64 1
%42 = load i8*, i8** %41
%43 = load i32, i32* @errno
%44 = call i8* @strerror(i32 %43)
%45 = call i32 (i32, i8*, ...) @fprintf(i32 %39, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i8* %42, i8* %44)
%46 = load i32, i32* %6
%47 = call i32 @close(i32 %46)
store i32 -1, i32* %3
br label %93
48: ; preds = %25
%49 = load i32, i32* %6
%50 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%51 = call i32 @connect(i32 %49, %struct.sockaddr* %50, i32 12)
%52 = icmp eq i32 %51, -1
br i1 %52, label %53, label %60
53: ; preds = %48
%54 = load i32, i32* @stderr
%55 = load i32, i32* @errno
%56 = call i8* @strerror(i32 %55)
%57 = call i32 (i32, i8*, ...) @fprintf(i32 %54, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i8* %56)
%58 = load i32, i32* %6
%59 = call i32 @close(i32 %58)
store i32 -1, i32* %3
br label %93
60: ; preds = %48
br label %61
61: ; preds = %79, %60
%62 = load i32, i32* %6
%63 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 0
%64 = call i64 @read(i32 %62, i8* %63, i32 128)
store i64 %64, i64* %8
%65 = icmp ugt i64 %64, 0
br i1 %65, label %66, label %80
66: ; preds = %61
%67 = load i64, i64* %8
%68 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 %67
store i8 0, i8* %68
%69 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 0
%70 = load i32, i32* @stdout
%71 = call i64 @fputs(i8* %69, i32 %70)
%72 = load i64, i64* @EOF
%73 = icmp eq i64 %71, %72
br i1 %73, label %74, label %79
74: ; preds = %66
%75 = load i32, i32* @stderr
%76 = call i32 (i32, i8*, ...) @fprintf(i32 %75, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0))
%77 = load i32, i32* %6
%78 = call i32 @close(i32 %77)
store i32 -1, i32* %3
br label %93
79: ; preds = %66
br label %61
80: ; preds = %61
%81 = load i64, i64* %8
%82 = icmp ult i64 %81, 0
br i1 %82, label %83, label %90
83: ; preds = %80
%84 = load i32, i32* @stderr
%85 = load i32, i32* @errno
%86 = call i8* @strerror(i32 %85)
%87 = call i32 (i32, i8*, ...) @fprintf(i32 %84, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0), i8* %86)
%88 = load i32, i32* %6
%89 = call i32 @close(i32 %88)
store i32 -1, i32* %3
br label %93
90: ; preds = %80
%91 = load i32, i32* %6
%92 = call i32 @close(i32 %91)
store i32 0, i32* %3
br label %93
93: ; preds = %90, %83, %74, %53, %38, %23, %12
%94 = load i32, i32* %3
ret i32 %94
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [24 x i8] c"Usage: %s <IP address>\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [18 x i8] c"socket() failed: \00"
@.str.2 = [31 x i8] c"inet_pton() for %s failed: %s\0A\00"
@errno = global i32 0
@.str.3 = [22 x i8] c"connect() failed: %s\0A\00"
@stdout = global i32 0
@EOF = global i64 0
@.str.4 = [15 x i8] c"fputs() error\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca [128 x i8]
%5 = bitcast %struct.sockaddr_in* %3 to i8*
%6 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%7 = icmp eq i32 %0, 2
br i1 %7, label %12, label %8
8: ; preds = %2
%9 = load i32, i32* @stderr
%10 = load i8*, i8** %1
%11 = tail call i32 (i32, i8*, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %10)
br label %63
12: ; preds = %2
%13 = load i32, i32* @AF_INET
%14 = load i32, i32* @SOCK_STREAM
%15 = tail call i32 @socket(i32 %13, i32 %14, i32 0)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %19
17: ; preds = %12
%18 = tail call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
br label %63
19: ; preds = %12
%20 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 12)
%21 = load i32, i32* @AF_INET
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %21, i32* %22
%23 = call i32 @htons(i32 13)
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %23, i32* %24
%25 = load i32, i32* @AF_INET
%26 = getelementptr inbounds i8*, i8** %1, i64 1
%27 = load i8*, i8** %26
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
%29 = call i32 @inet_pton(i32 %25, i8* %27, i32* %28)
%30 = icmp eq i32 %29, 1
br i1 %30, label %38, label %31
31: ; preds = %19
%32 = load i32, i32* @stderr
%33 = load i8*, i8** %26
%34 = load i32, i32* @errno
%35 = call i8* @strerror(i32 %34)
%36 = call i32 (i32, i8*, ...) @fprintf(i32 %32, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i8* %33, i8* %35)
%37 = call i32 @close(i32 %15)
br label %63
38: ; preds = %19
%39 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%40 = call i32 @connect(i32 %15, %struct.sockaddr* %39, i32 12)
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %48
42: ; preds = %38
%43 = load i32, i32* @stderr
%44 = load i32, i32* @errno
%45 = call i8* @strerror(i32 %44)
%46 = call i32 (i32, i8*, ...) @fprintf(i32 %43, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i8* %45)
%47 = call i32 @close(i32 %15)
br label %63
48: ; preds = %38, %51
%49 = call i64 @read(i32 %15, i8* %6, i32 128)
%50 = icmp eq i64 %49, 0
br i1 %50, label %61, label %51
51: ; preds = %48
%52 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 %49
store i8 0, i8* %52
%53 = load i32, i32* @stdout
%54 = call i64 @fputs(i8* %6, i32 %53)
%55 = load i64, i64* @EOF
%56 = icmp eq i64 %54, %55
br i1 %56, label %57, label %48
57: ; preds = %51
%58 = load i32, i32* @stderr
%59 = call i32 (i32, i8*, ...) @fprintf(i32 %58, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0))
%60 = call i32 @close(i32 %15)
br label %63
61: ; preds = %48
%62 = call i32 @close(i32 %15)
br label %63
63: ; preds = %61, %57, %42, %31, %17, %8
%64 = phi i32 [ -1, %8 ], [ -1, %17 ], [ -1, %31 ], [ -1, %42 ], [ -1, %57 ], [ 0, %61 ]
ret i32 %64
}