#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 sockaddr_in {
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct addrinfo {
  int /*<<< orphan*/ ai_addr;
};
typedef int /*<<< orphan*/ sa;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
char *gai_strerror(int);
int getaddrinfo(char const *, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                struct addrinfo **);
int getnameinfo(struct sockaddr *, int, char *, int, int /*<<< orphan*/ *,
                int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ memcpy(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
void get_host_name(const char *domain, char *out) {
  struct sockaddr_in sa;
  struct addrinfo *result;
  sa.sin_family = AF_INET;
  int error;
  error = getaddrinfo(domain, NULL, NULL, &result);
  if (error != 0) {
    printf("no such host %s.\ngetaddrinfo error: %s\n", domain,
           gai_strerror(error));
    exit(-1);
  }
  memcpy(&sa, result->ai_addr, sizeof(sa));
  error = getnameinfo((struct sockaddr *)&sa, sizeof(sa), out, 256, NULL, 0, 0);
  if (error != 0) {
    printf("getnameinfo error:%d", error);
    exit(-1);
  }
  freeaddrinfo(result);
}


x86 assembly (clang, -O0):
<get_host_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e61(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%edx
lea    -0x20(%rbp),%rcx
mov    %rdx,%rsi
call   1080 <getaddrinfo@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     11dc <get_host_name+0x6c>
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x24(%rbp),%edi
call   1040 <gai_strerror@plt>
mov    -0x30(%rbp),%rsi
mov    %rax,%rdx
lea    0xe35(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdi
mov    $0x4,%edx
call   1060 <memcpy@plt>
lea    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x4,%esi
mov    $0x100,%ecx
xor    %eax,%eax
mov    %eax,%r8d
xor    %r9d,%r9d
movl   $0x0,(%rsp)
call   1050 <getnameinfo@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     123e <get_host_name+0xce>
mov    -0x24(%rbp),%esi
lea    0xdfb(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
mov    -0x20(%rbp),%rdi
call   1090 <freeaddrinfo@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_host_name>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2e64(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
lea    0x10(%rsp),%rcx
xor    %esi,%esi
xor    %edx,%edx
call   1080 <getaddrinfo@plt>
test   %eax,%eax
jne    11ed <get_host_name+0x7d>
mov    0x10(%rsp),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x4,%edx
call   1060 <memcpy@plt>
movl   $0x0,(%rsp)
mov    %rbx,%rdi
mov    $0x4,%esi
mov    %r14,%rdx
mov    $0x100,%ecx
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1050 <getnameinfo@plt>
test   %eax,%eax
jne    1212 <get_host_name+0xa2>
mov    0x10(%rsp),%rdi
call   1090 <freeaddrinfo@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret
mov    %eax,%edi
call   1040 <gai_strerror@plt>
lea    0xe05(%rip),%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1030 <printf@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
lea    0xe0f(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1030 <printf@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.addrinfo = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@.str = [40 x i8] c"no such host %s.\0Agetaddrinfo error: %s\0A\00"
@.str.1 = [21 x i8] c"getnameinfo error:%d\00"
define void @get_host_name(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca %struct.sockaddr_in
%6 = alloca %struct.addrinfo*
%7 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
%8 = load i32, i32* @AF_INET
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 0
store i32 %8, i32* %9
%10 = load i8*, i8** %3
%11 = call i32 @getaddrinfo(i8* %10, i32* null, i32* null, %struct.addrinfo** %6)
store i32 %11, i32* %7
%12 = load i32, i32* %7
%13 = icmp ne i32 %12, 0
br i1 %13, label %14, label %20
14: ; preds = %2
%15 = load i8*, i8** %3
%16 = load i32, i32* %7
%17 = call i8* @gai_strerror(i32 %16)
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i8* %15, i8* %17)
%19 = call i32 @exit(i32 -1)
unreachable
20: ; preds = %2
%21 = load %struct.addrinfo*, %struct.addrinfo** %6
%22 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %21, i32 0, i32 0
%23 = load i32, i32* %22
%24 = call i32 @memcpy(%struct.sockaddr_in* %5, i32 %23, i32 4)
%25 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%26 = load i8*, i8** %4
%27 = call i32 @getnameinfo(%struct.sockaddr* %25, i32 4, i8* %26, i32 256, i32* null, i32 0, i32 0)
store i32 %27, i32* %7
%28 = load i32, i32* %7
%29 = icmp ne i32 %28, 0
br i1 %29, label %30, label %34
30: ; preds = %20
%31 = load i32, i32* %7
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %31)
%33 = call i32 @exit(i32 -1)
unreachable
34: ; preds = %20
%35 = load %struct.addrinfo*, %struct.addrinfo** %6
%36 = call i32 @freeaddrinfo(%struct.addrinfo* %35)
ret void
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.addrinfo = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@.str = [40 x i8] c"no such host %s.\0Agetaddrinfo error: %s\0A\00"
@.str.1 = [21 x i8] c"getnameinfo error:%d\00"
define void @get_host_name(i8* %0, i8* %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca %struct.addrinfo*
%5 = bitcast %struct.sockaddr_in* %3 to i8*
%6 = bitcast %struct.addrinfo** %4 to i8*
%7 = load i32, i32* @AF_INET
%8 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %7, i32* %8
%9 = call i32 @getaddrinfo(i8* %0, i32* null, i32* null, %struct.addrinfo** %4)
%10 = icmp eq i32 %9, 0
br i1 %10, label %15, label %11
11: ; preds = %2
%12 = call i8* @gai_strerror(i32 %9)
%13 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %12)
%14 = call i32 @exit(i32 -1)
unreachable
15: ; preds = %2
%16 = load %struct.addrinfo*, %struct.addrinfo** %4
%17 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %16, i64 0, i32 0
%18 = load i32, i32* %17
%19 = call i32 @memcpy(%struct.sockaddr_in* %3, i32 %18, i32 4)
%20 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%21 = call i32 @getnameinfo(%struct.sockaddr* %20, i32 4, i8* %1, i32 256, i32* null, i32 0, i32 0)
%22 = icmp eq i32 %21, 0
br i1 %22, label %26, label %23
23: ; preds = %15
%24 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %21)
%25 = call i32 @exit(i32 -1)
unreachable
26: ; preds = %15
%27 = load %struct.addrinfo*, %struct.addrinfo** %4
%28 = call i32 @freeaddrinfo(%struct.addrinfo* %27)
ret void
}