#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_addrlen;
  int /*<<< orphan*/ ai_addr;
};
typedef int /*<<< orphan*/ host;

/* Variables and functions */
int /*<<< orphan*/ NI_NUMERICHOST;
int /*<<< orphan*/ errx(int, char *, int /*<<< orphan*/);
int /*<<< orphan*/ gai_strerror(int);
int getaddrinfo(char const *, int /*<<< orphan*/, int /*<<< orphan*/,
                struct addrinfo **);
int getnameinfo(int /*<<< orphan*/, int /*<<< orphan*/, char *, int,
                int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
char *resolve_host(const char *addr) {
  static char host[256];
  struct addrinfo *ai;
  int r;
  if ((r = getaddrinfo(addr, 0, 0, &ai)))
    errx(1, "getaddrinfo: %s", gai_strerror(r));
  if ((r = getnameinfo(ai->ai_addr, ai->ai_addrlen, host, sizeof(host), 0, 0,
                       NI_NUMERICHOST)))
    errx(1, "getnameinfo: %s", gai_strerror(r));
  return host;
}


x86 assembly (clang, -O0):
<resolve_host>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
xor    %edx,%edx
lea    -0x10(%rbp),%rcx
mov    %edx,%esi
call   1060 <getaddrinfo@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
je     1184 <resolve_host+0x44>
mov    -0x14(%rbp),%edi
call   1040 <gai_strerror@plt>
mov    %eax,%edx
mov    $0x1,%edi
lea    0xe81(%rip),%rsi
call   1030 <errx@plt>
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%esi
mov    0x2e48(%rip),%rax
mov    (%rax),%eax
lea    0x2eaf(%rip),%rdx
mov    $0x100,%ecx
xor    %r9d,%r9d
mov    %r9d,%r8d
mov    %eax,(%rsp)
call   1050 <getnameinfo@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
je     11db <resolve_host+0x9b>
mov    -0x14(%rbp),%edi
call   1040 <gai_strerror@plt>
mov    %eax,%edx
mov    $0x1,%edi
lea    0xe3a(%rip),%rsi
call   1030 <errx@plt>
lea    0x2e6e(%rip),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<resolve_host>:
sub    $0x18,%rsp
lea    0x10(%rsp),%rcx
xor    %esi,%esi
xor    %edx,%edx
call   1060 <getaddrinfo@plt>
test   %eax,%eax
je     1170 <resolve_host+0x30>
mov    %eax,%edi
call   1040 <gai_strerror@plt>
lea    0xe9c(%rip),%rsi
mov    $0x1,%edi
mov    %eax,%edx
call   1030 <errx@plt>
mov    0x10(%rsp),%rax
mov    (%rax),%esi
mov    0x4(%rax),%edi
mov    0x2e5f(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%rsp)
lea    0x2ec3(%rip),%rdx
mov    $0x100,%ecx
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1050 <getnameinfo@plt>
test   %eax,%eax
je     11bb <resolve_host+0x7b>
mov    %eax,%edi
call   1040 <gai_strerror@plt>
lea    0xe61(%rip),%rsi
mov    $0x1,%edi
mov    %eax,%edx
call   1030 <errx@plt>
lea    0x2e8e(%rip),%rax
add    $0x18,%rsp
ret

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32 }
@resolve_host.host = internal global [256 x i8] zeroinitializer
@.str = [16 x i8] c"getaddrinfo: %s\00"
@NI_NUMERICHOST = global i32 0
@.str.1 = [16 x i8] c"getnameinfo: %s\00"
define i8* @resolve_host(i8* %0) {
%2 = alloca i8*
%3 = alloca %struct.addrinfo*
%4 = alloca i32
store i8* %0, i8** %2
%5 = load i8*, i8** %2
%6 = call i32 @getaddrinfo(i8* %5, i32 0, i32 0, %struct.addrinfo** %3)
store i32 %6, i32* %4
%7 = icmp ne i32 %6, 0
br i1 %7, label %8, label %12
8: ; preds = %1
%9 = load i32, i32* %4
%10 = call i32 @gai_strerror(i32 %9)
%11 = call i32 @errx(i32 1, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %10)
br label %12
12: ; preds = %8, %1
%13 = load %struct.addrinfo*, %struct.addrinfo** %3
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %13, i32 0, i32 1
%15 = load i32, i32* %14
%16 = load %struct.addrinfo*, %struct.addrinfo** %3
%17 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %16, i32 0, i32 0
%18 = load i32, i32* %17
%19 = load i32, i32* @NI_NUMERICHOST
%20 = call i32 @getnameinfo(i32 %15, i32 %18, i8* getelementptr inbounds ([256 x i8], [256 x i8]* @resolve_host.host, i64 0, i64 0), i32 256, i32 0, i32 0, i32 %19)
store i32 %20, i32* %4
%21 = icmp ne i32 %20, 0
br i1 %21, label %22, label %26
22: ; preds = %12
%23 = load i32, i32* %4
%24 = call i32 @gai_strerror(i32 %23)
%25 = call i32 @errx(i32 1, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %24)
br label %26
26: ; preds = %22, %12
ret i8* getelementptr inbounds ([256 x i8], [256 x i8]* @resolve_host.host, i64 0, i64 0)
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32 }
@resolve_host.host = internal global [256 x i8] zeroinitializer
@.str = [16 x i8] c"getaddrinfo: %s\00"
@NI_NUMERICHOST = global i32 0
@.str.1 = [16 x i8] c"getnameinfo: %s\00"
define i8* @resolve_host(i8* %0) {
%2 = alloca %struct.addrinfo*
%3 = bitcast %struct.addrinfo** %2 to i8*
%4 = call i32 @getaddrinfo(i8* %0, i32 0, i32 0, %struct.addrinfo** %2)
%5 = icmp eq i32 %4, 0
br i1 %5, label %9, label %6
6: ; preds = %1
%7 = call i32 @gai_strerror(i32 %4)
%8 = call i32 @errx(i32 1, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %7)
br label %9
9: ; preds = %6, %1
%10 = load %struct.addrinfo*, %struct.addrinfo** %2
%11 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %10, i64 0, i32 1
%12 = load i32, i32* %11
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %10, i64 0, i32 0
%14 = load i32, i32* %13
%15 = load i32, i32* @NI_NUMERICHOST
%16 = call i32 @getnameinfo(i32 %12, i32 %14, i8* getelementptr inbounds ([256 x i8], [256 x i8]* @resolve_host.host, i64 0, i64 0), i32 256, i32 0, i32 0, i32 %15)
%17 = icmp eq i32 %16, 0
br i1 %17, label %21, label %18
18: ; preds = %9
%19 = call i32 @gai_strerror(i32 %16)
%20 = call i32 @errx(i32 1, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %19)
br label %21
21: ; preds = %18, %9
ret i8* getelementptr inbounds ([256 x i8], [256 x i8]* @resolve_host.host, i64 0, i64 0)
}