#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 dummy;
};
struct ifaddrs {
  int ifa_flags;
  int /*<<< orphan*/ 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 IFF_LOOPBACK;
int IFF_UP;
int /*<<< orphan*/ freeifaddrs(struct ifaddrs *);
scalar_t__ getifaddrs(struct ifaddrs **);
int /*<<< orphan*/ memcpy(struct sockaddr *, TYPE_1__ *, int);
scalar_t__ strcmp(char const *, int /*<<< orphan*/);
char *strdup(int /*<<< orphan*/);
struct sockaddr *iface_to_sockaddr(char const **iface_name) {
  static struct sockaddr ret;
  struct sockaddr *r;
  struct ifaddrs *ifap;
  ;
  if (getifaddrs(&ifap) < 0) {
    return NULL;
  }
  r = NULL;
  struct ifaddrs *iface;
  for (iface = ifap; iface; iface = iface->ifa_next) {
    if (!iface->ifa_addr) {
      continue;
    }
    if (iface->ifa_addr->sa_family != AF_INET ||
        (iface->ifa_flags & IFF_LOOPBACK) == IFF_LOOPBACK ||
        (iface->ifa_flags & IFF_UP) != IFF_UP)
      continue;
    if (*iface_name && strcmp(*iface_name, iface->ifa_name))
      continue;
    ;
    r = &ret;
    memcpy(&ret, iface->ifa_addr, sizeof(struct sockaddr));
    if (!*iface_name) {
      *iface_name = strdup(iface->ifa_name);
    }
    break;
  }
  freeifaddrs(ifap);
  return r;
}


x86 assembly (clang, -O0):
<iface_to_sockaddr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0x20(%rbp),%rdi
call   1050 <getifaddrs@plt>
cmp    $0x0,%rax
jge    117c <iface_to_sockaddr+0x2c>
movq   $0x0,-0x8(%rbp)
jmp    12a6 <iface_to_sockaddr+0x156>
movq   $0x0,-0x18(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1295 <iface_to_sockaddr+0x145>
mov    -0x28(%rbp),%rax
cmpq   $0x0,0x8(%rax)
jne    11ab <iface_to_sockaddr+0x5b>
jmp    1284 <iface_to_sockaddr+0x134>
mov    -0x28(%rbp),%rax
mov    0x8(%rax),%rax
mov    (%rax),%rax
mov    0x2e23(%rip),%rcx
cmp    (%rcx),%rax
jne    1202 <iface_to_sockaddr+0xb2>
mov    -0x28(%rbp),%rax
mov    (%rax),%eax
mov    0x2e25(%rip),%rcx
and    (%rcx),%eax
mov    0x2e1c(%rip),%rcx
cmp    (%rcx),%eax
je     1202 <iface_to_sockaddr+0xb2>
mov    -0x28(%rbp),%rax
mov    (%rax),%eax
mov    0x2de7(%rip),%rcx
and    (%rcx),%eax
mov    0x2dde(%rip),%rcx
cmp    (%rcx),%eax
je     1207 <iface_to_sockaddr+0xb7>
jmp    1284 <iface_to_sockaddr+0x134>
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
je     1237 <iface_to_sockaddr+0xe7>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x28(%rbp),%rax
mov    0x4(%rax),%esi
call   1030 <strcmp@plt>
cmp    $0x0,%rax
je     1237 <iface_to_sockaddr+0xe7>
jmp    1284 <iface_to_sockaddr+0x134>
lea    0x2e12(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0x28(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0x2dff(%rip),%rdi
mov    $0x4,%edx
call   1040 <memcpy@plt>
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
jne    127f <iface_to_sockaddr+0x12f>
mov    -0x28(%rbp),%rax
mov    0x4(%rax),%edi
call   1070 <strdup@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
jmp    1295 <iface_to_sockaddr+0x145>
mov    -0x28(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x28(%rbp)
jmp    118c <iface_to_sockaddr+0x3c>
mov    -0x20(%rbp),%rdi
call   1060 <freeifaddrs@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<iface_to_sockaddr>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
mov    %rsp,%rdi
call   1050 <getifaddrs@plt>
test   %rax,%rax
js     1213 <iface_to_sockaddr+0xc3>
mov    (%rsp),%rbx
xor    %r15d,%r15d
test   %rbx,%rbx
je     1208 <iface_to_sockaddr+0xb8>
mov    0x2e5a(%rip),%r12
mov    0x2e6b(%rip),%r13
mov    0x2e44(%rip),%rbp
jmp    11a9 <iface_to_sockaddr+0x59>
cs nopw 0x0(%rax,%rax,1)
mov    0x10(%rbx),%rbx
test   %rbx,%rbx
je     1208 <iface_to_sockaddr+0xb8>
mov    0x8(%rbx),%rsi
test   %rsi,%rsi
je     11a0 <iface_to_sockaddr+0x50>
mov    (%rsi),%rax
cmp    (%r12),%rax
jne    11a0 <iface_to_sockaddr+0x50>
mov    (%rbx),%eax
not    %eax
test   %eax,0x0(%r13)
je     11a0 <iface_to_sockaddr+0x50>
test   %eax,0x0(%rbp)
jne    11a0 <iface_to_sockaddr+0x50>
mov    (%r14),%rdi
test   %rdi,%rdi
je     11e3 <iface_to_sockaddr+0x93>
mov    0x4(%rbx),%esi
call   1030 <strcmp@plt>
test   %rax,%rax
jne    11a0 <iface_to_sockaddr+0x50>
mov    0x8(%rbx),%rsi
lea    0x2e66(%rip),%r15
mov    %r15,%rdi
mov    $0x4,%edx
call   1040 <memcpy@plt>
cmpq   $0x0,(%r14)
jne    1208 <iface_to_sockaddr+0xb8>
mov    0x4(%rbx),%edi
call   1070 <strdup@plt>
mov    %rax,(%r14)
mov    (%rsp),%rdi
call   1060 <freeifaddrs@plt>
jmp    1216 <iface_to_sockaddr+0xc6>
xor    %r15d,%r15d
mov    %r15,%rax
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 { i32, i32, %struct.TYPE_2__*, %struct.ifaddrs* }
%struct.TYPE_2__ = type { i64 }
@iface_to_sockaddr.ret = internal global %struct.sockaddr zeroinitializer
@AF_INET = global i64 0
@IFF_LOOPBACK = global i32 0
@IFF_UP = global i32 0
define %struct.sockaddr* @iface_to_sockaddr(i8** %0) {
%2 = alloca %struct.sockaddr*
%3 = alloca i8**
%4 = alloca %struct.sockaddr*
%5 = alloca %struct.ifaddrs*
%6 = alloca %struct.ifaddrs*
store i8** %0, i8*** %3
%7 = call i64 @getifaddrs(%struct.ifaddrs** %5)
%8 = icmp slt i64 %7, 0
br i1 %8, label %9, label %10
9: ; preds = %1
store %struct.sockaddr* null, %struct.sockaddr** %2
br label %82
10: ; preds = %1
store %struct.sockaddr* null, %struct.sockaddr** %4
%11 = load %struct.ifaddrs*, %struct.ifaddrs** %5
store %struct.ifaddrs* %11, %struct.ifaddrs** %6
br label %12
12: ; preds = %74, %10
%13 = load %struct.ifaddrs*, %struct.ifaddrs** %6
%14 = icmp ne %struct.ifaddrs* %13, null
br i1 %14, label %15, label %78
15: ; preds = %12
%16 = load %struct.ifaddrs*, %struct.ifaddrs** %6
%17 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %16, i32 0, i32 2
%18 = load %struct.TYPE_2__*, %struct.TYPE_2__** %17
%19 = icmp ne %struct.TYPE_2__* %18, null
br i1 %19, label %21, label %20
20: ; preds = %15
br label %74
21: ; preds = %15
%22 = load %struct.ifaddrs*, %struct.ifaddrs** %6
%23 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %22, i32 0, i32 2
%24 = load %struct.TYPE_2__*, %struct.TYPE_2__** %23
%25 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %24, i32 0, i32 0
%26 = load i64, i64* %25
%27 = load i64, i64* @AF_INET
%28 = icmp ne i64 %26, %27
br i1 %28, label %45, label %29
29: ; preds = %21
%30 = load %struct.ifaddrs*, %struct.ifaddrs** %6
%31 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %30, i32 0, i32 0
%32 = load i32, i32* %31
%33 = load i32, i32* @IFF_LOOPBACK
%34 = and i32 %32, %33
%35 = load i32, i32* @IFF_LOOPBACK
%36 = icmp eq i32 %34, %35
br i1 %36, label %45, label %37
37: ; preds = %29
%38 = load %struct.ifaddrs*, %struct.ifaddrs** %6
%39 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %38, i32 0, i32 0
%40 = load i32, i32* %39
%41 = load i32, i32* @IFF_UP
%42 = and i32 %40, %41
%43 = load i32, i32* @IFF_UP
%44 = icmp ne i32 %42, %43
br i1 %44, label %45, label %46
45: ; preds = %37, %29, %21
br label %74
46: ; preds = %37
%47 = load i8**, i8*** %3
%48 = load i8*, i8** %47
%49 = icmp ne i8* %48, null
br i1 %49, label %50, label %59
50: ; preds = %46
%51 = load i8**, i8*** %3
%52 = load i8*, i8** %51
%53 = load %struct.ifaddrs*, %struct.ifaddrs** %6
%54 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %53, i32 0, i32 1
%55 = load i32, i32* %54
%56 = call i64 @strcmp(i8* %52, i32 %55)
%57 = icmp ne i64 %56, 0
br i1 %57, label %58, label %59
58: ; preds = %50
br label %74
59: ; preds = %50, %46
store %struct.sockaddr* @iface_to_sockaddr.ret, %struct.sockaddr** %4
%60 = load %struct.ifaddrs*, %struct.ifaddrs** %6
%61 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %60, i32 0, i32 2
%62 = load %struct.TYPE_2__*, %struct.TYPE_2__** %61
%63 = call i32 @memcpy(%struct.sockaddr* @iface_to_sockaddr.ret, %struct.TYPE_2__* %62, i32 4)
%64 = load i8**, i8*** %3
%65 = load i8*, i8** %64
%66 = icmp ne i8* %65, null
br i1 %66, label %73, label %67
67: ; preds = %59
%68 = load %struct.ifaddrs*, %struct.ifaddrs** %6
%69 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %68, i32 0, i32 1
%70 = load i32, i32* %69
%71 = call i8* @strdup(i32 %70)
%72 = load i8**, i8*** %3
store i8* %71, i8** %72
br label %73
73: ; preds = %67, %59
br label %78
74: ; preds = %58, %45, %20
%75 = load %struct.ifaddrs*, %struct.ifaddrs** %6
%76 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %75, i32 0, i32 3
%77 = load %struct.ifaddrs*, %struct.ifaddrs** %76
store %struct.ifaddrs* %77, %struct.ifaddrs** %6
br label %12
78: ; preds = %73, %12
%79 = load %struct.ifaddrs*, %struct.ifaddrs** %5
%80 = call i32 @freeifaddrs(%struct.ifaddrs* %79)
%81 = load %struct.sockaddr*, %struct.sockaddr** %4
store %struct.sockaddr* %81, %struct.sockaddr** %2
br label %82
82: ; preds = %78, %9
%83 = load %struct.sockaddr*, %struct.sockaddr** %2
ret %struct.sockaddr* %83
}

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