#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_DGRAM;
int /*<<< orphan*/ close(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*/ printf(char *, int, ...);
int recvfrom(int, char *, int, int /*<<< orphan*/, struct sockaddr *, int *);
int sendto(int, char *, int, int /*<<< orphan*/, struct sockaddr *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  char *host = "localhost";
  int sockfd;
  int len, result;
  struct sockaddr_in address;
  struct hostent *hostinfo;
  struct servent *servinfo;
  char buffer[128];
  hostinfo = gethostbyname(host);
  if (!hostinfo) {
    fprintf(stderr, "no host: %s\n", host);
    exit(1);
  }
  servinfo = getservbyname("daytime", "udp");
  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_DGRAM, 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 = sendto(sockfd, buffer, 1, 0, (struct sockaddr *)&address, len);
  result = recvfrom(sockfd, buffer, sizeof(buffer), 0,
                    (struct sockaddr *)&address, &len);
  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)
lea    0xe40(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1070 <gethostbyname@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1201 <main+0x61>
mov    0x2ded(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xe1a(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    0xe0f(%rip),%rdi
lea    0xe10(%rip),%rsi
call   1040 <getservbyname@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    1244 <main+0xa4>
mov    0x2da6(%rip),%rax
mov    (%rax),%edi
lea    0xdf0(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x40(%rbp),%rax
mov    (%rax),%edi
call   10a0 <ntohs@plt>
mov    %eax,%esi
lea    0xddf(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2d82(%rip),%rax
mov    (%rax),%edi
mov    0x2d71(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2d66(%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    -0xc0(%rbp),%rsi
lea    -0x30(%rbp),%r8
mov    -0x20(%rbp),%r9d
mov    $0x1,%edx
xor    %ecx,%ecx
call   1050 <sendto@plt>
mov    %eax,-0x24(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0xc0(%rbp),%rsi
lea    -0x30(%rbp),%r8
mov    $0x80,%edx
xor    %ecx,%ecx
lea    -0x20(%rbp),%r9
call   1090 <recvfrom@plt>
mov    %eax,-0x24(%rbp)
movslq -0x24(%rbp),%rax
movb   $0x0,-0xc0(%rbp,%rax,1)
mov    -0x24(%rbp),%esi
lea    -0xc0(%rbp),%rdx
lea    0xd46(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
xor    %edi,%edi
call   10b0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0xa8,%rsp
lea    0xe4c(%rip),%rdi
call   1070 <gethostbyname@plt>
test   %rax,%rax
jne    11e6 <main+0x46>
mov    0x2e0b(%rip),%rax
mov    (%rax),%edi
lea    0xe3c(%rip),%rsi
lea    0xe2b(%rip),%rdx
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    %rax,%rbx
lea    0xe27(%rip),%rdi
lea    0xe28(%rip),%rsi
call   1040 <getservbyname@plt>
test   %rax,%rax
jne    1222 <main+0x82>
mov    0x2dc8(%rip),%rax
mov    (%rax),%edi
lea    0xe12(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    %rax,%rbp
mov    (%rax),%edi
call   10a0 <ntohs@plt>
lea    0xe04(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1030 <printf@plt>
mov    0x2da5(%rip),%r15
mov    (%r15),%edi
mov    0x2d93(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,%r14d
mov    (%r15),%eax
mov    %eax,0x18(%rsp)
mov    0x0(%rbp),%eax
mov    %eax,0x14(%rsp)
mov    (%rbx),%rax
mov    (%rax),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
movl   $0xc,0xc(%rsp)
lea    0x20(%rsp),%rbx
lea    0x10(%rsp),%rbp
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
xor    %ecx,%ecx
mov    %rbp,%r8
mov    $0xc,%r9d
call   1050 <sendto@plt>
lea    0xc(%rsp),%r9
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x80,%edx
xor    %ecx,%ecx
mov    %rbp,%r8
call   1090 <recvfrom@plt>
movslq %eax,%rsi
movb   $0x0,0x20(%rsp,%rsi,1)
lea    0xd82(%rip),%rdi
mov    %rbx,%rdx
xor    %eax,%eax
call   1030 <printf@plt>
mov    %r14d,%edi
call   1060 <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"udp\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_DGRAM = global i32 0
@.str.6 = [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
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8** %6
%14 = load i8*, i8** %6
%15 = call %struct.hostent* @gethostbyname(i8* %14)
store %struct.hostent* %15, %struct.hostent** %11
%16 = load %struct.hostent*, %struct.hostent** %11
%17 = icmp ne %struct.hostent* %16, null
br i1 %17, label %23, label %18
18: ; preds = %2
%19 = load i32, i32* @stderr
%20 = load i8*, i8** %6
%21 = call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* %20)
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %2
%24 = 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* %24, %struct.servent** %12
%25 = load %struct.servent*, %struct.servent** %12
%26 = icmp ne %struct.servent* %25, null
br i1 %26, label %31, label %27
27: ; preds = %23
%28 = load i32, i32* @stderr
%29 = call i32 (i32, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0))
%30 = call i32 @exit(i32 1)
unreachable
31: ; preds = %23
%32 = load %struct.servent*, %struct.servent** %12
%33 = getelementptr inbounds %struct.servent, %struct.servent* %32, i32 0, i32 0
%34 = load i32, i32* %33
%35 = call i32 @ntohs(i32 %34)
%36 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0), i32 %35)
%37 = load i32, i32* @AF_INET
%38 = load i32, i32* @SOCK_DGRAM
%39 = call i32 @socket(i32 %37, i32 %38, i32 0)
store i32 %39, i32* %7
%40 = load i32, i32* @AF_INET
%41 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 2
store i32 %40, i32* %41
%42 = load %struct.servent*, %struct.servent** %12
%43 = getelementptr inbounds %struct.servent, %struct.servent* %42, i32 0, i32 0
%44 = load i32, i32* %43
%45 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 1
store i32 %44, i32* %45
%46 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 0
%47 = load %struct.hostent*, %struct.hostent** %11
%48 = getelementptr inbounds %struct.hostent, %struct.hostent* %47, i32 0, i32 0
%49 = load i64*, i64** %48
%50 = load i64, i64* %49
%51 = inttoptr i64 %50 to %struct.in_addr*
%52 = bitcast %struct.in_addr* %46 to i8*
%53 = bitcast %struct.in_addr* %51 to i8*
store i32 12, i32* %8
%54 = load i32, i32* %7
%55 = getelementptr inbounds [128 x i8], [128 x i8]* %13, i64 0, i64 0
%56 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%57 = load i32, i32* %8
%58 = call i32 @sendto(i32 %54, i8* %55, i32 1, i32 0, %struct.sockaddr* %56, i32 %57)
store i32 %58, i32* %9
%59 = load i32, i32* %7
%60 = getelementptr inbounds [128 x i8], [128 x i8]* %13, i64 0, i64 0
%61 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%62 = call i32 @recvfrom(i32 %59, i8* %60, i32 128, i32 0, %struct.sockaddr* %61, i32* %8)
store i32 %62, i32* %9
%63 = load i32, i32* %9
%64 = sext i32 %63 to i64
%65 = getelementptr inbounds [128 x i8], [128 x i8]* %13, i64 0, i64 %64
store i8 0, i8* %65
%66 = load i32, i32* %9
%67 = getelementptr inbounds [128 x i8], [128 x i8]* %13, i64 0, i64 0
%68 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.6, i64 0, i64 0), i32 %66, i8* %67)
%69 = load i32, i32* %7
%70 = call i32 @close(i32 %69)
%71 = 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"udp\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_DGRAM = global i32 0
@.str.6 = [18 x i8] c"read %d bytes: %s\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = alloca [128 x i8]
%6 = bitcast i32* %3 to i8*
%7 = bitcast %struct.sockaddr_in* %4 to i8*
%8 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%9 = tail call %struct.hostent* @gethostbyname(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%10 = icmp eq %struct.hostent* %9, null
br i1 %10, label %11, label %15
11: ; preds = %2
%12 = load i32, i32* @stderr
%13 = tail call i32 (i32, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%14 = tail call i32 @exit(i32 1)
unreachable
15: ; preds = %2
%16 = 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))
%17 = icmp eq %struct.servent* %16, null
br i1 %17, label %18, label %22
18: ; preds = %15
%19 = load i32, i32* @stderr
%20 = tail call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0))
%21 = tail call i32 @exit(i32 1)
unreachable
22: ; preds = %15
%23 = getelementptr inbounds %struct.servent, %struct.servent* %16, i64 0, i32 0
%24 = load i32, i32* %23
%25 = tail call i32 @ntohs(i32 %24)
%26 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0), i32 %25)
%27 = load i32, i32* @AF_INET
%28 = load i32, i32* @SOCK_DGRAM
%29 = tail call i32 @socket(i32 %27, i32 %28, i32 0)
%30 = load i32, i32* @AF_INET
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %30, i32* %31
%32 = load i32, i32* %23
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
store i32 %32, i32* %33
%34 = getelementptr inbounds %struct.hostent, %struct.hostent* %9, i64 0, i32 0
%35 = load i64*, i64** %34
%36 = load i64, i64* %35
%37 = inttoptr i64 %36 to i32*
%38 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0, i32 0
%39 = load i32, i32* %37
store i32 %39, i32* %38
store i32 12, i32* %3
%40 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%41 = call i32 @sendto(i32 %29, i8* %8, i32 1, i32 0, %struct.sockaddr* %40, i32 12)
%42 = call i32 @recvfrom(i32 %29, i8* %8, i32 128, i32 0, %struct.sockaddr* %40, i32* %3)
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 %43
store i8 0, i8* %44
%45 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.6, i64 0, i64 0), i32 %42, i8* %8)
%46 = call i32 @close(i32 %29)
%47 = call i32 @exit(i32 0)
unreachable
}