#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 addrinfo {
  int /*<<< orphan*/ ai_socktype;
  int /*<<< orphan*/ ai_protocol;
  int /*<<< orphan*/ ai_family;
};
typedef int in_port_t;

/* Variables and functions */
int /*<<< orphan*/ AF_UNSPEC;
int EAI_SYSTEM;
int /*<<< orphan*/ IPPROTO_TCP;
int NI_MAXSERV;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
char *gai_strerror(int);
int getaddrinfo(char const *, char *, struct addrinfo *, struct addrinfo **);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int /*<<< orphan*/ stderr;
struct addrinfo *lookuphost(const char *hostname, in_port_t port) {
  struct addrinfo *ai = 0;
  struct addrinfo hints = {.ai_family = AF_UNSPEC,
                           .ai_protocol = IPPROTO_TCP,
                           .ai_socktype = SOCK_STREAM};
  char service[NI_MAXSERV];
  int error;
  (void)snprintf(service, NI_MAXSERV, "%d", port);
  if ((error = getaddrinfo(hostname, service, &hints, &ai)) != 0) {
    if (error != EAI_SYSTEM) {
      fprintf(stderr, "getaddrinfo(): %s\n", gai_strerror(error));
    } else {
      perror("getaddrinfo()");
    }
  }
  return ai;
}


