#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*/ fprintf(int /*<<< orphan*/, char *, char *);
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*/ stderr;
void get_hostname(const char *hostname, char *out_ip) {
  struct sockaddr_in sa;
  struct addrinfo *res;
  int n;
  sa.sin_family = AF_INET;
  if ((n = getaddrinfo(hostname, NULL, NULL, &res)) != 0) {
    fprintf(stderr, "getaddrinfo() failed: %s\n", gai_strerror(n));
    exit(-1);
  }
  memcpy(&sa, res->ai_addr, sizeof(sa));
  n = getnameinfo((struct sockaddr *)&sa, sizeof(sa), out_ip, 256, NULL, 0, 0);
  if (n != 0) {
    fprintf(stderr, "getnameinfo() failed: %s\n", gai_strerror(n));
    exit(-1);
  }
  freeaddrinfo(res);
}


x86 assembly (clang, -O0):
<get_hostname>:
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)
cmp    $0x0,%eax
je     11dc <get_hostname+0x6c>
mov    0x2e25(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%edi
call   1030 <gai_strerror@plt>
mov    -0x28(%rbp),%edi
mov    %rax,%rdx
lea    0xe33(%rip),%rsi
call   1050 <fprintf@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   1040 <getnameinfo@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     1253 <get_hostname+0xe3>
mov    0x2dae(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x24(%rbp),%edi
call   1030 <gai_strerror@plt>
mov    -0x2c(%rbp),%edi
mov    %rax,%rdx
lea    0xdd6(%rip),%rsi
call   1050 <fprintf@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_hostname>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r14
mov    0x2e67(%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    11ea <get_hostname+0x7a>
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   1040 <getnameinfo@plt>
test   %eax,%eax
jne    1203 <get_hostname+0x93>
mov    0x10(%rsp),%rdi
call   1090 <freeaddrinfo@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2de7(%rip),%rcx
mov    (%rcx),%ebx
mov    %eax,%edi
call   1030 <gai_strerror@plt>
lea    0xdff(%rip),%rsi
jmp    121a <get_hostname+0xaa>
mov    0x2dce(%rip),%rcx
mov    (%rcx),%ebx
mov    %eax,%edi
call   1030 <gai_strerror@plt>
lea    0xe00(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1050 <fprintf@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
@stderr = global i32 0
@.str = [26 x i8] c"getaddrinfo() failed: %s\0A\00"
@.str.1 = [26 x i8] c"getnameinfo() failed: %s\0A\00"
define void @get_hostname(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 = icmp ne i32 %11, 0
br i1 %12, label %13, label %19
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = load i32, i32* %7
%16 = call i8* @gai_strerror(i32 %15)
%17 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %16)
%18 = call i32 @exit(i32 -1)
unreachable
19: ; preds = %2
%20 = load %struct.addrinfo*, %struct.addrinfo** %6
%21 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %20, i32 0, i32 0
%22 = load i32, i32* %21
%23 = call i32 @memcpy(%struct.sockaddr_in* %5, i32 %22, i32 4)
%24 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%25 = load i8*, i8** %4
%26 = call i32 @getnameinfo(%struct.sockaddr* %24, i32 4, i8* %25, i32 256, i32* null, i32 0, i32 0)
store i32 %26, i32* %7
%27 = load i32, i32* %7
%28 = icmp ne i32 %27, 0
br i1 %28, label %29, label %35
29: ; preds = %19
%30 = load i32, i32* @stderr
%31 = load i32, i32* %7
%32 = call i8* @gai_strerror(i32 %31)
%33 = call i32 @fprintf(i32 %30, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %32)
%34 = call i32 @exit(i32 -1)
unreachable
35: ; preds = %19
%36 = load %struct.addrinfo*, %struct.addrinfo** %6
%37 = call i32 @freeaddrinfo(%struct.addrinfo* %36)
ret void
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.addrinfo = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@stderr = global i32 0
@.str = [26 x i8] c"getaddrinfo() failed: %s\0A\00"
@.str.1 = [26 x i8] c"getnameinfo() failed: %s\0A\00"
define void @get_hostname(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 %16, label %11
11: ; preds = %2
%12 = load i32, i32* @stderr
%13 = call i8* @gai_strerror(i32 %9)
%14 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %13)
%15 = call i32 @exit(i32 -1)
unreachable
16: ; preds = %2
%17 = load %struct.addrinfo*, %struct.addrinfo** %4
%18 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %17, i64 0, i32 0
%19 = load i32, i32* %18
%20 = call i32 @memcpy(%struct.sockaddr_in* %3, i32 %19, i32 4)
%21 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%22 = call i32 @getnameinfo(%struct.sockaddr* %21, i32 4, i8* %1, i32 256, i32* null, i32 0, i32 0)
%23 = icmp eq i32 %22, 0
br i1 %23, label %29, label %24
24: ; preds = %16
%25 = load i32, i32* @stderr
%26 = call i8* @gai_strerror(i32 %22)
%27 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %26)
%28 = call i32 @exit(i32 -1)
unreachable
29: ; preds = %16
%30 = load %struct.addrinfo*, %struct.addrinfo** %4
%31 = call i32 @freeaddrinfo(%struct.addrinfo* %30)
ret void
}