#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 in_addr {
  scalar_t__ s_addr;
  int /*<<< orphan*/ member_0;
};
struct sockaddr_in {
  struct in_addr sin_addr;
};
struct ifaddrs {
  int ifa_flags;
  TYPE_1__ *ifa_addr;
  struct ifaddrs *ifa_next;
};
struct TYPE_2__ {
  scalar_t__ sa_family;
};

/* Variables and functions */
scalar_t__ AF_INET;
int /*<<< orphan*/ freeifaddrs(struct ifaddrs *);
scalar_t__ getifaddrs(struct ifaddrs **);
int /*<<< orphan*/ getnameinfo(TYPE_1__ *, int, char *, int,
                               int /*<<< orphan*/ *, int /*<<< orphan*/, int);
int /*<<< orphan*/ *inet_ntop(scalar_t__, struct in_addr *, char *, int);
int ipAddressLevel(struct in_addr *, int);
scalar_t__ strcmp(char *, char *);
int getLocalHostIp(char *ip) {
  struct ifaddrs *ifaddrs = NULL;
  if (getifaddrs(&ifaddrs)) {
    return -1;
  }
  struct in_addr res = {0};
  int res_level = 0, tmp_level = 0;
  char ipBuf[64];
  struct ifaddrs *ifa;
  for (ifa = ifaddrs; NULL != ifa; ifa = ifa->ifa_next) {
    char hostname[1025];
    if (ifa->ifa_addr->sa_family == AF_INET) {
      if (!(ifa->ifa_flags & 0x1)) {
        continue;
      }
      if (ifa->ifa_flags & 0x8) {
        continue;
      }
      getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in), hostname, 1025,
                  NULL, 0, 0x00000002);
      struct in_addr *addr = &((struct sockaddr_in *)ifa->ifa_addr)->sin_addr;
      if (NULL == inet_ntop(AF_INET, addr, ipBuf, 16)) {
        continue;
      }
      int offset = strcmp(ipBuf, hostname) == 0 ? 0 : 1;
      if (0 == res.s_addr) {
        res = *addr;
        res_level = ipAddressLevel(&res, offset);
      } else if (res_level < (tmp_level = ipAddressLevel(addr, offset))) {
        res = *addr;
        res_level = tmp_level;
      }
    }
  }
  freeifaddrs(ifaddrs);
  if (0 == res.s_addr) {
    return -1;
  } else {
    return NULL == inet_ntop(AF_INET, &res, ip, 16) ? -1 : 0;
  }
}


