#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 */
typedef scalar_t__ uint16_t;
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  scalar_t__ h_length;
  int /*<<< orphan*/ h_addr;
};
typedef int /*<<< orphan*/ serv_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ atoi(char *);
int /*<<< orphan*/ bcopy(int /*<<< orphan*/, char *, size_t);
int /*<<< orphan*/ bzero(char *, int);
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(scalar_t__);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int readn(int, char *, int);
int /*<<< orphan*/ shutdown(int, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
int write(int, char *, int);
int main(int argc, char *argv[]) {
  int sockfd, n;
  uint16_t portno;
  struct sockaddr_in serv_addr;
  struct hostent *server;
  char buffer[256];
  char *p = buffer;
  if (argc < 3) {
    fprintf(stderr, "usage %s hostname port\n", argv[0]);
    exit(0);
  }
  portno = (uint16_t)atoi(argv[2]);
  sockfd = socket(AF_INET, SOCK_STREAM, 0);
  if (sockfd < 0) {
    perror("ERROR opening socket");
    exit(1);
  }
  server = gethostbyname(argv[1]);
  if (server == NULL) {
    fprintf(stderr, "ERROR, no such host\n");
    exit(0);
  }
  bzero((char *)&serv_addr, sizeof(serv_addr));
  serv_addr.sin_family = AF_INET;
  bcopy(server->h_addr, (char *)&serv_addr.sin_addr.s_addr,
        (size_t)server->h_length);
  serv_addr.sin_port = htons(portno);
  if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
    perror("ERROR connecting");
    exit(1);
  }
  printf("Please enter the message: ");
  bzero(buffer, 256);
  fgets(buffer, 255, stdin);
  n = write(sockfd, buffer, 255);
  if (n < 0) {
    perror("ERROR writing to socket");
    exit(1);
  }
  bzero(buffer, 256);
  n = readn(sockfd, p, 255);
  printf("%s\n", buffer);
  shutdown(sockfd, 2);
  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)
