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

/* Variables and functions */
int IFF_LOOPBACK;
int /*<<< orphan*/ freeifaddrs(struct ifaddrs *);
int /*<<< orphan*/ getifaddrs(struct ifaddrs **);
char *malloc(scalar_t__);
int /*<<< orphan*/ strcpy(char *, int /*<<< orphan*/);
scalar_t__ strlen(int /*<<< orphan*/);
char *sock_get_primary_if(int family) {
  struct ifaddrs *ifap, *ifa;
  char *r = 0;
  getifaddrs(&ifap);
  for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) {
    if (ifa->ifa_flags & IFF_LOOPBACK) {
      continue;
    }
    if (ifa->ifa_addr == NULL)
      continue;
    if (ifa->ifa_addr->sa_family != family)
      continue;
    r = malloc(strlen(ifa->ifa_name) + 1);
    if (r)
      strcpy(r, ifa->ifa_name);
    break;
  }
  freeifaddrs(ifap);
  return r;
}


x86 assembly (clang, -O0):
<sock_get_primary_if>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
movq   $0x0,-0x20(%rbp)
lea    -0x10(%rbp),%rdi
call   1060 <getifaddrs@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1215 <sock_get_primary_if+0xc5>
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
mov    0x2e6c(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     119c <sock_get_primary_if+0x4c>
jmp    1204 <sock_get_primary_if+0xb4>
mov    -0x18(%rbp),%rax
cmpq   $0x0,0x8(%rax)
jne    11b0 <sock_get_primary_if+0x60>
jmp    1204 <sock_get_primary_if+0xb4>
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rax
mov    (%rax),%eax
cmp    -0x4(%rbp),%eax
je     11c8 <sock_get_primary_if+0x78>
jmp    1204 <sock_get_primary_if+0xb4>
mov    -0x18(%rbp),%rax
mov    0x4(%rax),%edi
call   1040 <strlen@plt>
mov    %rax,%rdi
add    $0x1,%rdi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11ff <sock_get_primary_if+0xaf>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    0x4(%rax),%esi
call   1030 <strcpy@plt>
jmp    1215 <sock_get_primary_if+0xc5>
mov    -0x18(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x18(%rbp)
jmp    1174 <sock_get_primary_if+0x24>
mov    -0x10(%rbp),%rdi
call   1070 <freeifaddrs@plt>
mov    -0x20(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sock_get_primary_if>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebp
lea    0x8(%rsp),%rdi
call   1060 <getifaddrs@plt>
mov    0x8(%rsp),%rbx
test   %rbx,%rbx
je     11c0 <sock_get_primary_if+0x70>
mov    0x2e83(%rip),%rax
mov    (%rax),%eax
xor    %r14d,%r14d
jmp    1189 <sock_get_primary_if+0x39>
nopl   0x0(%rax)
mov    0x10(%rbx),%rbx
test   %rbx,%rbx
je     11c3 <sock_get_primary_if+0x73>
test   %eax,(%rbx)
jne    1180 <sock_get_primary_if+0x30>
mov    0x8(%rbx),%rcx
test   %rcx,%rcx
je     1180 <sock_get_primary_if+0x30>
cmp    %ebp,(%rcx)
jne    1180 <sock_get_primary_if+0x30>
mov    0x4(%rbx),%edi
call   1040 <strlen@plt>
lea    0x1(%rax),%rdi
call   1050 <malloc@plt>
test   %rax,%rax
je     11c0 <sock_get_primary_if+0x70>
mov    %rax,%r14
mov    0x4(%rbx),%esi
mov    %rax,%rdi
call   1030 <strcpy@plt>
jmp    11c3 <sock_get_primary_if+0x73>
xor    %r14d,%r14d
mov    0x8(%rsp),%rdi
call   1070 <freeifaddrs@plt>
mov    %r14,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.ifaddrs = type { i32, i32, %struct.TYPE_2__*, %struct.ifaddrs* }
%struct.TYPE_2__ = type { i32 }
@IFF_LOOPBACK = global i32 0
define i8* @sock_get_primary_if(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.ifaddrs*
%4 = alloca %struct.ifaddrs*
%5 = alloca i8*
store i32 %0, i32* %2
store i8* null, i8** %5
%6 = call i32 @getifaddrs(%struct.ifaddrs** %3)
%7 = load %struct.ifaddrs*, %struct.ifaddrs** %3
store %struct.ifaddrs* %7, %struct.ifaddrs** %4
br label %8
8: ; preds = %50, %1
%9 = load %struct.ifaddrs*, %struct.ifaddrs** %4
%10 = icmp ne %struct.ifaddrs* %9, null
br i1 %10, label %11, label %54
11: ; preds = %8
%12 = load %struct.ifaddrs*, %struct.ifaddrs** %4
%13 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %12, i32 0, i32 0
%14 = load i32, i32* %13
%15 = load i32, i32* @IFF_LOOPBACK
%16 = and i32 %14, %15
%17 = icmp ne i32 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %11
br label %50
19: ; preds = %11
%20 = load %struct.ifaddrs*, %struct.ifaddrs** %4
%21 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %20, i32 0, i32 2
%22 = load %struct.TYPE_2__*, %struct.TYPE_2__** %21
%23 = icmp eq %struct.TYPE_2__* %22, null
br i1 %23, label %24, label %25
24: ; preds = %19
br label %50
25: ; preds = %19
%26 = load %struct.ifaddrs*, %struct.ifaddrs** %4
%27 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %26, i32 0, i32 2
%28 = load %struct.TYPE_2__*, %struct.TYPE_2__** %27
%29 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %28, i32 0, i32 0
%30 = load i32, i32* %29
%31 = load i32, i32* %2
%32 = icmp ne i32 %30, %31
br i1 %32, label %33, label %34
33: ; preds = %25
br label %50
34: ; preds = %25
%35 = load %struct.ifaddrs*, %struct.ifaddrs** %4
%36 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %35, i32 0, i32 1
%37 = load i32, i32* %36
%38 = call i64 @strlen(i32 %37)
%39 = add nsw i64 %38, 1
%40 = call i8* @malloc(i64 %39)
store i8* %40, i8** %5
%41 = load i8*, i8** %5
%42 = icmp ne i8* %41, null
br i1 %42, label %43, label %49
43: ; preds = %34
%44 = load i8*, i8** %5
%45 = load %struct.ifaddrs*, %struct.ifaddrs** %4
%46 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %45, i32 0, i32 1
%47 = load i32, i32* %46
%48 = call i32 @strcpy(i8* %44, i32 %47)
br label %49
49: ; preds = %43, %34
br label %54
50: ; preds = %33, %24, %18
%51 = load %struct.ifaddrs*, %struct.ifaddrs** %4
%52 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %51, i32 0, i32 3
%53 = load %struct.ifaddrs*, %struct.ifaddrs** %52
store %struct.ifaddrs* %53, %struct.ifaddrs** %4
br label %8
54: ; preds = %49, %8
%55 = load %struct.ifaddrs*, %struct.ifaddrs** %3
%56 = call i32 @freeifaddrs(%struct.ifaddrs* %55)
%57 = load i8*, i8** %5
ret i8* %57
}

LLVM IR (-O2):
%struct.ifaddrs = type { i32, i32, %struct.TYPE_2__*, %struct.ifaddrs* }
%struct.TYPE_2__ = type { i32 }
@IFF_LOOPBACK = global i32 0
define i8* @sock_get_primary_if(i32 %0) {
%2 = alloca %struct.ifaddrs*
%3 = bitcast %struct.ifaddrs** %2 to i8*
%4 = call i32 @getifaddrs(%struct.ifaddrs** %2)
%5 = load %struct.ifaddrs*, %struct.ifaddrs** %2
%6 = icmp eq %struct.ifaddrs* %5, null
br i1 %6, label %37, label %7
7: ; preds = %1
%8 = load i32, i32* @IFF_LOOPBACK
br label %9
9: ; preds = %7, %33
%10 = phi %struct.ifaddrs* [ %5, %7 ], [ %35, %33 ]
%11 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %10, i64 0, i32 0
%12 = load i32, i32* %11
%13 = and i32 %8, %12
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %33
15: ; preds = %9
%16 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %10, i64 0, i32 2
%17 = load %struct.TYPE_2__*, %struct.TYPE_2__** %16
%18 = icmp eq %struct.TYPE_2__* %17, null
br i1 %18, label %33, label %19
19: ; preds = %15
%20 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %17, i64 0, i32 0
%21 = load i32, i32* %20
%22 = icmp eq i32 %21, %0
br i1 %22, label %23, label %33
23: ; preds = %19
%24 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %10, i64 0, i32 1
%25 = load i32, i32* %24
%26 = call i64 @strlen(i32 %25)
%27 = add nsw i64 %26, 1
%28 = call i8* @malloc(i64 %27)
%29 = icmp eq i8* %28, null
br i1 %29, label %37, label %30
30: ; preds = %23
%31 = load i32, i32* %24
%32 = call i32 @strcpy(i8* %28, i32 %31)
br label %37
33: ; preds = %19, %15, %9
%34 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %10, i64 0, i32 3
%35 = load %struct.ifaddrs*, %struct.ifaddrs** %34
%36 = icmp eq %struct.ifaddrs* %35, null
br i1 %36, label %37, label %9
37: ; preds = %33, %1, %23, %30
%38 = phi i8* [ %28, %30 ], [ null, %23 ], [ null, %1 ], [ null, %33 ]
%39 = load %struct.ifaddrs*, %struct.ifaddrs** %2
%40 = call i32 @freeifaddrs(%struct.ifaddrs* %39)
ret i8* %38
}