x86 assembly (clang, -O0):
<getLocalHostIp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x4a0,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
lea    -0x18(%rbp),%rdi
call   1070 <getifaddrs@plt>
cmp    $0x0,%rax
je     11a6 <getLocalHostIp+0x36>
movl   $0xffffffff,-0x4(%rbp)
jmp    13ad <getLocalHostIp+0x23d>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
movl   $0x0,-0x2c(%rbp)
movl   $0x0,-0x30(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x78(%rbp)
xor    %eax,%eax
cmp    -0x78(%rbp),%rax
je     1359 <getLocalHostIp+0x1e9>
mov    -0x78(%rbp),%rax
mov    0x8(%rax),%rax
mov    (%rax),%rax
mov    0x2dfe(%rip),%rcx
cmp    (%rcx),%rax
jne    1343 <getLocalHostIp+0x1d3>
mov    -0x78(%rbp),%rax
mov    (%rax),%eax
and    $0x1,%eax
cmp    $0x0,%eax
jne    120a <getLocalHostIp+0x9a>
jmp    1348 <getLocalHostIp+0x1d8>
mov    -0x78(%rbp),%rax
mov    (%rax),%eax
and    $0x8,%eax
cmp    $0x0,%eax
je     1221 <getLocalHostIp+0xb1>
jmp    1348 <getLocalHostIp+0x1d8>
mov    -0x78(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x480(%rbp),%rdx
mov    $0x10,%esi
mov    $0x401,%ecx
xor    %eax,%eax
mov    %eax,%r8d
xor    %r9d,%r9d
movl   $0x2,(%rsp)
call   1050 <getnameinfo@plt>
mov    -0x78(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x488(%rbp)
mov    0x2d84(%rip),%rax
mov    (%rax),%rdi
mov    -0x488(%rbp),%rsi
lea    -0x70(%rbp),%rdx
mov    $0x10,%ecx
call   1030 <inet_ntop@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jne    128f <getLocalHostIp+0x11f>
jmp    1348 <getLocalHostIp+0x1d8>
lea    -0x70(%rbp),%rdi
lea    -0x480(%rbp),%rsi
call   1060 <strcmp@plt>
mov    %rax,%rdx
mov    $0x1,%eax
xor    %ecx,%ecx
cmp    $0x0,%rdx
cmove  %ecx,%eax
mov    %eax,-0x48c(%rbp)
xor    %eax,%eax
cmp    -0x28(%rbp),%rax
jne    12ef <getLocalHostIp+0x17f>
mov    -0x488(%rbp),%rax
mov    (%rax),%rcx
mov    %rcx,-0x28(%rbp)
mov    0x8(%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x48c(%rbp),%esi
lea    -0x28(%rbp),%rdi
call   1090 <ipAddressLevel@plt>
mov    %eax,-0x2c(%rbp)
jmp    133e <getLocalHostIp+0x1ce>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x490(%rbp)
mov    -0x488(%rbp),%rdi
mov    -0x48c(%rbp),%esi
call   1090 <ipAddressLevel@plt>
mov    %eax,%ecx
mov    -0x490(%rbp),%eax
mov    %ecx,-0x30(%rbp)
cmp    %ecx,%eax
jge    1339 <getLocalHostIp+0x1c9>
mov    -0x488(%rbp),%rax
mov    (%rax),%rcx
mov    %rcx,-0x28(%rbp)
mov    0x8(%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x30(%rbp),%eax
mov    %eax,-0x2c(%rbp)
jmp    133e <getLocalHostIp+0x1ce>
jmp    1343 <getLocalHostIp+0x1d3>
jmp    1348 <getLocalHostIp+0x1d8>
mov    -0x78(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x78(%rbp)
jmp    11cc <getLocalHostIp+0x5c>
mov    -0x18(%rbp),%rdi
call   1080 <freeifaddrs@plt>
xor    %eax,%eax
cmp    -0x28(%rbp),%rax
jne    137a <getLocalHostIp+0x20a>
movl   $0xffffffff,-0x4(%rbp)
jmp    13ad <getLocalHostIp+0x23d>
mov    0x2c67(%rip),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rdx
lea    -0x28(%rbp),%rsi
mov    $0x10,%ecx
call   1030 <inet_ntop@plt>
mov    %rax,%rsi
xor    %eax,%eax
mov    %eax,%edx
xor    %eax,%eax
mov    $0xffffffff,%ecx
cmp    %rsi,%rdx
cmove  %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x4a0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getLocalHostIp>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x478,%rsp
mov    %rdi,%rbp
movq   $0x0,0x8(%rsp)
lea    0x8(%rsp),%rdi
call   1060 <getifaddrs@plt>
mov    %rax,%rcx
mov    $0xffffffff,%eax
test   %rcx,%rcx
jne    12c1 <getLocalHostIp+0x161>
xorps  %xmm0,%xmm0
movaps %xmm0,0x10(%rsp)
mov    0x8(%rsp),%r13
xor    %ebx,%ebx
test   %r13,%r13
je     1288 <getLocalHostIp+0x128>
mov    %rbp,0x28(%rsp)
mov    0x2e2c(%rip),%r14
lea    0x70(%rsp),%r15
lea    0x30(%rsp),%r12
jmp    11ed <getLocalHostIp+0x8d>
movups 0x0(%rbp),%xmm0
movaps %xmm0,0x10(%rsp)
lea    0x10(%rsp),%rdi
call   1080 <ipAddressLevel@plt>
mov    %eax,%ebx
nopl   (%rax)
mov    0x10(%r13),%r13
test   %r13,%r13
je     127e <getLocalHostIp+0x11e>
mov    0x8(%r13),%rdi
mov    (%rdi),%rax
cmp    (%r14),%rax
jne    11e0 <getLocalHostIp+0x80>
mov    0x0(%r13),%eax
and    $0x9,%eax
cmp    $0x1,%eax
jne    11e0 <getLocalHostIp+0x80>
movl   $0x2,(%rsp)
mov    $0x10,%esi
mov    %r15,%rdx
mov    $0x401,%ecx
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1040 <getnameinfo@plt>
mov    0x8(%r13),%rbp
mov    (%r14),%rdi
mov    %rbp,%rsi
mov    %r12,%rdx
mov    $0x10,%ecx
call   1030 <inet_ntop@plt>
test   %rax,%rax
je     11e0 <getLocalHostIp+0x80>
mov    %r12,%rdi
mov    %r15,%rsi
call   1050 <strcmp@plt>
xor    %esi,%esi
test   %rax,%rax
setne  %sil
cmpq   $0x0,0x10(%rsp)
je     11c8 <getLocalHostIp+0x68>
mov    %rbp,%rdi
call   1080 <ipAddressLevel@plt>
cmp    %eax,%ebx
jge    11e0 <getLocalHostIp+0x80>
movups 0x0(%rbp),%xmm0
movaps %xmm0,0x10(%rsp)
jmp    11db <getLocalHostIp+0x7b>
mov    0x8(%rsp),%rbx
mov    0x28(%rsp),%rbp
mov    %rbx,%rdi
call   1070 <freeifaddrs@plt>
cmpq   $0x0,0x10(%rsp)
je     12bc <getLocalHostIp+0x15c>
mov    0x2d49(%rip),%rax
mov    (%rax),%rdi
lea    0x10(%rsp),%rsi
mov    %rbp,%rdx
mov    $0x10,%ecx
call   1030 <inet_ntop@plt>
cmp    $0x1,%rax
sbb    %eax,%eax
jmp    12c1 <getLocalHostIp+0x161>
mov    $0xffffffff,%eax
add    $0x478,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.ifaddrs = type { i32, %struct.TYPE_2__*, %struct.ifaddrs* }
%struct.TYPE_2__ = type { i64 }
%struct.in_addr = type { i64, i32 }
%struct.sockaddr_in = type { %struct.in_addr }
@AF_INET = global i64 0
define i32 @getLocalHostIp(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.ifaddrs*
%5 = alloca %struct.in_addr
%6 = alloca i32
%7 = alloca i32
%8 = alloca [64 x i8]
%9 = alloca %struct.ifaddrs*
%10 = alloca [1025 x i8]
%11 = alloca %struct.in_addr*
%12 = alloca i32
store i8* %0, i8** %3
store %struct.ifaddrs* null, %struct.ifaddrs** %4
%13 = call i64 @getifaddrs(%struct.ifaddrs** %4)
%14 = icmp ne i64 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %1
store i32 -1, i32* %2
br label %109
16: ; preds = %1
%17 = bitcast %struct.in_addr* %5 to i8*
store i32 0, i32* %6
store i32 0, i32* %7
%18 = load %struct.ifaddrs*, %struct.ifaddrs** %4
store %struct.ifaddrs* %18, %struct.ifaddrs** %9
br label %19
19: ; preds = %91, %16
%20 = load %struct.ifaddrs*, %struct.ifaddrs** %9
%21 = icmp ne %struct.ifaddrs* null, %20
br i1 %21, label %22, label %95
22: ; preds = %19
%23 = load %struct.ifaddrs*, %struct.ifaddrs** %9
%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 i64, i64* %26
%28 = load i64, i64* @AF_INET
%29 = icmp eq i64 %27, %28
br i1 %29, label %30, label %90
30: ; preds = %22
%31 = load %struct.ifaddrs*, %struct.ifaddrs** %9
%32 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %31, i32 0, i32 0
%33 = load i32, i32* %32
%34 = and i32 %33, 1
%35 = icmp ne i32 %34, 0
br i1 %35, label %37, label %36
36: ; preds = %30
br label %91
37: ; preds = %30
%38 = load %struct.ifaddrs*, %struct.ifaddrs** %9
%39 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %38, i32 0, i32 0
%40 = load i32, i32* %39
%41 = and i32 %40, 8
%42 = icmp ne i32 %41, 0
br i1 %42, label %43, label %44
43: ; preds = %37
br label %91
44: ; preds = %37
%45 = load %struct.ifaddrs*, %struct.ifaddrs** %9
%46 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %45, i32 0, i32 1
%47 = load %struct.TYPE_2__*, %struct.TYPE_2__** %46
%48 = getelementptr inbounds [1025 x i8], [1025 x i8]* %10, i64 0, i64 0
%49 = call i32 @getnameinfo(%struct.TYPE_2__* %47, i32 16, i8* %48, i32 1025, i32* null, i32 0, i32 2)
%50 = load %struct.ifaddrs*, %struct.ifaddrs** %9
%51 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %50, i32 0, i32 1
%52 = load %struct.TYPE_2__*, %struct.TYPE_2__** %51
%53 = bitcast %struct.TYPE_2__* %52 to %struct.sockaddr_in*
%54 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %53, i32 0, i32 0
store %struct.in_addr* %54, %struct.in_addr** %11
%55 = load i64, i64* @AF_INET
%56 = load %struct.in_addr*, %struct.in_addr** %11
%57 = getelementptr inbounds [64 x i8], [64 x i8]* %8, i64 0, i64 0
%58 = call i32* @inet_ntop(i64 %55, %struct.in_addr* %56, i8* %57, i32 16)
%59 = icmp eq i32* null, %58
br i1 %59, label %60, label %61
60: ; preds = %44
br label %91
61: ; preds = %44
%62 = getelementptr inbounds [64 x i8], [64 x i8]* %8, i64 0, i64 0
%63 = getelementptr inbounds [1025 x i8], [1025 x i8]* %10, i64 0, i64 0
%64 = call i64 @strcmp(i8* %62, i8* %63)
%65 = icmp eq i64 %64, 0
%66 = zext i1 %65 to i64
%67 = select i1 %65, i32 0, i32 1
store i32 %67, i32* %12
%68 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %5, i32 0, i32 0
%69 = load i64, i64* %68
%70 = icmp eq i64 0, %69
br i1 %70, label %71, label %77
71: ; preds = %61
%72 = load %struct.in_addr*, %struct.in_addr** %11
%73 = bitcast %struct.in_addr* %5 to i8*
%74 = bitcast %struct.in_addr* %72 to i8*
%75 = load i32, i32* %12
%76 = call i32 @ipAddressLevel(%struct.in_addr* %5, i32 %75)
store i32 %76, i32* %6
br label %89
77: ; preds = %61
%78 = load i32, i32* %6
%79 = load %struct.in_addr*, %struct.in_addr** %11
%80 = load i32, i32* %12
%81 = call i32 @ipAddressLevel(%struct.in_addr* %79, i32 %80)
store i32 %81, i32* %7
%82 = icmp slt i32 %78, %81
br i1 %82, label %83, label %88
83: ; preds = %77
%84 = load %struct.in_addr*, %struct.in_addr** %11
%85 = bitcast %struct.in_addr* %5 to i8*
%86 = bitcast %struct.in_addr* %84 to i8*
%87 = load i32, i32* %7
store i32 %87, i32* %6
br label %88
88: ; preds = %83, %77
br label %89
89: ; preds = %88, %71
br label %90
90: ; preds = %89, %22
br label %91
91: ; preds = %90, %60, %43, %36
%92 = load %struct.ifaddrs*, %struct.ifaddrs** %9
%93 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %92, i32 0, i32 2
%94 = load %struct.ifaddrs*, %struct.ifaddrs** %93
store %struct.ifaddrs* %94, %struct.ifaddrs** %9
br label %19
95: ; preds = %19
%96 = load %struct.ifaddrs*, %struct.ifaddrs** %4
%97 = call i32 @freeifaddrs(%struct.ifaddrs* %96)
%98 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %5, i32 0, i32 0
%99 = load i64, i64* %98
%100 = icmp eq i64 0, %99
br i1 %100, label %101, label %102
101: ; preds = %95
store i32 -1, i32* %2
br label %109
102: ; preds = %95
%103 = load i64, i64* @AF_INET
%104 = load i8*, i8** %3
%105 = call i32* @inet_ntop(i64 %103, %struct.in_addr* %5, i8* %104, i32 16)
%106 = icmp eq i32* null, %105
%107 = zext i1 %106 to i64
%108 = select i1 %106, i32 -1, i32 0
store i32 %108, i32* %2
br label %109
109: ; preds = %102, %101, %15
%110 = load i32, i32* %2
ret i32 %110
}

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