#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 */

/* Type definitions */
struct sockaddr_in {
  int /*<<< orphan*/ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  int /*<<< orphan*/ h_length;
  int /*<<< orphan*/ *h_addr_list;
};
typedef int /*<<< orphan*/ hostname;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ gethostname(char *, int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, int /*<<< orphan*/,
                          int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int read(int /*<<< orphan*/, char *, int);
scalar_t__ send(int, char *, int, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(char *, char *);
int main(int argc, char *argv[]) {
  int n, s, len;
  char buf[1024];
  char hostname[64];
  struct hostent *hp;
  struct sockaddr_in name;
  switch (argc) {
  case 1:
    gethostname(hostname, sizeof(hostname));
    break;
  case 2:
    strcpy(hostname, argv[1]);
    break;
  default:
    printf("Usage: %s [server_name]\n", argv[0]);
    exit(-1);
  }
  hp = gethostbyname(hostname);
  s = socket(AF_INET, SOCK_STREAM, 0);
  name.sin_family = AF_INET;
  name.sin_port = htons(12345);
  memcpy(&name.sin_addr, hp->h_addr_list[0], hp->h_length);
  len = sizeof(struct sockaddr_in);
  connect(s, (struct sockaddr *)&name, len);
  while ((n = read(0, buf, sizeof(buf))) > 0) {
    if (send(s, buf, n, 0) < 0) {
      perror("send");
      exit(1);
    }
  }
  close(s);
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x480,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x47c(%rbp)
sub    $0x1,%eax
je     1214 <main+0x44>
jmp    1200 <main+0x30>
mov    -0x47c(%rbp),%eax
sub    $0x2,%eax
je     122a <main+0x5a>
jmp    1243 <main+0x73>
lea    -0x460(%rbp),%rdi
mov    $0x40,%esi
call   10c0 <gethostname@plt>
jmp    1260 <main+0x90>
lea    -0x460(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
call   1030 <strcpy@plt>
jmp    1260 <main+0x90>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xdaf(%rip),%rdi
call   1060 <printf@plt>
mov    $0xffffffff,%edi
call   10d0 <exit@plt>
lea    -0x460(%rbp),%rdi
call   1090 <gethostbyname@plt>
mov    %rax,-0x468(%rbp)
mov    0x2d6e(%rip),%rax
mov    (%rax),%edi
mov    0x2d55(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,-0x18(%rbp)
mov    0x2d52(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x470(%rbp)
mov    $0x3039,%edi
call   1040 <htons@plt>
mov    %eax,-0x474(%rbp)
lea    -0x478(%rbp),%rdi
mov    -0x468(%rbp),%rax
mov    0x8(%rax),%rax
mov    (%rax),%esi
mov    -0x468(%rbp),%rax
mov    (%rax),%edx
call   10a0 <memcpy@plt>
movl   $0xc,-0x1c(%rbp)
mov    -0x18(%rbp),%edi
lea    -0x478(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   10e0 <connect@plt>
lea    -0x420(%rbp),%rsi
xor    %edi,%edi
mov    $0x400,%edx
call   1080 <read@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jle    1341 <main+0x171>
mov    -0x18(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    -0x14(%rbp),%edx
xor    %ecx,%ecx
call   1050 <send@plt>
cmp    $0x0,%rax
jge    133c <main+0x16c>
lea    0xcec(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
jmp    12e9 <main+0x119>
mov    -0x18(%rbp),%edi
call   1070 <close@plt>
xor    %edi,%edi
call   10d0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x450,%rsp
cmp    $0x2,%edi
je     11fa <main+0x2a>
cmp    $0x1,%edi
jne    12ac <main+0xdc>
lea    0x10(%rsp),%rdi
mov    $0x40,%esi
call   10c0 <gethostname@plt>
jmp    1208 <main+0x38>
mov    0x8(%rsi),%rsi
lea    0x10(%rsp),%rdi
call   1030 <strcpy@plt>
lea    0x10(%rsp),%rdi
call   1090 <gethostbyname@plt>
mov    %rax,%rbx
mov    0x2dcc(%rip),%r14
mov    (%r14),%edi
mov    0x2db2(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,%ebp
mov    (%r14),%eax
mov    %eax,0x8(%rsp)
mov    $0x3039,%edi
call   1040 <htons@plt>
mov    %eax,0x4(%rsp)
mov    0x8(%rbx),%rax
mov    (%rax),%esi
mov    (%rbx),%edx
mov    %rsp,%rbx
mov    %rbx,%rdi
call   10a0 <memcpy@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0xc,%edx
call   10e0 <connect@plt>
lea    0x50(%rsp),%rbx
nopl   (%rax)
xor    %edi,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1080 <read@plt>
test   %eax,%eax
jle    12c5 <main+0xf5>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1050 <send@plt>
test   %rax,%rax
jns    1270 <main+0xa0>
lea    0xd7c(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    (%rsi),%rsi
lea    0xd4a(%rip),%rdi
call   1060 <printf@plt>
mov    $0xffffffff,%edi
call   10d0 <exit@plt>
mov    %ebp,%edi
call   1070 <close@plt>
xor    %edi,%edi
call   10d0 <exit@plt>

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

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.hostent = type { i32, i32* }
%struct.sockaddr = type { i32 }
@.str = [25 x i8] c"Usage: %s [server_name]\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [5 x i8] c"send\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [1024 x i8]
%4 = alloca [64 x i8]
%5 = alloca %struct.sockaddr_in
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%7 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%8 = bitcast %struct.sockaddr_in* %5 to i8*
switch i32 %0, label %15 [
i32 1, label %9
i32 2, label %11
]
9: ; preds = %2
%10 = call i32 @gethostname(i8* %7, i32 64)
br label %19
11: ; preds = %2
%12 = getelementptr inbounds i8*, i8** %1, i64 1
%13 = load i8*, i8** %12
%14 = call i32 @strcpy(i8* %7, i8* %13)
br label %19
15: ; preds = %2
%16 = load i8*, i8** %1
%17 = tail call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %16)
%18 = tail call i32 @exit(i32 -1)
unreachable
19: ; preds = %11, %9
%20 = call %struct.hostent* @gethostbyname(i8* %7)
%21 = load i32, i32* @AF_INET
%22 = load i32, i32* @SOCK_STREAM
%23 = call i32 @socket(i32 %21, i32 %22, i32 0)
%24 = load i32, i32* @AF_INET
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 2
store i32 %24, i32* %25
%26 = call i32 @htons(i32 12345)
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 1
store i32 %26, i32* %27
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 0
%29 = getelementptr inbounds %struct.hostent, %struct.hostent* %20, i64 0, i32 1
%30 = load i32*, i32** %29
%31 = load i32, i32* %30
%32 = getelementptr inbounds %struct.hostent, %struct.hostent* %20, i64 0, i32 0
%33 = load i32, i32* %32
%34 = call i32 @memcpy(i32* %28, i32 %31, i32 %33)
%35 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%36 = call i32 @connect(i32 %23, %struct.sockaddr* %35, i32 12)
br label %37
37: ; preds = %40, %19
%38 = call i32 @read(i32 0, i8* %6, i32 1024)
%39 = icmp sgt i32 %38, 0
br i1 %39, label %40, label %46
40: ; preds = %37
%41 = call i64 @send(i32 %23, i8* %6, i32 %38, i32 0)
%42 = icmp slt i64 %41, 0
br i1 %42, label %43, label %37
43: ; preds = %40
%44 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%45 = call i32 @exit(i32 1)
unreachable
46: ; preds = %37
%47 = call i32 @close(i32 %23)
%48 = call i32 @exit(i32 0)
unreachable
}