#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 int /*<<< orphan*/ servaddr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
scalar_t__ EOF;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fputs(char *, int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int inet_pton(int /*<<< orphan*/, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stdout;
char *strerror(int /*<<< orphan*/);
int main(int argc, char **argv) {
  if (2 != argc) {
    printf("usage: %s <IPAddress>\n", argv[0]);
    exit(0);
  }
  int sockfd = socket(AF_INET, SOCK_STREAM, 0);
  if (-1 == sockfd) {
    perror("socket error");
    exit(0);
  }
  struct sockaddr_in servaddr;
  bzero(&servaddr, sizeof(servaddr));
  servaddr.sin_family = AF_INET;
  servaddr.sin_port = htons((13));
  if (1 != inet_pton(AF_INET, argv[1], &servaddr.sin_addr)) {
    printf("inet_pton error: %s\n", argv[1]);
    close(sockfd);
    exit(0);
  }
  if (0 != connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr))) {
    perror("connect error");
    close(sockfd);
    exit(0);
  }
  int n;
  char recvline[(50) + 1];
  while (0 < (n = read(sockfd, recvline, (50)))) {
    recvline[n] = 0;
    if (EOF == fputs(recvline, stdout)) {
      printf("fputs error\n");
      close(sockfd);
      exit(0);
    }
  }
  if (0 > n) {
    printf("read error: %s\n", strerror(errno));
  }
  close(sockfd);
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x2,%eax
cmp    -0x8(%rbp),%eax
je     1200 <main+0x40>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe0e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %edi,%edi
call   10b0 <exit@plt>
mov    0x2de1(%rip),%rax
mov    (%rax),%edi
mov    0x2dc0(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    %eax,-0x14(%rbp)
mov    $0xffffffff,%eax
cmp    -0x14(%rbp),%eax
jne    123d <main+0x7d>
lea    0xde6(%rip),%rdi
call   1090 <perror@plt>
xor    %edi,%edi
call   10b0 <exit@plt>
lea    -0x20(%rbp),%rdi
mov    $0xc,%esi
call   10a0 <bzero@plt>
mov    0x2d96(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    $0xd,%edi
call   1030 <htons@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2d7d(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    -0x20(%rbp),%rdx
call   1080 <inet_pton@plt>
mov    %eax,%ecx
mov    $0x1,%eax
cmp    %ecx,%eax
je     12b2 <main+0xf2>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xd88(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
xor    %edi,%edi
call   10b0 <exit@plt>
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   10c0 <connect@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
je     12ec <main+0x12c>
lea    0xd61(%rip),%rdi
call   1090 <perror@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
xor    %edi,%edi
call   10b0 <exit@plt>
jmp    12f1 <main+0x131>
mov    -0x14(%rbp),%edi
lea    -0x60(%rbp),%rsi
mov    $0x32,%edx
call   1070 <read@plt>
mov    %eax,%ecx
mov    %ecx,-0x24(%rbp)
xor    %eax,%eax
cmp    %ecx,%eax
jge    136c <main+0x1ac>
movslq -0x24(%rbp),%rax
movb   $0x0,-0x60(%rbp,%rax,1)
mov    0x2cbf(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x68(%rbp)
lea    -0x60(%rbp),%rdi
mov    0x2c8d(%rip),%rax
mov    (%rax),%esi
call   1050 <fputs@plt>
mov    %rax,%rcx
mov    -0x68(%rbp),%rax
cmp    %rcx,%rax
jne    1367 <main+0x1a7>
lea    0xcf6(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
xor    %edi,%edi
call   10b0 <exit@plt>
jmp    12f1 <main+0x131>
xor    %eax,%eax
cmp    -0x24(%rbp),%eax
jle    1396 <main+0x1d6>
mov    0x2c4a(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
mov    %rax,%rsi
lea    0xcc5(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
xor    %edi,%edi
call   10b0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x50,%rsp
mov    %rsi,%r14
cmp    $0x2,%edi
jne    1215 <main+0x45>
mov    0x2dfd(%rip),%rbx
mov    (%rbx),%edi
mov    0x2ddc(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
cmp    $0xffffffff,%eax
jne    122d <main+0x5d>
lea    0xe0e(%rip),%rdi
call   10a0 <perror@plt>
xor    %edi,%edi
call   10c0 <exit@plt>
mov    (%r14),%rsi
lea    0xde1(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %edi,%edi
call   10c0 <exit@plt>
mov    %eax,%r12d
mov    %rsp,%r15
mov    %r15,%rdi
mov    $0xc,%esi
call   10b0 <bzero@plt>
mov    (%rbx),%eax
mov    %eax,0x8(%rsp)
mov    $0xd,%edi
call   1040 <htons@plt>
mov    %eax,0x4(%rsp)
mov    (%rbx),%edi
mov    0x8(%r14),%rsi
mov    %r15,%rdx
call   1090 <inet_pton@plt>
cmp    $0x1,%eax
jne    12cc <main+0xfc>
mov    %rsp,%rsi
mov    %r12d,%edi
mov    $0xc,%edx
call   10d0 <connect@plt>
test   %rax,%rax
jne    12e0 <main+0x110>
lea    0x10(%rsp),%rbx
mov    0x2d58(%rip),%r14
mov    0x2d31(%rip),%r15
nop
mov    %r12d,%edi
mov    %rbx,%rsi
mov    $0x32,%edx
call   1080 <read@plt>
test   %eax,%eax
jle    12fb <main+0x12b>
mov    %eax,%eax
movb   $0x0,0x10(%rsp,%rax,1)
mov    (%r14),%rbp
mov    (%r15),%esi
mov    %rbx,%rdi
call   1060 <fputs@plt>
cmp    %rax,%rbp
jne    1290 <main+0xc0>
lea    0xd92(%rip),%rdi
call   1030 <puts@plt>
jmp    12ec <main+0x11c>
mov    0x8(%r14),%rsi
lea    0xd4d(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    12ec <main+0x11c>
lea    0xd52(%rip),%rdi
call   10a0 <perror@plt>
mov    %r12d,%edi
call   1070 <close@plt>
xor    %edi,%edi
call   10c0 <exit@plt>
jns    12ec <main+0x11c>
mov    0x2cc4(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
lea    0xd35(%rip),%rdi
mov    %rax,%rsi
jmp    12d7 <main+0x107>

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

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [23 x i8] c"usage: %s <IPAddress>\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [13 x i8] c"socket error\00"
@.str.2 = [21 x i8] c"inet_pton error: %s\0A\00"
@.str.3 = [14 x i8] c"connect error\00"
@EOF = global i64 0
@stdout = global i32 0
@.str.5 = [16 x i8] c"read error: %s\0A\00"
@errno = global i32 0
@str = [12 x i8] c"fputs error\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca [51 x i8]
%5 = icmp eq i32 %0, 2
br i1 %5, label %10, label %6
6: ; preds = %2
%7 = load i8*, i8** %1
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %7)
%9 = tail call i32 @exit(i32 0)
unreachable
10: ; preds = %2
%11 = load i32, i32* @AF_INET
%12 = load i32, i32* @SOCK_STREAM
%13 = tail call i32 @socket(i32 %11, i32 %12, i32 0)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %18
15: ; preds = %10
%16 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%17 = tail call i32 @exit(i32 0)
unreachable
18: ; preds = %10
%19 = bitcast %struct.sockaddr_in* %3 to i8*
%20 = call i32 @bzero(%struct.sockaddr_in* %3, 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 = tail 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 %36, label %31
31: ; preds = %18
%32 = load i8*, i8** %26
%33 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %32)
%34 = call i32 @close(i32 %13)
%35 = call i32 @exit(i32 0)
unreachable
36: ; preds = %18
%37 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%38 = call i64 @connect(i32 %13, %struct.sockaddr* %37, i32 12)
%39 = icmp eq i64 %38, 0
br i1 %39, label %44, label %40
40: ; preds = %36
%41 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
%42 = call i32 @close(i32 %13)
%43 = call i32 @exit(i32 0)
unreachable
44: ; preds = %36
%45 = getelementptr inbounds [51 x i8], [51 x i8]* %4, i64 0, i64 0
br label %46
46: ; preds = %49, %44
%47 = call i32 @read(i32 %13, i8* %45, i32 50)
%48 = icmp sgt i32 %47, 0
br i1 %48, label %49, label %60
49: ; preds = %46
%50 = zext i32 %47 to i64
%51 = getelementptr inbounds [51 x i8], [51 x i8]* %4, i64 0, i64 %50
store i8 0, i8* %51
%52 = load i64, i64* @EOF
%53 = load i32, i32* @stdout
%54 = call i64 @fputs(i8* %45, i32 %53)
%55 = icmp eq i64 %52, %54
br i1 %55, label %56, label %46
56: ; preds = %49
%57 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
%58 = call i32 @close(i32 %13)
%59 = call i32 @exit(i32 0)
unreachable
60: ; preds = %46
%61 = icmp slt i32 %47, 0
br i1 %61, label %62, label %66
62: ; preds = %60
%63 = load i32, i32* @errno
%64 = call i8* @strerror(i32 %63)
%65 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0), i8* %64)
br label %66
66: ; preds = %62, %60
%67 = call i32 @close(i32 %13)
%68 = call i32 @exit(i32 0)
unreachable
}