#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*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ atoi(char *);
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*/ herror(char *);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int /*<<< orphan*/ memset(int /*<<< orphan*/, char, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int recv(int, char *, int, int /*<<< orphan*/);
int send(int, char *, int /*<<< orphan*/, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strlen(char *);
int main(int argc, char *argv[]) {
  int sockfd, numbytes;
  char buf[100];
  struct hostent *he;
  struct sockaddr_in their_addr;
  if ((argc == 1) || (argc == 2)) {
    fprintf(stderr, "usage: client hostname\nEx:\n$./client01 ip port\n");
    exit(1);
  }
  if ((he = gethostbyname(argv[1])) == NULL) {
    herror("gethostbyname");
    exit(1);
  }
  if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
    perror("socket");
    exit(1);
  }
  their_addr.sin_family = AF_INET;
  their_addr.sin_port = htons(atoi(argv[2]));
  their_addr.sin_addr = *((struct in_addr *)he->h_addr);
  memset(their_addr.sin_zero, '\0', sizeof their_addr.sin_zero);
  if (connect(sockfd, (struct sockaddr *)&their_addr, sizeof their_addr) ==
      -1) {
    perror("connect");
    exit(1);
  }
  char *hello = "hello, time server!";
  if (send(sockfd, hello, strlen(hello), 0) == -1) {
    perror("send");
  }
  if ((numbytes = recv(sockfd, buf, 100 - 1, 0)) == -1) {
    perror("recv");
  } else if (numbytes == 0) {
    printf("Remote server has shutdown!\n");
  }
  buf[numbytes] = '\0';
  printf("Received: [%s]\n", buf);
  close(sockfd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x1,-0x8(%rbp)
je     121d <main+0x2d>
cmpl   $0x2,-0x8(%rbp)
jne    123c <main+0x4c>
mov    0x2db4(%rip),%rax
mov    (%rax),%edi
lea    0xdd3(%rip),%rsi
call   10b0 <fprintf@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10a0 <gethostbyname@plt>
mov    %rax,-0x88(%rbp)
cmp    $0x0,%rax
jne    1270 <main+0x80>
lea    0xdcf(%rip),%rdi
call   10c0 <herror@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
mov    0x2d51(%rip),%rax
mov    (%rax),%edi
mov    0x2d50(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1110 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    12ab <main+0xbb>
lea    0xda2(%rip),%rdi
call   10d0 <perror@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
mov    0x2d36(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10e0 <atoi@plt>
mov    %eax,%edi
call   1050 <htons@plt>
mov    %eax,-0x90(%rbp)
mov    -0x88(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%eax
mov    %eax,-0x94(%rbp)
mov    -0x98(%rbp),%edi
xor    %esi,%esi
mov    $0x4,%edx
call   1080 <memset@plt>
mov    -0x14(%rbp),%edi
lea    -0x98(%rbp),%rsi
mov    $0x10,%edx
call   1100 <connect@plt>
cmp    $0xffffffff,%eax
jne    132b <main+0x13b>
lea    0xd29(%rip),%rdi
call   10d0 <perror@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
lea    0xd1b(%rip),%rax
mov    %rax,-0xa0(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0xac(%rbp)
mov    -0xa0(%rbp),%rax
mov    %rax,-0xa8(%rbp)
mov    -0xa0(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0xac(%rbp),%edi
mov    -0xa8(%rbp),%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1060 <send@plt>
cmp    $0xffffffff,%eax
jne    1387 <main+0x197>
lea    0xcdf(%rip),%rdi
call   10d0 <perror@plt>
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    13b7 <main+0x1c7>
lea    0xcb9(%rip),%rdi
call   10d0 <perror@plt>
jmp    13d4 <main+0x1e4>
cmpl   $0x0,-0x18(%rbp)
jne    13cf <main+0x1df>
lea    0xca3(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
jmp    13d4 <main+0x1e4>
movslq -0x18(%rbp),%rax
movb   $0x0,-0x80(%rbp,%rax,1)
lea    -0x80(%rbp),%rsi
lea    0xca0(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x14(%rbp),%edi
call   1090 <close@plt>
xor    %eax,%eax
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x80,%rsp
add    $0xffffffff,%edi
cmp    $0x1,%edi
jbe    133f <main+0x13f>
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
call   10b0 <gethostbyname@plt>
test   %rax,%rax
je     135e <main+0x15e>
mov    %rax,%rbp
mov    0x2d92(%rip),%rax
mov    (%rax),%edi
mov    0x2d91(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1120 <socket@plt>
cmp    $0xffffffff,%eax
je     1374 <main+0x174>
mov    %eax,%r14d
mov    0x2d8d(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    0x10(%rbx),%rdi
call   10f0 <atoi@plt>
mov    %eax,%edi
call   1060 <htons@plt>
mov    %eax,0x8(%rsp)
mov    0x0(%rbp),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rsp)
xor    %esi,%esi
mov    $0x4,%edx
call   1090 <memset@plt>
mov    %rsp,%rsi
mov    %r14d,%edi
mov    $0x10,%edx
call   1110 <connect@plt>
cmp    $0xffffffff,%eax
je     137d <main+0x17d>
lea    0xda2(%rip),%rbx
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1070 <send@plt>
cmp    $0xffffffff,%eax
je     1331 <main+0x131>
lea    0x10(%rsp),%rsi
mov    %r14d,%edi
mov    $0x63,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,%ebx
test   %eax,%eax
je     12f4 <main+0xf4>
cmp    $0xffffffff,%ebx
jne    1300 <main+0x100>
lea    0xd79(%rip),%rdi
call   10e0 <perror@plt>
jmp    1300 <main+0x100>
lea    0xd80(%rip),%rdi
call   1040 <puts@plt>
movslq %ebx,%rax
movb   $0x0,0x10(%rsp,%rax,1)
lea    0xd5c(%rip),%rdi
lea    0x10(%rsp),%rsi
xor    %eax,%eax
call   1080 <printf@plt>
mov    %r14d,%edi
call   10a0 <close@plt>
xor    %eax,%eax
add    $0x80,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xd29(%rip),%rdi
call   10e0 <perror@plt>
jmp    12c7 <main+0xc7>
mov    0x2c92(%rip),%rax
mov    (%rax),%edi
lea    0xcb1(%rip),%rsi
call   10c0 <fprintf@plt>
mov    $0x1,%edi
call   1100 <exit@plt>
lea    0xccb(%rip),%rdi
call   10d0 <herror@plt>
mov    $0x1,%edi
call   1100 <exit@plt>
lea    0xcc3(%rip),%rdi
jmp    1384 <main+0x184>
lea    0xcc1(%rip),%rdi
call   10e0 <perror@plt>
mov    $0x1,%edi
call   1100 <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 }
@stderr = global i32 0
@.str = [48 x i8] c"usage: client hostname\0AEx:\0A$./client01 ip port\0A\00"
@.str.1 = [14 x i8] c"gethostbyname\00"
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.2 = [7 x i8] c"socket\00"
@AF_INET = global i32 0
@.str.3 = [8 x i8] c"connect\00"
@.str.4 = [20 x i8] c"hello, time server!\00"
@.str.5 = [5 x i8] c"send\00"
@.str.6 = [5 x i8] c"recv\00"
@.str.7 = [29 x i8] c"Remote server has shutdown!\0A\00"
@.str.8 = [16 x i8] c"Received: [%s]\0A\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 i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* %4
%13 = icmp eq i32 %12, 1
br i1 %13, label %17, label %14
14: ; preds = %2
%15 = load i32, i32* %4
%16 = icmp eq i32 %15, 2
br i1 %16, label %17, label %21
17: ; preds = %14, %2
%18 = load i32, i32* @stderr
%19 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str, i64 0, i64 0))
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %14
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 1
%24 = load i8*, i8** %23
%25 = call %struct.hostent* @gethostbyname(i8* %24)
store %struct.hostent* %25, %struct.hostent** %9
%26 = icmp eq %struct.hostent* %25, null
br i1 %26, label %27, label %30
27: ; preds = %21
%28 = call i32 @herror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %21
%31 = load i32, i32* @PF_INET
%32 = load i32, i32* @SOCK_STREAM
%33 = call i32 @socket(i32 %31, i32 %32, i32 0)
store i32 %33, i32* %6
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %38
35: ; preds = %30
%36 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%37 = call i32 @exit(i32 1)
unreachable
38: ; preds = %30
%39 = load i32, i32* @AF_INET
%40 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 3
store i32 %39, i32* %40
%41 = load i8**, i8*** %5
%42 = getelementptr inbounds i8*, i8** %41, i64 2
%43 = load i8*, i8** %42
%44 = call i32 @atoi(i8* %43)
%45 = call i32 @htons(i32 %44)
%46 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 2
store i32 %45, i32* %46
%47 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 1
%48 = load %struct.hostent*, %struct.hostent** %9
%49 = getelementptr inbounds %struct.hostent, %struct.hostent* %48, i32 0, i32 0
%50 = load i64, i64* %49
%51 = inttoptr i64 %50 to %struct.in_addr*
%52 = bitcast %struct.in_addr* %47 to i8*
%53 = bitcast %struct.in_addr* %51 to i8*
%54 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 0
%55 = load i32, i32* %54
%56 = call i32 @memset(i32 %55, i8 signext 0, i32 4)
%57 = load i32, i32* %6
%58 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%59 = call i32 @connect(i32 %57, %struct.sockaddr* %58, i32 16)
%60 = icmp eq i32 %59, -1
br i1 %60, label %61, label %64
61: ; preds = %38
%62 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%63 = call i32 @exit(i32 1)
unreachable
64: ; preds = %38
store i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i8** %11
%65 = load i32, i32* %6
%66 = load i8*, i8** %11
%67 = load i8*, i8** %11
%68 = call i32 @strlen(i8* %67)
%69 = call i32 @send(i32 %65, i8* %66, i32 %68, i32 0)
%70 = icmp eq i32 %69, -1
br i1 %70, label %71, label %73
71: ; preds = %64
%72 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
br label %73
73: ; preds = %71, %64
%74 = load i32, i32* %6
%75 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
%76 = call i32 @recv(i32 %74, i8* %75, i32 99, i32 0)
store i32 %76, i32* %7
%77 = icmp eq i32 %76, -1
br i1 %77, label %78, label %80
78: ; preds = %73
%79 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0))
br label %86
80: ; preds = %73
%81 = load i32, i32* %7
%82 = icmp eq i32 %81, 0
br i1 %82, label %83, label %85
83: ; preds = %80
%84 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.7, i64 0, i64 0))
br label %85
85: ; preds = %83, %80
br label %86
86: ; preds = %85, %78
%87 = load i32, i32* %7
%88 = sext i32 %87 to i64
%89 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 %88
store i8 0, i8* %89
%90 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
%91 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.8, i64 0, i64 0), i8* %90)
%92 = load i32, i32* %6
%93 = call i32 @close(i32 %92)
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 }
@stderr = global i32 0
@.str = [48 x i8] c"usage: client hostname\0AEx:\0A$./client01 ip port\0A\00"
@.str.1 = [14 x i8] c"gethostbyname\00"
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.2 = [7 x i8] c"socket\00"
@AF_INET = global i32 0
@.str.3 = [8 x i8] c"connect\00"
@.str.4 = [20 x i8] c"hello, time server!\00"
@.str.5 = [5 x i8] c"send\00"
@.str.6 = [5 x i8] c"recv\00"
@.str.8 = [16 x i8] c"Received: [%s]\0A\00"
@str = [28 x i8] c"Remote server has shutdown!\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [100 x i8]
%4 = alloca %struct.sockaddr_in
%5 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%6 = bitcast %struct.sockaddr_in* %4 to i8*
%7 = add i32 %0, -1
%8 = icmp ult i32 %7, 2
br i1 %8, label %9, label %13
9: ; preds = %2
%10 = load i32, i32* @stderr
%11 = tail call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str, i64 0, i64 0))
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %2
%14 = getelementptr inbounds i8*, i8** %1, i64 1
%15 = load i8*, i8** %14
%16 = tail call %struct.hostent* @gethostbyname(i8* %15)
%17 = icmp eq %struct.hostent* %16, null
br i1 %17, label %18, label %21
18: ; preds = %13
%19 = tail call i32 @herror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%20 = tail call i32 @exit(i32 1)
unreachable
21: ; preds = %13
%22 = load i32, i32* @PF_INET
%23 = load i32, i32* @SOCK_STREAM
%24 = tail call i32 @socket(i32 %22, i32 %23, i32 0)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %29
26: ; preds = %21
%27 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%28 = tail call i32 @exit(i32 1)
unreachable
29: ; preds = %21
%30 = load i32, i32* @AF_INET
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 3
store i32 %30, i32* %31
%32 = getelementptr inbounds i8*, i8** %1, i64 2
%33 = load i8*, i8** %32
%34 = tail call i32 @atoi(i8* %33)
%35 = tail call i32 @htons(i32 %34)
%36 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %35, i32* %36
%37 = getelementptr inbounds %struct.hostent, %struct.hostent* %16, i64 0, i32 0
%38 = load i64, i64* %37
%39 = inttoptr i64 %38 to i32*
%40 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1, i32 0
%41 = load i32, i32* %39
store i32 %41, i32* %40
%42 = tail call i32 @memset(i32 undef, i8 signext 0, i32 4)
%43 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%44 = call i32 @connect(i32 %24, %struct.sockaddr* %43, i32 16)
%45 = icmp eq i32 %44, -1
br i1 %45, label %46, label %49
46: ; preds = %29
%47 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%48 = call i32 @exit(i32 1)
unreachable
49: ; preds = %29
%50 = call i32 @strlen(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0))
%51 = call i32 @send(i32 %24, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i32 %50, i32 0)
%52 = icmp eq i32 %51, -1
br i1 %52, label %53, label %55
53: ; preds = %49
%54 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
br label %55
55: ; preds = %53, %49
%56 = call i32 @recv(i32 %24, i8* %5, i32 99, i32 0)
switch i32 %56, label %61 [
i32 -1, label %57
i32 0, label %59
]
57: ; preds = %55
%58 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0))
br label %61
59: ; preds = %55
%60 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str, i64 0, i64 0))
br label %61
61: ; preds = %55, %59, %57
%62 = sext i32 %56 to i64
%63 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 %62
store i8 0, i8* %63
%64 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.8, i64 0, i64 0), i8* %5)
%65 = call i32 @close(i32 %24)
ret i32 0
}