#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct hostent {
  int /*<<< orphan*/ h_length;
  scalar_t__ h_addr;
};
typedef int /*<<< orphan*/ serv_addr;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SHUT_RDWR;
int /*<<< orphan*/ SOCK_STREAM;
int atoi(char *);
int /*<<< orphan*/ bcopy(char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ bzero(char *, int);
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr_in *, int);
int /*<<< orphan*/ feof(int /*<<< orphan*/);
int /*<<< orphan*/ ferror(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int fread(char *, int, int, int /*<<< orphan*/);
int /*<<< orphan*/ fwrite(char *, int, int, int /*<<< orphan*/);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ send(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ shutdown(int, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
int main(int argc, char *argv[]) {
  int port, sockfd;
  struct hostent *server;
  struct sockaddr_in serv_addr;
  char buf[256];
  int size;
  char *ret;
  if (argc < 3) {
    fprintf(stderr, "usage is %s hostname port", argv[0]);
    return 1;
  }
  port = atoi(argv[2]);
  server = gethostbyname(argv[1]);
  if (server == NULL) {
    fprintf(stderr, "no such host", argv[0]);
    return 1;
  }
  sockfd = socket(AF_INET, SOCK_STREAM, 0);
  bzero((char *)&serv_addr, sizeof(serv_addr));
  serv_addr.sin_family = AF_INET;
  bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr,
        server->h_length);
  serv_addr.sin_port = htons(port);
  if (connect(sockfd, &serv_addr, sizeof(serv_addr)) < 0) {
    fprintf(stderr, "error connecting", argv[0]);
    return 1;
  }
  while (!feof(stdin) && !ferror(stdin)) {
    size = fread(buf, 1, sizeof(buf), stdin);
    send(sockfd, buf, size, 0);
    fwrite(buf, 1, size, stdout);
    if (size < sizeof(buf))
      break;
  }
  shutdown(sockfd, SHUT_RDWR);
  close(sockfd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x150,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    123b <main+0x4b>
mov    0x2dae(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xdd6(%rip),%rsi
call   10a0 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    141c <main+0x22c>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10e0 <atoi@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1090 <gethostbyname@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    128f <main+0x9f>
mov    0x2d5a(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xd9c(%rip),%rsi
call   10a0 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    141c <main+0x22c>
mov    0x2d4a(%rip),%rax
mov    (%rax),%edi
mov    0x2d21(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1110 <socket@plt>
mov    %eax,-0x18(%rbp)
lea    -0x30(%rbp),%rdi
mov    $0xc,%esi
call   10d0 <bzero@plt>
mov    0x2d20(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x30(%rbp),%rsi
add    $0x4,%rsi
mov    -0x20(%rbp),%rax
mov    (%rax),%edx
call   10c0 <bcopy@plt>
mov    -0x14(%rbp),%edi
call   1060 <htons@plt>
mov    %eax,-0x30(%rbp)
mov    -0x18(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0xc,%edx
call   10f0 <connect@plt>
cmp    $0x0,%rax
jge    132e <main+0x13e>
mov    0x2cbb(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xd0a(%rip),%rsi
call   10a0 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    141c <main+0x22c>
jmp    1333 <main+0x143>
mov    0x2c9e(%rip),%rax
mov    (%rax),%edi
call   10b0 <feof@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x141(%rbp)
jne    1370 <main+0x180>
mov    0x2c7d(%rip),%rax
mov    (%rax),%edi
call   1030 <ferror@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
mov    %al,-0x141(%rbp)
mov    -0x141(%rbp),%al
test   $0x1,%al
jne    1383 <main+0x193>
jmp    13fc <main+0x20c>
lea    -0x130(%rbp),%rdi
mov    0x2c47(%rip),%rax
mov    (%rax),%ecx
mov    $0x1,%esi
mov    $0x100,%edx
call   1040 <fread@plt>
mov    %eax,-0x134(%rbp)
mov    -0x18(%rbp),%edi
lea    -0x130(%rbp),%rsi
mov    -0x134(%rbp),%edx
xor    %ecx,%ecx
call   1070 <send@plt>
lea    -0x130(%rbp),%rdi
mov    -0x134(%rbp),%edx
mov    0x2be5(%rip),%rax
mov    (%rax),%ecx
mov    $0x1,%esi
call   1100 <fwrite@plt>
movslq -0x134(%rbp),%rax
cmp    $0x100,%rax
jae    13f7 <main+0x207>
jmp    13fc <main+0x20c>
jmp    1333 <main+0x143>
mov    -0x18(%rbp),%edi
mov    0x2bea(%rip),%rax
mov    (%rax),%esi
call   1050 <shutdown@plt>
mov    -0x18(%rbp),%edi
call   1080 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x150,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x118,%rsp
mov    %rsi,%rbx
cmp    $0x2,%edi
jg     1221 <main+0x31>
mov    0x2db8(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xde4(%rip),%rsi
jmp    135b <main+0x16b>
mov    0x10(%rbx),%rdi
call   10e0 <atoi@plt>
mov    %eax,%r14d
mov    0x8(%rbx),%rdi
call   1090 <gethostbyname@plt>
test   %rax,%rax
je     1333 <main+0x143>
mov    %rax,%rbp
mov    0x2d97(%rip),%r13
mov    0x0(%r13),%edi
mov    0x2d6c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1110 <socket@plt>
mov    %eax,%r15d
mov    %rsp,%r12
mov    %r12,%rdi
mov    $0xc,%esi
call   10d0 <bzero@plt>
mov    0x0(%r13),%eax
mov    %eax,0x8(%rsp)
mov    0x8(%rbp),%rsi
lea    0x4(%rsp),%rdi
mov    0x0(%rbp),%edx
call   10c0 <memmove@plt>
mov    %r14d,%edi
call   1060 <htons@plt>
mov    %eax,(%rsp)
mov    %r15d,%edi
mov    %r12,%rsi
mov    $0xc,%edx
call   10f0 <connect@plt>
test   %rax,%rax
js     1348 <main+0x158>
mov    0x2d24(%rip),%rbx
lea    0x10(%rsp),%r14
mov    0x2cf8(%rip),%r12
mov    (%rbx),%edi
call   10b0 <feof@plt>
test   %eax,%eax
jne    1316 <main+0x126>
mov    (%rbx),%edi
call   1030 <ferror@plt>
test   %eax,%eax
jne    1316 <main+0x126>
mov    (%rbx),%ecx
mov    %r14,%rdi
mov    $0x1,%esi
mov    $0x100,%edx
call   1040 <fread@plt>
mov    %eax,%ebp
mov    %r15d,%edi
mov    %r14,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1070 <send@plt>
mov    (%r12),%ecx
mov    %r14,%rdi
mov    $0x1,%esi
mov    %ebp,%edx
call   1100 <fwrite@plt>
cmp    $0xff,%ebp
ja     12c0 <main+0xd0>
mov    0x2cd3(%rip),%rax
mov    (%rax),%esi
mov    %r15d,%edi
call   1050 <shutdown@plt>
mov    %r15d,%edi
call   1080 <close@plt>
xor    %eax,%eax
jmp    1365 <main+0x175>
mov    0x2c8e(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xcd4(%rip),%rsi
jmp    135b <main+0x16b>
mov    0x2c79(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xccc(%rip),%rsi
call   10a0 <fprintf@plt>
mov    $0x1,%eax
add    $0x118,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.hostent = type { i32, i64 }
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
@stderr = global i32 0
@.str = [26 x i8] c"usage is %s hostname port\00"
@.str.1 = [13 x i8] c"no such host\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.2 = [17 x i8] c"error connecting\00"
@stdin = global i32 0
@stdout = global i32 0
@SHUT_RDWR = 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 %struct.hostent*
%9 = alloca %struct.sockaddr_in
%10 = alloca [256 x i8]
%11 = alloca i32
%12 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i32, i32* %4
%14 = icmp slt i32 %13, 3
br i1 %14, label %15, label %21
15: ; preds = %2
%16 = load i32, i32* @stderr
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 0
%19 = load i8*, i8** %18
%20 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %19)
store i32 1, i32* %3
br label %104
21: ; preds = %2
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 2
%24 = load i8*, i8** %23
%25 = call i32 @atoi(i8* %24)
store i32 %25, i32* %6
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 1
%28 = load i8*, i8** %27
%29 = call %struct.hostent* @gethostbyname(i8* %28)
store %struct.hostent* %29, %struct.hostent** %8
%30 = load %struct.hostent*, %struct.hostent** %8
%31 = icmp eq %struct.hostent* %30, null
br i1 %31, label %32, label %38
32: ; preds = %21
%33 = load i32, i32* @stderr
%34 = load i8**, i8*** %5
%35 = getelementptr inbounds i8*, i8** %34, i64 0
%36 = load i8*, i8** %35
%37 = call i32 @fprintf(i32 %33, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* %36)
store i32 1, i32* %3
br label %104
38: ; preds = %21
%39 = load i32, i32* @AF_INET
%40 = load i32, i32* @SOCK_STREAM
%41 = call i32 @socket(i32 %39, i32 %40, i32 0)
store i32 %41, i32* %7
%42 = bitcast %struct.sockaddr_in* %9 to i8*
%43 = call i32 @bzero(i8* %42, i32 12)
%44 = load i32, i32* @AF_INET
%45 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 2
store i32 %44, i32* %45
%46 = load %struct.hostent*, %struct.hostent** %8
%47 = getelementptr inbounds %struct.hostent, %struct.hostent* %46, i32 0, i32 1
%48 = load i64, i64* %47
%49 = inttoptr i64 %48 to i8*
%50 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 1
%51 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %50, i32 0, i32 0
%52 = bitcast i32* %51 to i8*
%53 = load %struct.hostent*, %struct.hostent** %8
%54 = getelementptr inbounds %struct.hostent, %struct.hostent* %53, i32 0, i32 0
%55 = load i32, i32* %54
%56 = call i32 @bcopy(i8* %49, i8* %52, i32 %55)
%57 = load i32, i32* %6
%58 = call i32 @htons(i32 %57)
%59 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
store i32 %58, i32* %59
%60 = load i32, i32* %7
%61 = call i64 @connect(i32 %60, %struct.sockaddr_in* %9, i32 12)
%62 = icmp slt i64 %61, 0
br i1 %62, label %63, label %69
63: ; preds = %38
%64 = load i32, i32* @stderr
%65 = load i8**, i8*** %5
%66 = getelementptr inbounds i8*, i8** %65, i64 0
%67 = load i8*, i8** %66
%68 = call i32 @fprintf(i32 %64, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i8* %67)
store i32 1, i32* %3
br label %104
69: ; preds = %38
br label %70
70: ; preds = %97, %69
%71 = load i32, i32* @stdin
%72 = call i32 @feof(i32 %71)
%73 = icmp ne i32 %72, 0
br i1 %73, label %79, label %74
74: ; preds = %70
%75 = load i32, i32* @stdin
%76 = call i32 @ferror(i32 %75)
%77 = icmp ne i32 %76, 0
%78 = xor i1 %77, true
br label %79
79: ; preds = %74, %70
%80 = phi i1 [ false, %70 ], [ %78, %74 ]
br i1 %80, label %81, label %98
81: ; preds = %79
%82 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%83 = load i32, i32* @stdin
%84 = call i32 @fread(i8* %82, i32 1, i32 256, i32 %83)
store i32 %84, i32* %11
%85 = load i32, i32* %7
%86 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%87 = load i32, i32* %11
%88 = call i32 @send(i32 %85, i8* %86, i32 %87, i32 0)
%89 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%90 = load i32, i32* %11
%91 = load i32, i32* @stdout
%92 = call i32 @fwrite(i8* %89, i32 1, i32 %90, i32 %91)
%93 = load i32, i32* %11
%94 = sext i32 %93 to i64
%95 = icmp ult i64 %94, 256
br i1 %95, label %96, label %97
96: ; preds = %81
br label %98
97: ; preds = %81
br label %70
98: ; preds = %96, %79
%99 = load i32, i32* %7
%100 = load i32, i32* @SHUT_RDWR
%101 = call i32 @shutdown(i32 %99, i32 %100)
%102 = load i32, i32* %7
%103 = call i32 @close(i32 %102)
store i32 0, i32* %3
br label %104
104: ; preds = %98, %63, %32, %15
%105 = load i32, i32* %3
ret i32 %105
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i32, i64 }
@stderr = global i32 0
@.str = [26 x i8] c"usage is %s hostname port\00"
@.str.1 = [13 x i8] c"no such host\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.2 = [17 x i8] c"error connecting\00"
@stdin = global i32 0
@stdout = global i32 0
@SHUT_RDWR = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca [256 x i8]
%5 = bitcast %struct.sockaddr_in* %3 to i8*
%6 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%7 = icmp slt i32 %0, 3
br i1 %7, label %8, label %12
8: ; preds = %2
%9 = load i32, i32* @stderr
%10 = load i8*, i8** %1
%11 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %10)
br label %65
12: ; preds = %2
%13 = getelementptr inbounds i8*, i8** %1, i64 2
%14 = load i8*, i8** %13
%15 = tail call i32 @atoi(i8* %14)
%16 = getelementptr inbounds i8*, i8** %1, i64 1
%17 = load i8*, i8** %16
%18 = tail call %struct.hostent* @gethostbyname(i8* %17)
%19 = icmp eq %struct.hostent* %18, null
br i1 %19, label %20, label %24
20: ; preds = %12
%21 = load i32, i32* @stderr
%22 = load i8*, i8** %1
%23 = tail call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* %22)
br label %65
24: ; preds = %12
%25 = load i32, i32* @AF_INET
%26 = load i32, i32* @SOCK_STREAM
%27 = tail call i32 @socket(i32 %25, i32 %26, i32 0)
%28 = call i32 @bzero(i8* %5, i32 12)
%29 = load i32, i32* @AF_INET
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %29, i32* %30
%31 = getelementptr inbounds %struct.hostent, %struct.hostent* %18, i64 0, i32 1
%32 = load i64, i64* %31
%33 = inttoptr i64 %32 to i8*
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
%35 = bitcast i32* %34 to i8*
%36 = getelementptr inbounds %struct.hostent, %struct.hostent* %18, i64 0, i32 0
%37 = load i32, i32* %36
%38 = tail call i32 @htons(i32 %15)
%39 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %38, i32* %39
%40 = call i64 @connect(i32 %27, %struct.sockaddr_in* %3, i32 12)
%41 = icmp slt i64 %40, 0
br i1 %41, label %42, label %46
42: ; preds = %24
%43 = load i32, i32* @stderr
%44 = load i8*, i8** %1
%45 = call i32 @fprintf(i32 %43, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i8* %44)
br label %65
46: ; preds = %24, %54
%47 = load i32, i32* @stdin
%48 = call i32 @feof(i32 %47)
%49 = icmp eq i32 %48, 0
br i1 %49, label %50, label %61
50: ; preds = %46
%51 = load i32, i32* @stdin
%52 = call i32 @ferror(i32 %51)
%53 = icmp eq i32 %52, 0
br i1 %53, label %54, label %61
54: ; preds = %50
%55 = load i32, i32* @stdin
%56 = call i32 @fread(i8* %6, i32 1, i32 256, i32 %55)
%57 = call i32 @send(i32 %27, i8* %6, i32 %56, i32 0)
%58 = load i32, i32* @stdout
%59 = call i32 @fwrite(i8* %6, i32 1, i32 %56, i32 %58)
%60 = icmp ult i32 %56, 256
br i1 %60, label %61, label %46
61: ; preds = %46, %54, %50
%62 = load i32, i32* @SHUT_RDWR
%63 = call i32 @shutdown(i32 %27, i32 %62)
%64 = call i32 @close(i32 %27)
br label %65
65: ; preds = %61, %42, %20, %8
%66 = phi i32 [ 1, %8 ], [ 1, %20 ], [ 1, %42 ], [ 0, %61 ]
ret i32 %66
}