#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct sockaddr_in6 {
  int dummy;
};
struct ifaddrs {
  TYPE_1__ *ifa_addr;
  int /*<<< orphan*/ ifa_name;
  struct ifaddrs *ifa_next;
};
struct TYPE_2__ {
  unsigned short sa_family;
};

/* Variables and functions */
int NI_MAXHOST;
int /*<<< orphan*/ NI_NUMERICHOST;
char const *bprintf(char *, char *);
int /*<<< orphan*/ freeifaddrs(struct ifaddrs *);
int /*<<< orphan*/ gai_strerror(int);
scalar_t__ getifaddrs(struct ifaddrs **);
int getnameinfo(TYPE_1__ *, int, char *, int, int /*<<< orphan*/ *,
                int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcmp(int /*<<< orphan*/, char const *);
int /*<<< orphan*/ warn(char *, ...);
const char *ip(const char *interface, unsigned short sa_family) {
  struct ifaddrs *ifaddr, *ifa;
  int s;
  char host[NI_MAXHOST];
  if (getifaddrs(&ifaddr) < 0) {
    warn("getifaddrs:");
    return NULL;
  }
  for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
    if (!ifa->ifa_addr) {
      continue;
    }
    s = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in6), host,
                    NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
    if (!strcmp(ifa->ifa_name, interface) &&
        (ifa->ifa_addr->sa_family == sa_family)) {
      freeifaddrs(ifaddr);
      if (s != 0) {
        warn("getnameinfo: %s", gai_strerror(s));
        return NULL;
      }
      return bprintf("%s", host);
    }
  }
  freeifaddrs(ifaddr);
  return NULL;
}


