#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);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
scalar_t__ fputs(char *, int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
scalar_t__ inet_pton(int /*<<< orphan*/, char *, int /*<<< orphan*/ *);
int read(int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int main(int argc, char **argv) {
  int sockfd, n;
  char recvline[2048 + 1];
  struct sockaddr_in servaddr;
  if (argc != 2) {
    fprintf(stderr, "usage: a.out <IPaddress>\n");
    exit(1);
  }
  if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
    fprintf(stderr, "socket error\n");
    exit(1);
  }
  bzero(&servaddr, sizeof(servaddr));
  servaddr.sin_family = AF_INET;
  servaddr.sin_port = htons(13);
  if (inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0) {
    fprintf(stderr, "inet_pton error for %s\n", argv[1]);
    exit(1);
  }
  if (connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) {
    fprintf(stderr, "connect error\n");
    exit(1);
  }
  while ((n = read(sockfd, recvline, 2048)) > 0) {
    recvline[n] = 0;
    if (fputs(recvline, stdout) == EOF) {
      fprintf(stderr, "fputs error\n");
      exit(1);
    }
  }
  if (n < 0) {
    fprintf(stderr, "read error\n");
    exit(1);
  }
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x830,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11d4 <main+0x44>
mov    0x2e16(%rip),%rax
mov    (%rax),%edi
lea    0xe3d(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    0x2e0d(%rip),%rax
mov    (%rax),%edi
mov    0x2de4(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    121a <main+0x8a>
mov    0x2dd0(%rip),%rax
mov    (%rax),%edi
lea    0xe11(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    -0x830(%rbp),%rdi
mov    $0xc,%esi
call   1080 <bzero@plt>
mov    0x2db6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x828(%rbp)
mov    $0xd,%edi
call   1030 <htons@plt>
mov    %eax,-0x82c(%rbp)
mov    0x2d97(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    -0x830(%rbp),%rdx
call   1070 <inet_pton@plt>
cmp    $0x0,%rax
jg     129a <main+0x10a>
mov    0x2d58(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xd9f(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x14(%rbp),%edi
lea    -0x830(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <connect@plt>
cmp    $0x0,%rax
jge    12d9 <main+0x149>
mov    0x2d11(%rip),%rax
mov    (%rax),%edi
lea    0xd78(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
jmp    12de <main+0x14e>
mov    -0x14(%rbp),%edi
lea    -0x820(%rbp),%rsi
mov    $0x800,%edx
call   1050 <read@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jle    1355 <main+0x1c5>
movslq -0x18(%rbp),%rax
movb   $0x0,-0x820(%rbp,%rax,1)
lea    -0x820(%rbp),%rdi
mov    0x2ca8(%rip),%rax
mov    (%rax),%esi
call   1040 <fputs@plt>
mov    0x2cba(%rip),%rcx
cmp    (%rcx),%rax
jne    1350 <main+0x1c0>
mov    0x2c9a(%rip),%rax
mov    (%rax),%edi
lea    0xd10(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
jmp    12de <main+0x14e>
cmpl   $0x0,-0x18(%rbp)
jge    1380 <main+0x1f0>
mov    0x2c6a(%rip),%rax
mov    (%rax),%edi
lea    0xced(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
xor    %edi,%edi
call   1090 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x818,%rsp
cmp    $0x2,%edi
jne    11d4 <main+0x44>
mov    %rsi,%r14
mov    0x2e3c(%rip),%rbx
mov    (%rbx),%edi
mov    0x2e13(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
test   %eax,%eax
jns    11f5 <main+0x65>
mov    0x2e07(%rip),%rax
mov    (%rax),%edi
lea    0xe48(%rip),%rsi
jmp    11e4 <main+0x54>
mov    0x2df5(%rip),%rax
mov    (%rax),%edi
lea    0xe1c(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    %eax,%ebp
mov    %rsp,%r15
mov    %r15,%rdi
mov    $0xc,%esi
call   1080 <bzero@plt>
mov    (%rbx),%eax
mov    %eax,0x8(%rsp)
mov    $0xd,%edi
call   1030 <htons@plt>
mov    %eax,0x4(%rsp)
mov    (%rbx),%edi
mov    0x8(%r14),%rsi
mov    %r15,%rdx
call   1070 <inet_pton@plt>
test   %rax,%rax
jg     1253 <main+0xc3>
mov    0x2d9b(%rip),%rax
mov    (%rax),%edi
mov    0x8(%r14),%rdx
lea    0xde6(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0xc,%edx
call   10a0 <connect@plt>
test   %rax,%rax
jns    127c <main+0xec>
mov    0x2d62(%rip),%rax
mov    (%rax),%edi
lea    0xdc9(%rip),%rsi
jmp    11e4 <main+0x54>
lea    0x10(%rsp),%rbx
mov    0x2d38(%rip),%r14
mov    0x2d51(%rip),%r15
nop
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x800,%edx
call   1050 <read@plt>
test   %eax,%eax
jle    12cf <main+0x13f>
mov    %eax,%eax
movb   $0x0,0x10(%rsp,%rax,1)
mov    (%r14),%esi
mov    %rbx,%rdi
call   1040 <fputs@plt>
cmp    (%r15),%rax
jne    1290 <main+0x100>
mov    0x2d0f(%rip),%rax
mov    (%rax),%edi
lea    0xd85(%rip),%rsi
jmp    11e4 <main+0x54>
jns    12e6 <main+0x156>
mov    0x2cf8(%rip),%rax
mov    (%rax),%edi
lea    0xd7b(%rip),%rsi
jmp    11e4 <main+0x54>
xor    %edi,%edi
call   1090 <exit@plt>

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

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