#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 servent {
  int /*<<< orphan*/ s_port;
};
struct hostent {
  scalar_t__ *h_addr_list;
};
typedef int /*<<< orphan*/ buffer;
typedef int /*<<< orphan*/ address;

/* 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 *);
struct servent *getservbyname(char *, char *);
int ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, ...);
int read(int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  char *host;
  int sockfd;
  int len, result;
  struct sockaddr_in address;
  struct hostent *hostinfo;
  struct servent *servinfo;
  char buffer[128];
  if (argc == 1)
    host = "localhost";
  else
    host = argv[1];
  hostinfo = gethostbyname(host);
  if (!hostinfo) {
    fprintf(stderr, "no host: %s\n", host);
    exit(1);
  }
  servinfo = getservbyname("daytime", "tcp");
  if (!servinfo) {
    fprintf(stderr, "no daytime service\n");
    exit(1);
  }
  printf("daytime port is %d\n", ntohs(servinfo->s_port));
  sockfd = socket(AF_INET, SOCK_STREAM, 0);
  address.sin_family = AF_INET;
  address.sin_port = servinfo->s_port;
  address.sin_addr = *(struct in_addr *)*hostinfo->h_addr_list;
  len = sizeof(address);
  result = connect(sockfd, (struct sockaddr *)&address, len);
  if (result == -1) {
    perror("oops: getdate");
    exit(1);
  }
  result = read(sockfd, buffer, sizeof(buffer));
  buffer[result] = '\0';
  printf("read %d bytes: %s", result, buffer);
  close(sockfd);
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x1,-0x8(%rbp)
jne    11e3 <main+0x33>
lea    0xe26(%rip),%rax
mov    %rax,-0x18(%rbp)
jmp    11ef <main+0x3f>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1070 <gethostbyname@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    122c <main+0x7c>
mov    0x2dca(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xdef(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    0xde4(%rip),%rdi
lea    0xde5(%rip),%rsi
call   1040 <getservbyname@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    126f <main+0xbf>
mov    0x2d83(%rip),%rax
mov    (%rax),%edi
lea    0xdc5(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x40(%rbp),%rax
mov    (%rax),%edi
call   1090 <ntohs@plt>
mov    %eax,%esi
lea    0xdb4(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2d57(%rip),%rax
mov    (%rax),%edi
mov    0x2d36(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2d3b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x40(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x38(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
movl   $0xc,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    -0x20(%rbp),%edx
call   10c0 <connect@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    1303 <main+0x153>
lea    0xd57(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x1c(%rbp),%edi
lea    -0xc0(%rbp),%rsi
mov    $0x80,%edx
call   1060 <read@plt>
mov    %eax,-0x24(%rbp)
movslq -0x24(%rbp),%rax
movb   $0x0,-0xc0(%rbp,%rax,1)
mov    -0x24(%rbp),%esi
lea    -0xc0(%rbp),%rdx
lea    0xd22(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
xor    %edi,%edi
call   10b0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x98,%rsp
cmp    $0x1,%edi
jne    11cb <main+0x1b>
lea    0xe37(%rip),%rbp
jmp    11cf <main+0x1f>
mov    0x8(%rsi),%rbp
mov    %rbp,%rdi
call   1070 <gethostbyname@plt>
test   %rax,%rax
jne    1200 <main+0x50>
mov    0x2df5(%rip),%rax
mov    (%rax),%edi
lea    0xe1e(%rip),%rsi
mov    %rbp,%rdx
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    %rax,%rbx
lea    0xe0d(%rip),%rdi
lea    0xe0e(%rip),%rsi
call   1040 <getservbyname@plt>
test   %rax,%rax
jne    123c <main+0x8c>
mov    0x2db6(%rip),%rax
mov    (%rax),%edi
lea    0xdf8(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    %rax,%rbp
mov    (%rax),%edi
call   1090 <ntohs@plt>
lea    0xdea(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1030 <printf@plt>
mov    0x2d8b(%rip),%r15
mov    (%r15),%edi
mov    0x2d69(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,%r14d
mov    (%r15),%eax
mov    %eax,0x8(%rsp)
mov    0x0(%rbp),%eax
mov    %eax,0x4(%rsp)
mov    (%rbx),%rax
mov    (%rax),%rax
mov    (%rax),%eax
mov    %eax,(%rsp)
mov    %rsp,%rsi
mov    %r14d,%edi
mov    $0xc,%edx
call   10c0 <connect@plt>
cmp    $0xffffffff,%eax
jne    12b7 <main+0x107>
lea    0xda3(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    0x10(%rsp),%rbx
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x80,%edx
call   1060 <read@plt>
movslq %eax,%rsi
movb   $0x0,0x10(%rsp,%rsi,1)
lea    0xd7e(%rip),%rdi
mov    %rbx,%rdx
xor    %eax,%eax
call   1030 <printf@plt>
mov    %r14d,%edi
call   1050 <close@plt>
xor    %edi,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.in_addr, i32, i32 }
%struct.in_addr = type { i32 }
%struct.hostent = type { i64* }
%struct.servent = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [10 x i8] c"localhost\00"
@stderr = global i32 0
@.str.1 = [13 x i8] c"no host: %s\0A\00"
@.str.2 = [8 x i8] c"daytime\00"
@.str.3 = [4 x i8] c"tcp\00"
@.str.4 = [20 x i8] c"no daytime service\0A\00"
@.str.5 = [20 x i8] c"daytime port is %d\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.6 = [14 x i8] c"oops: getdate\00"
@.str.7 = [18 x i8] c"read %d bytes: %s\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.sockaddr_in
%11 = alloca %struct.hostent*
%12 = alloca %struct.servent*
%13 = alloca [128 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%14 = load i32, i32* %4
%15 = icmp eq i32 %14, 1
br i1 %15, label %16, label %17
16: ; preds = %2
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8** %6
br label %21
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
store i8* %20, i8** %6
br label %21
21: ; preds = %17, %16
%22 = load i8*, i8** %6
%23 = call %struct.hostent* @gethostbyname(i8* %22)
store %struct.hostent* %23, %struct.hostent** %11
%24 = load %struct.hostent*, %struct.hostent** %11
%25 = icmp ne %struct.hostent* %24, null
br i1 %25, label %31, label %26
26: ; preds = %21
%27 = load i32, i32* @stderr
%28 = load i8*, i8** %6
%29 = call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* %28)
%30 = call i32 @exit(i32 1)
unreachable
31: ; preds = %21
%32 = call %struct.servent* @getservbyname(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
store %struct.servent* %32, %struct.servent** %12
%33 = load %struct.servent*, %struct.servent** %12
%34 = icmp ne %struct.servent* %33, null
br i1 %34, label %39, label %35
35: ; preds = %31
%36 = load i32, i32* @stderr
%37 = call i32 (i32, i8*, ...) @fprintf(i32 %36, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0))
%38 = call i32 @exit(i32 1)
unreachable
39: ; preds = %31
%40 = load %struct.servent*, %struct.servent** %12
%41 = getelementptr inbounds %struct.servent, %struct.servent* %40, i32 0, i32 0
%42 = load i32, i32* %41
%43 = call i32 @ntohs(i32 %42)
%44 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0), i32 %43)
%45 = load i32, i32* @AF_INET
%46 = load i32, i32* @SOCK_STREAM
%47 = call i32 @socket(i32 %45, i32 %46, i32 0)
store i32 %47, i32* %7
%48 = load i32, i32* @AF_INET
%49 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 2
store i32 %48, i32* %49
%50 = load %struct.servent*, %struct.servent** %12
%51 = getelementptr inbounds %struct.servent, %struct.servent* %50, i32 0, i32 0
%52 = load i32, i32* %51
%53 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 1
store i32 %52, i32* %53
%54 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 0
%55 = load %struct.hostent*, %struct.hostent** %11
%56 = getelementptr inbounds %struct.hostent, %struct.hostent* %55, i32 0, i32 0
%57 = load i64*, i64** %56
%58 = load i64, i64* %57
%59 = inttoptr i64 %58 to %struct.in_addr*
%60 = bitcast %struct.in_addr* %54 to i8*
%61 = bitcast %struct.in_addr* %59 to i8*
store i32 12, i32* %8
%62 = load i32, i32* %7
%63 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%64 = load i32, i32* %8
%65 = call i32 @connect(i32 %62, %struct.sockaddr* %63, i32 %64)
store i32 %65, i32* %9
%66 = load i32, i32* %9
%67 = icmp eq i32 %66, -1
br i1 %67, label %68, label %71
68: ; preds = %39
%69 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.6, i64 0, i64 0))
%70 = call i32 @exit(i32 1)
unreachable
71: ; preds = %39
%72 = load i32, i32* %7
%73 = getelementptr inbounds [128 x i8], [128 x i8]* %13, i64 0, i64 0
%74 = call i32 @read(i32 %72, i8* %73, i32 128)
store i32 %74, i32* %9
%75 = load i32, i32* %9
%76 = sext i32 %75 to i64
%77 = getelementptr inbounds [128 x i8], [128 x i8]* %13, i64 0, i64 %76
store i8 0, i8* %77
%78 = load i32, i32* %9
%79 = getelementptr inbounds [128 x i8], [128 x i8]* %13, i64 0, i64 0
%80 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.7, i64 0, i64 0), i32 %78, i8* %79)
%81 = load i32, i32* %7
%82 = call i32 @close(i32 %81)
%83 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.in_addr, i32, i32 }
%struct.in_addr = type { i32 }
%struct.hostent = type { i64* }
%struct.servent = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [10 x i8] c"localhost\00"
@stderr = global i32 0
@.str.1 = [13 x i8] c"no host: %s\0A\00"
@.str.2 = [8 x i8] c"daytime\00"
@.str.3 = [4 x i8] c"tcp\00"
@.str.4 = [20 x i8] c"no daytime service\0A\00"
@.str.5 = [20 x i8] c"daytime port is %d\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.6 = [14 x i8] c"oops: getdate\00"
@.str.7 = [18 x i8] c"read %d bytes: %s\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca [128 x i8]
%5 = bitcast %struct.sockaddr_in* %3 to i8*
%6 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%7 = icmp eq i32 %0, 1
br i1 %7, label %11, label %8
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
br label %11
11: ; preds = %2, %8
%12 = phi i8* [ %10, %8 ], [ getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), %2 ]
%13 = tail call %struct.hostent* @gethostbyname(i8* %12)
%14 = icmp eq %struct.hostent* %13, null
br i1 %14, label %15, label %19
15: ; preds = %11
%16 = load i32, i32* @stderr
%17 = tail call i32 (i32, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* %12)
%18 = tail call i32 @exit(i32 1)
unreachable
19: ; preds = %11
%20 = tail call %struct.servent* @getservbyname(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
%21 = icmp eq %struct.servent* %20, null
br i1 %21, label %22, label %26
22: ; preds = %19
%23 = load i32, i32* @stderr
%24 = tail call i32 (i32, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0))
%25 = tail call i32 @exit(i32 1)
unreachable
26: ; preds = %19
%27 = getelementptr inbounds %struct.servent, %struct.servent* %20, i64 0, i32 0
%28 = load i32, i32* %27
%29 = tail call i32 @ntohs(i32 %28)
%30 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0), i32 %29)
%31 = load i32, i32* @AF_INET
%32 = load i32, i32* @SOCK_STREAM
%33 = tail call i32 @socket(i32 %31, i32 %32, i32 0)
%34 = load i32, i32* @AF_INET
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %34, i32* %35
%36 = load i32, i32* %27
%37 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %36, i32* %37
%38 = getelementptr inbounds %struct.hostent, %struct.hostent* %13, i64 0, i32 0
%39 = load i64*, i64** %38
%40 = load i64, i64* %39
%41 = inttoptr i64 %40 to i32*
%42 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0, i32 0
%43 = load i32, i32* %41
store i32 %43, i32* %42
%44 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%45 = call i32 @connect(i32 %33, %struct.sockaddr* %44, i32 12)
%46 = icmp eq i32 %45, -1
br i1 %46, label %47, label %50
47: ; preds = %26
%48 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.6, i64 0, i64 0))
%49 = call i32 @exit(i32 1)
unreachable
50: ; preds = %26
%51 = call i32 @read(i32 %33, i8* %6, i32 128)
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 %52
store i8 0, i8* %53
%54 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.7, i64 0, i64 0), i32 %51, i8* %6)
%55 = call i32 @close(i32 %33)
%56 = call i32 @exit(i32 0)
unreachable
}