#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 sockaddr {
  int dummy;
};
struct hostent {
  int /*<<< orphan*/ h_length;
  scalar_t__ h_addr;
};
typedef int /*<<< orphan*/ serv_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
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 *, int);
int /*<<< orphan*/ error(char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/ *, int);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ strlen(char *);
int write(int, char *, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  int sockfd, portno, n;
  struct sockaddr_in serv_addr;
  struct hostent *server;
  char buffer[256];
  if (argc < 3) {
    fprintf(stderr, "usage %s hostname port\n", argv[0]);
    exit(0);
  }
  portno = atoi(argv[2]);
  while (1) {
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd < 0)
      error("Error,opening socket");
    server = gethostbyname(argv[1]);
    if (server == NULL) {
      fprintf(stderr, "Error, no such host\n");
      exit(0);
    }
    memset((char *)&serv_addr, NULL, 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(portno);
    if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
      error("Error connecting");
    printf("Please enter the message: ");
    bzero(buffer, 256);
    fgets(buffer, 255, stdin);
    n = write(sockfd, buffer, strlen(buffer));
    if (n < 0)
      error("Error writing to socket");
    bzero(buffer, 256);
    n = read(sockfd, buffer, 255);
    if (n < 0)
      error("Error reading from socket");
    printf("%s\n", buffer);
    close(sockfd);
  }
  return 0;
}


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   $0x3,-0x8(%rbp)
jge    1258 <main+0x48>
mov    0x2d96(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xdb6(%rip),%rsi
mov    $0x0,%al
call   10c0 <fprintf@plt>
xor    %edi,%edi
call   1110 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1100 <atoi@plt>
mov    %eax,-0x18(%rbp)
mov    0x2d79(%rip),%rax
mov    (%rax),%edi
mov    0x2d50(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1130 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    129a <main+0x8a>
lea    0xd83(%rip),%rdi
call   10e0 <error@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10b0 <gethostbyname@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    12d4 <main+0xc4>
mov    0x2d13(%rip),%rax
mov    (%rax),%edi
lea    0xd67(%rip),%rsi
mov    $0x0,%al
call   10c0 <fprintf@plt>
xor    %edi,%edi
call   1110 <exit@plt>
lea    -0x28(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    0x2cfb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x30(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x28(%rbp),%rsi
add    $0x4,%rsi
mov    -0x30(%rbp),%rax
mov    (%rax),%edx
call   10d0 <bcopy@plt>
mov    -0x18(%rbp),%edi
call   1050 <htons@plt>
mov    %eax,-0x28(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   1120 <connect@plt>
cmp    $0x0,%rax
jge    133f <main+0x12f>
lea    0xd08(%rip),%rdi
call   10e0 <error@plt>
lea    0xd0d(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    -0x130(%rbp),%rdi
mov    $0x100,%esi
call   10f0 <bzero@plt>
lea    -0x130(%rbp),%rdi
mov    0x2c74(%rip),%rax
mov    (%rax),%edx
mov    $0xff,%esi
call   10a0 <fgets@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x13c(%rbp)
lea    -0x130(%rbp),%rax
mov    %rax,-0x138(%rbp)
lea    -0x130(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x13c(%rbp),%edi
mov    -0x138(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    13c8 <main+0x1b8>
lea    0xcab(%rip),%rdi
call   10e0 <error@plt>
lea    -0x130(%rbp),%rdi
mov    $0x100,%esi
call   10f0 <bzero@plt>
mov    -0x14(%rbp),%edi
lea    -0x130(%rbp),%rsi
mov    $0xff,%edx
call   1090 <read@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1406 <main+0x1f6>
lea    0xc85(%rip),%rdi
call   10e0 <error@plt>
lea    -0x130(%rbp),%rsi
lea    0xc8c(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x14(%rbp),%edi
call   1080 <close@plt>
jmp    1268 <main+0x58>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x128,%rsp
mov    %rsi,%rbx
cmp    $0x2,%edi
jg     125a <main+0x3a>
mov    0x2d90(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xdb4(%rip),%rsi
xor    %eax,%eax
call   10d0 <fprintf@plt>
xor    %edi,%edi
call   1120 <exit@plt>
mov    0x10(%rbx),%rdi
call   1110 <atoi@plt>
mov    %eax,0xc(%rsp)
lea    0x10(%rsp),%r12
lea    0x20(%rsp),%r13
jmp    1290 <main+0x70>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r13,%rdi
call   1030 <puts@plt>
mov    %r14d,%edi
call   1090 <close@plt>
mov    0x2d51(%rip),%rax
mov    (%rax),%edi
mov    0x2d28(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1140 <socket@plt>
mov    %eax,%r14d
test   %eax,%eax
jns    12bc <main+0x9c>
lea    0xd61(%rip),%rdi
call   10f0 <error@plt>
mov    %rbx,%rbp
mov    0x8(%rbx),%rdi
call   10c0 <gethostbyname@plt>
test   %rax,%rax
je     13be <main+0x19e>
mov    %rax,%r15
mov    %r12,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1080 <memset@plt>
mov    0x2cfe(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    0x8(%r15),%rsi
mov    (%r15),%edx
lea    0x14(%rsp),%rdi
call   10e0 <memmove@plt>
mov    0xc(%rsp),%edi
call   1060 <htons@plt>
mov    %eax,0x10(%rsp)
mov    %r14d,%edi
mov    %r12,%rsi
mov    $0xc,%edx
call   1130 <connect@plt>
test   %rax,%rax
jns    132f <main+0x10f>
lea    0xd18(%rip),%rdi
call   10f0 <error@plt>
lea    0xd1d(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %r13,%rdi
mov    $0x100,%esi
call   1100 <bzero@plt>
mov    0x2c8f(%rip),%rax
mov    (%rax),%edx
mov    %r13,%rdi
mov    $0xff,%esi
call   10b0 <fgets@plt>
mov    %r13,%rdi
call   1050 <strlen@plt>
mov    %r14d,%edi
mov    %r13,%rsi
mov    %eax,%edx
call   1040 <write@plt>
test   %eax,%eax
mov    %rbp,%rbx
jns    1388 <main+0x168>
lea    0xceb(%rip),%rdi
call   10f0 <error@plt>
mov    %r13,%rdi
mov    $0x100,%esi
call   1100 <bzero@plt>
mov    %r14d,%edi
mov    %r13,%rsi
mov    $0xff,%edx
call   10a0 <read@plt>
test   %eax,%eax
jns    1280 <main+0x60>
lea    0xcd2(%rip),%rdi
call   10f0 <error@plt>
jmp    1280 <main+0x60>
mov    0x2c0b(%rip),%rax
mov    (%rax),%edi
lea    0xc5f(%rip),%rsi
xor    %eax,%eax
call   10d0 <fprintf@plt>
xor    %edi,%edi
call   1120 <exit@plt>

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

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
%struct.hostent = type { i32, i64 }
@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 = [26 x i8] c"Error reading from 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 i32 @atoi(i8* %15)
%17 = getelementptr inbounds i8*, i8** %1, i64 1
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
%20 = bitcast i32* %19 to i8*
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
%22 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
br label %23
23: ; preds = %67, %13
%24 = load i32, i32* @AF_INET
%25 = load i32, i32* @SOCK_STREAM
%26 = call i32 @socket(i32 %24, i32 %25, i32 0)
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %23
%29 = call i32 @error(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
br label %30
30: ; preds = %28, %23
%31 = load i8*, i8** %17
%32 = call %struct.hostent* @gethostbyname(i8* %31)
%33 = icmp eq %struct.hostent* %32, null
br i1 %33, label %34, label %38
34: ; preds = %30
%35 = load i32, i32* @stderr
%36 = call i32 (i32, i8*, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%37 = call i32 @exit(i32 0)
unreachable
38: ; preds = %30
%39 = call i32 @memset(i8* %5, i32* null, i32 12)
%40 = load i32, i32* @AF_INET
store i32 %40, i32* %18
%41 = getelementptr inbounds %struct.hostent, %struct.hostent* %32, i64 0, i32 1
%42 = load i64, i64* %41
%43 = inttoptr i64 %42 to i8*
%44 = getelementptr inbounds %struct.hostent, %struct.hostent* %32, i64 0, i32 0
%45 = load i32, i32* %44
%46 = call i32 @htons(i32 %16)
store i32 %46, i32* %21
%47 = call i64 @connect(i32 %26, %struct.sockaddr* %22, i32 12)
%48 = icmp slt i64 %47, 0
br i1 %48, label %49, label %51
49: ; preds = %38
%50 = call i32 @error(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
br label %51
51: ; preds = %49, %38
%52 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0))
%53 = call i32 @bzero(i8* %6, i32 256)
%54 = load i32, i32* @stdin
%55 = call i32 @fgets(i8* %6, i32 255, i32 %54)
%56 = call i32 @strlen(i8* %6)
%57 = call i32 @write(i32 %26, i8* %6, i32 %56)
%58 = icmp slt i32 %57, 0
br i1 %58, label %59, label %61
59: ; preds = %51
%60 = call i32 @error(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0))
br label %61
61: ; preds = %59, %51
%62 = call i32 @bzero(i8* %6, i32 256)
%63 = call i32 @read(i32 %26, i8* %6, i32 255)
%64 = icmp slt i32 %63, 0
br i1 %64, label %65, label %67
65: ; preds = %61
%66 = call i32 @error(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.6, i64 0, i64 0))
br label %67
67: ; preds = %65, %61
%68 = call i32 @puts(i8* %6)
%69 = call i32 @close(i32 %26)
br label %23
}