lea    -0x140(%rbp),%rax
mov    %rax,-0x148(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    1256 <main+0x56>
mov    0x2d98(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xdb8(%rip),%rsi
mov    $0x0,%al
call   10b0 <fprintf@plt>
xor    %edi,%edi
call   1100 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10f0 <atoi@plt>
mov    %rax,-0x20(%rbp)
mov    0x2d7a(%rip),%rax
mov    (%rax),%edi
mov    0x2d51(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1120 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    12a3 <main+0xa3>
lea    0xd84(%rip),%rdi
call   10d0 <perror@plt>
mov    $0x1,%edi
call   1100 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10a0 <gethostbyname@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    12dd <main+0xdd>
mov    0x2d0a(%rip),%rax
mov    (%rax),%edi
lea    0xd5e(%rip),%rsi
mov    $0x0,%al
call   10b0 <fprintf@plt>
xor    %edi,%edi
call   1100 <exit@plt>
lea    -0x30(%rbp),%rdi
mov    $0xc,%esi
call   10e0 <bzero@plt>
mov    0x2cf6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x38(%rbp),%rax
mov    0x8(%rax),%edi
lea    -0x30(%rbp),%rsi
add    $0x4,%rsi
mov    -0x38(%rbp),%rax
mov    (%rax),%rdx
call   10c0 <bcopy@plt>
mov    -0x20(%rbp),%rdi
call   1050 <htons@plt>
mov    %eax,-0x30(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0xc,%edx
call   1110 <connect@plt>
cmp    $0x0,%rax
jge    134f <main+0x14f>
lea    0xd02(%rip),%rdi
call   10d0 <perror@plt>
mov    $0x1,%edi
call   1100 <exit@plt>
lea    0xcfd(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    -0x140(%rbp),%rdi
mov    $0x100,%esi
call   10e0 <bzero@plt>
lea    -0x140(%rbp),%rdi
mov    0x2c64(%rip),%rax
mov    (%rax),%edx
mov    $0xff,%esi
call   1090 <fgets@plt>
mov    -0x14(%rbp),%edi
lea    -0x140(%rbp),%rsi
mov    $0xff,%edx
call   1030 <write@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    13bf <main+0x1bf>
lea    0xcbe(%rip),%rdi
call   10d0 <perror@plt>
mov    $0x1,%edi
call   1100 <exit@plt>
lea    -0x140(%rbp),%rdi
mov    $0x100,%esi
call   10e0 <bzero@plt>
mov    -0x14(%rbp),%edi
mov    -0x148(%rbp),%rsi
mov    $0xff,%edx
call   1070 <readn@plt>
mov    %eax,-0x18(%rbp)
lea    -0x140(%rbp),%rsi
lea    0xc91(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x14(%rbp),%edi
mov    $0x2,%esi
call   1040 <shutdown@plt>
mov    -0x14(%rbp),%edi
call   1080 <close@plt>
xor    %eax,%eax
add    $0x150,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x110,%rsp
mov    %rsi,%rbx
cmp    $0x2,%edi
jle    1357 <main+0x147>
mov    0x10(%rbx),%rdi
call   1100 <atoi@plt>
mov    %rax,%r14
mov    0x2daa(%rip),%r12
mov    (%r12),%edi
mov    0x2d7f(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1130 <socket@plt>
test   %eax,%eax
js     1378 <main+0x168>
mov    %eax,%ebp
mov    0x8(%rbx),%rdi
call   10b0 <gethostbyname@plt>
test   %rax,%rax
je     1381 <main+0x171>
mov    %rax,%rbx
mov    %rsp,%r15
mov    %r15,%rdi
mov    $0xc,%esi
call   10f0 <bzero@plt>
mov    (%r12),%eax
mov    %eax,0x8(%rsp)
mov    0x8(%rbx),%edi
lea    0x4(%rsp),%rsi
mov    (%rbx),%rdx
call   10d0 <bcopy@plt>
mov    %r14,%rdi
call   1060 <htons@plt>
mov    %eax,(%rsp)
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0xc,%edx
call   1120 <connect@plt>
test   %rax,%rax
js     139f <main+0x18f>
lea    0xd90(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x100,%esi
call   10f0 <bzero@plt>
mov    0x2cfd(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    $0xff,%esi
call   10a0 <fgets@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0xff,%edx
call   1040 <write@plt>
test   %eax,%eax
js     13a8 <main+0x198>
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x100,%esi
call   10f0 <bzero@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0xff,%edx
call   1080 <readn@plt>
mov    %rbx,%rdi
call   1030 <puts@plt>
mov    %ebp,%edi
mov    $0x2,%esi
call   1050 <shutdown@plt>
mov    %ebp,%edi
call   1090 <close@plt>
xor    %eax,%eax
add    $0x110,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2c72(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xc96(%rip),%rsi
xor    %eax,%eax
call   10c0 <fprintf@plt>
xor    %edi,%edi
call   1110 <exit@plt>
lea    0xc99(%rip),%rdi
jmp    13af <main+0x19f>
mov    0x2c48(%rip),%rax
mov    (%rax),%edi
lea    0xc9c(%rip),%rsi
xor    %eax,%eax
call   10c0 <fprintf@plt>
xor    %edi,%edi
call   1110 <exit@plt>
lea    0xc9c(%rip),%rdi
jmp    13af <main+0x19f>
lea    0xcbf(%rip),%rdi
call   10e0 <perror@plt>
mov    $0x1,%edi
call   1110 <exit@plt>

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

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i64, i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [24 x i8] c"usage %s hostname port\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [21 x i8] c"ERROR opening socket\00"
@.str.2 = [21 x i8] c"ERROR, no such host\0A\00"
@.str.3 = [17 x i8] c"ERROR connecting\00"
@.str.4 = [27 x i8] c"Please enter the message: \00"
@stdin = global i32 0
@.str.5 = [24 x i8] c"ERROR writing to socket\00"
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 %13
8: ; preds = %2
%9 = load i32, i32* @stderr
%10 = load i8*, i8** %1
%11 = tail call i32 (i32, i8*, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %10)
%12 = tail call i32 @exit(i32 0)
unreachable
13: ; preds = %2
%14 = getelementptr inbounds i8*, i8** %1, i64 2
%15 = load i8*, i8** %14
%16 = tail call i64 @atoi(i8* %15)
%17 = load i32, i32* @AF_INET
%18 = load i32, i32* @SOCK_STREAM
%19 = tail call i32 @socket(i32 %17, i32 %18, i32 0)
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %13
%22 = tail call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%23 = tail call i32 @exit(i32 1)
unreachable
24: ; preds = %13
%25 = getelementptr inbounds i8*, i8** %1, i64 1
%26 = load i8*, i8** %25
%27 = tail call %struct.hostent* @gethostbyname(i8* %26)
%28 = icmp eq %struct.hostent* %27, null
br i1 %28, label %29, label %33
29: ; preds = %24
%30 = load i32, i32* @stderr
%31 = tail call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%32 = tail call i32 @exit(i32 0)
unreachable
33: ; preds = %24
%34 = call i32 @bzero(i8* %5, i32 12)
%35 = load i32, i32* @AF_INET
%36 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %35, i32* %36
%37 = getelementptr inbounds %struct.hostent, %struct.hostent* %27, i64 0, i32 1
%38 = load i32, i32* %37
%39 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
%40 = bitcast i32* %39 to i8*
%41 = getelementptr inbounds %struct.hostent, %struct.hostent* %27, i64 0, i32 0
%42 = load i64, i64* %41
%43 = call i32 @bcopy(i32 %38, i8* %40, i64 %42)
%44 = call i32 @htons(i64 %16)
%45 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %44, i32* %45
%46 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%47 = call i64 @connect(i32 %19, %struct.sockaddr* %46, i32 12)
%48 = icmp slt i64 %47, 0
br i1 %48, label %49, label %52
49: ; preds = %33
%50 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
%51 = call i32 @exit(i32 1)
unreachable
52: ; preds = %33
%53 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0))
%54 = call i32 @bzero(i8* %6, i32 256)
%55 = load i32, i32* @stdin
%56 = call i32 @fgets(i8* %6, i32 255, i32 %55)
%57 = call i32 @write(i32 %19, i8* %6, i32 255)
%58 = icmp slt i32 %57, 0
br i1 %58, label %59, label %62
59: ; preds = %52
%60 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0))
%61 = call i32 @exit(i32 1)
unreachable
62: ; preds = %52
%63 = call i32 @bzero(i8* %6, i32 256)
%64 = call i32 @readn(i32 %19, i8* %6, i32 255)
%65 = call i32 @puts(i8* %6)
%66 = call i32 @shutdown(i32 %19, i32 2)
%67 = call i32 @close(i32 %19)
ret i32 0
}