#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__ {
  scalar_t__ sa_family;
};

/* Variables and functions */
scalar_t__ AF_INET6;
int NI_MAXHOST;
int /*<<< orphan*/ NI_NUMERICHOST;
char const *bprintf(char *, char *);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ freeifaddrs(struct ifaddrs *);
char *gai_strerror(int);
int getifaddrs(struct ifaddrs **);
int getnameinfo(TYPE_1__ *, int, char *, int, int /*<<< orphan*/ *,
                int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
scalar_t__ strcmp(int /*<<< orphan*/, char const *);
char *strerror(int /*<<< orphan*/);
const char *ipv6(const char *iface) {
  struct ifaddrs *ifaddr, *ifa;
  int s;
  char host[NI_MAXHOST];
  if (getifaddrs(&ifaddr) == -1) {
    fprintf(stderr, "getifaddrs: %s\n", strerror(errno));
    return NULL;
  }
  for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
    if (ifa->ifa_addr == NULL) {
      continue;
    }
    s = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in6), host,
                    NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
    if ((strcmp(ifa->ifa_name, iface) == 0) &&
        (ifa->ifa_addr->sa_family == AF_INET6)) {
      if (s != 0) {
        fprintf(stderr, "getnameinfo: %s\n", gai_strerror(s));
        return NULL;
      }
      return bprintf("%s", host);
    }
  }
  freeifaddrs(ifaddr);
  return NULL;
}


x86 assembly (clang, -O0):
<ipv6>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e2d(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x38(%rbp)
lea    -0x18(%rbp),%rdi
call   1080 <getifaddrs@plt>
cmp    $0xffffffff,%eax
jne    120a <ipv6+0x8a>
mov    0x2e07(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    0x2deb(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x4c(%rbp),%edi
mov    %rax,%rdx
lea    0xe0f(%rip),%rsi
call   1070 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    1331 <ipv6+0x1b1>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1319 <ipv6+0x199>
mov    -0x20(%rbp),%rax
cmpq   $0x0,(%rax)
jne    1230 <ipv6+0xb0>
jmp    1308 <ipv6+0x188>
mov    -0x48(%rbp),%rdx
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
mov    0x2d7e(%rip),%rax
mov    (%rax),%ecx
mov    0x2d85(%rip),%rax
mov    (%rax),%eax
mov    $0x4,%esi
xor    %r8d,%r8d
xor    %r9d,%r9d
sub    $0x10,%rsp
mov    %eax,(%rsp)
call   1050 <getnameinfo@plt>
add    $0x10,%rsp
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%edi
mov    -0x10(%rbp),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    1303 <ipv6+0x183>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%rax
mov    0x2d52(%rip),%rcx
cmp    (%rcx),%rax
jne    1303 <ipv6+0x183>
cmpl   $0x0,-0x24(%rbp)
je     12e3 <ipv6+0x163>
mov    0x2d28(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x50(%rbp)
mov    -0x24(%rbp),%edi
call   1030 <gai_strerror@plt>
mov    -0x50(%rbp),%edi
mov    %rax,%rdx
lea    0xd46(%rip),%rsi
call   1070 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    1331 <ipv6+0x1b1>
mov    -0x48(%rbp),%rsi
lea    0xd33(%rip),%rdi
call   1040 <bprintf@plt>
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    1331 <ipv6+0x1b1>
jmp    1308 <ipv6+0x188>
mov    -0x20(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x20(%rbp)
jmp    1212 <ipv6+0x92>
mov    -0x18(%rbp),%rdi
call   1090 <freeifaddrs@plt>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x3c(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
mov    -0x8(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ipv6>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r14
mov    %rsp,%rbx
mov    0x2e22(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%r15
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r15
mov    %r15,%rsp
lea    -0x38(%rbp),%rdi
call   1080 <getifaddrs@plt>
cmp    $0xffffffff,%eax
je     1276 <ipv6+0xf6>
mov    -0x38(%rbp),%r12
test   %r12,%r12
je     12ac <ipv6+0x12c>
mov    %rbx,-0x30(%rbp)
mov    0x2df5(%rip),%r13
jmp    11ee <ipv6+0x6e>
nopl   (%rax)
mov    0x10(%r12),%r12
test   %r12,%r12
je     12a2 <ipv6+0x122>
mov    (%r12),%rdi
test   %rdi,%rdi
je     11e0 <ipv6+0x60>
mov    0x2dc2(%rip),%rax
mov    (%rax),%ecx
mov    0x0(%r13),%eax
sub    $0x8,%rsp
mov    $0x4,%esi
mov    %r15,%rdx
xor    %r8d,%r8d
xor    %r9d,%r9d
push   %rax
call   1050 <getnameinfo@plt>
add    $0x10,%rsp
mov    %eax,%ebx
mov    0x8(%r12),%edi
mov    %r14,%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
jne    11e0 <ipv6+0x60>
mov    (%r12),%rax
mov    (%rax),%rax
mov    0x2da6(%rip),%rcx
cmp    (%rcx),%rax
jne    11e0 <ipv6+0x60>
test   %ebx,%ebx
je     12c7 <ipv6+0x147>
mov    0x2d86(%rip),%rax
mov    (%rax),%r14d
mov    %ebx,%edi
call   1030 <gai_strerror@plt>
lea    0xdad(%rip),%rsi
mov    %r14d,%edi
mov    %rax,%rdx
call   1070 <fprintf@plt>
xor    %eax,%eax
mov    -0x30(%rbp),%rbx
jmp    12b5 <ipv6+0x135>
mov    0x2d5b(%rip),%rax
mov    (%rax),%r14d
mov    0x2d41(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xd6b(%rip),%rsi
mov    %r14d,%edi
mov    %rax,%rdx
call   1070 <fprintf@plt>
jmp    12b3 <ipv6+0x133>
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rbx
jmp    12ae <ipv6+0x12e>
xor    %edi,%edi
call   1090 <freeifaddrs@plt>
xor    %eax,%eax
mov    %rbx,%rsp
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd53(%rip),%rdi
mov    %r15,%rsi
call   1040 <bprintf@plt>
mov    -0x30(%rbp),%rbx
jmp    12b5 <ipv6+0x135>

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

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