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

/* Variables and functions */
scalar_t__ 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*/);
scalar_t__ malloc(scalar_t__);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
scalar_t__ strlen(char *);
char *get_my_ip_addr() {
  struct ifaddrs *ifaddr, *ifa;
  int family, s;
  char host[NI_MAXHOST];
  if (getifaddrs(&ifaddr) == -1) {
    perror("getifaddrs");
    exit(EXIT_FAILURE);
  }
  for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
    if (ifa->ifa_addr == NULL)
      continue;
    s = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in), host, NI_MAXHOST,
                    NULL, 0, NI_NUMERICHOST);
    if ((strcmp(ifa->ifa_name, "eth0") == 0) &&
        (ifa->ifa_addr->sa_family == AF_INET)) {
      if (s != 0) {
        printf("getnameinfo() failed: %s\n", gai_strerror(s));
        exit(EXIT_FAILURE);
      }
      printf("\tInterface : <%s>\n", ifa->ifa_name);
      printf("\t  Address : <%s>\n", host);
    }
  }
  freeifaddrs(ifaddr);
  char *ret = (char *)malloc(strlen(host) + 1);
  strcpy(ret, host);
  return ret;
}


x86 assembly (clang, -O0):
<get_my_ip_addr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    0x2e09(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
lea    -0x8(%rbp),%rdi
call   10a0 <getifaddrs@plt>
cmp    $0xffffffff,%eax
jne    1210 <get_my_ip_addr+0x60>
lea    0xe03(%rip),%rdi
call   10c0 <perror@plt>
mov    0x2dc7(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1313 <get_my_ip_addr+0x163>
mov    -0x10(%rbp),%rax
cmpq   $0x0,0x8(%rax)
jne    1237 <get_my_ip_addr+0x87>
jmp    1302 <get_my_ip_addr+0x152>
mov    -0x38(%rbp),%rdx
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2d7e(%rip),%rax
mov    (%rax),%ecx
mov    0x2d85(%rip),%rax
mov    (%rax),%eax
mov    $0x4,%esi
xor    %r8d,%r8d
xor    %r9d,%r9d
sub    $0x10,%rsp
mov    %eax,(%rsp)
call   1070 <getnameinfo@plt>
add    $0x10,%rsp
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
lea    0xd8a(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
jne    12fd <get_my_ip_addr+0x14d>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    (%rax),%rax
mov    0x2d46(%rip),%rcx
cmp    (%rcx),%rax
jne    12fd <get_my_ip_addr+0x14d>
cmpl   $0x0,-0x18(%rbp)
je     12da <get_my_ip_addr+0x12a>
mov    -0x18(%rbp),%edi
call   1060 <gai_strerror@plt>
mov    %rax,%rsi
lea    0xd49(%rip),%rdi
call   1050 <printf@plt>
mov    0x2cfd(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xd42(%rip),%rdi
call   1050 <printf@plt>
mov    -0x38(%rbp),%rsi
lea    0xd45(%rip),%rdi
call   1050 <printf@plt>
jmp    1302 <get_my_ip_addr+0x152>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x10(%rbp)
jmp    1218 <get_my_ip_addr+0x68>
mov    -0x8(%rbp),%rdi
call   10b0 <freeifaddrs@plt>
mov    -0x38(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rdi
add    $0x1,%rdi
call   1090 <malloc@plt>
mov    -0x38(%rbp),%rsi
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
call   1030 <strcpy@plt>
mov    -0x30(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_my_ip_addr>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsp,%r14
mov    0x2dfd(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%r13
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r13
mov    %r13,%rsp
lea    -0x30(%rbp),%rdi
call   10a0 <getifaddrs@plt>
cmp    $0xffffffff,%eax
je     130c <get_my_ip_addr+0x15c>
mov    -0x30(%rbp),%r12
test   %r12,%r12
je     12b9 <get_my_ip_addr+0x109>
mov    %r14,-0x38(%rbp)
mov    0x2dd0(%rip),%r15
lea    0xdfc(%rip),%r14
jmp    122e <get_my_ip_addr+0x7e>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    0x10(%r12),%r12
test   %r12,%r12
je     12af <get_my_ip_addr+0xff>
mov    0x8(%r12),%rdi
test   %rdi,%rdi
je     1220 <get_my_ip_addr+0x70>
mov    0x2d89(%rip),%rax
mov    (%rax),%ecx
mov    (%r15),%eax
sub    $0x8,%rsp
mov    $0x4,%esi
mov    %r13,%rdx
xor    %r8d,%r8d
xor    %r9d,%r9d
push   %rax
call   1070 <getnameinfo@plt>
add    $0x10,%rsp
mov    %eax,%ebx
mov    (%r12),%rdi
mov    %r14,%rsi
call   1080 <strcmp@plt>
test   %rax,%rax
jne    1220 <get_my_ip_addr+0x70>
mov    0x8(%r12),%rax
mov    (%rax),%rax
mov    0x2d66(%rip),%rcx
cmp    (%rcx),%rax
jne    1220 <get_my_ip_addr+0x70>
test   %ebx,%ebx
jne    12f4 <get_my_ip_addr+0x144>
mov    (%r12),%rsi
lea    0xd94(%rip),%rdi
call   1050 <printf@plt>
lea    0xd9b(%rip),%rdi
mov    %r13,%rsi
call   1050 <printf@plt>
jmp    1220 <get_my_ip_addr+0x70>
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%r14
jmp    12bb <get_my_ip_addr+0x10b>
xor    %edi,%edi
call   10b0 <freeifaddrs@plt>
mov    %r13,%rdi
call   1040 <strlen@plt>
lea    0x1(%rax),%rdi
call   1090 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r13,%rsi
call   1030 <strcpy@plt>
mov    %r14,%rsp
mov    %rbx,%rax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %ebx,%edi
call   1060 <gai_strerror@plt>
lea    0xd0e(%rip),%rdi
mov    %rax,%rsi
call   1050 <printf@plt>
jmp    1318 <get_my_ip_addr+0x168>
lea    0xced(%rip),%rdi
call   10c0 <perror@plt>
mov    0x2cb1(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>

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

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