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

/* Variables and functions */
int AF_INET;
int AF_INET6;
int IF_NAMESIZE;
struct addr *addr_create_from_sockaddr(TYPE_1__ *);
int /*<<< orphan*/ freeifaddrs(struct ifaddrs *);
int getifaddrs(struct ifaddrs **);
int /*<<< orphan*/ *if_indextoname(int, char *);
scalar_t__ strcmp(int /*<<< orphan*/, char *);
struct addr *iface_ip_addr(int if_index, int type) {
  int family = (type == 1) ? AF_INET : AF_INET6;
  char if_name[IF_NAMESIZE];
  if (if_indextoname(if_index, if_name) == NULL)
    return NULL;
  struct ifaddrs *if_addrs;
  if (getifaddrs(&if_addrs) == -1)
    return NULL;
  struct addr *addr = NULL;
  struct ifaddrs *ifa;
  for (ifa = if_addrs; ifa != NULL; ifa = ifa->ifa_next) {
    if (strcmp(ifa->ifa_name, if_name) != 0)
      continue;
    if (ifa->ifa_addr != NULL && ifa->ifa_addr->sa_family == family) {
      addr = addr_create_from_sockaddr(ifa->ifa_addr);
      break;
    }
  }
  freeifaddrs(if_addrs);
  return addr;
}


