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

/* Variables and functions */
int AF_INET;
int AF_INET6;
int /*<<< orphan*/ freeifaddrs(struct ifaddrs *);
scalar_t__ getifaddrs(struct ifaddrs **);
int /*<<< orphan*/ memcpy(struct sockaddr *, TYPE_1__ *, int /*<<< orphan*/);
scalar_t__ strcmp(char *, int /*<<< orphan*/);
int get_iface_ip(struct sockaddr *addr, socklen_t addrlen, char *iface,
                 int domain) {
  if (addr == NULL || iface == NULL ||
      (domain != AF_INET && domain != AF_INET6)) {
    return -1;
  }
  struct ifaddrs *ifap = NULL;
  int ret = -1;
  if (getifaddrs(&ifap)) {
    return ret;
  }
  struct ifaddrs *ifptr = ifap;
  while (ifptr) {
    if (strcmp(iface, ifptr->ifa_name) == 0 && ifptr->ifa_addr != NULL &&
        ifptr->ifa_addr->sa_family == domain) {
      addr->sa_family = domain;
      memcpy(addr, ifptr->ifa_addr, addrlen);
      ret = 0;
      break;
    }
    ifptr = ifptr->ifa_next;
  }
  freeifaddrs(ifap);
  return ret;
}


x86 assembly (clang, -O0):
<get_iface_ip>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1190 <get_iface_ip+0x50>
cmpq   $0x0,-0x20(%rbp)
je     1190 <get_iface_ip+0x50>
mov    -0x24(%rbp),%eax
mov    0x2e72(%rip),%rcx
cmp    (%rcx),%eax
je     119c <get_iface_ip+0x5c>
mov    -0x24(%rbp),%eax
mov    0x2e58(%rip),%rcx
cmp    (%rcx),%eax
je     119c <get_iface_ip+0x5c>
movl   $0xffffffff,-0x4(%rbp)
jmp    1260 <get_iface_ip+0x120>
movq   $0x0,-0x30(%rbp)
movl   $0xffffffff,-0x34(%rbp)
lea    -0x30(%rbp),%rdi
call   1050 <getifaddrs@plt>
cmp    $0x0,%rax
je     11c9 <get_iface_ip+0x89>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1260 <get_iface_ip+0x120>
mov    -0x30(%rbp),%rax
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
je     1251 <get_iface_ip+0x111>
mov    -0x20(%rbp),%rdi
mov    -0x40(%rbp),%rax
mov    0x10(%rax),%esi
call   1030 <strcmp@plt>
cmp    $0x0,%rax
jne    1241 <get_iface_ip+0x101>
mov    -0x40(%rbp),%rax
cmpq   $0x0,0x8(%rax)
je     1241 <get_iface_ip+0x101>
mov    -0x40(%rbp),%rax
mov    0x8(%rax),%rax
mov    (%rax),%eax
cmp    -0x24(%rbp),%eax
jne    1241 <get_iface_ip+0x101>
mov    -0x24(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rdi
mov    -0x40(%rbp),%rax
mov    0x8(%rax),%rsi
mov    -0x14(%rbp),%edx
call   1040 <memcpy@plt>
movl   $0x0,-0x34(%rbp)
jmp    1251 <get_iface_ip+0x111>
mov    -0x40(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x40(%rbp)
jmp    11d1 <get_iface_ip+0x91>
mov    -0x30(%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_iface_ip>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    $0xffffffff,%r12d
test   %rdi,%rdi
je     11f1 <get_iface_ip+0xb1>
mov    %rdx,%rbx
test   %rdx,%rdx
je     11f1 <get_iface_ip+0xb1>
mov    %ecx,%r13d
mov    %esi,%r14d
mov    %rdi,%r15
mov    0x2e72(%rip),%rax
cmp    %ecx,(%rax)
je     1186 <get_iface_ip+0x46>
mov    0x2e5f(%rip),%rax
cmp    %r13d,(%rax)
jne    11f1 <get_iface_ip+0xb1>
movq   $0x0,(%rsp)
mov    %rsp,%rdi
call   1050 <getifaddrs@plt>
mov    $0xffffffff,%r12d
test   %rax,%rax
jne    11f1 <get_iface_ip+0xb1>
mov    (%rsp),%rbp
mov    $0xffffffff,%r12d
jmp    11b4 <get_iface_ip+0x74>
nopl   (%rax)
mov    0x0(%rbp),%rbp
test   %rbp,%rbp
je     11e8 <get_iface_ip+0xa8>
mov    0x10(%rbp),%esi
mov    %rbx,%rdi
call   1030 <strcmp@plt>
test   %rax,%rax
jne    11b0 <get_iface_ip+0x70>
mov    0x8(%rbp),%rsi
test   %rsi,%rsi
je     11b0 <get_iface_ip+0x70>
cmp    %r13d,(%rsi)
jne    11b0 <get_iface_ip+0x70>
mov    %r13d,(%r15)
mov    %r15,%rdi
mov    %r14d,%edx
call   1040 <memcpy@plt>
xor    %r12d,%r12d
mov    (%rsp),%rdi
call   1060 <freeifaddrs@plt>
mov    %r12d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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