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

/* Variables and functions */
int AF_INET6;
int /*<<< orphan*/ EINVAL;
int INET6_ADDRSTRLEN;
int INET_ADDRSTRLEN;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ freeifaddrs(struct ifaddrs *);
scalar_t__ getifaddrs(struct ifaddrs **);
int /*<<< orphan*/ *inet_ntop(int, void *, char *, int);
scalar_t__ strcmp(char const *, char *);
int get_ip_of_if(const char *if_name, int af, char *IP) {
  struct ifaddrs *ifa_head;
  struct ifaddrs *ifa_cur;
  int result, addrstr_len;
  ;
  void *src;
  if (!if_name || !IP) {
    errno = EINVAL;
    return -1;
  }
  if (getifaddrs(&ifa_head) != 0)
    return -1;
  result = -1;
  for (ifa_cur = ifa_head; ifa_cur; ifa_cur = ifa_cur->ifa_next) {
    if (!ifa_cur->ifa_name)
      continue;
    if (!ifa_cur->ifa_addr)
      continue;
    if (ifa_cur->ifa_addr->sa_family != af)
      continue;
    if (strcmp(if_name, (char *)ifa_cur->ifa_name) != 0)
      continue;
    if (af == AF_INET6) {
      addrstr_len = INET6_ADDRSTRLEN;
      src = &(((struct sockaddr_in6 *)ifa_cur->ifa_addr)->sin6_addr);
    } else {
      addrstr_len = INET_ADDRSTRLEN;
      src = &(((struct sockaddr_in *)ifa_cur->ifa_addr)->sin_addr);
    }
    if (inet_ntop(af, src, IP, addrstr_len) != NULL)
      result = 0;
    break;
  }
  freeifaddrs(ifa_head);
  return result;
}


