#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  int s_addr;
};
struct sockaddr_in {
  int sin_family;
  int sin_port;
  TYPE_1__ sin_addr;
};
struct servent {
  int /*<<< orphan*/ s_port;
};
struct hostent {
  scalar_t__ *h_addr_list;
};
typedef int in_addr_t;

/* Variables and functions */
int AF_INET;
int /*<<< orphan*/ EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
struct hostent *gethostbyname(char *);
struct servent *getservbyname(char *, char *);
int htons(int);
int ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, int, int, int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int main(void) {
  char *host_name = "www.lwn.com";
  char *service_name = "http";
  char *proto_name = "tcp";
  struct hostent *host_info;
  struct servent *service;
  struct sockaddr_in transport_address;
  int domain = AF_INET;
  in_addr_t address = 0;
  int port = 0;
  if ((host_info = gethostbyname(host_name)) == NULL) {
    fprintf(stderr, "Err. gethostbyname() - %s\n", strerror(errno));
    exit(EXIT_FAILURE);
  }
  address = *((in_addr_t *)host_info->h_addr_list[0]);
  if ((service = getservbyname(service_name, proto_name)) == NULL) {
    fprintf(stderr, "Err. getservbyname() \n");
    exit(EXIT_FAILURE);
  }
  port = ntohs(service->s_port);
  transport_address.sin_family = domain;
  transport_address.sin_addr.s_addr = address;
  transport_address.sin_port = htons(port);
  printf("\t%u\t%u\t%u\n", transport_address.sin_family,
         transport_address.sin_addr.s_addr, transport_address.sin_port);
  return (EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe6a(%rip),%rax
mov    %rax,-0x10(%rbp)
lea    0xe6b(%rip),%rax
mov    %rax,-0x18(%rbp)
lea    0xe65(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0x2e31(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
movl   $0x0,-0x48(%rbp)
movl   $0x0,-0x4c(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <gethostbyname@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    121d <main+0x9d>
mov    0x2df0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x50(%rbp)
mov    0x2ddc(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x50(%rbp),%edi
mov    %rax,%rdx
lea    0xe0d(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    0x2db2(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%rax
mov    (%rax),%eax
mov    %eax,-0x48(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1050 <getservbyname@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    126c <main+0xec>
mov    0x2d8a(%rip),%rax
mov    (%rax),%edi
lea    0xdd9(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    0x2d63(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x30(%rbp),%rax
mov    (%rax),%edi
call   1080 <ntohs@plt>
mov    %eax,-0x4c(%rbp)
mov    -0x44(%rbp),%eax
mov    %eax,-0x40(%rbp)
mov    -0x48(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x4c(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x40(%rbp),%esi
mov    -0x38(%rbp),%edx
mov    -0x3c(%rbp),%ecx
lea    0xda6(%rip),%rdi
call   1040 <printf@plt>
mov    0x2d13(%rip),%rax
mov    (%rax),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
mov    0x2e5e(%rip),%rax
mov    (%rax),%ebx
lea    0xe6d(%rip),%rdi
call   1060 <gethostbyname@plt>
test   %rax,%rax
je     11ed <main+0x6d>
mov    (%rax),%rax
mov    (%rax),%rax
mov    (%rax),%ebp
lea    0xe60(%rip),%rdi
lea    0xe5e(%rip),%rsi
call   1050 <getservbyname@plt>
test   %rax,%rax
je     1219 <main+0x99>
mov    (%rax),%edi
call   1080 <ntohs@plt>
mov    %eax,%edi
call   1030 <htons@plt>
lea    0xe75(%rip),%rdi
mov    %ebx,%esi
mov    %ebp,%edx
mov    %eax,%ecx
call   1040 <printf@plt>
mov    0x2ddc(%rip),%rax
mov    (%rax),%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2de4(%rip),%rax
mov    (%rax),%ebx
mov    0x2dd3(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xe0a(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    1230 <main+0xb0>
mov    0x2db8(%rip),%rax
mov    (%rax),%edi
lea    0xe07(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    0x2d91(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.hostent = type { i64* }
%struct.servent = type { i32 }
%struct.sockaddr_in = type { i32, i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
@.str = [12 x i8] c"www.lwn.com\00"
@.str.1 = [5 x i8] c"http\00"
@.str.2 = [4 x i8] c"tcp\00"
@AF_INET = global i32 0
@stderr = global i32 0
@.str.3 = [27 x i8] c"Err. gethostbyname() - %s\0A\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@.str.4 = [23 x i8] c"Err. getservbyname() \0A\00"
@.str.5 = [11 x i8] c"\09%u\09%u\09%u\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca %struct.hostent*
%6 = alloca %struct.servent*
%7 = alloca %struct.sockaddr_in
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 0, i32* %1
store i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8** %2
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8** %3
store i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8** %4
%11 = load i32, i32* @AF_INET
store i32 %11, i32* %8
store i32 0, i32* %9
store i32 0, i32* %10
%12 = load i8*, i8** %2
%13 = call %struct.hostent* @gethostbyname(i8* %12)
store %struct.hostent* %13, %struct.hostent** %5
%14 = icmp eq %struct.hostent* %13, null
br i1 %14, label %15, label %22
15: ; preds = %0
%16 = load i32, i32* @stderr
%17 = load i32, i32* @errno
%18 = call i8* @strerror(i32 %17)
%19 = call i32 (i32, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0), i8* %18)
%20 = load i32, i32* @EXIT_FAILURE
%21 = call i32 @exit(i32 %20)
unreachable
22: ; preds = %0
%23 = load %struct.hostent*, %struct.hostent** %5
%24 = getelementptr inbounds %struct.hostent, %struct.hostent* %23, i32 0, i32 0
%25 = load i64*, i64** %24
%26 = getelementptr inbounds i64, i64* %25, i64 0
%27 = load i64, i64* %26
%28 = inttoptr i64 %27 to i32*
%29 = load i32, i32* %28
store i32 %29, i32* %9
%30 = load i8*, i8** %3
%31 = load i8*, i8** %4
%32 = call %struct.servent* @getservbyname(i8* %30, i8* %31)
store %struct.servent* %32, %struct.servent** %6
%33 = icmp eq %struct.servent* %32, null
br i1 %33, label %34, label %39
34: ; preds = %22
%35 = load i32, i32* @stderr
%36 = call i32 (i32, i8*, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0))
%37 = load i32, i32* @EXIT_FAILURE
%38 = call i32 @exit(i32 %37)
unreachable
39: ; preds = %22
%40 = load %struct.servent*, %struct.servent** %6
%41 = getelementptr inbounds %struct.servent, %struct.servent* %40, i32 0, i32 0
%42 = load i32, i32* %41
%43 = call i32 @ntohs(i32 %42)
store i32 %43, i32* %10
%44 = load i32, i32* %8
%45 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
store i32 %44, i32* %45
%46 = load i32, i32* %9
%47 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
%48 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %47, i32 0, i32 0
store i32 %46, i32* %48
%49 = load i32, i32* %10
%50 = call i32 @htons(i32 %49)
%51 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
store i32 %50, i32* %51
%52 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
%53 = load i32, i32* %52
%54 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
%55 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %54, i32 0, i32 0
%56 = load i32, i32* %55
%57 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
%58 = load i32, i32* %57
%59 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0), i32 %53, i32 %56, i32 %58)
%60 = load i32, i32* @EXIT_SUCCESS
ret i32 %60
}

LLVM IR (-O2):
%struct.hostent = type { i64* }
%struct.servent = type { i32 }
@.str = [12 x i8] c"www.lwn.com\00"
@.str.1 = [5 x i8] c"http\00"
@.str.2 = [4 x i8] c"tcp\00"
@AF_INET = global i32 0
@stderr = global i32 0
@.str.3 = [27 x i8] c"Err. gethostbyname() - %s\0A\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@.str.4 = [23 x i8] c"Err. getservbyname() \0A\00"
@.str.5 = [11 x i8] c"\09%u\09%u\09%u\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = load i32, i32* @AF_INET
%2 = tail call %struct.hostent* @gethostbyname(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq %struct.hostent* %2, null
br i1 %3, label %4, label %11
4: ; preds = %0
%5 = load i32, i32* @stderr
%6 = load i32, i32* @errno
%7 = tail call i8* @strerror(i32 %6)
%8 = tail call i32 (i32, i8*, ...) @fprintf(i32 %5, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0), i8* %7)
%9 = load i32, i32* @EXIT_FAILURE
%10 = tail call i32 @exit(i32 %9)
unreachable
11: ; preds = %0
%12 = getelementptr inbounds %struct.hostent, %struct.hostent* %2, i64 0, i32 0
%13 = load i64*, i64** %12
%14 = load i64, i64* %13
%15 = inttoptr i64 %14 to i32*
%16 = load i32, i32* %15
%17 = tail call %struct.servent* @getservbyname(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%18 = icmp eq %struct.servent* %17, null
br i1 %18, label %19, label %24
19: ; preds = %11
%20 = load i32, i32* @stderr
%21 = tail call i32 (i32, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0))
%22 = load i32, i32* @EXIT_FAILURE
%23 = tail call i32 @exit(i32 %22)
unreachable
24: ; preds = %11
%25 = getelementptr inbounds %struct.servent, %struct.servent* %17, i64 0, i32 0
%26 = load i32, i32* %25
%27 = tail call i32 @ntohs(i32 %26)
%28 = tail call i32 @htons(i32 %27)
%29 = tail call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0), i32 %1, i32 %16, i32 %28)
%30 = load i32, i32* @EXIT_SUCCESS
ret i32 %30
}