#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;
};
typedef int /*<<< orphan*/ server;
typedef int /*<<< orphan*/ in_port_t;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int /*<<< orphan*/ memcpy(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ strncmp(char *, char *, int);
int /*<<< orphan*/ write(int, char *, int);
int main() {
  struct hostent *server_ent;
  struct sockaddr_in server;
  int soc;
  char hostname[] = "localhost";
  char buf[512];
  if ((server_ent = gethostbyname(hostname)) == NULL) {
    perror("gethostname");
    return 1;
  }
  memset((char *)&server, 0, sizeof(server));
  server.sin_family = AF_INET;
  server.sin_port = htons((in_port_t)50000);
  memcpy((char *)&server.sin_addr, server_ent->h_addr, server_ent->h_length);
  if ((soc = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
    perror("socket");
    return 1;
  }
  if (connect(soc, (struct sockaddr *)&server, sizeof(server)) == -1) {
    perror("connect");
    return 1;
  }
  write(1, "Wait\n", 5);
  do {
    int n;
    n = read(soc, buf, 512);
    write(1, buf, n);
    n = read(0, buf, 512);
    write(soc, buf, n);
  } while (strncmp(buf, "quit", 4) != 0);
  close(soc);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x240,%rsp
movl   $0x0,-0x4(%rbp)
mov    0xe37(%rip),%rax
mov    %rax,-0x2e(%rbp)
mov    0xe34(%rip),%ax
mov    %ax,-0x26(%rbp)
lea    -0x2e(%rbp),%rdi
call   1090 <gethostbyname@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    1207 <main+0x57>
lea    0xe14(%rip),%rdi
call   10b0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    135b <main+0x1ab>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2dca(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    $0xc350,%edi
call   1050 <htons@plt>
mov    %eax,-0x1c(%rbp)
lea    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x10(%rbp),%rax
mov    (%rax),%edx
call   10a0 <memcpy@plt>
mov    0x2d9b(%rip),%rax
mov    (%rax),%edi
mov    0x2d82(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    1283 <main+0xd3>
lea    0xda4(%rip),%rdi
call   10b0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    135b <main+0x1ab>
mov    -0x24(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   10c0 <connect@plt>
cmp    $0xffffffff,%eax
jne    12b5 <main+0x105>
lea    0xd79(%rip),%rdi
call   10b0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    135b <main+0x1ab>
mov    $0x1,%edi
lea    0xd64(%rip),%rsi
mov    $0x5,%edx
call   1040 <write@plt>
mov    -0x24(%rbp),%edi
lea    -0x230(%rbp),%rsi
mov    $0x200,%edx
call   1080 <read@plt>
mov    %eax,-0x234(%rbp)
lea    -0x230(%rbp),%rsi
mov    -0x234(%rbp),%edx
mov    $0x1,%edi
call   1040 <write@plt>
lea    -0x230(%rbp),%rsi
xor    %edi,%edi
mov    $0x200,%edx
call   1080 <read@plt>
mov    %eax,-0x234(%rbp)
mov    -0x24(%rbp),%edi
lea    -0x230(%rbp),%rsi
mov    -0x234(%rbp),%edx
call   1040 <write@plt>
lea    -0x230(%rbp),%rdi
lea    0xcf3(%rip),%rsi
mov    $0x4,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    12cb <main+0x11b>
mov    -0x24(%rbp),%edi
call   1070 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x240,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x220,%rsp
movabs $0x736f686c61636f6c,%rax
68 6f 73
mov    %rax,0x10(%rsp)
movw   $0x74,0x18(%rsp)
lea    0x10(%rsp),%rdi
call   1090 <gethostbyname@plt>
test   %rax,%rax
je     12e3 <main+0x133>
mov    %rax,%rbx
mov    %rsp,%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2de8(%rip),%rbp
mov    0x0(%rbp),%eax
mov    %eax,0x8(%rsp)
mov    $0xc350,%edi
call   1050 <htons@plt>
mov    %eax,0x4(%rsp)
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
mov    %r14,%rdi
call   10a0 <memcpy@plt>
mov    0x0(%rbp),%edi
mov    0x2dac(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
test   %eax,%eax
js     12ec <main+0x13c>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0xc,%edx
call   10c0 <connect@plt>
cmp    $0xffffffff,%eax
je     12f5 <main+0x145>
lea    0xdc7(%rip),%rsi
mov    $0x1,%edi
mov    $0x5,%edx
call   1040 <write@plt>
lea    0x20(%rsp),%rbx
lea    0xdb2(%rip),%r14
nopl   0x0(%rax)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x200,%edx
call   1080 <read@plt>
mov    $0x1,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <write@plt>
xor    %edi,%edi
mov    %rbx,%rsi
mov    $0x200,%edx
call   1080 <read@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    $0x4,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
jne    1280 <main+0xd0>
mov    %ebp,%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x220,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xd20(%rip),%rdi
jmp    12fc <main+0x14c>
lea    0xd23(%rip),%rdi
jmp    12fc <main+0x14c>
lea    0xd21(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%eax
jmp    12d7 <main+0x127>

LLVM IR (-O0):
%struct.hostent = type { i32, i32 }
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@__const.main.hostname = [10 x i8] c"localhost\00"
@.str = [12 x i8] c"gethostname\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [7 x i8] c"socket\00"
@.str.2 = [8 x i8] c"connect\00"
@.str.3 = [6 x i8] c"Wait\0A\00"
@.str.4 = [5 x i8] c"quit\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.hostent*
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = alloca [10 x i8]
%6 = alloca [512 x i8]
%7 = alloca i32
store i32 0, i32* %1
%8 = bitcast [10 x i8]* %5 to i8*
%9 = getelementptr inbounds [10 x i8], [10 x i8]* %5, i64 0, i64 0
%10 = call %struct.hostent* @gethostbyname(i8* %9)
store %struct.hostent* %10, %struct.hostent** %2
%11 = icmp eq %struct.hostent* %10, null
br i1 %11, label %12, label %14
12: ; preds = %0
%13 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %65
14: ; preds = %0
%15 = bitcast %struct.sockaddr_in* %3 to i8*
%16 = call i32 @memset(i8* %15, i32 0, i32 12)
%17 = load i32, i32* @AF_INET
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 2
store i32 %17, i32* %18
%19 = call i32 @htons(i32 50000)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 1
store i32 %19, i32* %20
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 0
%22 = bitcast i32* %21 to i8*
%23 = load %struct.hostent*, %struct.hostent** %2
%24 = getelementptr inbounds %struct.hostent, %struct.hostent* %23, i32 0, i32 1
%25 = load i32, i32* %24
%26 = load %struct.hostent*, %struct.hostent** %2
%27 = getelementptr inbounds %struct.hostent, %struct.hostent* %26, i32 0, i32 0
%28 = load i32, i32* %27
%29 = call i32 @memcpy(i8* %22, i32 %25, i32 %28)
%30 = load i32, i32* @AF_INET
%31 = load i32, i32* @SOCK_STREAM
%32 = call i32 @socket(i32 %30, i32 %31, i32 0)
store i32 %32, i32* %4
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %14
%35 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %1
br label %65
36: ; preds = %14
%37 = load i32, i32* %4
%38 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%39 = call i32 @connect(i32 %37, %struct.sockaddr* %38, i32 12)
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %43
41: ; preds = %36
%42 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %1
br label %65
43: ; preds = %36
%44 = call i32 @write(i32 1, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32 5)
br label %45
45: ; preds = %58, %43
%46 = load i32, i32* %4
%47 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%48 = call i32 @read(i32 %46, i8* %47, i32 512)
store i32 %48, i32* %7
%49 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%50 = load i32, i32* %7
%51 = call i32 @write(i32 1, i8* %49, i32 %50)
%52 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%53 = call i32 @read(i32 0, i8* %52, i32 512)
store i32 %53, i32* %7
%54 = load i32, i32* %4
%55 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%56 = load i32, i32* %7
%57 = call i32 @write(i32 %54, i8* %55, i32 %56)
br label %58
58: ; preds = %45
%59 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%60 = call i64 @strncmp(i8* %59, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i32 4)
%61 = icmp ne i64 %60, 0
br i1 %61, label %45, label %62
62: ; preds = %58
%63 = load i32, i32* %4
%64 = call i32 @close(i32 %63)
store i32 0, i32* %1
br label %65
65: ; preds = %62, %41, %34, %12
%66 = load i32, i32* %1
ret i32 %66
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.hostent = type { i32, i32 }
%struct.sockaddr = type { i32 }
@__const.main.hostname = [10 x i8] c"localhost\00"
@.str = [12 x i8] c"gethostname\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [7 x i8] c"socket\00"
@.str.2 = [8 x i8] c"connect\00"
@.str.3 = [6 x i8] c"Wait\0A\00"
@.str.4 = [5 x i8] c"quit\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_in
%2 = alloca [10 x i8]
%3 = alloca [512 x i8]
%4 = bitcast %struct.sockaddr_in* %1 to i8*
%5 = getelementptr inbounds [10 x i8], [10 x i8]* %2, i64 0, i64 0
%6 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%7 = call %struct.hostent* @gethostbyname(i8* %5)
%8 = icmp eq %struct.hostent* %7, null
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %45
11: ; preds = %0
%12 = call i32 @memset(i8* %4, i32 0, i32 12)
%13 = load i32, i32* @AF_INET
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %13, i32* %14
%15 = call i32 @htons(i32 50000)
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1
store i32 %15, i32* %16
%17 = getelementptr inbounds %struct.hostent, %struct.hostent* %7, i64 0, i32 1
%18 = load i32, i32* %17
%19 = getelementptr inbounds %struct.hostent, %struct.hostent* %7, i64 0, i32 0
%20 = load i32, i32* %19
%21 = call i32 @memcpy(i8* %4, i32 %18, i32 %20)
%22 = load i32, i32* @AF_INET
%23 = load i32, i32* @SOCK_STREAM
%24 = call i32 @socket(i32 %22, i32 %23, i32 0)
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %11
%27 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %45
28: ; preds = %11
%29 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%30 = call i32 @connect(i32 %24, %struct.sockaddr* %29, i32 12)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %34
32: ; preds = %28
%33 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %45
34: ; preds = %28
%35 = call i32 @write(i32 1, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32 5)
br label %36
36: ; preds = %36, %34
%37 = call i32 @read(i32 %24, i8* %6, i32 512)
%38 = call i32 @write(i32 1, i8* %6, i32 %37)
%39 = call i32 @read(i32 0, i8* %6, i32 512)
%40 = call i32 @write(i32 %24, i8* %6, i32 %39)
%41 = call i64 @strncmp(i8* %6, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i32 4)
%42 = icmp eq i64 %41, 0
br i1 %42, label %43, label %36
43: ; preds = %36
%44 = call i32 @close(i32 %24)
br label %45
45: ; preds = %43, %32, %26, %9
%46 = phi i32 [ 1, %9 ], [ 1, %26 ], [ 1, %32 ], [ 0, %43 ]
ret i32 %46
}