x86 assembly (clang, -O0):
<ip>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %si,%ax
mov    %rdi,-0x10(%rbp)
mov    %ax,-0x12(%rbp)
mov    0x2e4e(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x50(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x40(%rbp)
lea    -0x20(%rbp),%rdi
call   1080 <getifaddrs@plt>
cmp    $0x0,%rax
jge    11e4 <ip+0x74>
lea    0xe37(%rip),%rdi
mov    $0x0,%al
call   1030 <warn@plt>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    1301 <ip+0x191>
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     12e9 <ip+0x179>
mov    -0x28(%rbp),%rax
cmpq   $0x0,(%rax)
jne    120a <ip+0x9a>
jmp    12d8 <ip+0x168>
mov    -0x50(%rbp),%rdx
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
mov    0x2dbc(%rip),%rax
mov    (%rax),%ecx
mov    0x2dbb(%rip),%rax
mov    (%rax),%eax
mov    $0x4,%esi
xor    %r8d,%r8d
xor    %r9d,%r9d
sub    $0x10,%rsp
mov    %eax,(%rsp)
call   1060 <getnameinfo@plt>
add    $0x10,%rsp
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%rax
mov    0x8(%rax),%edi
mov    -0x10(%rbp),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%eax
jne    12d3 <ip+0x163>
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
movzwl (%rax),%eax
movzwl -0x12(%rbp),%ecx
cmp    %ecx,%eax
jne    12d3 <ip+0x163>
mov    -0x20(%rbp),%rdi
call   1090 <freeifaddrs@plt>
cmpl   $0x0,-0x2c(%rbp)
je     12b3 <ip+0x143>
mov    -0x2c(%rbp),%edi
call   1040 <gai_strerror@plt>
mov    %eax,%esi
lea    0xd74(%rip),%rdi
mov    $0x0,%al
call   1030 <warn@plt>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    1301 <ip+0x191>
mov    -0x50(%rbp),%rsi
lea    0xd5b(%rip),%rdi
call   1050 <bprintf@plt>
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    1301 <ip+0x191>
jmp    12d8 <ip+0x168>
mov    -0x28(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x28(%rbp)
jmp    11ec <ip+0x7c>
mov    -0x20(%rbp),%rdi
call   1090 <freeifaddrs@plt>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x44(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,%rsp
mov    -0x8(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ip>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %esi,-0x3c(%rbp)
mov    %rdi,%r15
mov    %rsp,%r13
mov    0x2e47(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%r12
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r12
mov    %r12,%rsp
lea    -0x38(%rbp),%rdi
call   1080 <getifaddrs@plt>
test   %rax,%rax
js     1257 <ip+0xe7>
mov    -0x38(%rbp),%r14
test   %r14,%r14
je     1273 <ip+0x103>
mov    %r13,-0x30(%rbp)
mov    0x2e12(%rip),%r13
jmp    11dd <ip+0x6d>
mov    0x10(%r14),%r14
test   %r14,%r14
je     1269 <ip+0xf9>
mov    (%r14),%rdi
test   %rdi,%rdi
je     11d0 <ip+0x60>
mov    0x2dec(%rip),%rax
mov    (%rax),%ecx
mov    0x0(%r13),%eax
sub    $0x8,%rsp
mov    $0x4,%esi
mov    %r12,%rdx
xor    %r8d,%r8d
xor    %r9d,%r9d
push   %rax
call   1060 <getnameinfo@plt>
add    $0x10,%rsp
mov    %eax,%ebx
mov    0x8(%r14),%edi
mov    %r15,%rsi
call   1070 <strcmp@plt>
test   %eax,%eax
jne    11d0 <ip+0x60>
mov    (%r14),%rax
mov    -0x3c(%rbp),%ecx
cmp    %cx,(%rax)
jne    11d0 <ip+0x60>
mov    -0x38(%rbp),%rdi
call   1090 <freeifaddrs@plt>
test   %ebx,%ebx
je     1291 <ip+0x121>
mov    %ebx,%edi
call   1040 <gai_strerror@plt>
lea    0xdc6(%rip),%rdi
xor    %ebx,%ebx
mov    %eax,%esi
xor    %eax,%eax
call   1030 <warn@plt>
mov    -0x30(%rbp),%r13
jmp    127c <ip+0x10c>
lea    0xda2(%rip),%rdi
xor    %ebx,%ebx
xor    %eax,%eax
call   1030 <warn@plt>
jmp    127c <ip+0x10c>
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%r13
jmp    1275 <ip+0x105>
xor    %edi,%edi
call   1090 <freeifaddrs@plt>
xor    %ebx,%ebx
mov    %r13,%rsp
mov    %rbx,%rax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd81(%rip),%rdi
mov    %r12,%rsi
call   1050 <bprintf@plt>
mov    %rax,%rbx
mov    -0x30(%rbp),%r13
jmp    127c <ip+0x10c>

LLVM IR (-O0):
%struct.ifaddrs = type { %struct.TYPE_2__*, i32, %struct.ifaddrs* }
%struct.TYPE_2__ = type { i16 }
@NI_MAXHOST = global i32 0
@.str = [12 x i8] c"getifaddrs:\00"
@NI_NUMERICHOST = global i32 0
@.str.1 = [16 x i8] c"getnameinfo: %s\00"
@.str.2 = [3 x i8] c"%s\00"
define i8* @ip(i8* %0, i16 zeroext %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i16
%6 = alloca %struct.ifaddrs*
%7 = alloca %struct.ifaddrs*
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i32
store i8* %0, i8** %4
store i16 %1, i16* %5
%12 = load i32, i32* @NI_MAXHOST
%13 = zext i32 %12 to i64
%14 = call i8* @llvm.stacksave()
store i8* %14, i8** %9
%15 = alloca i8, i64 %13
store i64 %13, i64* %10
%16 = call i64 @getifaddrs(%struct.ifaddrs** %6)
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %2
%19 = call i32 (i8*, ...) @warn(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
store i8* null, i8** %3
store i32 1, i32* %11
br label %73
20: ; preds = %2
%21 = load %struct.ifaddrs*, %struct.ifaddrs** %6
store %struct.ifaddrs* %21, %struct.ifaddrs** %7
br label %22
22: ; preds = %66, %20
%23 = load %struct.ifaddrs*, %struct.ifaddrs** %7
%24 = icmp ne %struct.ifaddrs* %23, null
br i1 %24, label %25, label %70
25: ; preds = %22
%26 = load %struct.ifaddrs*, %struct.ifaddrs** %7
%27 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %26, i32 0, i32 0
%28 = load %struct.TYPE_2__*, %struct.TYPE_2__** %27
%29 = icmp ne %struct.TYPE_2__* %28, null
br i1 %29, label %31, label %30
30: ; preds = %25
br label %66
31: ; preds = %25
%32 = load %struct.ifaddrs*, %struct.ifaddrs** %7
%33 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %32, i32 0, i32 0
%34 = load %struct.TYPE_2__*, %struct.TYPE_2__** %33
%35 = load i32, i32* @NI_MAXHOST
%36 = load i32, i32* @NI_NUMERICHOST
%37 = call i32 @getnameinfo(%struct.TYPE_2__* %34, i32 4, i8* %15, i32 %35, i32* null, i32 0, i32 %36)
store i32 %37, i32* %8
%38 = load %struct.ifaddrs*, %struct.ifaddrs** %7
%39 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %38, i32 0, i32 1
%40 = load i32, i32* %39
%41 = load i8*, i8** %4
%42 = call i32 @strcmp(i32 %40, i8* %41)
%43 = icmp ne i32 %42, 0
br i1 %43, label %65, label %44
44: ; preds = %31
%45 = load %struct.ifaddrs*, %struct.ifaddrs** %7
%46 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %45, i32 0, i32 0
%47 = load %struct.TYPE_2__*, %struct.TYPE_2__** %46
%48 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %47, i32 0, i32 0
%49 = load i16, i16* %48
%50 = zext i16 %49 to i32
%51 = load i16, i16* %5
%52 = zext i16 %51 to i32
%53 = icmp eq i32 %50, %52
br i1 %53, label %54, label %65
54: ; preds = %44
%55 = load %struct.ifaddrs*, %struct.ifaddrs** %6
%56 = call i32 @freeifaddrs(%struct.ifaddrs* %55)
%57 = load i32, i32* %8
%58 = icmp ne i32 %57, 0
br i1 %58, label %59, label %63
59: ; preds = %54
%60 = load i32, i32* %8
%61 = call i32 @gai_strerror(i32 %60)
%62 = call i32 (i8*, ...) @warn(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %61)
store i8* null, i8** %3
store i32 1, i32* %11
br label %73
63: ; preds = %54
%64 = call i8* @bprintf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %15)
store i8* %64, i8** %3
store i32 1, i32* %11
br label %73
65: ; preds = %44, %31
br label %66
66: ; preds = %65, %30
%67 = load %struct.ifaddrs*, %struct.ifaddrs** %7
%68 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %67, i32 0, i32 2
%69 = load %struct.ifaddrs*, %struct.ifaddrs** %68
store %struct.ifaddrs* %69, %struct.ifaddrs** %7
br label %22
70: ; preds = %22
%71 = load %struct.ifaddrs*, %struct.ifaddrs** %6
%72 = call i32 @freeifaddrs(%struct.ifaddrs* %71)
store i8* null, i8** %3
store i32 1, i32* %11
br label %73
73: ; preds = %70, %63, %59, %18
%74 = load i8*, i8** %9
%75 = load i8*, i8** %3
ret i8* %75
}

LLVM IR (-O2):
%struct.ifaddrs = type { %struct.TYPE_2__*, i32, %struct.ifaddrs* }
%struct.TYPE_2__ = type { i16 }
@NI_MAXHOST = global i32 0
@.str = [12 x i8] c"getifaddrs:\00"
@NI_NUMERICHOST = global i32 0
@.str.1 = [16 x i8] c"getnameinfo: %s\00"
@.str.2 = [3 x i8] c"%s\00"
define i8* @ip(i8* %0, i16 zeroext %1) {
%3 = alloca %struct.ifaddrs*
%4 = bitcast %struct.ifaddrs** %3 to i8*
%5 = load i32, i32* @NI_MAXHOST
%6 = zext i32 %5 to i64
%7 = tail call i8* @llvm.stacksave()
%8 = alloca i8, i64 %6
%9 = call i64 @getifaddrs(%struct.ifaddrs** %3)
%10 = icmp slt i64 %9, 0
br i1 %10, label %14, label %11
11: ; preds = %2
%12 = load %struct.ifaddrs*, %struct.ifaddrs** %3
%13 = icmp eq %struct.ifaddrs* %12, null
br i1 %13, label %49, label %16
14: ; preds = %2
%15 = call i32 (i8*, ...) @warn(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %52
16: ; preds = %11, %43
%17 = phi %struct.ifaddrs* [ %45, %43 ], [ %12, %11 ]
%18 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %17, i64 0, i32 0
%19 = load %struct.TYPE_2__*, %struct.TYPE_2__** %18
%20 = icmp eq %struct.TYPE_2__* %19, null
br i1 %20, label %43, label %21
21: ; preds = %16
%22 = load i32, i32* @NI_MAXHOST
%23 = load i32, i32* @NI_NUMERICHOST
%24 = call i32 @getnameinfo(%struct.TYPE_2__* %19, i32 4, i8* %8, i32 %22, i32* null, i32 0, i32 %23)
%25 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %17, i64 0, i32 1
%26 = load i32, i32* %25
%27 = call i32 @strcmp(i32 %26, i8* %0)
%28 = icmp eq i32 %27, 0
br i1 %28, label %29, label %43
29: ; preds = %21
%30 = load %struct.TYPE_2__*, %struct.TYPE_2__** %18
%31 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %30, i64 0, i32 0
%32 = load i16, i16* %31
%33 = icmp eq i16 %32, %1
br i1 %33, label %34, label %43
34: ; preds = %29
%35 = load %struct.ifaddrs*, %struct.ifaddrs** %3
%36 = call i32 @freeifaddrs(%struct.ifaddrs* %35)
%37 = icmp eq i32 %24, 0
br i1 %37, label %41, label %38
38: ; preds = %34
%39 = call i32 @gai_strerror(i32 %24)
%40 = call i32 (i8*, ...) @warn(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %39)
br label %52
41: ; preds = %34
%42 = call i8* @bprintf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %8)
br label %52
43: ; preds = %21, %29, %16
%44 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %17, i64 0, i32 2
%45 = load %struct.ifaddrs*, %struct.ifaddrs** %44
%46 = icmp eq %struct.ifaddrs* %45, null
br i1 %46, label %47, label %16
47: ; preds = %43
%48 = load %struct.ifaddrs*, %struct.ifaddrs** %3
br label %49
49: ; preds = %47, %11
%50 = phi %struct.ifaddrs* [ %48, %47 ], [ null, %11 ]
%51 = call i32 @freeifaddrs(%struct.ifaddrs* %50)
br label %52
52: ; preds = %49, %41, %38, %14
%53 = phi i8* [ null, %14 ], [ null, %38 ], [ %42, %41 ], [ null, %49 ]
ret i8* %53
}