#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*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
char *localIP() {
  struct ifaddrs *ifaddr, *ifa;
  int family, s, n;
  char host[NI_MAXHOST];
  char *myIP = malloc(sizeof(char) * NI_MAXHOST);
  if (getifaddrs(&ifaddr) == -1) {
    perror("getifaddrs");
    exit(EXIT_FAILURE);
  }
  for (ifa = ifaddr, n = 0; ifa != NULL; ifa = ifa->ifa_next, n++) {
    if (ifa->ifa_addr == NULL) {
      continue;
    }
    family = ifa->ifa_addr->sa_family;
    if (family == AF_INET) {
      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("127.0.0.1", host) != 0) {
        strcpy(myIP, host);
      }
    }
  }
  freeifaddrs(ifaddr);
  return myIP;
}


x86 assembly (clang, -O0):
<localIP>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    0x2e19(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x40(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x30(%rbp)
mov    0x2ded(%rip),%rax
movslq (%rax),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1080 <malloc@plt>
mov    %rax,-0x38(%rbp)
lea    -0x8(%rbp),%rdi
call   1090 <getifaddrs@plt>
cmp    $0xffffffff,%eax
jne    1219 <localIP+0x79>
lea    0xdfa(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2dbe(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     132d <localIP+0x18d>
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
jne    1246 <localIP+0xa6>
jmp    1313 <localIP+0x173>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    0x2d8c(%rip),%rcx
cmp    (%rcx),%eax
jne    130e <localIP+0x16e>
mov    -0x40(%rbp),%rdx
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x14(%rbp),%ecx
mov    0x2d6f(%rip),%rax
mov    (%rax),%esi
mov    $0x4,%eax
cmp    %esi,%ecx
cmove  %rax,%rax
mov    %eax,%esi
mov    0x2d39(%rip),%rax
mov    (%rax),%ecx
mov    0x2d40(%rip),%rax
mov    (%rax),%eax
xor    %r8d,%r8d
xor    %r9d,%r9d
sub    $0x10,%rsp
mov    %eax,(%rsp)
call   1060 <getnameinfo@plt>
add    $0x10,%rsp
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     12e2 <localIP+0x142>
mov    -0x18(%rbp),%edi
call   1050 <gai_strerror@plt>
mov    %rax,%rsi
lea    0xd3c(%rip),%rdi
call   1040 <printf@plt>
mov    0x2cf5(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    -0x40(%rbp),%rsi
lea    0xd38(%rip),%rdi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
je     1309 <localIP+0x169>
mov    -0x40(%rbp),%rsi
mov    -0x38(%rbp),%rdi
call   1030 <strcpy@plt>
jmp    130e <localIP+0x16e>
jmp    1313 <localIP+0x173>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x10(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1228 <localIP+0x88>
mov    -0x8(%rbp),%rdi
call   10a0 <freeifaddrs@plt>
mov    -0x38(%rbp),%rax
mov    -0x28(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<localIP>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsp,%r15
mov    0x2e0d(%rip),%rcx
mov    (%rcx),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    (%rcx),%edi
call   1080 <malloc@plt>
mov    %rax,%r14
lea    -0x30(%rbp),%rdi
call   1090 <getifaddrs@plt>
cmp    $0xffffffff,%eax
je     12c4 <localIP+0x124>
mov    -0x30(%rbp),%r12
test   %r12,%r12
je     1284 <localIP+0xe4>
mov    %r15,-0x38(%rbp)
mov    0x2de6(%rip),%r13
lea    0xe1c(%rip),%r15
jmp    121a <localIP+0x7a>
nopl   0x0(%rax,%rax,1)
mov    0x8(%r12),%r12
test   %r12,%r12
je     127a <localIP+0xda>
mov    (%r12),%rdi
test   %rdi,%rdi
je     1210 <localIP+0x70>
mov    (%rdi),%eax
cmp    0x0(%r13),%eax
jne    1210 <localIP+0x70>
mov    0x2d96(%rip),%rax
mov    (%rax),%ecx
mov    0x2d9d(%rip),%rax
mov    (%rax),%eax
sub    $0x8,%rsp
mov    $0x4,%esi
mov    %rbx,%rdx
xor    %r8d,%r8d
xor    %r9d,%r9d
push   %rax
call   1060 <getnameinfo@plt>
add    $0x10,%rsp
test   %eax,%eax
jne    12a0 <localIP+0x100>
mov    %r15,%rdi
mov    %rbx,%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
je     1210 <localIP+0x70>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1030 <strcpy@plt>
jmp    1210 <localIP+0x70>
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%r15
jmp    1286 <localIP+0xe6>
xor    %edi,%edi
call   10a0 <freeifaddrs@plt>
mov    %r15,%rsp
mov    %r14,%rax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %eax,%edi
call   1050 <gai_strerror@plt>
lea    0xd5d(%rip),%rdi
mov    %rax,%rsi
call   1040 <printf@plt>
mov    0x2d13(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
lea    0xd35(%rip),%rdi
call   10b0 <perror@plt>
jmp    12b6 <localIP+0x116>

LLVM IR (-O0):
%struct.ifaddrs = type { %struct.TYPE_2__*, %struct.ifaddrs* }
%struct.TYPE_2__ = type { i32 }
@NI_MAXHOST = global i32 0
@.str = [11 x i8] c"getifaddrs\00"
@EXIT_FAILURE = 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"
define i8* @localIP() {
%1 = alloca %struct.ifaddrs*
%2 = alloca %struct.ifaddrs*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8*
%9 = load i32, i32* @NI_MAXHOST
%10 = zext i32 %9 to i64
%11 = call i8* @llvm.stacksave()
store i8* %11, i8** %6
%12 = alloca i8, i64 %10
store i64 %10, i64* %7
%13 = load i32, i32* @NI_MAXHOST
%14 = sext i32 %13 to i64
%15 = mul i64 1, %14
%16 = trunc i64 %15 to i32
%17 = call i8* @malloc(i32 %16)
store i8* %17, i8** %8
%18 = call i32 @getifaddrs(%struct.ifaddrs** %1)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %24
20: ; preds = %0
%21 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%22 = load i32, i32* @EXIT_FAILURE
%23 = call i32 @exit(i32 %22)
unreachable
24: ; preds = %0
%25 = load %struct.ifaddrs*, %struct.ifaddrs** %1
store %struct.ifaddrs* %25, %struct.ifaddrs** %2
store i32 0, i32* %5
br label %26
26: ; preds = %73, %24
%27 = load %struct.ifaddrs*, %struct.ifaddrs** %2
%28 = icmp ne %struct.ifaddrs* %27, null
br i1 %28, label %29, label %79
29: ; preds = %26
%30 = load %struct.ifaddrs*, %struct.ifaddrs** %2
%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 %73
35: ; preds = %29
%36 = load %struct.ifaddrs*, %struct.ifaddrs** %2
%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* %3
%41 = load i32, i32* %3
%42 = load i32, i32* @AF_INET
%43 = icmp eq i32 %41, %42
br i1 %43, label %44, label %72
44: ; preds = %35
%45 = load %struct.ifaddrs*, %struct.ifaddrs** %2
%46 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %45, i32 0, i32 0
%47 = load %struct.TYPE_2__*, %struct.TYPE_2__** %46
%48 = load i32, i32* %3
%49 = load i32, i32* @AF_INET
%50 = icmp eq i32 %48, %49
%51 = zext i1 %50 to i64
%52 = select i1 %50, i64 4, i64 4
%53 = trunc i64 %52 to i32
%54 = load i32, i32* @NI_MAXHOST
%55 = load i32, i32* @NI_NUMERICHOST
%56 = call i32 @getnameinfo(%struct.TYPE_2__* %47, i32 %53, i8* %12, i32 %54, i32* null, i32 0, i32 %55)
store i32 %56, i32* %4
%57 = load i32, i32* %4
%58 = icmp ne i32 %57, 0
br i1 %58, label %59, label %65
59: ; preds = %44
%60 = load i32, i32* %4
%61 = call i8* @gai_strerror(i32 %60)
%62 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %61)
%63 = load i32, i32* @EXIT_FAILURE
%64 = call i32 @exit(i32 %63)
unreachable
65: ; preds = %44
%66 = call i64 @strcmp(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8* %12)
%67 = icmp ne i64 %66, 0
br i1 %67, label %68, label %71
68: ; preds = %65
%69 = load i8*, i8** %8
%70 = call i32 @strcpy(i8* %69, i8* %12)
br label %71
71: ; preds = %68, %65
br label %72
72: ; preds = %71, %35
br label %73
73: ; preds = %72, %34
%74 = load %struct.ifaddrs*, %struct.ifaddrs** %2
%75 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %74, i32 0, i32 1
%76 = load %struct.ifaddrs*, %struct.ifaddrs** %75
store %struct.ifaddrs* %76, %struct.ifaddrs** %2
%77 = load i32, i32* %5
%78 = add nsw i32 %77, 1
store i32 %78, i32* %5
br label %26
79: ; preds = %26
%80 = load %struct.ifaddrs*, %struct.ifaddrs** %1
%81 = call i32 @freeifaddrs(%struct.ifaddrs* %80)
%82 = load i8*, i8** %8
%83 = load i8*, i8** %6
ret i8* %82
}

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