#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;
typedef int /*<<< orphan*/ buffer;

/* 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*/ fprintf(int /*<<< orphan*/, char *, ...);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ printf(char *, char *);
int read(int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
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 < 4) {
    fprintf(stderr, "usage %s hostname port message\n", argv[0]);
    exit(0);
  }
  portno = atoi(argv[2]);
  strncpy(buffer, argv[3], sizeof(buffer));
  buffer[sizeof(buffer) - 1] = '\0';
  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);
  }
  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(portno);
  if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
    error("ERROR connecting");
  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   $0x4,-0x8(%rbp)
jge    1248 <main+0x48>
mov    0x2dae(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xdc6(%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    %eax,-0x18(%rbp)
lea    -0x130(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rsi
mov    $0x100,%edx
call   1030 <strncpy@plt>
movb   $0x0,-0x31(%rbp)
mov    0x2d6c(%rip),%rax
mov    (%rax),%edi
mov    0x2d4b(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1120 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    12a7 <main+0xa7>
lea    0xd7e(%rip),%rdi
call   10d0 <error@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10a0 <gethostbyname@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    12e1 <main+0xe1>
mov    0x2d0e(%rip),%rax
mov    (%rax),%edi
lea    0xd62(%rip),%rsi
mov    $0x0,%al
call   10b0 <fprintf@plt>
xor    %edi,%edi
call   1100 <exit@plt>
lea    -0x28(%rbp),%rdi
mov    $0xc,%esi
call   10e0 <bzero@plt>
mov    0x2cf2(%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   10c0 <bcopy@plt>
mov    -0x18(%rbp),%edi
call   1060 <htons@plt>
mov    %eax,-0x28(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   1110 <connect@plt>
cmp    $0x0,%rax
jge    1348 <main+0x148>
lea    0xd07(%rip),%rdi
call   10d0 <error@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x13c(%rbp)
lea    -0x130(%rbp),%rax
mov    %rax,-0x138(%rbp)
lea    -0x130(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x13c(%rbp),%edi
mov    -0x138(%rbp),%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1398 <main+0x198>
lea    0xcc8(%rip),%rdi
call   10d0 <error@plt>
lea    -0x130(%rbp),%rdi
mov    $0x100,%esi
call   10e0 <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    13d6 <main+0x1d6>
lea    0xca2(%rip),%rdi
call   10d0 <error@plt>
lea    -0x130(%rbp),%rsi
lea    0xca9(%rip),%rdi
call   1070 <printf@plt>
mov    -0x14(%rbp),%edi
call   1080 <close@plt>
xor    %eax,%eax
add    $0x140,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x110,%rsp
mov    %rsi,%rbp
cmp    $0x3,%edi
jle    135c <main+0x15c>
mov    0x10(%rbp),%rdi
call   10f0 <atoi@plt>
mov    %eax,%r14d
mov    0x18(%rbp),%rsi
lea    0x10(%rsp),%rdi
mov    $0x100,%edx
call   1030 <strncpy@plt>
movb   $0x0,0x10f(%rsp)
mov    0x2d9f(%rip),%r12
mov    (%r12),%edi
mov    0x2d7c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1120 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    126f <main+0x6f>
lea    0xdb6(%rip),%rdi
call   10d0 <error@plt>
mov    0x8(%rbp),%rdi
call   10a0 <gethostbyname@plt>
test   %rax,%rax
je     137e <main+0x17e>
mov    %rax,%rbp
mov    %rsp,%r15
mov    %r15,%rdi
mov    $0xc,%esi
call   10e0 <bzero@plt>
mov    (%r12),%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    %ebx,%edi
mov    %r15,%rsi
mov    $0xc,%edx
call   1110 <connect@plt>
test   %rax,%rax
jns    12d8 <main+0xd8>
lea    0xd77(%rip),%rdi
call   10d0 <error@plt>
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
call   1050 <strlen@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   1040 <write@plt>
test   %eax,%eax
jns    1301 <main+0x101>
lea    0xd5f(%rip),%rdi
call   10d0 <error@plt>
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x100,%esi
call   10e0 <bzero@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0xff,%edx
call   1090 <read@plt>
test   %eax,%eax
jns    1332 <main+0x132>
lea    0xd46(%rip),%rdi
call   10d0 <error@plt>
lea    0xd54(%rip),%rdi
lea    0x10(%rsp),%rsi
call   1070 <printf@plt>
mov    %ebx,%edi
call   1080 <close@plt>
xor    %eax,%eax
add    $0x110,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2c75(%rip),%rax
mov    (%rax),%edi
mov    0x0(%rbp),%rdx
lea    0xc90(%rip),%rsi
xor    %eax,%eax
call   10b0 <fprintf@plt>
xor    %edi,%edi
call   1100 <exit@plt>
mov    0x2c53(%rip),%rax
mov    (%rax),%edi
lea    0xca7(%rip),%rsi
xor    %eax,%eax
call   10b0 <fprintf@plt>
xor    %edi,%edi
call   1100 <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 = [32 x i8] c"usage %s hostname port message\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 = [24 x i8] c"ERROR writing to socket\00"
@.str.5 = [26 x i8] c"ERROR reading from 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 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, 4
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 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
%26 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 3
%29 = load i8*, i8** %28
%30 = call i32 @strncpy(i8* %26, i8* %29, i32 256)
%31 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 255
store i8 0, i8* %31
%32 = load i32, i32* @AF_INET
%33 = load i32, i32* @SOCK_STREAM
%34 = call i32 @socket(i32 %32, i32 %33, i32 0)
store i32 %34, i32* %6
%35 = load i32, i32* %6
%36 = icmp slt i32 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %21
%38 = call i32 @error(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
br label %39
39: ; preds = %37, %21
%40 = load i8**, i8*** %5
%41 = getelementptr inbounds i8*, i8** %40, i64 1
%42 = load i8*, i8** %41
%43 = call %struct.hostent* @gethostbyname(i8* %42)
store %struct.hostent* %43, %struct.hostent** %10
%44 = load %struct.hostent*, %struct.hostent** %10
%45 = icmp eq %struct.hostent* %44, null
br i1 %45, label %46, label %50
46: ; preds = %39
%47 = load i32, i32* @stderr
%48 = call i32 (i32, i8*, ...) @fprintf(i32 %47, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%49 = call i32 @exit(i32 0)
unreachable
50: ; preds = %39
%51 = bitcast %struct.sockaddr_in* %9 to i8*
%52 = call i32 @bzero(i8* %51, i32 12)
%53 = load i32, i32* @AF_INET
%54 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 2
store i32 %53, i32* %54
%55 = load %struct.hostent*, %struct.hostent** %10
%56 = getelementptr inbounds %struct.hostent, %struct.hostent* %55, i32 0, i32 1
%57 = load i64, i64* %56
%58 = inttoptr i64 %57 to i8*
%59 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 1
%60 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %59, i32 0, i32 0
%61 = bitcast i32* %60 to i8*
%62 = load %struct.hostent*, %struct.hostent** %10
%63 = getelementptr inbounds %struct.hostent, %struct.hostent* %62, i32 0, i32 0
%64 = load i32, i32* %63
%65 = call i32 @bcopy(i8* %58, i8* %61, i32 %64)
%66 = load i32, i32* %7
%67 = call i32 @htons(i32 %66)
%68 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
store i32 %67, i32* %68
%69 = load i32, i32* %6
%70 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%71 = call i64 @connect(i32 %69, %struct.sockaddr* %70, i32 12)
%72 = icmp slt i64 %71, 0
br i1 %72, label %73, label %75
73: ; preds = %50
%74 = call i32 @error(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
br label %75
75: ; preds = %73, %50
%76 = load i32, i32* %6
%77 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%78 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%79 = call i32 @strlen(i8* %78)
%80 = call i32 @write(i32 %76, i8* %77, i32 %79)
store i32 %80, i32* %8
%81 = load i32, i32* %8
%82 = icmp slt i32 %81, 0
br i1 %82, label %83, label %85
83: ; preds = %75
%84 = call i32 @error(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0))
br label %85
85: ; preds = %83, %75
%86 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%87 = call i32 @bzero(i8* %86, i32 256)
%88 = load i32, i32* %6
%89 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%90 = call i32 @read(i32 %88, i8* %89, i32 255)
store i32 %90, i32* %8
%91 = load i32, i32* %8
%92 = icmp slt i32 %91, 0
br i1 %92, label %93, label %95
93: ; preds = %85
%94 = call i32 @error(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0))
br label %95
95: ; preds = %93, %85
%96 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%97 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i8* %96)
%98 = load i32, i32* %6
%99 = call i32 @close(i32 %98)
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 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [32 x i8] c"usage %s hostname port message\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 = [24 x i8] c"ERROR writing to socket\00"
@.str.5 = [26 x i8] c"ERROR reading from socket\00"
@.str.6 = [4 x i8] c"%s\0A\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, 4
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 = 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 3
%18 = load i8*, i8** %17
%19 = call i32 @strncpy(i8* %6, i8* %18, i32 256)
%20 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 255
store i8 0, i8* %20
%21 = load i32, i32* @AF_INET
%22 = load i32, i32* @SOCK_STREAM
%23 = call i32 @socket(i32 %21, i32 %22, i32 0)
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %13
%26 = call i32 @error(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
br label %27
27: ; preds = %25, %13
%28 = getelementptr inbounds i8*, i8** %1, i64 1
%29 = load i8*, i8** %28
%30 = call %struct.hostent* @gethostbyname(i8* %29)
%31 = icmp eq %struct.hostent* %30, null
br i1 %31, label %32, label %36
32: ; preds = %27
%33 = load i32, i32* @stderr
%34 = call i32 (i32, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%35 = call i32 @exit(i32 0)
unreachable
36: ; preds = %27
%37 = call i32 @bzero(i8* %5, i32 12)
%38 = load i32, i32* @AF_INET
%39 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %38, i32* %39
%40 = getelementptr inbounds %struct.hostent, %struct.hostent* %30, i64 0, i32 1
%41 = load i64, i64* %40
%42 = inttoptr i64 %41 to i8*
%43 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
%44 = bitcast i32* %43 to i8*
%45 = getelementptr inbounds %struct.hostent, %struct.hostent* %30, i64 0, i32 0
%46 = load i32, i32* %45
%47 = call i32 @htons(i32 %16)
%48 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %47, i32* %48
%49 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%50 = call i64 @connect(i32 %23, %struct.sockaddr* %49, i32 12)
%51 = icmp slt i64 %50, 0
br i1 %51, label %52, label %54
52: ; preds = %36
%53 = call i32 @error(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
br label %54
54: ; preds = %52, %36
%55 = call i32 @strlen(i8* %6)
%56 = call i32 @write(i32 %23, i8* %6, i32 %55)
%57 = icmp slt i32 %56, 0
br i1 %57, label %58, label %60
58: ; preds = %54
%59 = call i32 @error(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0))
br label %60
60: ; preds = %58, %54
%61 = call i32 @bzero(i8* %6, i32 256)
%62 = call i32 @read(i32 %23, i8* %6, i32 255)
%63 = icmp slt i32 %62, 0
br i1 %63, label %64, label %66
64: ; preds = %60
%65 = call i32 @error(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0))
br label %66
66: ; preds = %64, %60
%67 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i8* %6)
%68 = call i32 @close(i32 %23)
ret i32 0
}