#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 sockaddr_in {
  int dummy;
};
struct ifaddrs {
  char *ifa_name;
  TYPE_1__ *ifa_addr;
  struct ifaddrs *ifa_next;
};
typedef int /*<<< orphan*/ host;
struct TYPE_2__ {
  int sa_family;
};

/* Variables and functions */
int AF_INET;
int AF_INET6;
int EAI_SYSTEM;
int /*<<< orphan*/ NI_NUMERICHOST;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ freeifaddrs(struct ifaddrs *);
char *gai_strerror(int);
scalar_t__ getifaddrs(struct ifaddrs **);
int getnameinfo(TYPE_1__ *, int, char *, int, int /*<<< orphan*/ *,
                int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *, char *, char *);
int /*<<< orphan*/ stderr;
int main() {
  struct ifaddrs *nets;
  if (getifaddrs(&nets) < 0) {
    perror("listnets: getifaddrs");
    return 1;
  }
  for (struct ifaddrs *p = nets; p; p = p->ifa_next) {
    if (!p->ifa_addr)
      continue;
    int family = p->ifa_addr->sa_family;
    if (family != AF_INET && family != AF_INET6)
      continue;
    char host[1025];
    int r = getnameinfo(p->ifa_addr,
                        family == AF_INET ? sizeof(struct sockaddr_in)
                                          : sizeof(struct sockaddr_in6),
                        host, sizeof(host), NULL, 0, NI_NUMERICHOST);
    if (r) {
      if (r == EAI_SYSTEM) {
        perror("listnets: getnameinfo");
      } else {
        fprintf(stderr, "listnets: getnameinfo: %s\n", gai_strerror(r));
      }
      continue;
    }
    printf("%s %s %s\n", p->ifa_name, family == AF_INET ? "ipv4" : "ipv6",
           host);
  }
  freeifaddrs(nets);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x10(%rbp),%rdi
call   1070 <getifaddrs@plt>
cmp    $0x0,%rax
jge    11ad <main+0x3d>
lea    0xe64(%rip),%rdi
call   1090 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1316 <main+0x1a6>
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1306 <main+0x196>
mov    -0x18(%rbp),%rax
cmpq   $0x0,0x8(%rax)
jne    11d4 <main+0x64>
jmp    12f5 <main+0x185>
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    0x2dfd(%rip),%rcx
cmp    (%rcx),%eax
je     120a <main+0x9a>
mov    -0x1c(%rbp),%eax
mov    0x2ddb(%rip),%rcx
cmp    (%rcx),%eax
je     120a <main+0x9a>
jmp    12f5 <main+0x185>
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x1c(%rbp),%ecx
mov    0x2dcc(%rip),%rax
mov    (%rax),%edx
mov    $0x4,%eax
cmp    %edx,%ecx
cmove  %rax,%rax
mov    %eax,%esi
lea    -0x420(%rbp),%rdx
mov    0x2d87(%rip),%rax
mov    (%rax),%eax
mov    $0x401,%ecx
xor    %r8d,%r8d
xor    %r9d,%r9d
mov    %eax,(%rsp)
call   1050 <getnameinfo@plt>
mov    %eax,-0x424(%rbp)
cmpl   $0x0,-0x424(%rbp)
je     12bb <main+0x14b>
mov    -0x424(%rbp),%eax
mov    0x2d72(%rip),%rcx
cmp    (%rcx),%eax
jne    1287 <main+0x117>
lea    0xd98(%rip),%rdi
call   1090 <perror@plt>
jmp    12b6 <main+0x146>
mov    0x2d3a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x428(%rbp)
mov    -0x424(%rbp),%edi
call   1040 <gai_strerror@plt>
mov    -0x428(%rbp),%edi
mov    %rax,%rdx
lea    0xd7a(%rip),%rsi
call   1060 <fprintf@plt>
jmp    12f5 <main+0x185>
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
mov    -0x1c(%rbp),%ecx
mov    0x2d1c(%rip),%rax
mov    (%rax),%edi
lea    0xd80(%rip),%rdx
lea    0xd74(%rip),%rax
cmp    %edi,%ecx
cmove  %rax,%rdx
lea    -0x420(%rbp),%rcx
lea    0xd56(%rip),%rdi
call   1030 <printf@plt>
mov    -0x18(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x18(%rbp)
jmp    11b5 <main+0x45>
mov    -0x10(%rbp),%rdi
call   1080 <freeifaddrs@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
lea    0x8(%rsp),%rdi
call   1070 <getifaddrs@plt>
test   %rax,%rax
js     1293 <main+0x123>
mov    0x8(%rsp),%r12
xor    %ebx,%ebx
test   %r12,%r12
je     1278 <main+0x108>
mov    0x2e3d(%rip),%r15
mov    0x2e26(%rip),%r14
mov    0x2e07(%rip),%r13
lea    0x10(%rsp),%rbx
jmp    11fa <main+0x8a>
mov    (%r12),%rsi
cmp    (%r15),%ebp
lea    0xe87(%rip),%rdx
lea    0xe7b(%rip),%rax
cmove  %rax,%rdx
lea    0xe66(%rip),%rdi
mov    %rbx,%rcx
call   1030 <printf@plt>
nopl   0x0(%rax,%rax,1)
mov    0x10(%r12),%r12
test   %r12,%r12
je     126f <main+0xff>
mov    0x8(%r12),%rdi
test   %rdi,%rdi
je     11f0 <main+0x80>
mov    (%rdi),%ebp
cmp    (%r15),%ebp
je     1210 <main+0xa0>
cmp    (%r14),%ebp
jne    11f0 <main+0x80>
mov    0x0(%r13),%eax
mov    %eax,(%rsp)
mov    $0x4,%esi
mov    %rbx,%rdx
mov    $0x401,%ecx
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1050 <getnameinfo@plt>
test   %eax,%eax
je     11c0 <main+0x50>
mov    0x2da6(%rip),%rcx
cmp    (%rcx),%eax
je     1261 <main+0xf1>
mov    0x2d83(%rip),%rcx
mov    (%rcx),%ebp
mov    %eax,%edi
call   1040 <gai_strerror@plt>
mov    %ebp,%edi
lea    0xdd4(%rip),%rsi
mov    %rax,%rdx
call   1060 <fprintf@plt>
jmp    11f0 <main+0x80>
lea    0xdad(%rip),%rdi
call   1090 <perror@plt>
jmp    11f0 <main+0x80>
mov    0x8(%rsp),%rdi
xor    %ebx,%ebx
jmp    127a <main+0x10a>
xor    %edi,%edi
call   1080 <freeifaddrs@plt>
mov    %ebx,%eax
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd66(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%ebx
jmp    127f <main+0x10f>

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

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