x86 assembly (clang, -O0):
<iface_ip_addr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0xc(%rbp)
mov    %esi,-0x10(%rbp)
cmpl   $0x1,-0x10(%rbp)
jne    1179 <iface_ip_addr+0x29>
mov    0x2e79(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
jmp    1185 <iface_ip_addr+0x35>
mov    0x2e58(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x4c(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    0x2e4e(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rsi
sub    %rcx,%rsi
mov    %rsi,-0x58(%rbp)
mov    %rsi,%rsp
mov    %rax,-0x28(%rbp)
mov    -0xc(%rbp),%edi
call   1060 <if_indextoname@plt>
cmp    $0x0,%rax
jne    11dd <iface_ip_addr+0x8d>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x2c(%rbp)
jmp    12a0 <iface_ip_addr+0x150>
lea    -0x38(%rbp),%rdi
call   1050 <getifaddrs@plt>
cmp    $0xffffffff,%eax
jne    1203 <iface_ip_addr+0xb3>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x2c(%rbp)
jmp    12a0 <iface_ip_addr+0x150>
movq   $0x0,-0x40(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
je     1288 <iface_ip_addr+0x138>
mov    -0x58(%rbp),%rsi
mov    -0x48(%rbp),%rax
mov    0x8(%rax),%edi
call   1030 <strcmp@plt>
cmp    $0x0,%rax
je     123d <iface_ip_addr+0xed>
jmp    1277 <iface_ip_addr+0x127>
mov    -0x48(%rbp),%rax
cmpq   $0x0,(%rax)
je     1272 <iface_ip_addr+0x122>
mov    -0x48(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%eax
cmp    -0x14(%rbp),%eax
jne    1272 <iface_ip_addr+0x122>
mov    -0x48(%rbp),%rax
mov    (%rax),%rdi
call   1040 <addr_create_from_sockaddr@plt>
mov    %rax,-0x40(%rbp)
jmp    1288 <iface_ip_addr+0x138>
jmp    1277 <iface_ip_addr+0x127>
mov    -0x48(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x48(%rbp)
jmp    1213 <iface_ip_addr+0xc3>
mov    -0x38(%rbp),%rdi
call   1070 <freeifaddrs@plt>
mov    -0x40(%rbp),%rax
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x2c(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x8(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<iface_ip_addr>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
cmp    $0x1,%esi
jne    116d <iface_ip_addr+0x1d>
mov    0x2e7d(%rip),%rax
jmp    1174 <iface_ip_addr+0x24>
mov    0x2e64(%rip),%rax
mov    (%rax),%r15d
mov    0x2e62(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%r12
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r12
mov    %r12,%rsp
mov    %r12,%rsi
call   1060 <if_indextoname@plt>
test   %rax,%rax
je     11f9 <iface_ip_addr+0xa9>
lea    -0x28(%rbp),%rdi
call   1050 <getifaddrs@plt>
cmp    $0xffffffff,%eax
je     11f9 <iface_ip_addr+0xa9>
mov    -0x28(%rbp),%rbx
xor    %r14d,%r14d
jmp    11c4 <iface_ip_addr+0x74>
cs nopw 0x0(%rax,%rax,1)
nop
mov    0x10(%rbx),%rbx
test   %rbx,%rbx
je     11ee <iface_ip_addr+0x9e>
mov    0x8(%rbx),%edi
mov    %r12,%rsi
call   1030 <strcmp@plt>
test   %rax,%rax
jne    11c0 <iface_ip_addr+0x70>
mov    (%rbx),%rdi
test   %rdi,%rdi
je     11c0 <iface_ip_addr+0x70>
cmp    %r15d,(%rdi)
jne    11c0 <iface_ip_addr+0x70>
call   1040 <addr_create_from_sockaddr@plt>
mov    %rax,%r14
mov    -0x28(%rbp),%rdi
call   1070 <freeifaddrs@plt>
jmp    11fc <iface_ip_addr+0xac>
xor    %r14d,%r14d
mov    %r14,%rax
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.addr = type { i32 }
%struct.ifaddrs = type { %struct.TYPE_2__*, i32, %struct.ifaddrs* }
%struct.TYPE_2__ = type { i32 }
@AF_INET = global i32 0
@AF_INET6 = global i32 0
@IF_NAMESIZE = global i32 0
define %struct.addr* @iface_ip_addr(i32 %0, i32 %1) {
%3 = alloca %struct.ifaddrs*
%4 = icmp eq i32 %1, 1
%5 = load i32, i32* @AF_INET
%6 = load i32, i32* @AF_INET6
%7 = select i1 %4, i32 %5, i32 %6
%8 = load i32, i32* @IF_NAMESIZE
%9 = zext i32 %8 to i64
%10 = alloca i8, i64 %9
%11 = call i32* @if_indextoname(i32 %0, i8* %10)
%12 = icmp eq i32* %11, null
br i1 %12, label %46, label %13
13: ; preds = %2
%14 = bitcast %struct.ifaddrs** %3 to i8*
%15 = call i32 @getifaddrs(%struct.ifaddrs** %3)
%16 = icmp eq i32 %15, -1
br i1 %16, label %44, label %17
17: ; preds = %13
%18 = load %struct.ifaddrs*, %struct.ifaddrs** %3
%19 = icmp eq %struct.ifaddrs* %18, null
br i1 %19, label %40, label %20
20: ; preds = %17, %36
%21 = phi %struct.ifaddrs* [ %38, %36 ], [ %18, %17 ]
%22 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %21, i64 0, i32 1
%23 = load i32, i32* %22
%24 = call i64 @strcmp(i32 %23, i8* %10)
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %36
26: ; preds = %20
%27 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %21, i64 0, i32 0
%28 = load %struct.TYPE_2__*, %struct.TYPE_2__** %27
%29 = icmp eq %struct.TYPE_2__* %28, null
br i1 %29, label %36, label %30
30: ; preds = %26
%31 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %28, i64 0, i32 0
%32 = load i32, i32* %31
%33 = icmp eq i32 %32, %7
br i1 %33, label %34, label %36
34: ; preds = %30
%35 = call %struct.addr* @addr_create_from_sockaddr(%struct.TYPE_2__* %28)
br label %40
36: ; preds = %26, %30, %20
%37 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %21, i64 0, i32 2
%38 = load %struct.ifaddrs*, %struct.ifaddrs** %37
%39 = icmp eq %struct.ifaddrs* %38, null
br i1 %39, label %40, label %20
40: ; preds = %36, %17, %34
%41 = phi %struct.addr* [ %35, %34 ], [ null, %17 ], [ null, %36 ]
%42 = load %struct.ifaddrs*, %struct.ifaddrs** %3
%43 = call i32 @freeifaddrs(%struct.ifaddrs* %42)
br label %44
44: ; preds = %13, %40
%45 = phi %struct.addr* [ %41, %40 ], [ null, %13 ]
br label %46
46: ; preds = %2, %44
%47 = phi %struct.addr* [ %45, %44 ], [ null, %2 ]
ret %struct.addr* %47
}