#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_in6 {
  int /*<<< orphan*/ sin6_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_addr;
};
struct addrinfo {
  int ai_family;
  scalar_t__ ai_addr;
  struct addrinfo *ai_next;
};
typedef int /*<<< orphan*/ ipname;
typedef int /*<<< orphan*/ hints;

/* Variables and functions */
scalar_t__ AF_INET;
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int getaddrinfo(char const *, int /*<<< orphan*/ *, struct addrinfo *,
                struct addrinfo **);
int /*<<< orphan*/ *inet_ntop(scalar_t__, void *, char *, int);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
char *safe_strdup(char *);
char *get_ip_address(int family, const char *host) {
  struct addrinfo *res, *rp;
  struct addrinfo hints;
  int err;
  static char ipname[64];
  void *addr;
  memset(&hints, 0, sizeof(hints));
  hints.ai_family = family;
  err = getaddrinfo(host, NULL, &hints, &res);
  if (err != 0)
    return NULL;
  for (rp = res; rp != NULL; rp = rp->ai_next) {
    if (rp->ai_family == AF_INET)
      addr = (&((struct sockaddr_in *)(rp->ai_addr))->sin_addr);
    else
      addr = (&((struct sockaddr_in6 *)(rp->ai_addr))->sin6_addr);
    if (inet_ntop(rp->ai_family, addr, ipname, sizeof(ipname)) != NULL) {
      freeaddrinfo(res);
      return safe_strdup(ipname);
    }
  }
  freeaddrinfo(res);
  return NULL;
}


