#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;

/* 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);
scalar_t__ connect(int, struct sockaddr_in *, 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*/ 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;
  char buffer[256];
  struct sockaddr_in serv_addr;
  struct hostent *server;
  int n;
  if (argc < 3) {
    fprintf(stderr, "Error: usage %s hostname port \n", argv[0]);
    exit(0);
  }
  sockfd = socket(AF_INET, SOCK_STREAM, 0);
  if (sockfd < 0) {
    error("Error while opening the socket ...");
  }
  bzero((char *)&serv_addr, sizeof(serv_addr));
  portno = atoi(argv[2]);
  server = gethostbyname(argv[1]);
  if (server == NULL) {
    fprintf(stderr, "Error: no such host \n");
    exit(0);
  }
  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, &serv_addr, sizeof(serv_addr)) < 0) {
    error("Error while connecting the socket ...");
  }
  printf("Please enter a message:");
  bzero(buffer, 256);
  fgets(buffer, 255, stdin);
  n = write(sockfd, buffer, strlen(buffer));
  if (n < 0) {
    error("Error while writing to the socket ...");
  }
  bzero(buffer, 256);
  n = read(sockfd, buffer, 255);
  if (n < 0) {
    error("Error while reading from the socket");
  }
  printf("Received an incoming message from the socket: %s \n", buffer);
  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    1238 <main+0x48>
