#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__ {
  scalar_t__ sa_family;
};

/* Variables and functions */
scalar_t__ AF_PACKET;
scalar_t__ check_wireless(int /*<<< orphan*/);
int /*<<< orphan*/ freeifaddrs(struct ifaddrs *);
int getifaddrs(struct ifaddrs **);
char *malloc(size_t);
int /*<<< orphan*/ perror(char *);
int strlen(int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(char *, int /*<<< orphan*/, size_t);
char *find_wifi_interface(void) {
  struct ifaddrs *iface_addr;
  struct ifaddrs *iter;
  char *ret_iface = NULL;
  size_t ifa_name_len;
  if (getifaddrs(&iface_addr) == -1) {
    perror("getifaddrs");
    return NULL;
  }
  for (iter = iface_addr; iter != NULL; iter = iter->ifa_next) {
    if (iter->ifa_addr == NULL || iter->ifa_addr->sa_family != AF_PACKET) {
      continue;
    }
    if (check_wireless(iter->ifa_name)) {
      ifa_name_len = strlen(iter->ifa_name) + 1;
      ret_iface = malloc(ifa_name_len * sizeof(char));
      strncpy(ret_iface, iter->ifa_name, ifa_name_len - 1);
      ret_iface[ifa_name_len - 1] = '\0';
      break;
    }
  }
  freeifaddrs(iface_addr);
  return ret_iface;
}


x86 assembly (clang, -O0):
<find_wifi_interface>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movq   $0x0,-0x20(%rbp)
lea    -0x10(%rbp),%rdi
call   1060 <getifaddrs@plt>
cmp    $0xffffffff,%eax
jne    11ab <find_wifi_interface+0x3b>
lea    0xe67(%rip),%rdi
call   1090 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    127a <find_wifi_interface+0x10a>
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1269 <find_wifi_interface+0xf9>
mov    -0x18(%rbp),%rax
cmpq   $0x0,0x8(%rax)
je     11e8 <find_wifi_interface+0x78>
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rax
mov    (%rax),%rax
mov    0x2e01(%rip),%rcx
cmp    (%rcx),%rax
je     11ed <find_wifi_interface+0x7d>
jmp    1258 <find_wifi_interface+0xe8>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
call   1070 <check_wireless@plt>
cmp    $0x0,%rax
je     1253 <find_wifi_interface+0xe3>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
call   1040 <strlen@plt>
add    $0x1,%eax
cltq
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
shl    $0x0,%rdi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%esi
mov    -0x28(%rbp),%rdx
sub    $0x1,%rdx
call   1030 <strncpy@plt>
mov    -0x20(%rbp),%rax
mov    -0x28(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    1269 <find_wifi_interface+0xf9>
jmp    1258 <find_wifi_interface+0xe8>
mov    -0x18(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x18(%rbp)
jmp    11b3 <find_wifi_interface+0x43>
mov    -0x10(%rbp),%rdi
call   1080 <freeifaddrs@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_wifi_interface>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
lea    0x8(%rsp),%rdi
call   1060 <getifaddrs@plt>
cmp    $0xffffffff,%eax
je     1205 <find_wifi_interface+0x95>
mov    0x8(%rsp),%rbx
xor    %r15d,%r15d
test   %rbx,%rbx
je     11ee <find_wifi_interface+0x7e>
mov    0x2e44(%rip),%r14
jmp    11a9 <find_wifi_interface+0x39>
xchg   %ax,%ax
mov    0x10(%rbx),%rbx
test   %rbx,%rbx
je     11ee <find_wifi_interface+0x7e>
mov    0x8(%rbx),%rax
test   %rax,%rax
je     11a0 <find_wifi_interface+0x30>
mov    (%rax),%rax
cmp    (%r14),%rax
jne    11a0 <find_wifi_interface+0x30>
mov    (%rbx),%edi
call   1070 <check_wireless@plt>
test   %rax,%rax
je     11a0 <find_wifi_interface+0x30>
mov    (%rbx),%edi
call   1040 <strlen@plt>
movslq %eax,%r14
lea    0x1(%r14),%rdi
call   1050 <malloc@plt>
mov    %rax,%r15
mov    (%rbx),%esi
mov    %rax,%rdi
mov    %r14,%rdx
call   1030 <strncpy@plt>
movb   $0x0,(%r15,%r14,1)
mov    0x8(%rsp),%rdi
call   1080 <freeifaddrs@plt>
mov    %r15,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xdf4(%rip),%rdi
call   1090 <perror@plt>
xor    %r15d,%r15d
jmp    11f8 <find_wifi_interface+0x88>

LLVM IR (-O0):
%struct.ifaddrs = type { i32, %struct.TYPE_2__*, %struct.ifaddrs* }
%struct.TYPE_2__ = type { i64 }
@.str = [11 x i8] c"getifaddrs\00"
@AF_PACKET = global i64 0
define i8* @find_wifi_interface() {
%1 = alloca i8*
%2 = alloca %struct.ifaddrs*
%3 = alloca %struct.ifaddrs*
%4 = alloca i8*
%5 = alloca i64
store i8* null, i8** %4
%6 = call i32 @getifaddrs(%struct.ifaddrs** %2)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
store i8* null, i8** %1
br label %65
10: ; preds = %0
%11 = load %struct.ifaddrs*, %struct.ifaddrs** %2
store %struct.ifaddrs* %11, %struct.ifaddrs** %3
br label %12
12: ; preds = %57, %10
%13 = load %struct.ifaddrs*, %struct.ifaddrs** %3
%14 = icmp ne %struct.ifaddrs* %13, null
br i1 %14, label %15, label %61
15: ; preds = %12
%16 = load %struct.ifaddrs*, %struct.ifaddrs** %3
%17 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %16, i32 0, i32 1
%18 = load %struct.TYPE_2__*, %struct.TYPE_2__** %17
%19 = icmp eq %struct.TYPE_2__* %18, null
br i1 %19, label %28, label %20
20: ; preds = %15
%21 = load %struct.ifaddrs*, %struct.ifaddrs** %3
%22 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %21, i32 0, i32 1
%23 = load %struct.TYPE_2__*, %struct.TYPE_2__** %22
%24 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %23, i32 0, i32 0
%25 = load i64, i64* %24
%26 = load i64, i64* @AF_PACKET
%27 = icmp ne i64 %25, %26
br i1 %27, label %28, label %29
28: ; preds = %20, %15
br label %57
29: ; preds = %20
%30 = load %struct.ifaddrs*, %struct.ifaddrs** %3
%31 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %30, i32 0, i32 0
%32 = load i32, i32* %31
%33 = call i64 @check_wireless(i32 %32)
%34 = icmp ne i64 %33, 0
br i1 %34, label %35, label %56
35: ; preds = %29
%36 = load %struct.ifaddrs*, %struct.ifaddrs** %3
%37 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %36, i32 0, i32 0
%38 = load i32, i32* %37
%39 = call i32 @strlen(i32 %38)
%40 = add nsw i32 %39, 1
%41 = sext i32 %40 to i64
store i64 %41, i64* %5
%42 = load i64, i64* %5
%43 = mul i64 %42, 1
%44 = call i8* @malloc(i64 %43)
store i8* %44, i8** %4
%45 = load i8*, i8** %4
%46 = load %struct.ifaddrs*, %struct.ifaddrs** %3
%47 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %46, i32 0, i32 0
%48 = load i32, i32* %47
%49 = load i64, i64* %5
%50 = sub i64 %49, 1
%51 = call i32 @strncpy(i8* %45, i32 %48, i64 %50)
%52 = load i8*, i8** %4
%53 = load i64, i64* %5
%54 = sub i64 %53, 1
%55 = getelementptr inbounds i8, i8* %52, i64 %54
store i8 0, i8* %55
br label %61
56: ; preds = %29
br label %57
57: ; preds = %56, %28
%58 = load %struct.ifaddrs*, %struct.ifaddrs** %3
%59 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %58, i32 0, i32 2
%60 = load %struct.ifaddrs*, %struct.ifaddrs** %59
store %struct.ifaddrs* %60, %struct.ifaddrs** %3
br label %12
61: ; preds = %35, %12
%62 = load %struct.ifaddrs*, %struct.ifaddrs** %2
%63 = call i32 @freeifaddrs(%struct.ifaddrs* %62)
%64 = load i8*, i8** %4
store i8* %64, i8** %1
br label %65
65: ; preds = %61, %8
%66 = load i8*, i8** %1
ret i8* %66
}

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