x86 assembly (clang, -O0):
<lookuphost>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movq   $0x0,-0x18(%rbp)
mov    0x2e4a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2e5e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2e62(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2e4e(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x48(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x38(%rbp)
mov    0x2e22(%rip),%rax
mov    (%rax),%esi
mov    -0xc(%rbp),%ecx
lea    0xe36(%rip),%rdx
call   1030 <snprintf@plt>
mov    -0x48(%rbp),%rsi
mov    -0x8(%rbp),%rdi
lea    -0x28(%rbp),%rdx
lea    -0x18(%rbp),%rcx
call   1070 <getaddrinfo@plt>
mov    %eax,-0x3c(%rbp)
cmp    $0x0,%eax
je     123e <lookuphost+0xee>
mov    -0x3c(%rbp),%eax
mov    0x2dd6(%rip),%rcx
cmp    (%rcx),%eax
je     122d <lookuphost+0xdd>
mov    0x2db7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x3c(%rbp),%edi
call   1040 <gai_strerror@plt>
mov    -0x4c(%rbp),%edi
mov    %rax,%rdx
lea    0xde0(%rip),%rsi
call   1050 <fprintf@plt>
jmp    1239 <lookuphost+0xe9>
lea    0xde2(%rip),%rdi
call   1060 <perror@plt>
jmp    123e <lookuphost+0xee>
mov    -0x18(%rbp),%rax
mov    -0x30(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lookuphost>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %esi,%ecx
mov    %rdi,%r14
mov    %rsp,%r15
movq   $0x0,-0x20(%rbp)
mov    0x2e44(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2e58(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2e5c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2e48(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rbx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
sub    %rdx,%rbx
mov    %rbx,%rsp
mov    (%rax),%esi
lea    0xe4c(%rip),%rdx
mov    %rbx,%rdi
call   1030 <snprintf@plt>
lea    -0x30(%rbp),%rdx
lea    -0x20(%rbp),%rcx
mov    %r14,%rdi
mov    %rbx,%rsi
call   1070 <getaddrinfo@plt>
test   %eax,%eax
je     11ff <lookuphost+0xaf>
mov    0x2df6(%rip),%rcx
cmp    (%rcx),%eax
je     1211 <lookuphost+0xc1>
mov    0x2ddb(%rip),%rcx
mov    (%rcx),%ebx
mov    %eax,%edi
call   1040 <gai_strerror@plt>
lea    0xe0e(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1050 <fprintf@plt>
mov    -0x20(%rbp),%rax
mov    %r15,%rsp
lea    -0x18(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdfe(%rip),%rdi
call   1060 <perror@plt>
jmp    11ff <lookuphost+0xaf>

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i32 }
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@AF_UNSPEC = global i32 0
@NI_MAXSERV = global i32 0
@.str = [3 x i8] c"%d\00"
@EAI_SYSTEM = global i32 0
@stderr = global i32 0
@.str.1 = [19 x i8] c"getaddrinfo(): %s\0A\00"
@.str.2 = [14 x i8] c"getaddrinfo()\00"
define %struct.addrinfo* @lookuphost(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.addrinfo*
%6 = alloca %struct.addrinfo
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
store %struct.addrinfo* null, %struct.addrinfo** %5
%10 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 0
%11 = load i32, i32* @SOCK_STREAM
store i32 %11, i32* %10
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 1
%13 = load i32, i32* @IPPROTO_TCP
store i32 %13, i32* %12
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 2
%15 = load i32, i32* @AF_UNSPEC
store i32 %15, i32* %14
%16 = load i32, i32* @NI_MAXSERV
%17 = zext i32 %16 to i64
%18 = call i8* @llvm.stacksave()
store i8* %18, i8** %7
%19 = alloca i8, i64 %17
store i64 %17, i64* %8
%20 = load i32, i32* @NI_MAXSERV
%21 = load i32, i32* %4
%22 = call i32 @snprintf(i8* %19, i32 %20, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %21)
%23 = load i8*, i8** %3
%24 = call i32 @getaddrinfo(i8* %23, i8* %19, %struct.addrinfo* %6, %struct.addrinfo** %5)
store i32 %24, i32* %9
%25 = icmp ne i32 %24, 0
br i1 %25, label %26, label %38
26: ; preds = %2
%27 = load i32, i32* %9
%28 = load i32, i32* @EAI_SYSTEM
%29 = icmp ne i32 %27, %28
br i1 %29, label %30, label %35
30: ; preds = %26
%31 = load i32, i32* @stderr
%32 = load i32, i32* %9
%33 = call i8* @gai_strerror(i32 %32)
%34 = call i32 @fprintf(i32 %31, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %33)
br label %37
35: ; preds = %26
%36 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %37
37: ; preds = %35, %30
br label %38
38: ; preds = %37, %2
%39 = load %struct.addrinfo*, %struct.addrinfo** %5
%40 = load i8*, i8** %7
ret %struct.addrinfo* %39
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32 }
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@AF_UNSPEC = global i32 0
@NI_MAXSERV = global i32 0
@.str = [3 x i8] c"%d\00"
@EAI_SYSTEM = global i32 0
@stderr = global i32 0
@.str.1 = [19 x i8] c"getaddrinfo(): %s\0A\00"
@.str.2 = [14 x i8] c"getaddrinfo()\00"
define %struct.addrinfo* @lookuphost(i8* %0, i32 %1) {
%3 = alloca %struct.addrinfo*
%4 = alloca %struct.addrinfo
%5 = bitcast %struct.addrinfo** %3 to i8*
store %struct.addrinfo* null, %struct.addrinfo** %3
%6 = bitcast %struct.addrinfo* %4 to i8*
%7 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 0
%8 = load i32, i32* @SOCK_STREAM
store i32 %8, i32* %7
%9 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 1
%10 = load i32, i32* @IPPROTO_TCP
store i32 %10, i32* %9
%11 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 2
%12 = load i32, i32* @AF_UNSPEC
store i32 %12, i32* %11
%13 = load i32, i32* @NI_MAXSERV
%14 = zext i32 %13 to i64
%15 = tail call i8* @llvm.stacksave()
%16 = alloca i8, i64 %14
%17 = load i32, i32* @NI_MAXSERV
%18 = call i32 @snprintf(i8* %16, i32 %17, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %1)
%19 = call i32 @getaddrinfo(i8* %0, i8* %16, %struct.addrinfo* %4, %struct.addrinfo** %3)
%20 = icmp eq i32 %19, 0
br i1 %20, label %30, label %21
21: ; preds = %2
%22 = load i32, i32* @EAI_SYSTEM
%23 = icmp eq i32 %19, %22
br i1 %23, label %28, label %24
24: ; preds = %21
%25 = load i32, i32* @stderr
%26 = call i8* @gai_strerror(i32 %19)
%27 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %26)
br label %30
28: ; preds = %21
%29 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %30
30: ; preds = %24, %28, %2
%31 = load %struct.addrinfo*, %struct.addrinfo** %3
ret %struct.addrinfo* %31
}