#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 {
  int /*<<< orphan*/ sin_zero;
  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*/ bzero(int /*<<< orphan*/ *, int);
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int recv(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ scanf(char *, char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  int sockfd;
  int recvbytes;
  char hostname[1000];
  char buf[100];
  struct hostent *host;
  struct sockaddr_in serv_addr;
  printf("Please enter the server's hostname!\n");
  scanf("%s", hostname);
  if ((host = gethostbyname(hostname)) == NULL) {
    perror("gethostbyname出错!");
    exit(1);
  }
  if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
    perror("socket创建出错!");
    exit(1);
  }
  serv_addr.sin_family = AF_INET;
  serv_addr.sin_port = htons(1707);
  serv_addr.sin_addr = *((struct in_addr *)host->h_addr);
  bzero(&(serv_addr.sin_zero), 8);
  if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(struct sockaddr)) ==
      -1) {
    perror("connect出错!");
    exit(1);
  }
  if ((recvbytes = recv(sockfd, buf, 100, 0)) == -1) {
    perror("recv出错!");
    exit(1);
  }
  buf[recvbytes] = '\0';
  printf("Received: %s", buf);
  close(sockfd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x490,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe30(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x400(%rbp),%rsi
lea    0xe8e(%rip),%rdi
call   1080 <scanf@plt>
lea    -0x400(%rbp),%rdi
call   1070 <gethostbyname@plt>
mov    %rax,-0x478(%rbp)
cmp    $0x0,%rax
jne    121d <main+0x6d>
lea    0xe17(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    0x2dc4(%rip),%rax
mov    (%rax),%edi
mov    0x2dab(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    1258 <main+0xa8>
lea    0xdf1(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    0x2d89(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x47c(%rbp)
mov    $0x6ab,%edi
call   1040 <htons@plt>
mov    %eax,-0x480(%rbp)
mov    -0x478(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%eax
mov    %eax,-0x484(%rbp)
lea    -0x488(%rbp),%rdi
mov    $0x8,%esi
call   10a0 <bzero@plt>
mov    -0x14(%rbp),%edi
lea    -0x488(%rbp),%rsi
mov    $0x4,%edx
call   10c0 <connect@plt>
cmp    $0xffffffff,%eax
jne    12cd <main+0x11d>
lea    0xd90(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x14(%rbp),%edi
lea    -0x470(%rbp),%rsi
mov    $0x64,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    1305 <main+0x155>
lea    0xd67(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
movslq -0x18(%rbp),%rax
movb   $0x0,-0x470(%rbp,%rax,1)
lea    -0x470(%rbp),%rsi
lea    0xd4a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x490,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x470,%rsp
lea    0xe7e(%rip),%rdi
call   1040 <puts@plt>
lea    0xe6f(%rip),%rdi
lea    0x80(%rsp),%rbx
mov    %rbx,%rsi
call   1090 <scanf@plt>
mov    %rbx,%rdi
call   1080 <gethostbyname@plt>
test   %rax,%rax
je     12b2 <main+0xf2>
mov    %rax,%r14
mov    0x2dde(%rip),%r15
mov    (%r15),%edi
mov    0x2dc4(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
cmp    $0xffffffff,%eax
je     12bb <main+0xfb>
mov    %eax,%ebx
mov    (%r15),%eax
mov    %eax,0xc(%rsp)
mov    $0x6ab,%edi
call   1050 <htons@plt>
mov    %eax,0x8(%rsp)
mov    (%r14),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rsp)
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0x8,%esi
call   10b0 <bzero@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x4,%edx
call   10d0 <connect@plt>
cmp    $0xffffffff,%eax
je     12c4 <main+0x104>
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0x64,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
cmp    $0xffffffff,%eax
je     12cd <main+0x10d>
cltq
movb   $0x0,0x10(%rsp,%rax,1)
lea    0xdb4(%rip),%rdi
lea    0x10(%rsp),%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %ebx,%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x470,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xd47(%rip),%rdi
jmp    12d4 <main+0x114>
lea    0xd53(%rip),%rdi
jmp    12d4 <main+0x114>
lea    0xd5e(%rip),%rdi
jmp    12d4 <main+0x114>
lea    0xd64(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
%struct.hostent = type { i64 }
%struct.sockaddr_in = type { i32, %struct.in_addr, i32, i32 }
%struct.in_addr = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [37 x i8] c"Please enter the server's hostname!\0A\00"
@.str.1 = [3 x i8] c"%s\00"
@.str.2 = [21 x i8] c"gethostbyname\E5\87\BA\E9\94\99!\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.3 = [20 x i8] c"socket\E5\88\9B\E5\BB\BA\E5\87\BA\E9\94\99!\00"
@.str.4 = [15 x i8] c"connect\E5\87\BA\E9\94\99!\00"
@.str.5 = [12 x i8] c"recv\E5\87\BA\E9\94\99!\00"
@.str.6 = [13 x i8] c"Received: %s\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 [1000 x i8]
%9 = alloca [100 x i8]
%10 = alloca %struct.hostent*
%11 = alloca %struct.sockaddr_in
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
%13 = getelementptr inbounds [1000 x i8], [1000 x i8]* %8, i64 0, i64 0
%14 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %13)
%15 = getelementptr inbounds [1000 x i8], [1000 x i8]* %8, i64 0, i64 0
%16 = call %struct.hostent* @gethostbyname(i8* %15)
store %struct.hostent* %16, %struct.hostent** %10
%17 = icmp eq %struct.hostent* %16, null
br i1 %17, label %18, label %21
18: ; preds = %2
%19 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %2
%22 = load i32, i32* @AF_INET
%23 = load i32, i32* @SOCK_STREAM
%24 = call i32 @socket(i32 %22, i32 %23, i32 0)
store i32 %24, i32* %6
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %29
26: ; preds = %21
%27 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %21
%30 = load i32, i32* @AF_INET
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 3
store i32 %30, i32* %31
%32 = call i32 @htons(i32 1707)
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 2
store i32 %32, i32* %33
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 1
%35 = load %struct.hostent*, %struct.hostent** %10
%36 = getelementptr inbounds %struct.hostent, %struct.hostent* %35, i32 0, i32 0
%37 = load i64, i64* %36
%38 = inttoptr i64 %37 to %struct.in_addr*
%39 = bitcast %struct.in_addr* %34 to i8*
%40 = bitcast %struct.in_addr* %38 to i8*
%41 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 0
%42 = call i32 @bzero(i32* %41, i32 8)
%43 = load i32, i32* %6
%44 = bitcast %struct.sockaddr_in* %11 to %struct.sockaddr*
%45 = call i32 @connect(i32 %43, %struct.sockaddr* %44, i32 4)
%46 = icmp eq i32 %45, -1
br i1 %46, label %47, label %50
47: ; preds = %29
%48 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0))
%49 = call i32 @exit(i32 1)
unreachable
50: ; preds = %29
%51 = load i32, i32* %6
%52 = getelementptr inbounds [100 x i8], [100 x i8]* %9, i64 0, i64 0
%53 = call i32 @recv(i32 %51, i8* %52, i32 100, i32 0)
store i32 %53, i32* %7
%54 = icmp eq i32 %53, -1
br i1 %54, label %55, label %58
55: ; preds = %50
%56 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0))
%57 = call i32 @exit(i32 1)
unreachable
58: ; preds = %50
%59 = load i32, i32* %7
%60 = sext i32 %59 to i64
%61 = getelementptr inbounds [100 x i8], [100 x i8]* %9, i64 0, i64 %60
store i8 0, i8* %61
%62 = getelementptr inbounds [100 x i8], [100 x i8]* %9, i64 0, i64 0
%63 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.6, i64 0, i64 0), i8* %62)
%64 = load i32, i32* %6
%65 = call i32 @close(i32 %64)
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.in_addr, i32, i32 }
%struct.in_addr = type { i32 }
%struct.hostent = type { i64 }
%struct.sockaddr = type { i32 }
@.str.1 = [3 x i8] c"%s\00"
@.str.2 = [21 x i8] c"gethostbyname\E5\87\BA\E9\94\99!\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.3 = [20 x i8] c"socket\E5\88\9B\E5\BB\BA\E5\87\BA\E9\94\99!\00"
@.str.4 = [15 x i8] c"connect\E5\87\BA\E9\94\99!\00"
@.str.5 = [12 x i8] c"recv\E5\87\BA\E9\94\99!\00"
@.str.6 = [13 x i8] c"Received: %s\00"
@str = [36 x i8] c"Please enter the server's hostname!\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [1000 x i8]
%4 = alloca [100 x i8]
%5 = alloca %struct.sockaddr_in
%6 = getelementptr inbounds [1000 x i8], [1000 x i8]* %3, i64 0, i64 0
%7 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%8 = bitcast %struct.sockaddr_in* %5 to i8*
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @str, i64 0, i64 0))
%10 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %6)
%11 = call %struct.hostent* @gethostbyname(i8* %6)
%12 = icmp eq %struct.hostent* %11, null
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %2
%17 = load i32, i32* @AF_INET
%18 = load i32, i32* @SOCK_STREAM
%19 = call i32 @socket(i32 %17, i32 %18, i32 0)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %24
21: ; preds = %16
%22 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %16
%25 = load i32, i32* @AF_INET
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 3
store i32 %25, i32* %26
%27 = call i32 @htons(i32 1707)
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 2
store i32 %27, i32* %28
%29 = getelementptr inbounds %struct.hostent, %struct.hostent* %11, i64 0, i32 0
%30 = load i64, i64* %29
%31 = inttoptr i64 %30 to i32*
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 1, i32 0
%33 = load i32, i32* %31
store i32 %33, i32* %32
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 0
%35 = call i32 @bzero(i32* %34, i32 8)
%36 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%37 = call i32 @connect(i32 %19, %struct.sockaddr* %36, i32 4)
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %42
39: ; preds = %24
%40 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0))
%41 = call i32 @exit(i32 1)
unreachable
42: ; preds = %24
%43 = call i32 @recv(i32 %19, i8* %7, i32 100, i32 0)
%44 = icmp eq i32 %43, -1
br i1 %44, label %45, label %48
45: ; preds = %42
%46 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0))
%47 = call i32 @exit(i32 1)
unreachable
48: ; preds = %42
%49 = sext i32 %43 to i64
%50 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 %49
store i8 0, i8* %50
%51 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.6, i64 0, i64 0), i8* %7)
%52 = call i32 @close(i32 %19)
ret i32 0
}