#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 */
typedef int /*<<< orphan*/ their_addr;
struct in_addr {
  int dummy;
};
struct sockaddr_in {
  struct in_addr sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  scalar_t__ h_addr;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int recv(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ scanf(char *, char *);
int send(int, char *, int /*<<< orphan*/, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ strlen(char *);
int main(int argc, char *argv[]) {
  int sockfd, numbytes;
  char buf[100];
  struct hostent *he;
  struct sockaddr_in their_addr;
  char msg[100];
  if (argc != 2) {
    fprintf(stderr, "usage: client hostname\n");
    exit(1);
  }
  if ((he = gethostbyname(argv[1])) == NULL) {
    perror("Client gethostbyname");
    exit(1);
  }
  if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
    perror("Client socket");
    exit(1);
  }
  memset(&their_addr, 0, sizeof(their_addr));
  their_addr.sin_family = AF_INET;
  their_addr.sin_port = htons(3490);
  their_addr.sin_addr = *((struct in_addr *)he->h_addr);
  if (connect(sockfd, (struct sockaddr *)&their_addr,
              sizeof(struct sockaddr)) == -1) {
    perror("Client connect");
    exit(1);
  }
  do {
    printf("Message to send: ");
    scanf("%s", msg);
    if ((numbytes = send(sockfd, msg, strlen(msg), 0)) == -1) {
      perror("Client send");
      continue;
    }
    if ((numbytes = recv(sockfd, buf, 100 - 1, 0)) == -1) {
      perror("Client recv");
      continue;
    }
    buf[numbytes] = '\0';
    printf("Received: %s\n", buf);
  } while (strcmp(msg, "quit") != 0);
  close(sockfd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x110,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     1232 <main+0x42>
mov    0x2dbe(%rip),%rax
mov    (%rax),%edi
lea    0xddd(%rip),%rsi
call   10c0 <fprintf@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10b0 <gethostbyname@plt>
mov    %rax,-0x88(%rbp)
cmp    $0x0,%rax
jne    1266 <main+0x76>
lea    0xdc1(%rip),%rdi
call   10e0 <perror@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
mov    0x2d7b(%rip),%rax
mov    (%rax),%edi
mov    0x2d5a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1110 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    12a1 <main+0xb1>
lea    0xd9b(%rip),%rdi
call   10e0 <perror@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
lea    -0x98(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1080 <memset@plt>
mov    0x2d2d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x90(%rbp)
mov    $0xda2,%edi
call   1050 <htons@plt>
mov    %eax,-0x94(%rbp)
mov    -0x88(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%eax
mov    %eax,-0x98(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x98(%rbp),%rsi
mov    $0x4,%edx
call   1100 <connect@plt>
cmp    $0xffffffff,%eax
jne    1318 <main+0x128>
lea    0xd32(%rip),%rdi
call   10e0 <perror@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
jmp    131d <main+0x12d>
lea    0xd26(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
lea    -0x100(%rbp),%rsi
lea    0xd23(%rip),%rdi
call   10d0 <scanf@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x10c(%rbp)
lea    -0x100(%rbp),%rax
mov    %rax,-0x108(%rbp)
lea    -0x100(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x10c(%rbp),%edi
mov    -0x108(%rbp),%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1060 <send@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    1394 <main+0x1a4>
lea    0xcd5(%rip),%rdi
call   10e0 <perror@plt>
jmp    13df <main+0x1ef>
mov    -0x14(%rbp),%edi
lea    -0x80(%rbp),%rsi
mov    $0x63,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    13c4 <main+0x1d4>
lea    0xcb1(%rip),%rdi
call   10e0 <perror@plt>
jmp    13df <main+0x1ef>
movslq -0x18(%rbp),%rax
movb   $0x0,-0x80(%rbp,%rax,1)
lea    -0x80(%rbp),%rsi
lea    0xc9f(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
lea    -0x100(%rbp),%rdi
lea    0xc98(%rip),%rsi
call   10a0 <strcmp@plt>
cmp    $0x0,%rax
jne    131d <main+0x12d>
mov    -0x14(%rbp),%edi
call   1090 <close@plt>
xor    %eax,%eax
add    $0x110,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xe8,%rsp
cmp    $0x2,%edi
jne    1357 <main+0x167>
mov    0x8(%rsi),%rdi
call   10b0 <gethostbyname@plt>
test   %rax,%rax
je     1376 <main+0x186>
mov    %rax,%rbx
mov    0x2dc2(%rip),%r15
mov    (%r15),%edi
mov    0x2da0(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1110 <socket@plt>
cmp    $0xffffffff,%eax
je     137f <main+0x18f>
mov    %eax,%ebp
mov    %rsp,%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1080 <memset@plt>
mov    (%r15),%eax
mov    %eax,0x8(%rsp)
mov    $0xda2,%edi
call   1050 <htons@plt>
mov    %eax,0x4(%rsp)
mov    (%rbx),%rax
mov    (%rax),%eax
mov    %eax,(%rsp)
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x4,%edx
call   1100 <connect@plt>
cmp    $0xffffffff,%eax
je     1388 <main+0x198>
lea    0xdb8(%rip),%r14
lea    0xdc3(%rip),%r15
lea    0x80(%rsp),%rbx
lea    0xddd(%rip),%r13
lea    0x10(%rsp),%r12
jmp    12d0 <main+0xe0>
lea    0xda9(%rip),%rdi
call   10e0 <perror@plt>
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %r13,%rsi
call   10a0 <strcmp@plt>
test   %rax,%rax
je     133c <main+0x14c>
mov    %r14,%rdi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
call   10d0 <scanf@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1060 <send@plt>
cmp    $0xffffffff,%eax
je     12af <main+0xbf>
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0x63,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
cmp    $0xffffffff,%eax
je     1330 <main+0x140>
cltq
movb   $0x0,0x10(%rsp,%rax,1)
lea    0xd53(%rip),%rdi
mov    %r12,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
jmp    12c0 <main+0xd0>
lea    0xd34(%rip),%rdi
jmp    12b6 <main+0xc6>
mov    %ebp,%edi
call   1090 <close@plt>
xor    %eax,%eax
add    $0xe8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2c7a(%rip),%rax
mov    (%rax),%edi
lea    0xc99(%rip),%rsi
call   10c0 <fprintf@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
lea    0xc9b(%rip),%rdi
jmp    138f <main+0x19f>
lea    0xca7(%rip),%rdi
jmp    138f <main+0x19f>
lea    0xcac(%rip),%rdi
call   10e0 <perror@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>

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

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.in_addr, i32, i32 }
%struct.in_addr = type { i32 }
%struct.hostent = type { i64 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [24 x i8] c"usage: client hostname\0A\00"
@.str.1 = [21 x i8] c"Client gethostbyname\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.2 = [14 x i8] c"Client socket\00"
@.str.3 = [15 x i8] c"Client connect\00"
@.str.4 = [18 x i8] c"Message to send: \00"
@.str.5 = [3 x i8] c"%s\00"
@.str.6 = [12 x i8] c"Client send\00"
@.str.7 = [12 x i8] c"Client recv\00"
@.str.8 = [14 x i8] c"Received: %s\0A\00"
@.str.9 = [5 x i8] c"quit\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [100 x i8]
%4 = alloca %struct.sockaddr_in
%5 = alloca [100 x i8]
%6 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%7 = bitcast %struct.sockaddr_in* %4 to i8*
%8 = getelementptr inbounds [100 x i8], [100 x i8]* %5, i64 0, i64 0
%9 = icmp eq i32 %0, 2
br i1 %9, label %14, label %10
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = tail call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%13 = tail call i32 @exit(i32 1)
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 %22
19: ; preds = %14
%20 = tail call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%21 = tail call i32 @exit(i32 1)
unreachable
22: ; preds = %14
%23 = load i32, i32* @AF_INET
%24 = load i32, i32* @SOCK_STREAM
%25 = tail call i32 @socket(i32 %23, i32 %24, i32 0)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %30
27: ; preds = %22
%28 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%29 = tail call i32 @exit(i32 1)
unreachable
30: ; preds = %22
%31 = call i32 @memset(%struct.sockaddr_in* %4, i32 0, i32 12)
%32 = load i32, i32* @AF_INET
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %32, i32* %33
%34 = call i32 @htons(i32 3490)
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
store i32 %34, i32* %35
%36 = getelementptr inbounds %struct.hostent, %struct.hostent* %17, i64 0, i32 0
%37 = load i64, i64* %36
%38 = inttoptr i64 %37 to i32*
%39 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0, i32 0
%40 = load i32, i32* %38
store i32 %40, i32* %39
%41 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%42 = call i32 @connect(i32 %25, %struct.sockaddr* %41, i32 4)
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %47
44: ; preds = %30
%45 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
%46 = call i32 @exit(i32 1)
unreachable
47: ; preds = %30, %64
%48 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
%49 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i8* %8)
%50 = call i32 @strlen(i8* %8)
%51 = call i32 @send(i32 %25, i8* %8, i32 %50, i32 0)
%52 = icmp eq i32 %51, -1
br i1 %52, label %53, label %55
53: ; preds = %47
%54 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.6, i64 0, i64 0))
br label %64
55: ; preds = %47
%56 = call i32 @recv(i32 %25, i8* %6, i32 99, i32 0)
%57 = icmp eq i32 %56, -1
br i1 %57, label %58, label %60
58: ; preds = %55
%59 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.7, i64 0, i64 0))
br label %64
60: ; preds = %55
%61 = sext i32 %56 to i64
%62 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 %61
store i8 0, i8* %62
%63 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.8, i64 0, i64 0), i8* %6)
br label %64
64: ; preds = %60, %58, %53
%65 = call i64 @strcmp(i8* %8, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.9, i64 0, i64 0))
%66 = icmp eq i64 %65, 0
br i1 %66, label %67, label %47
67: ; preds = %64
%68 = call i32 @close(i32 %25)
ret i32 0
}