#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 {
  TYPE_1__ *ifa_addr;
  struct ifaddrs *ifa_next;
};
struct TYPE_2__ {
  int sa_family;
};

/* Variables and functions */
int AF_INET;
int /*<<< orphan*/ EXIT_FAILURE;
int NI_MAXHOST;
int /*<<< orphan*/ NI_NUMERICHOST;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
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*/);
char *malloc(int);
int /*<<< orphan*/ memset(char *, char, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int /*<<< orphan*/);
char *strrchr(char *, char);
char *localNetworkPrefix() {
  struct ifaddrs *ifaddr;
  if (getifaddrs(&ifaddr) == -1) {
    perror("getifaddrs failed - what is my IP address?");
    exit(EXIT_FAILURE);
  }
  char *prefix = malloc(NI_MAXHOST * sizeof(char *));
  memset(prefix, '\0', NI_MAXHOST);
  struct ifaddrs *ifa;
  int n;
  for (ifa = ifaddr, n = 0; ifa != NULL; ifa = ifa->ifa_next, n++) {
    if (ifa->ifa_addr == NULL) {
      continue;
    }
    int family = ifa->ifa_addr->sa_family;
    if (family == AF_INET) {
      char host[NI_MAXHOST];
      int s = getnameinfo(ifa->ifa_addr,
                          (family == AF_INET) ? sizeof(struct sockaddr_in)
                                              : sizeof(struct sockaddr_in6),
                          host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
      if (s != 0) {
        printf("getnameinfo() failed: %s\n", gai_strerror(s));
        exit(EXIT_FAILURE);
      }
      if (strcmp(host, "127.0.0.1") != 0) {
        printf("my address: %s\n", host);
        char *endP = strrchr(host, '.');
        *endP = '\0';
        printf("network prefix: %s\n", host);
        strncpy(prefix, host, strlen(host));
      }
    }
  }
  freeifaddrs(ifaddr);
  return prefix;
}


x86 assembly (clang, -O0):
<localNetworkPrefix>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
lea    -0x8(%rbp),%rdi
call   10c0 <getifaddrs@plt>
cmp    $0xffffffff,%eax
jne    1204 <localNetworkPrefix+0x34>
lea    0xe0f(%rip),%rdi
call   10e0 <perror@plt>
mov    0x2dd3(%rip),%rax
mov    (%rax),%edi
call   10f0 <exit@plt>
mov    0x2dbd(%rip),%rax
movslq (%rax),%rax
shl    $0x3,%rax
mov    %eax,%edi
call   10b0 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2da0(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <memset@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     13bb <localNetworkPrefix+0x1eb>
mov    -0x18(%rbp),%rax
cmpq   $0x0,(%rax)
jne    125e <localNetworkPrefix+0x8e>
jmp    13a1 <localNetworkPrefix+0x1d1>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    0x2d74(%rip),%rcx
cmp    (%rcx),%eax
jne    139c <localNetworkPrefix+0x1cc>
mov    0x2d45(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdx
sub    %rcx,%rdx
mov    %rdx,-0x48(%rbp)
mov    %rdx,%rsp
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
mov    -0x20(%rbp),%ecx
mov    0x2d2f(%rip),%rax
mov    (%rax),%esi
mov    $0x4,%eax
cmp    %esi,%ecx
cmove  %rax,%rax
mov    %eax,%esi
mov    0x2cf9(%rip),%rax
mov    (%rax),%ecx
mov    0x2d00(%rip),%rax
mov    (%rax),%eax
xor    %r8d,%r8d
xor    %r9d,%r9d
sub    $0x10,%rsp
mov    %eax,(%rsp)
call   1090 <getnameinfo@plt>
add    $0x10,%rsp
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
je     1322 <localNetworkPrefix+0x152>
mov    -0x34(%rbp),%edi
call   1060 <gai_strerror@plt>
mov    %rax,%rsi
lea    0xd1c(%rip),%rdi
call   1050 <printf@plt>
mov    0x2cb5(%rip),%rax
mov    (%rax),%edi
call   10f0 <exit@plt>
mov    -0x48(%rbp),%rdi
lea    0xd18(%rip),%rsi
call   10a0 <strcmp@plt>
cmp    $0x0,%rax
je     1395 <localNetworkPrefix+0x1c5>
mov    -0x48(%rbp),%rsi
lea    0xd08(%rip),%rdi
call   1050 <printf@plt>
mov    -0x48(%rbp),%rdi
mov    $0x2e,%esi
call   1070 <strrchr@plt>
mov    -0x48(%rbp),%rsi
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
movb   $0x0,(%rax)
lea    0xcef(%rip),%rdi
call   1050 <printf@plt>
mov    -0x48(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    %rax,-0x50(%rbp)
call   1040 <strlen@plt>
mov    -0x50(%rbp),%rdi
mov    -0x48(%rbp),%rsi
mov    %eax,%edx
call   1030 <strncpy@plt>
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
jmp    13a1 <localNetworkPrefix+0x1d1>
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1240 <localNetworkPrefix+0x70>
mov    -0x8(%rbp),%rdi
call   10d0 <freeifaddrs@plt>
mov    -0x10(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<localNetworkPrefix>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
lea    -0x38(%rbp),%rdi
call   10c0 <getifaddrs@plt>
cmp    $0xffffffff,%eax
je     1348 <localNetworkPrefix+0x178>
mov    0x2dce(%rip),%r15
mov    (%r15),%ebx
lea    0x0(,%rbx,8),%edi
call   10b0 <malloc@plt>
xor    %r14d,%r14d
mov    %rax,-0x30(%rbp)
mov    %rax,%rdi
xor    %esi,%esi
mov    %ebx,%edx
call   1080 <memset@plt>
mov    -0x38(%rbp),%r12
test   %r12,%r12
je     1309 <localNetworkPrefix+0x139>
mov    0x2db8(%rip),%r14
jmp    1251 <localNetworkPrefix+0x81>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r13,%rsp
mov    0x8(%r12),%r12
test   %r12,%r12
je     1305 <localNetworkPrefix+0x135>
mov    (%r12),%rax
test   %rax,%rax
je     1243 <localNetworkPrefix+0x73>
mov    (%rax),%eax
cmp    (%r14),%eax
jne    1243 <localNetworkPrefix+0x73>
mov    %rsp,%r13
mov    (%r15),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    (%r12),%rdi
mov    (%r15),%ecx
mov    0x2d52(%rip),%rax
mov    (%rax),%eax
sub    $0x8,%rsp
mov    $0x4,%esi
mov    %rbx,%rdx
xor    %r8d,%r8d
xor    %r9d,%r9d
push   %rax
call   1090 <getnameinfo@plt>
add    $0x10,%rsp
test   %eax,%eax
jne    1324 <localNetworkPrefix+0x154>
mov    %rbx,%rdi
lea    0xd93(%rip),%rsi
call   10a0 <strcmp@plt>
test   %rax,%rax
je     1240 <localNetworkPrefix+0x70>
lea    0xd8c(%rip),%rdi
mov    %rbx,%rsi
call   1050 <printf@plt>
mov    %rbx,%rdi
mov    $0x2e,%esi
call   1070 <strrchr@plt>
movb   $0x0,(%rax)
lea    0xd7d(%rip),%rdi
mov    %rbx,%rsi
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    -0x30(%rbp),%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <strncpy@plt>
jmp    1240 <localNetworkPrefix+0x70>
mov    -0x38(%rbp),%r14
mov    %r14,%rdi
call   10d0 <freeifaddrs@plt>
mov    -0x30(%rbp),%rax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %eax,%edi
call   1060 <gai_strerror@plt>
lea    0xcf9(%rip),%rdi
mov    %rax,%rsi
call   1050 <printf@plt>
mov    0x2c8f(%rip),%rax
mov    (%rax),%edi
call   10f0 <exit@plt>
lea    0xcb1(%rip),%rdi
call   10e0 <perror@plt>
jmp    133a <localNetworkPrefix+0x16a>

LLVM IR (-O0):
%struct.ifaddrs = type { %struct.TYPE_2__*, %struct.ifaddrs* }
%struct.TYPE_2__ = type { i32 }
@.str = [43 x i8] c"getifaddrs failed - what is my IP address?\00"
@EXIT_FAILURE = global i32 0
@NI_MAXHOST = global i32 0
@AF_INET = global i32 0
@NI_NUMERICHOST = global i32 0
@.str.1 = [26 x i8] c"getnameinfo() failed: %s\0A\00"
@.str.2 = [10 x i8] c"127.0.0.1\00"
@.str.3 = [16 x i8] c"my address: %s\0A\00"
@.str.4 = [20 x i8] c"network prefix: %s\0A\00"
define i8* @localNetworkPrefix() {
%1 = alloca %struct.ifaddrs*
%2 = alloca i8*
%3 = alloca %struct.ifaddrs*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i8*
%10 = call i32 @getifaddrs(%struct.ifaddrs** %1)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %16
12: ; preds = %0
%13 = call i32 @perror(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0))
%14 = load i32, i32* @EXIT_FAILURE
%15 = call i32 @exit(i32 %14)
unreachable
16: ; preds = %0
%17 = load i32, i32* @NI_MAXHOST
%18 = sext i32 %17 to i64
%19 = mul i64 %18, 8
%20 = trunc i64 %19 to i32
%21 = call i8* @malloc(i32 %20)
store i8* %21, i8** %2
%22 = load i8*, i8** %2
%23 = load i32, i32* @NI_MAXHOST
%24 = call i32 @memset(i8* %22, i8 signext 0, i32 %23)
%25 = load %struct.ifaddrs*, %struct.ifaddrs** %1
store %struct.ifaddrs* %25, %struct.ifaddrs** %3
store i32 0, i32* %4
br label %26
26: ; preds = %83, %16
%27 = load %struct.ifaddrs*, %struct.ifaddrs** %3
%28 = icmp ne %struct.ifaddrs* %27, null
br i1 %28, label %29, label %89
29: ; preds = %26
%30 = load %struct.ifaddrs*, %struct.ifaddrs** %3
%31 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %30, i32 0, i32 0
%32 = load %struct.TYPE_2__*, %struct.TYPE_2__** %31
%33 = icmp eq %struct.TYPE_2__* %32, null
br i1 %33, label %34, label %35
34: ; preds = %29
br label %83
35: ; preds = %29
%36 = load %struct.ifaddrs*, %struct.ifaddrs** %3
%37 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %36, i32 0, i32 0
%38 = load %struct.TYPE_2__*, %struct.TYPE_2__** %37
%39 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %38, i32 0, i32 0
%40 = load i32, i32* %39
store i32 %40, i32* %5
%41 = load i32, i32* %5
%42 = load i32, i32* @AF_INET
%43 = icmp eq i32 %41, %42
br i1 %43, label %44, label %82
44: ; preds = %35
%45 = load i32, i32* @NI_MAXHOST
%46 = zext i32 %45 to i64
%47 = call i8* @llvm.stacksave()
store i8* %47, i8** %6
%48 = alloca i8, i64 %46
store i64 %46, i64* %7
%49 = load %struct.ifaddrs*, %struct.ifaddrs** %3
%50 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %49, i32 0, i32 0
%51 = load %struct.TYPE_2__*, %struct.TYPE_2__** %50
%52 = load i32, i32* %5
%53 = load i32, i32* @AF_INET
%54 = icmp eq i32 %52, %53
%55 = zext i1 %54 to i64
%56 = select i1 %54, i64 4, i64 4
%57 = trunc i64 %56 to i32
%58 = load i32, i32* @NI_MAXHOST
%59 = load i32, i32* @NI_NUMERICHOST
%60 = call i32 @getnameinfo(%struct.TYPE_2__* %51, i32 %57, i8* %48, i32 %58, i32* null, i32 0, i32 %59)
store i32 %60, i32* %8
%61 = load i32, i32* %8
%62 = icmp ne i32 %61, 0
br i1 %62, label %63, label %69
63: ; preds = %44
%64 = load i32, i32* %8
%65 = call i8* @gai_strerror(i32 %64)
%66 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %65)
%67 = load i32, i32* @EXIT_FAILURE
%68 = call i32 @exit(i32 %67)
unreachable
69: ; preds = %44
%70 = call i64 @strcmp(i8* %48, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
%71 = icmp ne i64 %70, 0
br i1 %71, label %72, label %80
72: ; preds = %69
%73 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i8* %48)
%74 = call i8* @strrchr(i8* %48, i8 signext 46)
store i8* %74, i8** %9
%75 = load i8*, i8** %9
store i8 0, i8* %75
%76 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i8* %48)
%77 = load i8*, i8** %2
%78 = call i32 @strlen(i8* %48)
%79 = call i32 @strncpy(i8* %77, i8* %48, i32 %78)
br label %80
80: ; preds = %72, %69
%81 = load i8*, i8** %6
br label %82
82: ; preds = %80, %35
br label %83
83: ; preds = %82, %34
%84 = load %struct.ifaddrs*, %struct.ifaddrs** %3
%85 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %84, i32 0, i32 1
%86 = load %struct.ifaddrs*, %struct.ifaddrs** %85
store %struct.ifaddrs* %86, %struct.ifaddrs** %3
%87 = load i32, i32* %4
%88 = add nsw i32 %87, 1
store i32 %88, i32* %4
br label %26
89: ; preds = %26
%90 = load %struct.ifaddrs*, %struct.ifaddrs** %1
%91 = call i32 @freeifaddrs(%struct.ifaddrs* %90)
%92 = load i8*, i8** %2
ret i8* %92
}

LLVM IR (-O2):
%struct.ifaddrs = type { %struct.TYPE_2__*, %struct.ifaddrs* }
%struct.TYPE_2__ = type { i32 }
@.str = [43 x i8] c"getifaddrs failed - what is my IP address?\00"
@EXIT_FAILURE = global i32 0
@NI_MAXHOST = global i32 0
@AF_INET = global i32 0
@NI_NUMERICHOST = global i32 0
@.str.1 = [26 x i8] c"getnameinfo() failed: %s\0A\00"
@.str.2 = [10 x i8] c"127.0.0.1\00"
@.str.3 = [16 x i8] c"my address: %s\0A\00"
@.str.4 = [20 x i8] c"network prefix: %s\0A\00"
define i8* @localNetworkPrefix() {
%1 = alloca %struct.ifaddrs*
%2 = bitcast %struct.ifaddrs** %1 to i8*
%3 = call i32 @getifaddrs(%struct.ifaddrs** %1)
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %9
5: ; preds = %0
%6 = call i32 @perror(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0))
%7 = load i32, i32* @EXIT_FAILURE
%8 = call i32 @exit(i32 %7)
unreachable
9: ; preds = %0
%10 = load i32, i32* @NI_MAXHOST
%11 = shl i32 %10, 3
%12 = call i8* @malloc(i32 %11)
%13 = call i32 @memset(i8* %12, i8 signext 0, i32 %10)
%14 = load %struct.ifaddrs*, %struct.ifaddrs** %1
%15 = icmp eq %struct.ifaddrs* %14, null
br i1 %15, label %57, label %16
16: ; preds = %9, %51
%17 = phi %struct.ifaddrs* [ %53, %51 ], [ %14, %9 ]
%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 %51, label %21
21: ; preds = %16
%22 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %19, i64 0, i32 0
%23 = load i32, i32* %22
%24 = load i32, i32* @AF_INET
%25 = icmp eq i32 %23, %24
br i1 %25, label %26, label %51
26: ; preds = %21
%27 = load i32, i32* @NI_MAXHOST
%28 = zext i32 %27 to i64
%29 = call i8* @llvm.stacksave()
%30 = alloca i8, i64 %28
%31 = load %struct.TYPE_2__*, %struct.TYPE_2__** %18
%32 = load i32, i32* @NI_MAXHOST
%33 = load i32, i32* @NI_NUMERICHOST
%34 = call i32 @getnameinfo(%struct.TYPE_2__* %31, i32 4, i8* %30, i32 %32, i32* null, i32 0, i32 %33)
%35 = icmp eq i32 %34, 0
br i1 %35, label %41, label %36
36: ; preds = %26
%37 = call i8* @gai_strerror(i32 %34)
%38 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %37)
%39 = load i32, i32* @EXIT_FAILURE
%40 = call i32 @exit(i32 %39)
unreachable
41: ; preds = %26
%42 = call i64 @strcmp(i8* %30, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
%43 = icmp eq i64 %42, 0
br i1 %43, label %50, label %44
44: ; preds = %41
%45 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i8* %30)
%46 = call i8* @strrchr(i8* %30, i8 signext 46)
store i8 0, i8* %46
%47 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i8* %30)
%48 = call i32 @strlen(i8* %30)
%49 = call i32 @strncpy(i8* %12, i8* %30, i32 %48)
br label %50
50: ; preds = %44, %41
br label %51
51: ; preds = %21, %50, %16
%52 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %17, i64 0, i32 1
%53 = load %struct.ifaddrs*, %struct.ifaddrs** %52
%54 = icmp eq %struct.ifaddrs* %53, null
br i1 %54, label %55, label %16
55: ; preds = %51
%56 = load %struct.ifaddrs*, %struct.ifaddrs** %1
br label %57
57: ; preds = %55, %9
%58 = phi %struct.ifaddrs* [ %56, %55 ], [ null, %9 ]
%59 = call i32 @freeifaddrs(%struct.ifaddrs* %58)
ret i8* %12
}