mov    0x2db6(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xdd6(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
xor    %edi,%edi
call   10f0 <exit@plt>
mov    0x2da9(%rip),%rax
mov    (%rax),%edi
mov    0x2d80(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1110 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    126a <main+0x7a>
lea    0xdbb(%rip),%rdi
call   10c0 <error@plt>
lea    -0x130(%rbp),%rdi
mov    $0xc,%esi
call   10d0 <bzero@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10e0 <atoi@plt>
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1090 <gethostbyname@plt>
mov    %rax,-0x138(%rbp)
cmpq   $0x0,-0x138(%rbp)
jne    12cb <main+0xdb>
mov    0x2d1c(%rip),%rax
mov    (%rax),%edi
lea    0xd86(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
xor    %edi,%edi
call   10f0 <exit@plt>
mov    0x2d16(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x128(%rbp)
mov    -0x138(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x130(%rbp),%rsi
add    $0x4,%rsi
mov    -0x138(%rbp),%rax
mov    (%rax),%edx
call   10b0 <bcopy@plt>
mov    -0x18(%rbp),%edi
call   1050 <htons@plt>
mov    %eax,-0x130(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x130(%rbp),%rsi
mov    $0xc,%edx
call   1100 <connect@plt>
cmp    $0x0,%rax
jge    1336 <main+0x146>
lea    0xd28(%rip),%rdi
call   10c0 <error@plt>
lea    0xd42(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    -0x120(%rbp),%rdi
mov    $0x100,%esi
call   10d0 <bzero@plt>
lea    -0x120(%rbp),%rdi
mov    0x2c7d(%rip),%rax
mov    (%rax),%edx
mov    $0xff,%esi
call   1080 <fgets@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x14c(%rbp)
lea    -0x120(%rbp),%rax
mov    %rax,-0x148(%rbp)
lea    -0x120(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x14c(%rbp),%edi
mov    -0x148(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %eax,-0x13c(%rbp)
cmpl   $0x0,-0x13c(%rbp)
jge    13c5 <main+0x1d5>
lea    0xcd7(%rip),%rdi
call   10c0 <error@plt>
lea    -0x120(%rbp),%rdi
mov    $0x100,%esi
call   10d0 <bzero@plt>
mov    -0x14(%rbp),%edi
lea    -0x120(%rbp),%rsi
mov    $0xff,%edx
call   1070 <read@plt>
mov    %eax,-0x13c(%rbp)
cmpl   $0x0,-0x13c(%rbp)
jge    1409 <main+0x219>
lea    0xcb9(%rip),%rdi
call   10c0 <error@plt>
lea    -0x120(%rbp),%rsi
lea    0xcca(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
xor    %eax,%eax
add    $0x150,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x118,%rsp
mov    %rsi,%rbx
cmp    $0x2,%edi
jle    1352 <main+0x162>
mov    0x2dd8(%rip),%r15
mov    (%r15),%edi
mov    0x2dae(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1110 <socket@plt>
mov    %eax,%r14d
test   %eax,%eax
jns    1236 <main+0x46>
lea    0xdef(%rip),%rdi
call   10c0 <error@plt>
mov    %rsp,%rdi
mov    $0xc,%esi
call   10d0 <bzero@plt>
mov    0x10(%rbx),%rdi
call   10e0 <atoi@plt>
mov    %eax,%ebp
mov    0x8(%rbx),%rdi
call   1090 <gethostbyname@plt>
test   %rax,%rax
je     1373 <main+0x183>
mov    (%r15),%ecx
mov    %ecx,0x8(%rsp)
mov    0x8(%rax),%rsi
lea    0x4(%rsp),%rdi
mov    (%rax),%edx
call   10b0 <memmove@plt>
mov    %ebp,%edi
call   1050 <htons@plt>
mov    %eax,(%rsp)
mov    %rsp,%rsi
mov    %r14d,%edi
mov    $0xc,%edx
call   1100 <connect@plt>
test   %rax,%rax
jns    12a2 <main+0xb2>
lea    0xdbc(%rip),%rdi
call   10c0 <error@plt>
lea    0xdd6(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x100,%esi
call   10d0 <bzero@plt>
mov    0x2d17(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    $0xff,%esi
call   1080 <fgets@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
test   %eax,%eax
jns    12fd <main+0x10d>
lea    0xd9f(%rip),%rdi
call   10c0 <error@plt>
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x100,%esi
call   10d0 <bzero@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0xff,%edx
call   1070 <read@plt>
test   %eax,%eax
jns    132f <main+0x13f>
lea    0xd93(%rip),%rdi
call   10c0 <error@plt>
lea    0xdab(%rip),%rdi
lea    0x10(%rsp),%rsi
xor    %eax,%eax
call   1060 <printf@plt>
xor    %eax,%eax
add    $0x118,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2c77(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xc9b(%rip),%rsi
xor    %eax,%eax
call   10a0 <fprintf@plt>
xor    %edi,%edi
call   10f0 <exit@plt>
mov    0x2c56(%rip),%rax
mov    (%rax),%edi
lea    0xcc0(%rip),%rsi
xor    %eax,%eax
call   10a0 <fprintf@plt>
xor    %edi,%edi
call   10f0 <exit@plt>

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

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 = [32 x i8] c"Error: usage %s hostname port \0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [35 x i8] c"Error while opening the socket ...\00"
@.str.2 = [22 x i8] c"Error: no such host \0A\00"
@.str.3 = [38 x i8] c"Error while connecting the socket ...\00"
@.str.4 = [24 x i8] c"Please enter a message:\00"
@stdin = global i32 0
@.str.5 = [38 x i8] c"Error while writing to the socket ...\00"
@.str.6 = [36 x i8] c"Error while reading from the socket\00"
@.str.7 = [51 x i8] c"Received an incoming message from the socket: %s \0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [256 x i8]
%4 = alloca %struct.sockaddr_in
%5 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%6 = bitcast %struct.sockaddr_in* %4 to i8*
%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 ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %10)
%12 = tail call i32 @exit(i32 0)
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 %20
18: ; preds = %13
%19 = tail call i32 @error(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0))
br label %20
20: ; preds = %18, %13
%21 = call i32 @bzero(i8* %6, i32 12)
%22 = getelementptr inbounds i8*, i8** %1, i64 2
%23 = load i8*, i8** %22
%24 = tail call i32 @atoi(i8* %23)
%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 = %20
%30 = load i32, i32* @stderr
%31 = tail call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
%32 = tail call i32 @exit(i32 0)
unreachable
33: ; preds = %20
%34 = load i32, i32* @AF_INET
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %34, i32* %35
%36 = getelementptr inbounds %struct.hostent, %struct.hostent* %27, i64 0, i32 1
%37 = load i64, i64* %36
%38 = inttoptr i64 %37 to i8*
%39 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, 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 i32, i32* %41
%43 = tail call i32 @htons(i32 %24)
%44 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
store i32 %43, i32* %44
%45 = call i64 @connect(i32 %16, %struct.sockaddr_in* %4, i32 12)
%46 = icmp slt i64 %45, 0
br i1 %46, label %47, label %49
47: ; preds = %33
%48 = call i32 @error(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0))
br label %49
49: ; preds = %47, %33
%50 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0))
%51 = call i32 @bzero(i8* %5, i32 256)
%52 = load i32, i32* @stdin
%53 = call i32 @fgets(i8* %5, i32 255, i32 %52)
%54 = call i32 @strlen(i8* %5)
%55 = call i32 @write(i32 %16, i8* %5, i32 %54)
%56 = icmp slt i32 %55, 0
br i1 %56, label %57, label %59
57: ; preds = %49
%58 = call i32 @error(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.5, i64 0, i64 0))
br label %59
59: ; preds = %57, %49
%60 = call i32 @bzero(i8* %5, i32 256)
%61 = call i32 @read(i32 %16, i8* %5, i32 255)
%62 = icmp slt i32 %61, 0
br i1 %62, label %63, label %65
63: ; preds = %59
%64 = call i32 @error(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.6, i64 0, i64 0))
br label %65
65: ; preds = %63, %59
%66 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([51 x i8], [51 x i8]* @.str.7, i64 0, i64 0), i8* %5)
ret i32 0
}