x86 assembly (clang, -O0):
<get_ip_address>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1040 <memset@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x40(%rbp)
mov    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
lea    -0x40(%rbp),%rdx
lea    -0x20(%rbp),%rcx
call   1060 <getaddrinfo@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
je     11a4 <get_ip_address+0x54>
movq   $0x0,-0x8(%rbp)
jmp    1256 <get_ip_address+0x106>
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1245 <get_ip_address+0xf5>
mov    -0x28(%rbp),%rax
movslq (%rax),%rax
mov    0x2e23(%rip),%rcx
cmp    (%rcx),%rax
jne    11df <get_ip_address+0x8f>
mov    -0x28(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x50(%rbp)
jmp    11eb <get_ip_address+0x9b>
mov    -0x28(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x50(%rbp)
mov    -0x28(%rbp),%rax
movslq (%rax),%rdi
mov    -0x50(%rbp),%rsi
lea    0x2e63(%rip),%rdx
mov    $0x40,%ecx
call   1030 <inet_ntop@plt>
cmp    $0x0,%rax
je     122f <get_ip_address+0xdf>
mov    -0x20(%rbp),%rdi
call   1070 <freeaddrinfo@plt>
lea    0x2e3f(%rip),%rdi
call   1050 <safe_strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    1256 <get_ip_address+0x106>
jmp    1234 <get_ip_address+0xe4>
mov    -0x28(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x28(%rbp)
jmp    11ac <get_ip_address+0x5c>
mov    -0x20(%rbp),%rdi
call   1070 <freeaddrinfo@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_ip_address>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
lea    0x10(%rsp),%r15
xor    %r14d,%r14d
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1040 <memset@plt>
mov    %ebp,0x10(%rsp)
lea    0x8(%rsp),%rcx
mov    %rbx,%rdi
xor    %esi,%esi
mov    %r15,%rdx
call   1060 <getaddrinfo@plt>
test   %eax,%eax
jne    11ff <get_ip_address+0xaf>
mov    0x8(%rsp),%rbp
test   %rbp,%rbp
je     11da <get_ip_address+0x8a>
lea    0x2ebf(%rip),%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    0x8(%rbp),%rsi
movslq 0x0(%rbp),%rdi
mov    %rbx,%rdx
mov    $0x40,%ecx
call   1030 <inet_ntop@plt>
test   %rax,%rax
jne    11e6 <get_ip_address+0x96>
mov    0x10(%rbp),%rbp
test   %rbp,%rbp
jne    11b0 <get_ip_address+0x60>
mov    0x8(%rsp),%rdi
jmp    11dc <get_ip_address+0x8c>
xor    %edi,%edi
call   1070 <freeaddrinfo@plt>
xor    %r14d,%r14d
jmp    11ff <get_ip_address+0xaf>
mov    0x8(%rsp),%rdi
call   1070 <freeaddrinfo@plt>
lea    0x2e69(%rip),%rdi
call   1050 <safe_strdup@plt>
mov    %rax,%r14
mov    %r14,%rax
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.addrinfo = type { i32, i64, %struct.addrinfo* }
%struct.sockaddr_in = type { i32 }
%struct.sockaddr_in6 = type { i32 }
@get_ip_address.ipname = internal global [64 x i8] zeroinitializer
@AF_INET = global i64 0
define i8* @get_ip_address(i32 %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca %struct.addrinfo*
%7 = alloca %struct.addrinfo*
%8 = alloca %struct.addrinfo
%9 = alloca i32
%10 = alloca i8*
store i32 %0, i32* %4
store i8* %1, i8** %5
%11 = call i32 @memset(%struct.addrinfo* %8, i32 0, i32 24)
%12 = load i32, i32* %4
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i32 0, i32 0
store i32 %12, i32* %13
%14 = load i8*, i8** %5
%15 = call i32 @getaddrinfo(i8* %14, i32* null, %struct.addrinfo* %8, %struct.addrinfo** %6)
store i32 %15, i32* %9
%16 = load i32, i32* %9
%17 = icmp ne i32 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %2
store i8* null, i8** %3
br label %65
19: ; preds = %2
%20 = load %struct.addrinfo*, %struct.addrinfo** %6
store %struct.addrinfo* %20, %struct.addrinfo** %7
br label %21
21: ; preds = %58, %19
%22 = load %struct.addrinfo*, %struct.addrinfo** %7
%23 = icmp ne %struct.addrinfo* %22, null
br i1 %23, label %24, label %62
24: ; preds = %21
%25 = load %struct.addrinfo*, %struct.addrinfo** %7
%26 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %25, i32 0, i32 0
%27 = load i32, i32* %26
%28 = sext i32 %27 to i64
%29 = load i64, i64* @AF_INET
%30 = icmp eq i64 %28, %29
br i1 %30, label %31, label %38
31: ; preds = %24
%32 = load %struct.addrinfo*, %struct.addrinfo** %7
%33 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %32, i32 0, i32 1
%34 = load i64, i64* %33
%35 = inttoptr i64 %34 to %struct.sockaddr_in*
%36 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %35, i32 0, i32 0
%37 = bitcast i32* %36 to i8*
store i8* %37, i8** %10
br label %45
38: ; preds = %24
%39 = load %struct.addrinfo*, %struct.addrinfo** %7
%40 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %39, i32 0, i32 1
%41 = load i64, i64* %40
%42 = inttoptr i64 %41 to %struct.sockaddr_in6*
%43 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %42, i32 0, i32 0
%44 = bitcast i32* %43 to i8*
store i8* %44, i8** %10
br label %45
45: ; preds = %38, %31
%46 = load %struct.addrinfo*, %struct.addrinfo** %7
%47 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %46, i32 0, i32 0
%48 = load i32, i32* %47
%49 = sext i32 %48 to i64
%50 = load i8*, i8** %10
%51 = call i32* @inet_ntop(i64 %49, i8* %50, i8* getelementptr inbounds ([64 x i8], [64 x i8]* @get_ip_address.ipname, i64 0, i64 0), i32 64)
%52 = icmp ne i32* %51, null
br i1 %52, label %53, label %57
53: ; preds = %45
%54 = load %struct.addrinfo*, %struct.addrinfo** %6
%55 = call i32 @freeaddrinfo(%struct.addrinfo* %54)
%56 = call i8* @safe_strdup(i8* getelementptr inbounds ([64 x i8], [64 x i8]* @get_ip_address.ipname, i64 0, i64 0))
store i8* %56, i8** %3
br label %65
57: ; preds = %45
br label %58
58: ; preds = %57
%59 = load %struct.addrinfo*, %struct.addrinfo** %7
%60 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %59, i32 0, i32 2
%61 = load %struct.addrinfo*, %struct.addrinfo** %60
store %struct.addrinfo* %61, %struct.addrinfo** %7
br label %21
62: ; preds = %21
%63 = load %struct.addrinfo*, %struct.addrinfo** %6
%64 = call i32 @freeaddrinfo(%struct.addrinfo* %63)
store i8* null, i8** %3
br label %65
65: ; preds = %62, %53, %18
%66 = load i8*, i8** %3
ret i8* %66
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i64, %struct.addrinfo* }
@get_ip_address.ipname = internal global [64 x i8] zeroinitializer
@AF_INET = global i64 0
define i8* @get_ip_address(i32 %0, i8* %1) {
%3 = alloca %struct.addrinfo*
%4 = alloca %struct.addrinfo
%5 = bitcast %struct.addrinfo** %3 to i8*
%6 = bitcast %struct.addrinfo* %4 to i8*
%7 = call i32 @memset(%struct.addrinfo* %4, i32 0, i32 24)
%8 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 0
store i32 %0, i32* %8
%9 = call i32 @getaddrinfo(i8* %1, i32* null, %struct.addrinfo* %4, %struct.addrinfo** %3)
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %37
11: ; preds = %2
%12 = load %struct.addrinfo*, %struct.addrinfo** %3
%13 = icmp eq %struct.addrinfo* %12, null
br i1 %13, label %34, label %18
14: ; preds = %18
%15 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %19, i64 0, i32 2
%16 = load %struct.addrinfo*, %struct.addrinfo** %15
%17 = icmp eq %struct.addrinfo* %16, null
br i1 %17, label %32, label %18
18: ; preds = %11, %14
%19 = phi %struct.addrinfo* [ %16, %14 ], [ %12, %11 ]
%20 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %19, i64 0, i32 0
%21 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %19, i64 0, i32 1
%22 = load i64, i64* %21
%23 = inttoptr i64 %22 to i8*
%24 = load i32, i32* %20
%25 = sext i32 %24 to i64
%26 = call i32* @inet_ntop(i64 %25, i8* %23, i8* getelementptr inbounds ([64 x i8], [64 x i8]* @get_ip_address.ipname, i64 0, i64 0), i32 64)
%27 = icmp eq i32* %26, null
br i1 %27, label %14, label %28
28: ; preds = %18
%29 = load %struct.addrinfo*, %struct.addrinfo** %3
%30 = call i32 @freeaddrinfo(%struct.addrinfo* %29)
%31 = call i8* @safe_strdup(i8* getelementptr inbounds ([64 x i8], [64 x i8]* @get_ip_address.ipname, i64 0, i64 0))
br label %37
32: ; preds = %14
%33 = load %struct.addrinfo*, %struct.addrinfo** %3
br label %34
34: ; preds = %32, %11
%35 = phi %struct.addrinfo* [ %33, %32 ], [ null, %11 ]
%36 = call i32 @freeaddrinfo(%struct.addrinfo* %35)
br label %37
37: ; preds = %2, %34, %28
%38 = phi i8* [ %31, %28 ], [ null, %34 ], [ null, %2 ]
ret i8* %38
}