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


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11eb <main+0x4b>
mov    0x2e06(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe26(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1371 <main+0x1d1>
mov    0x2df6(%rip),%rax
mov    (%rax),%edi
mov    0x2dcd(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    1228 <main+0x88>
lea    0xdff(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1371 <main+0x1d1>
lea    -0x1030(%rbp),%rdi
mov    $0xc,%esi
call   1090 <bzero@plt>
mov    0x2da8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1028(%rbp)
mov    $0xd,%edi
call   1030 <htons@plt>
mov    %eax,-0x102c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x1030(%rbp),%rsi
call   1070 <inet_aton@plt>
cmp    $0x0,%rax
jg     129f <main+0xff>
mov    0x2d53(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xd95(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1371 <main+0x1d1>
mov    -0x14(%rbp),%edi
lea    -0x1030(%rbp),%rsi
mov    $0xc,%edx
call   10b0 <connect@plt>
cmp    $0x0,%rax
jge    12d5 <main+0x135>
lea    0xd77(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1371 <main+0x1d1>
jmp    12da <main+0x13a>
mov    -0x14(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    $0x1000,%edx
call   1050 <read@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jle    1348 <main+0x1a8>
movslq -0x18(%rbp),%rax
movb   $0x0,-0x1020(%rbp,%rax,1)
lea    -0x1020(%rbp),%rdi
mov    0x2cac(%rip),%rax
mov    (%rax),%esi
call   1040 <fputs@plt>
mov    0x2cbe(%rip),%rcx
cmp    (%rcx),%rax
jne    1343 <main+0x1a3>
lea    0xd17(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1371 <main+0x1d1>
jmp    12da <main+0x13a>
cmpl   $0x0,-0x18(%rbp)
jge    136a <main+0x1ca>
lea    0xcfc(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1371 <main+0x1d1>
xor    %edi,%edi
call   10a0 <exit@plt>
mov    -0x4(%rbp),%eax
add    $0x1030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x1018,%rsp
mov    %rsi,%r14
cmp    $0x2,%edi
jne    1221 <main+0x81>
mov    0x2e2c(%rip),%r15
mov    (%r15),%edi
mov    0x2e02(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
test   %eax,%eax
js     124c <main+0xac>
mov    %eax,%ebp
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0xc,%esi
call   1090 <bzero@plt>
mov    (%r15),%eax
mov    %eax,0x8(%rsp)
mov    $0xd,%edi
call   1030 <htons@plt>
mov    %eax,0x4(%rsp)
mov    0x8(%r14),%rdi
mov    %rbx,%rsi
call   1070 <inet_aton@plt>
test   %rax,%rax
jg     1255 <main+0xb5>
mov    0x2dbe(%rip),%rax
mov    (%rax),%edi
mov    0x8(%r14),%rdx
lea    0xe04(%rip),%rsi
jmp    1234 <main+0x94>
mov    0x2da8(%rip),%rax
mov    (%rax),%edi
mov    (%r14),%rdx
lea    0xdcc(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%eax
add    $0x1018,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdc3(%rip),%rdi
jmp    12cc <main+0x12c>
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0xc,%edx
call   10b0 <connect@plt>
test   %rax,%rax
jns    1272 <main+0xd2>
lea    0xdcb(%rip),%rdi
jmp    12cc <main+0x12c>
lea    0x10(%rsp),%rbx
mov    0x2d42(%rip),%r14
mov    0x2d5b(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
nop
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1000,%edx
call   1050 <read@plt>
test   %eax,%eax
jle    12c3 <main+0x123>
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+0xf0>
lea    0xd88(%rip),%rdi
jmp    12cc <main+0x12c>
jns    12d6 <main+0x136>
lea    0xd89(%rip),%rdi
call   1080 <perror@plt>
jmp    1239 <main+0x99>
xor    %edi,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [22 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 = [24 x i8] c"inet_aton error for %s\0A\00"
@.str.3 = [14 x i8] c"connect error\00"
@stdout = global i32 0
@EOF = global i64 0
@.str.4 = [12 x i8] c"fputs error\00"
@.str.5 = [11 x i8] c"read error\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 [4097 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 %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 @fprintf(i32 %13, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %16)
store i32 1, i32* %3
br label %75
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 slt i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %18
%24 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %75
25: ; preds = %18
%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 i8**, i8*** %5
%32 = getelementptr inbounds i8*, i8** %31, i64 1
%33 = load i8*, i8** %32
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
%35 = call i64 @inet_aton(i8* %33, i32* %34)
%36 = icmp sle i64 %35, 0
br i1 %36, label %37, label %43
37: ; preds = %25
%38 = load i32, i32* @stderr
%39 = load i8**, i8*** %5
%40 = getelementptr inbounds i8*, i8** %39, i64 1
%41 = load i8*, i8** %40
%42 = call i32 @fprintf(i32 %38, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %41)
store i32 1, i32* %3
br label %75
43: ; preds = %25
%44 = load i32, i32* %6
%45 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%46 = call i64 @connect(i32 %44, %struct.sockaddr* %45, i32 12)
%47 = icmp slt i64 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %43
%49 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %3
br label %75
50: ; preds = %43
br label %51
51: ; preds = %67, %50
%52 = load i32, i32* %6
%53 = getelementptr inbounds [4097 x i8], [4097 x i8]* %8, i64 0, i64 0
%54 = call i32 @read(i32 %52, i8* %53, i32 4096)
store i32 %54, i32* %7
%55 = icmp sgt i32 %54, 0
br i1 %55, label %56, label %68
56: ; preds = %51
%57 = load i32, i32* %7
%58 = sext i32 %57 to i64
%59 = getelementptr inbounds [4097 x i8], [4097 x i8]* %8, i64 0, i64 %58
store i8 0, i8* %59
%60 = getelementptr inbounds [4097 x i8], [4097 x i8]* %8, i64 0, i64 0
%61 = load i32, i32* @stdout
%62 = call i64 @fputs(i8* %60, i32 %61)
%63 = load i64, i64* @EOF
%64 = icmp eq i64 %62, %63
br i1 %64, label %65, label %67
65: ; preds = %56
%66 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %3
br label %75
67: ; preds = %56
br label %51
68: ; preds = %51
%69 = load i32, i32* %7
%70 = icmp slt i32 %69, 0
br i1 %70, label %71, label %73
71: ; preds = %68
%72 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0))
store i32 1, i32* %3
br label %75
73: ; preds = %68
%74 = call i32 @exit(i32 0)
unreachable
75: ; preds = %71, %65, %48, %37, %23, %12
%76 = load i32, i32* %3
ret i32 %76
}

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