#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 {
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  int /*<<< orphan*/ h_length;
  int /*<<< orphan*/ h_addr;
};
typedef int /*<<< orphan*/ saddr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ atoi(char *);
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, int /*<<< orphan*/,
                          int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
scalar_t__ write(int, char *, int);
int main(int argc, char **argv) {
  int sfd, rc;
  char buffer[1024];
  struct sockaddr_in saddr;
  struct hostent *addrent;
  if (argc != 3) {
    fprintf(stderr, "usage: %s server port\n", argv[0]);
    exit(EXIT_FAILURE);
  }
  if (!(addrent = gethostbyname(argv[1]))) {
    perror("gethostbyname()");
    exit(EXIT_FAILURE);
  }
  if ((sfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
    perror("socket()");
    exit(EXIT_FAILURE);
  }
  memset(&saddr, 0, sizeof(saddr));
  saddr.sin_family = AF_INET;
  saddr.sin_port = htons(atoi(argv[2]));
  memcpy(&saddr.sin_addr.s_addr, addrent->h_addr, addrent->h_length);
  if (connect(sfd, (struct sockaddr *)&saddr, sizeof(saddr)) < 0) {
    perror("connect()");
    exit(EXIT_FAILURE);
  }
  if ((rc = read(sfd, buffer, 1024)) < 0) {
    perror("read()");
    exit(EXIT_FAILURE);
  }
  if (write(1, buffer, rc) < 0) {
    perror("write()");
    exit(EXIT_FAILURE);
  }
  close(sfd);
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     121d <main+0x4d>
mov    0x2dde(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xdf6(%rip),%rsi
call   1090 <fprintf@plt>
mov    0x2db2(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <gethostbyname@plt>
mov    %rax,-0x438(%rbp)
cmp    $0x0,%rax
jne    1255 <main+0x85>
lea    0xdd5(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2d7a(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
mov    0x2d8c(%rip),%rax
mov    (%rax),%edi
mov    0x2d6b(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    1294 <main+0xc4>
lea    0xda6(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2d3b(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
lea    -0x430(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2d3a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x428(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10c0 <atoi@plt>
mov    %eax,%edi
call   1040 <htons@plt>
mov    %eax,-0x42c(%rbp)
lea    -0x430(%rbp),%rdi
mov    -0x438(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x438(%rbp),%rax
mov    (%rax),%edx
call   10a0 <memcpy@plt>
mov    -0x14(%rbp),%edi
lea    -0x430(%rbp),%rsi
mov    $0xc,%edx
call   10e0 <connect@plt>
cmp    $0x0,%rax
jge    1327 <main+0x157>
lea    0xd1c(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2ca8(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
mov    -0x14(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
call   1070 <read@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    1361 <main+0x191>
lea    0xcec(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2c6e(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
lea    -0x420(%rbp),%rsi
mov    -0x18(%rbp),%edx
mov    $0x1,%edi
call   1030 <write@plt>
cmp    $0x0,%rax
jge    1399 <main+0x1c9>
lea    0xcbb(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2c36(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    0x2c18(%rip),%rax
mov    (%rax),%eax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x410,%rsp
mov    %rsi,%r14
cmp    $0x3,%edi
jne    12bf <main+0xef>
mov    0x8(%r14),%rdi
call   1080 <gethostbyname@plt>
test   %rax,%rax
je     12d9 <main+0x109>
mov    %rax,%rbx
mov    0x2de1(%rip),%r12
mov    (%r12),%edi
mov    0x2dbe(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
test   %eax,%eax
js     12e2 <main+0x112>
mov    %eax,%ebp
mov    %rsp,%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    (%r12),%eax
mov    %eax,0x8(%rsp)
mov    0x10(%r14),%rdi
call   10c0 <atoi@plt>
mov    %eax,%edi
call   1040 <htons@plt>
mov    %eax,0x4(%rsp)
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
mov    %r15,%rdi
call   10a0 <memcpy@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0xc,%edx
call   10e0 <connect@plt>
test   %rax,%rax
js     12eb <main+0x11b>
lea    0x10(%rsp),%rsi
mov    %ebp,%edi
mov    $0x400,%edx
call   1070 <read@plt>
test   %eax,%eax
js     12f4 <main+0x124>
lea    0x10(%rsp),%rsi
mov    $0x1,%edi
mov    %eax,%edx
call   1030 <write@plt>
test   %rax,%rax
js     12fd <main+0x12d>
mov    %ebp,%edi
call   1060 <close@plt>
mov    0x2d13(%rip),%rax
mov    (%rax),%eax
add    $0x410,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d12(%rip),%rax
mov    (%rax),%edi
mov    (%r14),%rdx
lea    0xd2e(%rip),%rsi
call   1090 <fprintf@plt>
jmp    1309 <main+0x139>
lea    0xd37(%rip),%rdi
jmp    1304 <main+0x134>
lea    0xd3e(%rip),%rdi
jmp    1304 <main+0x134>
lea    0xd3e(%rip),%rdi
jmp    1304 <main+0x134>
lea    0xd3f(%rip),%rdi
jmp    1304 <main+0x134>
lea    0xd3d(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2cb8(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i32, i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [23 x i8] c"usage: %s server port\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [16 x i8] c"gethostbyname()\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.2 = [9 x i8] c"socket()\00"
@.str.3 = [10 x i8] c"connect()\00"
@.str.4 = [7 x i8] c"read()\00"
@.str.5 = [8 x i8] c"write()\00"
@EXIT_SUCCESS = 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 [1024 x i8]
%9 = alloca %struct.sockaddr_in
%10 = alloca %struct.hostent*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp ne i32 %11, 3
br i1 %12, label %13, label %21
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 0
%17 = load i8*, i8** %16
%18 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %17)
%19 = load i32, i32* @EXIT_FAILURE
%20 = call i32 @exit(i32 %19)
unreachable
21: ; preds = %2
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 1
%24 = load i8*, i8** %23
%25 = call %struct.hostent* @gethostbyname(i8* %24)
store %struct.hostent* %25, %struct.hostent** %10
%26 = icmp ne %struct.hostent* %25, null
br i1 %26, label %31, label %27
27: ; preds = %21
%28 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%29 = load i32, i32* @EXIT_FAILURE
%30 = call i32 @exit(i32 %29)
unreachable
31: ; preds = %21
%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 = icmp slt i32 %34, 0
br i1 %35, label %36, label %40
36: ; preds = %31
%37 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%38 = load i32, i32* @EXIT_FAILURE
%39 = call i32 @exit(i32 %38)
unreachable
40: ; preds = %31
%41 = call i32 @memset(%struct.sockaddr_in* %9, i32 0, i32 12)
%42 = load i32, i32* @AF_INET
%43 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 2
store i32 %42, i32* %43
%44 = load i8**, i8*** %5
%45 = getelementptr inbounds i8*, i8** %44, i64 2
%46 = load i8*, i8** %45
%47 = call i32 @atoi(i8* %46)
%48 = call i32 @htons(i32 %47)
%49 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 1
store i32 %48, i32* %49
%50 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
%51 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %50, i32 0, i32 0
%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 = load %struct.hostent*, %struct.hostent** %10
%56 = getelementptr inbounds %struct.hostent, %struct.hostent* %55, i32 0, i32 0
%57 = load i32, i32* %56
%58 = call i32 @memcpy(i32* %51, i32 %54, i32 %57)
%59 = load i32, i32* %6
%60 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%61 = call i64 @connect(i32 %59, %struct.sockaddr* %60, i32 12)
%62 = icmp slt i64 %61, 0
br i1 %62, label %63, label %67
63: ; preds = %40
%64 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
%65 = load i32, i32* @EXIT_FAILURE
%66 = call i32 @exit(i32 %65)
unreachable
67: ; preds = %40
%68 = load i32, i32* %6
%69 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%70 = call i32 @read(i32 %68, i8* %69, i32 1024)
store i32 %70, i32* %7
%71 = icmp slt i32 %70, 0
br i1 %71, label %72, label %76
72: ; preds = %67
%73 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%74 = load i32, i32* @EXIT_FAILURE
%75 = call i32 @exit(i32 %74)
unreachable
76: ; preds = %67
%77 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%78 = load i32, i32* %7
%79 = call i64 @write(i32 1, i8* %77, i32 %78)
%80 = icmp slt i64 %79, 0
br i1 %80, label %81, label %85
81: ; preds = %76
%82 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0))
%83 = load i32, i32* @EXIT_FAILURE
%84 = call i32 @exit(i32 %83)
unreachable
85: ; preds = %76
%86 = load i32, i32* %6
%87 = call i32 @close(i32 %86)
%88 = load i32, i32* @EXIT_SUCCESS
ret i32 %88
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i32, i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [23 x i8] c"usage: %s server port\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [16 x i8] c"gethostbyname()\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.2 = [9 x i8] c"socket()\00"
@.str.3 = [10 x i8] c"connect()\00"
@.str.4 = [7 x i8] c"read()\00"
@.str.5 = [8 x i8] c"write()\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [1024 x i8]
%4 = alloca %struct.sockaddr_in
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%6 = bitcast %struct.sockaddr_in* %4 to i8*
%7 = icmp eq i32 %0, 3
br i1 %7, label %14, label %8
8: ; preds = %2
%9 = load i32, i32* @stderr
%10 = load i8*, i8** %1
%11 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %10)
%12 = load i32, i32* @EXIT_FAILURE
%13 = tail call i32 @exit(i32 %12)
unreachable
14: ; preds = %2
%15 = getelementptr inbounds i8*, i8** %1, i64 1
%16 = load i8*, i8** %15
%17 = tail call %struct.hostent* @gethostbyname(i8* %16)
%18 = icmp eq %struct.hostent* %17, null
br i1 %18, label %19, label %23
19: ; preds = %14
%20 = tail call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%21 = load i32, i32* @EXIT_FAILURE
%22 = tail call i32 @exit(i32 %21)
unreachable
23: ; preds = %14
%24 = load i32, i32* @AF_INET
%25 = load i32, i32* @SOCK_STREAM
%26 = tail call i32 @socket(i32 %24, i32 %25, i32 0)
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %32
28: ; preds = %23
%29 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%30 = load i32, i32* @EXIT_FAILURE
%31 = tail call i32 @exit(i32 %30)
unreachable
32: ; preds = %23
%33 = call i32 @memset(%struct.sockaddr_in* %4, i32 0, i32 12)
%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 i8*, i8** %1, i64 2
%37 = load i8*, i8** %36
%38 = call i32 @atoi(i8* %37)
%39 = call i32 @htons(i32 %38)
%40 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
store i32 %39, i32* %40
%41 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0, i32 0
%42 = getelementptr inbounds %struct.hostent, %struct.hostent* %17, i64 0, i32 1
%43 = load i32, i32* %42
%44 = getelementptr inbounds %struct.hostent, %struct.hostent* %17, i64 0, i32 0
%45 = load i32, i32* %44
%46 = call i32 @memcpy(i32* %41, i32 %43, i32 %45)
%47 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%48 = call i64 @connect(i32 %26, %struct.sockaddr* %47, i32 12)
%49 = icmp slt i64 %48, 0
br i1 %49, label %50, label %54
50: ; preds = %32
%51 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
%52 = load i32, i32* @EXIT_FAILURE
%53 = call i32 @exit(i32 %52)
unreachable
54: ; preds = %32
%55 = call i32 @read(i32 %26, i8* %5, i32 1024)
%56 = icmp slt i32 %55, 0
br i1 %56, label %57, label %61
57: ; preds = %54
%58 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%59 = load i32, i32* @EXIT_FAILURE
%60 = call i32 @exit(i32 %59)
unreachable
61: ; preds = %54
%62 = call i64 @write(i32 1, i8* %5, i32 %55)
%63 = icmp slt i64 %62, 0
br i1 %63, label %64, label %68
64: ; preds = %61
%65 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0))
%66 = load i32, i32* @EXIT_FAILURE
%67 = call i32 @exit(i32 %66)
unreachable
68: ; preds = %61
%69 = call i32 @close(i32 %26)
%70 = load i32, i32* @EXIT_SUCCESS
ret i32 %70
}