#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_in {
  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_INET;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ 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 /*<<< orphan*/,
                int /*<<< orphan*/ *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ strcmp(int /*<<< orphan*/, char *);
void getIP(char *def_interface, char *host) {
  struct ifaddrs *ifaddr, *ifa;
  int family, info;
  if (getifaddrs(&ifaddr) == -1) {
    perror("getifaddrs");
    exit(EXIT_FAILURE);
  }
  for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
    if (ifa->ifa_addr && !strcmp(ifa->ifa_name, def_interface) &&
        ifa->ifa_addr->sa_family == AF_INET) {
      info = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in), host,
                         NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
      if (info != 0) {
        printf("getnameinfo() failed: %s\n", gai_strerror(info));
        exit(EXIT_FAILURE);
      }
    }
  }
  freeifaddrs(ifaddr);
}


x86 assembly (clang, -O0):
<getIP>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x18(%rbp),%rdi
call   1070 <getifaddrs@plt>
cmp    $0xffffffff,%eax
jne    11bc <getIP+0x3c>
lea    0xe57(%rip),%rdi
call   1090 <perror@plt>
mov    0x2e1b(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     128d <getIP+0x10d>
mov    -0x20(%rbp),%rax
cmpq   $0x0,(%rax)
je     1277 <getIP+0xf7>
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%edi
mov    -0x8(%rbp),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%eax
jne    1277 <getIP+0xf7>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%rax
mov    0x2de1(%rip),%rcx
cmp    (%rcx),%rax
jne    1277 <getIP+0xf7>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rdx
mov    0x2da6(%rip),%rax
mov    (%rax),%ecx
mov    0x2dad(%rip),%rax
mov    (%rax),%eax
mov    $0x4,%esi
xor    %r8d,%r8d
xor    %r9d,%r9d
mov    %eax,(%rsp)
call   1050 <getnameinfo@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
je     1272 <getIP+0xf2>
mov    -0x28(%rbp),%edi
call   1040 <gai_strerror@plt>
mov    %rax,%rsi
lea    0xdac(%rip),%rdi
call   1030 <printf@plt>
mov    0x2d65(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
jmp    1277 <getIP+0xf7>
jmp    127c <getIP+0xfc>
mov    -0x20(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x20(%rbp)
jmp    11c4 <getIP+0x44>
mov    -0x18(%rbp),%rdi
call   1080 <freeifaddrs@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getIP>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r14
mov    %rdi,%r15
lea    0x10(%rsp),%rdi
call   1070 <getifaddrs@plt>
cmp    $0xffffffff,%eax
je     1250 <getIP+0xd0>
mov    0x10(%rsp),%rbx
test   %rbx,%rbx
je     123a <getIP+0xba>
mov    0x2e2c(%rip),%r12
mov    0x2e05(%rip),%r13
mov    0x2e0e(%rip),%rbp
jmp    11d9 <getIP+0x59>
nopl   0x0(%rax)
mov    0x10(%rbx),%rbx
test   %rbx,%rbx
je     1233 <getIP+0xb3>
cmpq   $0x0,(%rbx)
je     11d0 <getIP+0x50>
mov    0x8(%rbx),%edi
mov    %r15,%rsi
call   1060 <strcmp@plt>
test   %eax,%eax
jne    11d0 <getIP+0x50>
mov    (%rbx),%rdi
mov    (%rdi),%rax
cmp    (%r12),%rax
jne    11d0 <getIP+0x50>
mov    0x0(%r13),%ecx
mov    0x0(%rbp),%eax
mov    %eax,(%rsp)
mov    $0x4,%esi
mov    %r14,%rdx
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1050 <getnameinfo@plt>
test   %eax,%eax
je     11d0 <getIP+0x50>
mov    %eax,%edi
call   1040 <gai_strerror@plt>
lea    0xde2(%rip),%rdi
mov    %rax,%rsi
call   1030 <printf@plt>
jmp    125c <getIP+0xdc>
mov    0x10(%rsp),%rdi
jmp    123c <getIP+0xbc>
xor    %edi,%edi
call   1080 <freeifaddrs@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xda9(%rip),%rdi
call   1090 <perror@plt>
mov    0x2d6d(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

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

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