#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;
  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", "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)
cmpl   $0x1,-0x8(%rbp)
jne    11d3 <main+0x33>
lea    0xe36(%rip),%rax
mov    %rax,-0x18(%rbp)
jmp    11df <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    121c <main+0x7c>
mov    0x2dd2(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xdff(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    0xdf4(%rip),%rdi
lea    0xdf5(%rip),%rsi
call   1040 <getservbyname@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    125f <main+0xbf>
mov    0x2d8b(%rip),%rax
mov    (%rax),%edi
lea    0xdd5(%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    0xdc4(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2d67(%rip),%rax
mov    (%rax),%edi
mov    0x2d56(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2d4b(%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    0xd2b(%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
cmp    $0x1,%edi
jne    11bb <main+0x1b>
lea    0xe47(%rip),%rbp
jmp    11bf <main+0x1f>
mov    0x8(%rsi),%rbp
mov    %rbp,%rdi
call   1070 <gethostbyname@plt>
test   %rax,%rax
jne    11f0 <main+0x50>
mov    0x2dfd(%rip),%rax
mov    (%rax),%edi
lea    0xe2e(%rip),%rsi
mov    %rbp,%rdx
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    %rax,%rbx
lea    0xe1d(%rip),%rdi
lea    0xe1e(%rip),%rsi
call   1040 <getservbyname@plt>
test   %rax,%rax
jne    122c <main+0x8c>
mov    0x2dbe(%rip),%rax
mov    (%rax),%edi
lea    0xe08(%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    0xdfa(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1030 <printf@plt>
mov    0x2d9b(%rip),%r15
mov    (%r15),%edi
mov    0x2d89(%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    0xd78(%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
%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_DGRAM
%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 = getelementptr inbounds [128 x i8], [128 x i8]* %13, i64 0, i64 0
%64 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%65 = load i32, i32* %8
%66 = call i32 @sendto(i32 %62, i8* %63, i32 1, i32 0, %struct.sockaddr* %64, i32 %65)
store i32 %66, i32* %9
%67 = load i32, i32* %7
%68 = getelementptr inbounds [128 x i8], [128 x i8]* %13, i64 0, i64 0
%69 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%70 = call i32 @recvfrom(i32 %67, i8* %68, i32 128, i32 0, %struct.sockaddr* %69, i32* %8)
store i32 %70, i32* %9
%71 = load i32, i32* %9
%72 = sext i32 %71 to i64
%73 = getelementptr inbounds [128 x i8], [128 x i8]* %13, i64 0, i64 %72
store i8 0, i8* %73
%74 = load i32, i32* %9
%75 = getelementptr inbounds [128 x i8], [128 x i8]* %13, i64 0, i64 0
%76 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.6, i64 0, i64 0), i32 %74, i8* %75)
%77 = load i32, i32* %7
%78 = call i32 @close(i32 %77)
%79 = 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 %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 = icmp eq i32 %0, 1
br i1 %9, label %13, label %10
10: ; preds = %2
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
br label %13
13: ; preds = %2, %10
%14 = phi i8* [ %12, %10 ], [ getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), %2 ]
%15 = tail call %struct.hostent* @gethostbyname(i8* %14)
%16 = icmp eq %struct.hostent* %15, null
br i1 %16, label %17, label %21
17: ; preds = %13
%18 = load i32, i32* @stderr
%19 = tail call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* %14)
%20 = tail call i32 @exit(i32 1)
unreachable
21: ; preds = %13
%22 = 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))
%23 = icmp eq %struct.servent* %22, null
br i1 %23, label %24, label %28
24: ; preds = %21
%25 = load i32, i32* @stderr
%26 = tail call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0))
%27 = tail call i32 @exit(i32 1)
unreachable
28: ; preds = %21
%29 = getelementptr inbounds %struct.servent, %struct.servent* %22, i64 0, i32 0
%30 = load i32, i32* %29
%31 = tail call i32 @ntohs(i32 %30)
%32 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0), i32 %31)
%33 = load i32, i32* @AF_INET
%34 = load i32, i32* @SOCK_DGRAM
%35 = tail call i32 @socket(i32 %33, i32 %34, i32 0)
%36 = load i32, i32* @AF_INET
%37 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %36, i32* %37
%38 = load i32, i32* %29
%39 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
store i32 %38, i32* %39
%40 = getelementptr inbounds %struct.hostent, %struct.hostent* %15, i64 0, i32 0
%41 = load i64*, i64** %40
%42 = load i64, i64* %41
%43 = inttoptr i64 %42 to i32*
%44 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0, i32 0
%45 = load i32, i32* %43
store i32 %45, i32* %44
store i32 12, i32* %3
%46 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%47 = call i32 @sendto(i32 %35, i8* %8, i32 1, i32 0, %struct.sockaddr* %46, i32 12)
%48 = call i32 @recvfrom(i32 %35, i8* %8, i32 128, i32 0, %struct.sockaddr* %46, i32* %3)
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 %49
store i8 0, i8* %50
%51 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.6, i64 0, i64 0), i32 %48, i8* %8)
%52 = call i32 @close(i32 %35)
%53 = call i32 @exit(i32 0)
unreachable
}