#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*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fputs(char *, int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
scalar_t__ inet_pton(int /*<<< orphan*/, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int read(int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stdout;
int main(int argc, char **argv) {
  int socketfd, n;
  char recvline[256 + 1];
  struct sockaddr_in servaddr;
  if (argc != 2) {
    printf("USAGE : %s <ip address>\n", argv[0]);
    exit(EXIT_FAILURE);
  }
  if ((socketfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
    perror("socket");
    exit(EXIT_FAILURE);
  }
  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) {
    perror("inet_pton");
    exit(EXIT_FAILURE);
  }
  if (connect(socketfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) {
    perror("connect");
    exit(EXIT_FAILURE);
  }
  while ((n = read(socketfd, recvline, 256)) > 0) {
    recvline[n] = 0;
    if (EOF == fputs(recvline, stdout)) {
      perror("fputs");
      exit(EXIT_FAILURE);
    }
  }
  if (n < 0) {
    perror("read");
    exit(EXIT_FAILURE);
  }
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x140,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11e4 <main+0x44>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe2f(%rip),%rdi
call   1040 <printf@plt>
mov    0x2deb(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    0x2dfd(%rip),%rax
mov    (%rax),%edi
mov    0x2ddc(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    1223 <main+0x83>
lea    0xe09(%rip),%rdi
call   1080 <perror@plt>
mov    0x2dac(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
lea    -0x130(%rbp),%rdi
mov    $0xc,%esi
call   1090 <bzero@plt>
mov    0x2dad(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x128(%rbp)
mov    $0xd,%edi
call   1030 <htons@plt>
mov    %eax,-0x12c(%rbp)
mov    0x2d8e(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    -0x130(%rbp),%rdx
call   1070 <inet_pton@plt>
cmp    $0x0,%rax
jg     1294 <main+0xf4>
lea    0xd9f(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d3b(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x14(%rbp),%edi
lea    -0x130(%rbp),%rsi
mov    $0xc,%edx
call   10b0 <connect@plt>
cmp    $0x0,%rax
jge    12cc <main+0x12c>
lea    0xd71(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d03(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
jmp    12d1 <main+0x131>
mov    -0x14(%rbp),%edi
lea    -0x120(%rbp),%rsi
mov    $0x100,%edx
call   1060 <read@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jle    1355 <main+0x1b5>
movslq -0x18(%rbp),%rax
movb   $0x0,-0x120(%rbp,%rax,1)
mov    0x2cdc(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x138(%rbp)
lea    -0x120(%rbp),%rdi
mov    0x2ca4(%rip),%rax
mov    (%rax),%esi
call   1050 <fputs@plt>
mov    %rax,%rcx
mov    -0x138(%rbp),%rax
cmp    %rcx,%rax
jne    1350 <main+0x1b0>
lea    0xcf5(%rip),%rdi
call   1080 <perror@plt>
mov    0x2c7f(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
jmp    12d1 <main+0x131>
cmpl   $0x0,-0x18(%rbp)
jge    1379 <main+0x1d9>
lea    0xcd2(%rip),%rdi
call   1080 <perror@plt>
mov    0x2c56(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    0x2c38(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x120,%rsp
mov    %rsi,%r14
cmp    $0x2,%edi
jne    11e0 <main+0x40>
mov    0x2e2a(%rip),%rbx
mov    (%rbx),%edi
mov    0x2e09(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
test   %eax,%eax
jns    11f4 <main+0x54>
lea    0xe3e(%rip),%rdi
jmp    12b0 <main+0x110>
mov    (%r14),%rsi
lea    0xe16(%rip),%rdi
call   1040 <printf@plt>
jmp    12b5 <main+0x115>
mov    %eax,%r12d
mov    %rsp,%r15
mov    %r15,%rdi
mov    $0xc,%esi
call   1090 <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     1237 <main+0x97>
lea    0xdeb(%rip),%rdi
jmp    12b0 <main+0x110>
mov    %rsp,%rsi
mov    %r12d,%edi
mov    $0xc,%edx
call   10b0 <connect@plt>
test   %rax,%rax
jns    1255 <main+0xb5>
lea    0xdd7(%rip),%rdi
jmp    12b0 <main+0x110>
lea    0x10(%rsp),%rbx
mov    0x2d7f(%rip),%r14
mov    0x2d58(%rip),%r15
nopl   0x0(%rax,%rax,1)
mov    %r12d,%edi
mov    %rbx,%rsi
mov    $0x100,%edx
call   1060 <read@plt>
test   %eax,%eax
jle    12a7 <main+0x107>
mov    %eax,%eax
movb   $0x0,0x10(%rsp,%rax,1)
mov    (%r14),%rbp
mov    (%r15),%esi
mov    %rbx,%rdi
call   1050 <fputs@plt>
cmp    %rax,%rbp
jne    1270 <main+0xd0>
lea    0xd8d(%rip),%rdi
jmp    12b0 <main+0x110>
jns    12c3 <main+0x123>
lea    0xd88(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d0c(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    0x2cee(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [25 x i8] c"USAGE : %s <ip address>\0A\00"
@EXIT_FAILURE = global i32 0
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [7 x i8] c"socket\00"
@.str.2 = [10 x i8] c"inet_pton\00"
@.str.3 = [8 x i8] c"connect\00"
@EOF = global i64 0
@stdout = global i32 0
@.str.4 = [6 x i8] c"fputs\00"
@.str.5 = [5 x i8] c"read\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca [257 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 %19
12: ; preds = %2
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 0
%15 = load i8*, i8** %14
%16 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %15)
%17 = load i32, i32* @EXIT_FAILURE
%18 = call i32 @exit(i32 %17)
unreachable
19: ; preds = %2
%20 = load i32, i32* @AF_INET
%21 = load i32, i32* @SOCK_STREAM
%22 = call i32 @socket(i32 %20, i32 %21, i32 0)
store i32 %22, i32* %6
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %28
24: ; preds = %19
%25 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%26 = load i32, i32* @EXIT_FAILURE
%27 = call i32 @exit(i32 %26)
unreachable
28: ; preds = %19
%29 = call i32 @bzero(%struct.sockaddr_in* %9, i32 12)
%30 = load i32, i32* @AF_INET
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 2
store i32 %30, i32* %31
%32 = call i32 @htons(i32 13)
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 1
store i32 %32, i32* %33
%34 = load i32, i32* @AF_INET
%35 = load i8**, i8*** %5
%36 = getelementptr inbounds i8*, i8** %35, i64 1
%37 = load i8*, i8** %36
%38 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
%39 = call i64 @inet_pton(i32 %34, i8* %37, i32* %38)
%40 = icmp sle i64 %39, 0
br i1 %40, label %41, label %45
41: ; preds = %28
%42 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
%43 = load i32, i32* @EXIT_FAILURE
%44 = call i32 @exit(i32 %43)
unreachable
45: ; preds = %28
%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 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%52 = load i32, i32* @EXIT_FAILURE
%53 = call i32 @exit(i32 %52)
unreachable
54: ; preds = %45
br label %55
55: ; preds = %73, %54
%56 = load i32, i32* %6
%57 = getelementptr inbounds [257 x i8], [257 x i8]* %8, i64 0, i64 0
%58 = call i32 @read(i32 %56, i8* %57, i32 256)
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 [257 x i8], [257 x i8]* %8, i64 0, i64 %62
store i8 0, i8* %63
%64 = load i64, i64* @EOF
%65 = getelementptr inbounds [257 x i8], [257 x i8]* %8, i64 0, i64 0
%66 = load i32, i32* @stdout
%67 = call i64 @fputs(i8* %65, i32 %66)
%68 = icmp eq i64 %64, %67
br i1 %68, label %69, label %73
69: ; preds = %60
%70 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%71 = load i32, i32* @EXIT_FAILURE
%72 = call i32 @exit(i32 %71)
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 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
%79 = load i32, i32* @EXIT_FAILURE
%80 = call i32 @exit(i32 %79)
unreachable
81: ; preds = %74
%82 = load i32, i32* @EXIT_SUCCESS
%83 = call i32 @exit(i32 %82)
unreachable
}

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