#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 in_addr {
  int dummy;
};
struct in6_addr {
  int dummy;
};
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 /*<<< orphan*/ errno;
int /*<<< orphan*/ freeifaddrs(struct ifaddrs *);
scalar_t__ getifaddrs(struct ifaddrs **);
int /*<<< orphan*/ memcpy(void *, void *, int);
scalar_t__ strcmp(char const *, char *);
int get_addr_of_if(const char *if_name, int af, void *addr) {
  struct ifaddrs *ifa_head;
  struct ifaddrs *ifa_cur;
  int result, addr_len;
  void *src;
  if (!if_name || !addr) {
    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) {
      addr_len = sizeof(struct in6_addr);
      src = &(((struct sockaddr_in6 *)ifa_cur->ifa_addr)->sin6_addr);
    } else {
      addr_len = sizeof(struct in_addr);
      src = &(((struct sockaddr_in *)ifa_cur->ifa_addr)->sin_addr);
    }
    memcpy(addr, src, addr_len);
    result = 0;
    break;
  }
  freeifaddrs(ifa_head);
  return result;
}


x86 assembly (clang, -O0):
<get_addr_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_addr_of_if+0x29>
cmpq   $0x0,-0x20(%rbp)
jne    1187 <get_addr_of_if+0x47>
mov    0x2e60(%rip),%rax
mov    (%rax),%ecx
mov    0x2e5f(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1295 <get_addr_of_if+0x155>
lea    -0x28(%rbp),%rdi
call   1050 <getifaddrs@plt>
cmp    $0x0,%rax
je     11a6 <get_addr_of_if+0x66>
movl   $0xffffffff,-0x4(%rbp)
jmp    1295 <get_addr_of_if+0x155>
movl   $0xffffffff,-0x34(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     1286 <get_addr_of_if+0x146>
mov    -0x30(%rbp),%rax
cmpq   $0x0,0x8(%rax)
jne    11d4 <get_addr_of_if+0x94>
jmp    1275 <get_addr_of_if+0x135>
mov    -0x30(%rbp),%rax
cmpq   $0x0,(%rax)
jne    11e7 <get_addr_of_if+0xa7>
jmp    1275 <get_addr_of_if+0x135>
mov    -0x30(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%eax
cmp    -0x14(%rbp),%eax
je     11fe <get_addr_of_if+0xbe>
jmp    1275 <get_addr_of_if+0x135>
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rax
mov    0x8(%rax),%rsi
call   1030 <strcmp@plt>
cmp    $0x0,%rax
je     121e <get_addr_of_if+0xde>
jmp    1275 <get_addr_of_if+0x135>
mov    -0x14(%rbp),%eax
mov    0x2dc0(%rip),%rcx
cmp    (%rcx),%eax
jne    1247 <get_addr_of_if+0x107>
movl   $0x4,-0x38(%rbp)
mov    -0x30(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x40(%rbp)
jmp    1259 <get_addr_of_if+0x119>
movl   $0x4,-0x38(%rbp)
mov    -0x30(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x40(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x40(%rbp),%rsi
mov    -0x38(%rbp),%edx
call   1040 <memcpy@plt>
movl   $0x0,-0x34(%rbp)
jmp    1286 <get_addr_of_if+0x146>
mov    -0x30(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x30(%rbp)
jmp    11b5 <get_addr_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_addr_of_if>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
test   %rdi,%rdi
je     11db <get_addr_of_if+0x9b>
mov    %rdx,%r14
test   %rdx,%rdx
je     11db <get_addr_of_if+0x9b>
mov    %esi,%r12d
mov    %rdi,%rbp
lea    0x8(%rsp),%rdi
call   1050 <getifaddrs@plt>
mov    $0xffffffff,%r15d
test   %rax,%rax
jne    11f3 <get_addr_of_if+0xb3>
mov    0x8(%rsp),%rbx
mov    $0xffffffff,%r15d
jmp    1194 <get_addr_of_if+0x54>
nopl   0x0(%rax)
mov    0x10(%rbx),%rbx
test   %rbx,%rbx
je     11cf <get_addr_of_if+0x8f>
mov    0x8(%rbx),%rsi
test   %rsi,%rsi
je     1190 <get_addr_of_if+0x50>
mov    (%rbx),%rax
test   %rax,%rax
je     1190 <get_addr_of_if+0x50>
cmp    %r12d,(%rax)
jne    1190 <get_addr_of_if+0x50>
mov    %rbp,%rdi
call   1030 <strcmp@plt>
test   %rax,%rax
jne    1190 <get_addr_of_if+0x50>
mov    (%rbx),%rsi
mov    %r14,%rdi
mov    $0x4,%edx
call   1040 <memcpy@plt>
xor    %r15d,%r15d
mov    0x8(%rsp),%rdi
call   1060 <freeifaddrs@plt>
jmp    11f3 <get_addr_of_if+0xb3>
mov    0x2df6(%rip),%rax
mov    (%rax),%eax
mov    0x2df5(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%r15d
mov    %r15d,%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
define i32 @get_addr_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 %90
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 %90
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 = %82, %24
%27 = load %struct.ifaddrs*, %struct.ifaddrs** %9
%28 = icmp ne %struct.ifaddrs* %27, null
br i1 %28, label %29, label %86
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 %82
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 %82
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 %82
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 %82
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 %70
63: ; preds = %59
store i32 4, i32* %11
%64 = load %struct.ifaddrs*, %struct.ifaddrs** %9
%65 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %64, i32 0, i32 0
%66 = load %struct.TYPE_2__*, %struct.TYPE_2__** %65
%67 = bitcast %struct.TYPE_2__* %66 to %struct.sockaddr_in6*
%68 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %67, i32 0, i32 0
%69 = bitcast i32* %68 to i8*
store i8* %69, i8** %12
br label %77
70: ; preds = %59
store i32 4, i32* %11
%71 = load %struct.ifaddrs*, %struct.ifaddrs** %9
%72 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %71, i32 0, i32 0
%73 = load %struct.TYPE_2__*, %struct.TYPE_2__** %72
%74 = bitcast %struct.TYPE_2__* %73 to %struct.sockaddr_in*
%75 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %74, i32 0, i32 0
%76 = bitcast i32* %75 to i8*
store i8* %76, i8** %12
br label %77
77: ; preds = %70, %63
%78 = load i8*, i8** %7
%79 = load i8*, i8** %12
%80 = load i32, i32* %11
%81 = call i32 @memcpy(i8* %78, i8* %79, i32 %80)
store i32 0, i32* %10
br label %86
82: ; preds = %58, %49, %40, %34
%83 = load %struct.ifaddrs*, %struct.ifaddrs** %9
%84 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %83, i32 0, i32 2
%85 = load %struct.ifaddrs*, %struct.ifaddrs** %84
store %struct.ifaddrs* %85, %struct.ifaddrs** %9
br label %26
86: ; preds = %77, %26
%87 = load %struct.ifaddrs*, %struct.ifaddrs** %8
%88 = call i32 @freeifaddrs(%struct.ifaddrs* %87)
%89 = load i32, i32* %10
store i32 %89, i32* %4
br label %90
90: ; preds = %86, %23, %18
%91 = load i32, i32* %4
ret i32 %91
}

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
define i32 @get_addr_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 %46
11: ; preds = %3
%12 = call i64 @getifaddrs(%struct.ifaddrs** %4)
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %46
14: ; preds = %11
%15 = load %struct.ifaddrs*, %struct.ifaddrs** %4
%16 = icmp eq %struct.ifaddrs* %15, null
br i1 %16, label %42, label %17
17: ; preds = %14, %38
%18 = phi %struct.ifaddrs* [ %40, %38 ], [ %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 %38, 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 %38, 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 %38
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 %38
34: ; preds = %30
%35 = bitcast %struct.ifaddrs* %18 to i8**
%36 = load i8*, i8** %35
%37 = call i32 @memcpy(i8* %2, i8* %36, i32 4)
br label %42
38: ; preds = %30, %26, %22, %17
%39 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %18, i64 0, i32 2
%40 = load %struct.ifaddrs*, %struct.ifaddrs** %39
%41 = icmp eq %struct.ifaddrs* %40, null
br i1 %41, label %42, label %17
42: ; preds = %38, %14, %34
%43 = phi i32 [ 0, %34 ], [ -1, %14 ], [ -1, %38 ]
%44 = load %struct.ifaddrs*, %struct.ifaddrs** %4
%45 = call i32 @freeifaddrs(%struct.ifaddrs* %44)
br label %46
46: ; preds = %11, %42, %9
%47 = phi i32 [ %43, %42 ], [ -1, %9 ], [ -1, %11 ]
ret i32 %47
}