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

/* Variables and functions */
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*/);
scalar_t__ strncmp(int /*<<< orphan*/, char *, int);
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 (strncmp(ifa->ifa_name, "lo", 2) == 0)
      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   1070 <getifaddrs@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     122c <sock_get_primary_if+0xcc>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
lea    0xe64(%rip),%rsi
mov    $0x2,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    11b5 <sock_get_primary_if+0x55>
jmp    121b <sock_get_primary_if+0xbb>
mov    -0x18(%rbp),%rax
cmpq   $0x0,0x8(%rax)
jne    11c9 <sock_get_primary_if+0x69>
jmp    121b <sock_get_primary_if+0xbb>
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rax
mov    (%rax),%eax
cmp    -0x4(%rbp),%eax
je     11e1 <sock_get_primary_if+0x81>
jmp    121b <sock_get_primary_if+0xbb>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
call   1050 <strlen@plt>
mov    %rax,%rdi
add    $0x1,%rdi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1216 <sock_get_primary_if+0xb6>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%esi
call   1040 <strcpy@plt>
jmp    122c <sock_get_primary_if+0xcc>
mov    -0x18(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x18(%rbp)
jmp    1184 <sock_get_primary_if+0x24>
mov    -0x10(%rbp),%rdi
call   1080 <freeifaddrs@plt>
mov    -0x20(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sock_get_primary_if>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%r15d
mov    %rsp,%rdi
call   1070 <getifaddrs@plt>
mov    (%rsp),%rbp
test   %rbp,%rbp
je     11e2 <sock_get_primary_if+0x82>
lea    0xe7e(%rip),%rbx
xor    %r14d,%r14d
jmp    1199 <sock_get_primary_if+0x39>
nopw   0x0(%rax,%rax,1)
mov    0x10(%rbp),%rbp
test   %rbp,%rbp
je     11e5 <sock_get_primary_if+0x85>
mov    0x0(%rbp),%edi
mov    %rbx,%rsi
mov    $0x2,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
je     1190 <sock_get_primary_if+0x30>
mov    0x8(%rbp),%rax
test   %rax,%rax
je     1190 <sock_get_primary_if+0x30>
cmp    %r15d,(%rax)
jne    1190 <sock_get_primary_if+0x30>
mov    0x0(%rbp),%edi
call   1050 <strlen@plt>
lea    0x1(%rax),%rdi
call   1060 <malloc@plt>
test   %rax,%rax
je     11e2 <sock_get_primary_if+0x82>
mov    %rax,%r14
mov    0x0(%rbp),%esi
mov    %rax,%rdi
call   1040 <strcpy@plt>
jmp    11e5 <sock_get_primary_if+0x85>
xor    %r14d,%r14d
mov    (%rsp),%rdi
call   1080 <freeifaddrs@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.ifaddrs = type { i32, %struct.TYPE_2__*, %struct.ifaddrs* }
%struct.TYPE_2__ = type { i32 }
@.str = [3 x i8] c"lo\00"
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 = %49, %1
%9 = load %struct.ifaddrs*, %struct.ifaddrs** %4
%10 = icmp ne %struct.ifaddrs* %9, null
br i1 %10, label %11, label %53
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 = call i64 @strncmp(i32 %14, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 2)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %11
br label %49
18: ; preds = %11
%19 = load %struct.ifaddrs*, %struct.ifaddrs** %4
%20 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %19, i32 0, i32 1
%21 = load %struct.TYPE_2__*, %struct.TYPE_2__** %20
%22 = icmp eq %struct.TYPE_2__* %21, null
br i1 %22, label %23, label %24
23: ; preds = %18
br label %49
24: ; preds = %18
%25 = load %struct.ifaddrs*, %struct.ifaddrs** %4
%26 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %25, i32 0, i32 1
%27 = load %struct.TYPE_2__*, %struct.TYPE_2__** %26
%28 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %27, i32 0, i32 0
%29 = load i32, i32* %28
%30 = load i32, i32* %2
%31 = icmp ne i32 %29, %30
br i1 %31, label %32, label %33
32: ; preds = %24
br label %49
33: ; preds = %24
%34 = load %struct.ifaddrs*, %struct.ifaddrs** %4
%35 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %34, i32 0, i32 0
%36 = load i32, i32* %35
%37 = call i64 @strlen(i32 %36)
%38 = add nsw i64 %37, 1
%39 = call i8* @malloc(i64 %38)
store i8* %39, i8** %5
%40 = load i8*, i8** %5
%41 = icmp ne i8* %40, null
br i1 %41, label %42, label %48
42: ; preds = %33
%43 = load i8*, i8** %5
%44 = load %struct.ifaddrs*, %struct.ifaddrs** %4
%45 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %44, i32 0, i32 0
%46 = load i32, i32* %45
%47 = call i32 @strcpy(i8* %43, i32 %46)
br label %48
48: ; preds = %42, %33
br label %53
49: ; preds = %32, %23, %17
%50 = load %struct.ifaddrs*, %struct.ifaddrs** %4
%51 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %50, i32 0, i32 2
%52 = load %struct.ifaddrs*, %struct.ifaddrs** %51
store %struct.ifaddrs* %52, %struct.ifaddrs** %4
br label %8
53: ; preds = %48, %8
%54 = load %struct.ifaddrs*, %struct.ifaddrs** %3
%55 = call i32 @freeifaddrs(%struct.ifaddrs* %54)
%56 = load i8*, i8** %5
ret i8* %56
}

LLVM IR (-O2):
%struct.ifaddrs = type { i32, %struct.TYPE_2__*, %struct.ifaddrs* }
%struct.TYPE_2__ = type { i32 }
@.str = [3 x i8] c"lo\00"
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 %35, label %7
7: ; preds = %1, %31
%8 = phi %struct.ifaddrs* [ %33, %31 ], [ %5, %1 ]
%9 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %8, i64 0, i32 0
%10 = load i32, i32* %9
%11 = call i64 @strncmp(i32 %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 2)
%12 = icmp eq i64 %11, 0
br i1 %12, label %31, label %13
13: ; preds = %7
%14 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %8, i64 0, i32 1
%15 = load %struct.TYPE_2__*, %struct.TYPE_2__** %14
%16 = icmp eq %struct.TYPE_2__* %15, null
br i1 %16, label %31, label %17
17: ; preds = %13
%18 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %15, i64 0, i32 0
%19 = load i32, i32* %18
%20 = icmp eq i32 %19, %0
br i1 %20, label %21, label %31
21: ; preds = %17
%22 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %8, i64 0, i32 0
%23 = load i32, i32* %22
%24 = call i64 @strlen(i32 %23)
%25 = add nsw i64 %24, 1
%26 = call i8* @malloc(i64 %25)
%27 = icmp eq i8* %26, null
br i1 %27, label %35, label %28
28: ; preds = %21
%29 = load i32, i32* %22
%30 = call i32 @strcpy(i8* %26, i32 %29)
br label %35
31: ; preds = %17, %13, %7
%32 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %8, i64 0, i32 2
%33 = load %struct.ifaddrs*, %struct.ifaddrs** %32
%34 = icmp eq %struct.ifaddrs* %33, null
br i1 %34, label %35, label %7
35: ; preds = %31, %1, %21, %28
%36 = phi i8* [ %26, %28 ], [ null, %21 ], [ null, %1 ], [ null, %31 ]
%37 = load %struct.ifaddrs*, %struct.ifaddrs** %2
%38 = call i32 @freeifaddrs(%struct.ifaddrs* %37)
ret i8* %36
}