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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
struct hostent *gethostbyname(char const *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int /*<<< orphan*/ scanf(char *, char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int write(int, char *, int);
int main(int argc, char const *argv[]) {
  int sockfd, nbytes;
  char buf[1024];
  struct hostent *he;
  struct sockaddr_in srvaddr;
  if (argc != 2) {
    perror("Usage:client hostname\n");
    exit(1);
  }
  if ((he = gethostbyname(argv[1])) == NULL) {
    perror("gethostbyname error\n");
    exit(1);
  }
  if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
    perror("socket error");
    exit(1);
  }
  bzero(&srvaddr, sizeof(srvaddr));
  srvaddr.sin_family = AF_INET;
  srvaddr.sin_port = htons(3491);
  srvaddr.sin_addr = *((struct in_addr *)he->h_addr);
  if (connect(sockfd, (struct sockaddr *)&srvaddr, sizeof(struct sockaddr)) ==
      -1) {
    perror("connect error");
    exit(1);
  }
  if ((nbytes = read(sockfd, buf, 5000)) == -1) {
    perror("read error");
    exit(1);
  }
  buf[nbytes] = '\0';
  printf("read: %s\n", buf);
  printf("Enter You Want to Say: ");
  scanf("%[^\n]", buf);
  if (write(sockfd, buf, 30) == -1) {
    perror("write error");
    exit(1);
  }
  close(sockfd);
  return 0;
}


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   $0x2,-0x8(%rbp)
je     11f9 <main+0x39>
lea    0xe16(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <gethostbyname@plt>
mov    %rax,-0x428(%rbp)
cmp    $0x0,%rax
jne    122d <main+0x6d>
lea    0xdf9(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    0x2db4(%rip),%rax
mov    (%rax),%edi
mov    0x2d9b(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    1268 <main+0xa8>
lea    0xdd3(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
lea    -0x438(%rbp),%rdi
mov    $0xc,%esi
call   10b0 <bzero@plt>
mov    0x2d68(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x430(%rbp)
mov    $0xda3,%edi
call   1040 <htons@plt>
mov    %eax,-0x434(%rbp)
mov    -0x428(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%eax
mov    %eax,-0x438(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x438(%rbp),%rsi
mov    $0x4,%edx
call   10d0 <connect@plt>
cmp    $0xffffffff,%eax
jne    12dd <main+0x11d>
lea    0xd6b(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x14(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x1388,%edx
call   1070 <read@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    1313 <main+0x153>
lea    0xd43(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
movslq -0x18(%rbp),%rax
movb   $0x0,-0x420(%rbp,%rax,1)
lea    -0x420(%rbp),%rsi
lea    0xd25(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xd21(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x420(%rbp),%rsi
lea    0xd24(%rip),%rdi
call   1090 <scanf@plt>
mov    -0x14(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x1e,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    1388 <main+0x1c8>
lea    0xd01(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x418,%rsp
cmp    $0x2,%edi
jne    12d8 <main+0x118>
mov    0x8(%rsi),%rdi
call   1080 <gethostbyname@plt>
test   %rax,%rax
je     12e1 <main+0x121>
mov    %rax,%r14
mov    0x2df5(%rip),%r12
mov    (%r12),%edi
mov    0x2dda(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
cmp    $0xffffffff,%eax
je     12ea <main+0x12a>
mov    %eax,%ebx
mov    %rsp,%r15
mov    %r15,%rdi
mov    $0xc,%esi
call   10b0 <bzero@plt>
mov    (%r12),%eax
mov    %eax,0x8(%rsp)
mov    $0xda3,%edi
call   1040 <htons@plt>
mov    %eax,0x4(%rsp)
mov    (%r14),%rax
mov    (%rax),%eax
mov    %eax,(%rsp)
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x4,%edx
call   10d0 <connect@plt>
cmp    $0xffffffff,%eax
je     12f3 <main+0x133>
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1388,%edx
call   1070 <read@plt>
cmp    $0xffffffff,%eax
je     12fc <main+0x13c>
cltq
movb   $0x0,0x10(%rsp,%rax,1)
lea    0xdd2(%rip),%rdi
lea    0x10(%rsp),%r14
mov    %r14,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xdc6(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xdd0(%rip),%rdi
mov    %r14,%rsi
call   1090 <scanf@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1e,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     1305 <main+0x145>
mov    %ebx,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xd21(%rip),%rdi
jmp    130c <main+0x14c>
lea    0xd2f(%rip),%rdi
jmp    130c <main+0x14c>
lea    0xd3b(%rip),%rdi
jmp    130c <main+0x14c>
lea    0xd3f(%rip),%rdi
jmp    130c <main+0x14c>
lea    0xd44(%rip),%rdi
jmp    130c <main+0x14c>
lea    0xd6e(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10c0 <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 }
@.str = [23 x i8] c"Usage:client hostname\0A\00"
@.str.1 = [21 x i8] c"gethostbyname error\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.2 = [13 x i8] c"socket error\00"
@.str.3 = [14 x i8] c"connect error\00"
@.str.4 = [11 x i8] c"read error\00"
@.str.5 = [10 x i8] c"read: %s\0A\00"
@.str.6 = [24 x i8] c"Enter You Want to Say: \00"
@.str.7 = [6 x i8] c"%[^\0A]\00"
@.str.8 = [12 x i8] c"write error\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 [1024 x i8]
%9 = alloca %struct.hostent*
%10 = alloca %struct.sockaddr_in
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, 2
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %2
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
%20 = call %struct.hostent* @gethostbyname(i8* %19)
store %struct.hostent* %20, %struct.hostent** %9
%21 = icmp eq %struct.hostent* %20, null
br i1 %21, label %22, label %25
22: ; preds = %16
%23 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %16
%26 = load i32, i32* @AF_INET
%27 = load i32, i32* @SOCK_STREAM
%28 = call i32 @socket(i32 %26, i32 %27, i32 0)
store i32 %28, i32* %6
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %33
30: ; preds = %25
%31 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%32 = call i32 @exit(i32 1)
unreachable
33: ; preds = %25
%34 = call i32 @bzero(%struct.sockaddr_in* %10, i32 12)
%35 = load i32, i32* @AF_INET
%36 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 2
store i32 %35, i32* %36
%37 = call i32 @htons(i32 3491)
%38 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 1
store i32 %37, i32* %38
%39 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 0
%40 = load %struct.hostent*, %struct.hostent** %9
%41 = getelementptr inbounds %struct.hostent, %struct.hostent* %40, i32 0, i32 0
%42 = load i64, i64* %41
%43 = inttoptr i64 %42 to %struct.in_addr*
%44 = bitcast %struct.in_addr* %39 to i8*
%45 = bitcast %struct.in_addr* %43 to i8*
%46 = load i32, i32* %6
%47 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%48 = call i32 @connect(i32 %46, %struct.sockaddr* %47, i32 4)
%49 = icmp eq i32 %48, -1
br i1 %49, label %50, label %53
50: ; preds = %33
%51 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
%52 = call i32 @exit(i32 1)
unreachable
53: ; preds = %33
%54 = load i32, i32* %6
%55 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%56 = call i32 @read(i32 %54, i8* %55, i32 5000)
store i32 %56, i32* %7
%57 = icmp eq i32 %56, -1
br i1 %57, label %58, label %61
58: ; preds = %53
%59 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
%60 = call i32 @exit(i32 1)
unreachable
61: ; preds = %53
%62 = load i32, i32* %7
%63 = sext i32 %62 to i64
%64 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 %63
store i8 0, i8* %64
%65 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%66 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i8* %65)
%67 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i64 0, i64 0))
%68 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%69 = call i32 @scanf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0), i8* %68)
%70 = load i32, i32* %6
%71 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%72 = call i32 @write(i32 %70, i8* %71, i32 30)
%73 = icmp eq i32 %72, -1
br i1 %73, label %74, label %77
74: ; preds = %61
%75 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.8, i64 0, i64 0))
%76 = call i32 @exit(i32 1)
unreachable
77: ; preds = %61
%78 = load i32, i32* %6
%79 = call i32 @close(i32 %78)
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 }
@.str = [23 x i8] c"Usage:client hostname\0A\00"
@.str.1 = [21 x i8] c"gethostbyname error\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.2 = [13 x i8] c"socket error\00"
@.str.3 = [14 x i8] c"connect error\00"
@.str.4 = [11 x i8] c"read error\00"
@.str.5 = [10 x i8] c"read: %s\0A\00"
@.str.6 = [24 x i8] c"Enter You Want to Say: \00"
@.str.7 = [6 x i8] c"%[^\0A]\00"
@.str.8 = [12 x i8] c"write error\00"
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, 2
br i1 %7, label %11, label %8
8: ; preds = %2
%9 = tail call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %2
%12 = getelementptr inbounds i8*, i8** %1, i64 1
%13 = load i8*, i8** %12
%14 = tail call %struct.hostent* @gethostbyname(i8* %13)
%15 = icmp eq %struct.hostent* %14, null
br i1 %15, label %16, label %19
16: ; preds = %11
%17 = tail call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%18 = tail call i32 @exit(i32 1)
unreachable
19: ; preds = %11
%20 = load i32, i32* @AF_INET
%21 = load i32, i32* @SOCK_STREAM
%22 = tail call i32 @socket(i32 %20, i32 %21, i32 0)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %27
24: ; preds = %19
%25 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%26 = tail call i32 @exit(i32 1)
unreachable
27: ; preds = %19
%28 = call i32 @bzero(%struct.sockaddr_in* %4, i32 12)
%29 = load i32, i32* @AF_INET
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %29, i32* %30
%31 = tail call i32 @htons(i32 3491)
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
store i32 %31, i32* %32
%33 = getelementptr inbounds %struct.hostent, %struct.hostent* %14, i64 0, i32 0
%34 = load i64, i64* %33
%35 = inttoptr i64 %34 to i32*
%36 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0, i32 0
%37 = load i32, i32* %35
store i32 %37, i32* %36
%38 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%39 = call i32 @connect(i32 %22, %struct.sockaddr* %38, i32 4)
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %44
41: ; preds = %27
%42 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
%43 = call i32 @exit(i32 1)
unreachable
44: ; preds = %27
%45 = call i32 @read(i32 %22, i8* %5, i32 5000)
%46 = icmp eq i32 %45, -1
br i1 %46, label %47, label %50
47: ; preds = %44
%48 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
%49 = call i32 @exit(i32 1)
unreachable
50: ; preds = %44
%51 = sext i32 %45 to i64
%52 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 %51
store i8 0, i8* %52
%53 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i8* %5)
%54 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i64 0, i64 0))
%55 = call i32 @scanf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0), i8* %5)
%56 = call i32 @write(i32 %22, i8* %5, i32 30)
%57 = icmp eq i32 %56, -1
br i1 %57, label %58, label %61
58: ; preds = %50
%59 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.8, i64 0, i64 0))
%60 = call i32 @exit(i32 1)
unreachable
61: ; preds = %50
%62 = call i32 @close(i32 %22)
ret i32 0
}