x86 assembly (clang, -O0):
<get_ip_of_if>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1169 <get_ip_of_if+0x29>
cmpq   $0x0,-0x20(%rbp)
jne    1187 <get_ip_of_if+0x47>
mov    0x2e50(%rip),%rax
mov    (%rax),%ecx
mov    0x2e57(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    12ac <get_ip_of_if+0x16c>
lea    -0x28(%rbp),%rdi
call   1050 <getifaddrs@plt>
cmp    $0x0,%rax
je     11a6 <get_ip_of_if+0x66>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ac <get_ip_of_if+0x16c>
movl   $0xffffffff,-0x34(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     129d <get_ip_of_if+0x15d>
mov    -0x30(%rbp),%rax
cmpq   $0x0,0x8(%rax)
jne    11d4 <get_ip_of_if+0x94>
jmp    128c <get_ip_of_if+0x14c>
mov    -0x30(%rbp),%rax
cmpq   $0x0,(%rax)
jne    11e7 <get_ip_of_if+0xa7>
jmp    128c <get_ip_of_if+0x14c>
mov    -0x30(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%eax
cmp    -0x14(%rbp),%eax
je     11fe <get_ip_of_if+0xbe>
jmp    128c <get_ip_of_if+0x14c>
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rax
mov    0x8(%rax),%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%rax
je     121e <get_ip_of_if+0xde>
jmp    128c <get_ip_of_if+0x14c>
mov    -0x14(%rbp),%eax
mov    0x2db8(%rip),%rcx
cmp    (%rcx),%eax
jne    124c <get_ip_of_if+0x10c>
mov    0x2db1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    -0x30(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x40(%rbp)
jmp    1263 <get_ip_of_if+0x123>
mov    0x2d75(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    -0x30(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x40(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x40(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x38(%rbp),%ecx
call   1030 <inet_ntop@plt>
cmp    $0x0,%rax
je     1287 <get_ip_of_if+0x147>
movl   $0x0,-0x34(%rbp)
jmp    129d <get_ip_of_if+0x15d>
mov    -0x30(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x30(%rbp)
jmp    11b5 <get_ip_of_if+0x75>
mov    -0x28(%rbp),%rdi
call   1060 <freeifaddrs@plt>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_ip_of_if>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
test   %rdi,%rdi
je     11d4 <get_ip_of_if+0x94>
mov    %rdx,%r14
test   %rdx,%rdx
je     11d4 <get_ip_of_if+0x94>
mov    %esi,%r15d
mov    %rdi,%rbp
lea    0x8(%rsp),%rdi
call   1050 <getifaddrs@plt>
mov    $0xffffffff,%r12d
test   %rax,%rax
jne    1213 <get_ip_of_if+0xd3>
mov    0x8(%rsp),%rbx
mov    $0xffffffff,%r12d
jmp    1194 <get_ip_of_if+0x54>
nopl   0x0(%rax)
mov    0x10(%rbx),%rbx
test   %rbx,%rbx
je     1209 <get_ip_of_if+0xc9>
mov    0x8(%rbx),%rsi
test   %rsi,%rsi
je     1190 <get_ip_of_if+0x50>
mov    (%rbx),%rax
test   %rax,%rax
je     1190 <get_ip_of_if+0x50>
cmp    %r15d,(%rax)
jne    1190 <get_ip_of_if+0x50>
mov    %rbp,%rdi
call   1040 <strcmp@plt>
test   %rax,%rax
jne    1190 <get_ip_of_if+0x50>
mov    0x2e1d(%rip),%rax
mov    (%rbx),%rsi
cmp    %r15d,(%rax)
jne    11ee <get_ip_of_if+0xae>
mov    0x2e16(%rip),%rax
jmp    11f5 <get_ip_of_if+0xb5>
mov    0x2de5(%rip),%rax
mov    (%rax),%eax
mov    0x2dec(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%r12d
jmp    1213 <get_ip_of_if+0xd3>
mov    0x2dd3(%rip),%rax
mov    (%rax),%ecx
mov    %r15d,%edi
mov    %r14,%rdx
call   1030 <inet_ntop@plt>
cmp    $0x1,%rax
sbb    %r12d,%r12d
mov    0x8(%rsp),%rdi
call   1060 <freeifaddrs@plt>
mov    %r12d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.ifaddrs = type { %struct.TYPE_2__*, i64, %struct.ifaddrs* }
%struct.TYPE_2__ = type { i32 }
@EINVAL = global i32 0
@errno = global i32 0
@AF_INET6 = global i32 0
@INET6_ADDRSTRLEN = global i32 0
@INET_ADDRSTRLEN = global i32 0
define i32 @get_ip_of_if(i8* %0, i32 %1, i8* %2) {
%4 = alloca %struct.ifaddrs*
%5 = bitcast %struct.ifaddrs** %4 to i8*
%6 = icmp ne i8* %0, null
%7 = icmp ne i8* %2, null
%8 = and i1 %6, %7
br i1 %8, label %11, label %9
9: ; preds = %3
%10 = load i32, i32* @EINVAL
store i32 %10, i32* @errno
br label %53
11: ; preds = %3
%12 = call i64 @getifaddrs(%struct.ifaddrs** %4)
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %53
14: ; preds = %11
%15 = load %struct.ifaddrs*, %struct.ifaddrs** %4
%16 = icmp eq %struct.ifaddrs* %15, null
br i1 %16, label %49, label %17
17: ; preds = %14, %45
%18 = phi %struct.ifaddrs* [ %47, %45 ], [ %15, %14 ]
%19 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %18, i64 0, i32 1
%20 = load i64, i64* %19
%21 = icmp eq i64 %20, 0
br i1 %21, label %45, label %22
22: ; preds = %17
%23 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %18, i64 0, i32 0
%24 = load %struct.TYPE_2__*, %struct.TYPE_2__** %23
%25 = icmp eq %struct.TYPE_2__* %24, null
br i1 %25, label %45, label %26
26: ; preds = %22
%27 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %24, i64 0, i32 0
%28 = load i32, i32* %27
%29 = icmp eq i32 %28, %1
br i1 %29, label %30, label %45
30: ; preds = %26
%31 = inttoptr i64 %20 to i8*
%32 = call i64 @strcmp(i8* %0, i8* %31)
%33 = icmp eq i64 %32, 0
br i1 %33, label %34, label %45
34: ; preds = %30
%35 = load i32, i32* @AF_INET6
%36 = icmp eq i32 %35, %1
%37 = bitcast %struct.ifaddrs* %18 to i8**
%38 = load i8*, i8** %37
%39 = load i32, i32* @INET6_ADDRSTRLEN
%40 = load i32, i32* @INET_ADDRSTRLEN
%41 = select i1 %36, i32 %39, i32 %40
%42 = call i32* @inet_ntop(i32 %1, i8* %38, i8* %2, i32 %41)
%43 = icmp eq i32* %42, null
%44 = sext i1 %43 to i32
br label %49
45: ; preds = %30, %26, %22, %17
%46 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %18, i64 0, i32 2
%47 = load %struct.ifaddrs*, %struct.ifaddrs** %46
%48 = icmp eq %struct.ifaddrs* %47, null
br i1 %48, label %49, label %17
49: ; preds = %45, %14, %34
%50 = phi i32 [ %44, %34 ], [ -1, %14 ], [ -1, %45 ]
%51 = load %struct.ifaddrs*, %struct.ifaddrs** %4
%52 = call i32 @freeifaddrs(%struct.ifaddrs* %51)
br label %53
53: ; preds = %11, %49, %9
%54 = phi i32 [ %50, %49 ], [ -1, %9 ], [ -1, %11 ]
ret i32 %54
}