#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 */

/* Type definitions */
typedef int uint16_t;
struct dirent {
  char *d_name;
};
typedef int /*<<< orphan*/ FILE;
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(int *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ perror(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, char *);
int /*<<< orphan*/ strncmp(char *, char *, int);
int has_wlan_interfaces(void) {
  struct dirent *d;
  DIR *dir = opendir("/sys/class/net");
  if (!dir) {
    perror("Unable to open /sys/class/net directory");
    return -1;
  }
  while ((d = readdir(dir))) {
    if (!strncmp(d->d_name, "wlan", 4)) {
      char buf[256];
      sprintf(buf, "/sys/class/net/%s/operstate", d->d_name);
      FILE *f = fopen(buf, "r");
      if (!f) {
        perror("Unable to open operstate file");
        continue;
      }
      uint16_t up;
      fread(&up, 2, 1, f);
      fclose(f);
      if (up == 0x7075) {
        closedir(dir);
        return 1;
      }
    }
  }
  closedir(dir);
  return 0;
}


x86 assembly (clang, -O0):
<has_wlan_interfaces>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
lea    0xe5e(%rip),%rdi
call   1060 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11ce <has_wlan_interfaces+0x3e>
lea    0xe52(%rip),%rdi
call   10a0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c6 <has_wlan_interfaces+0x136>
jmp    11d3 <has_wlan_interfaces+0x43>
mov    -0x18(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     12b6 <has_wlan_interfaces+0x126>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
lea    0xe3f(%rip),%rsi
mov    $0x4,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%eax
jne    12b1 <has_wlan_interfaces+0x121>
lea    -0x120(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe1c(%rip),%rsi
call   10b0 <sprintf@plt>
lea    -0x120(%rbp),%rdi
lea    0xe25(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x128(%rbp)
cmpq   $0x0,-0x128(%rbp)
jne    125e <has_wlan_interfaces+0xce>
lea    0xe06(%rip),%rdi
call   10a0 <perror@plt>
jmp    11d3 <has_wlan_interfaces+0x43>
mov    -0x128(%rbp),%rcx
lea    -0x12c(%rbp),%rdi
mov    $0x2,%esi
mov    $0x1,%edx
call   1040 <fread@plt>
mov    -0x128(%rbp),%rdi
call   1050 <fclose@plt>
cmpl   $0x7075,-0x12c(%rbp)
jne    12ac <has_wlan_interfaces+0x11c>
mov    -0x18(%rbp),%rdi
call   1070 <closedir@plt>
movl   $0x1,-0x4(%rbp)
jmp    12c6 <has_wlan_interfaces+0x136>
jmp    12b1 <has_wlan_interfaces+0x121>
jmp    11d3 <has_wlan_interfaces+0x43>
mov    -0x18(%rbp),%rdi
call   1070 <closedir@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<has_wlan_interfaces>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x118,%rsp
lea    0xe58(%rip),%rdi
call   1060 <opendir@plt>
test   %rax,%rax
je     129b <has_wlan_interfaces+0x10b>
mov    %rax,%rbx
mov    %rax,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
je     127f <has_wlan_interfaces+0xef>
mov    %rax,%rbp
lea    0xe63(%rip),%r14
lea    0xe61(%rip),%r15
lea    0x10(%rsp),%r12
lea    0xe71(%rip),%r13
jmp    1210 <has_wlan_interfaces+0x80>
lea    0xe6a(%rip),%rdi
call   10a0 <perror@plt>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rbx,%rdi
call   1080 <readdir@plt>
mov    %rax,%rbp
test   %rax,%rax
je     127f <has_wlan_interfaces+0xef>
mov    0x0(%rbp),%rdi
mov    %r14,%rsi
mov    $0x4,%edx
call   1030 <strncmp@plt>
test   %eax,%eax
jne    1200 <has_wlan_interfaces+0x70>
mov    0x0(%rbp),%rdx
mov    %r12,%rdi
mov    %r15,%rsi
call   10b0 <sprintf@plt>
mov    %r12,%rdi
mov    %r13,%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     11e9 <has_wlan_interfaces+0x59>
mov    %rax,%rbp
lea    0xc(%rsp),%rdi
mov    $0x2,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1040 <fread@plt>
mov    %rbp,%rdi
call   1050 <fclose@plt>
cmpl   $0x7075,0xc(%rsp)
jne    1200 <has_wlan_interfaces+0x70>
mov    %rbx,%rdi
call   1070 <closedir@plt>
mov    $0x1,%eax
jmp    1289 <has_wlan_interfaces+0xf9>
mov    %rbx,%rdi
call   1070 <closedir@plt>
xor    %eax,%eax
add    $0x118,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd6d(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%eax
jmp    1289 <has_wlan_interfaces+0xf9>

LLVM IR (-O0):
%struct.dirent = type { i8* }
@.str = [15 x i8] c"/sys/class/net\00"
@.str.1 = [40 x i8] c"Unable to open /sys/class/net directory\00"
@.str.2 = [5 x i8] c"wlan\00"
@.str.3 = [28 x i8] c"/sys/class/net/%s/operstate\00"
@.str.4 = [2 x i8] c"r\00"
@.str.5 = [30 x i8] c"Unable to open operstate file\00"
define i32 @has_wlan_interfaces() {
%1 = alloca i32
%2 = alloca %struct.dirent*
%3 = alloca i32*
%4 = alloca [256 x i8]
%5 = alloca i32*
%6 = alloca i32
%7 = call i32* @opendir(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
store i32* %7, i32** %3
%8 = load i32*, i32** %3
%9 = icmp ne i32* %8, null
br i1 %9, label %12, label %10
10: ; preds = %0
%11 = call i32 @perror(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %1
br label %50
12: ; preds = %0
br label %13
13: ; preds = %46, %33, %12
%14 = load i32*, i32** %3
%15 = call %struct.dirent* @readdir(i32* %14)
store %struct.dirent* %15, %struct.dirent** %2
%16 = icmp ne %struct.dirent* %15, null
br i1 %16, label %17, label %47
17: ; preds = %13
%18 = load %struct.dirent*, %struct.dirent** %2
%19 = getelementptr inbounds %struct.dirent, %struct.dirent* %18, i32 0, i32 0
%20 = load i8*, i8** %19
%21 = call i32 @strncmp(i8* %20, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 4)
%22 = icmp ne i32 %21, 0
br i1 %22, label %46, label %23
23: ; preds = %17
%24 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%25 = load %struct.dirent*, %struct.dirent** %2
%26 = getelementptr inbounds %struct.dirent, %struct.dirent* %25, i32 0, i32 0
%27 = load i8*, i8** %26
%28 = call i32 @sprintf(i8* %24, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i8* %27)
%29 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%30 = call i32* @fopen(i8* %29, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32* %30, i32** %5
%31 = load i32*, i32** %5
%32 = icmp ne i32* %31, null
br i1 %32, label %35, label %33
33: ; preds = %23
%34 = call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.5, i64 0, i64 0))
br label %13
35: ; preds = %23
%36 = load i32*, i32** %5
%37 = call i32 @fread(i32* %6, i32 2, i32 1, i32* %36)
%38 = load i32*, i32** %5
%39 = call i32 @fclose(i32* %38)
%40 = load i32, i32* %6
%41 = icmp eq i32 %40, 28789
br i1 %41, label %42, label %45
42: ; preds = %35
%43 = load i32*, i32** %3
%44 = call i32 @closedir(i32* %43)
store i32 1, i32* %1
br label %50
45: ; preds = %35
br label %46
46: ; preds = %45, %17
br label %13
47: ; preds = %13
%48 = load i32*, i32** %3
%49 = call i32 @closedir(i32* %48)
store i32 0, i32* %1
br label %50
50: ; preds = %47, %42, %10
%51 = load i32, i32* %1
ret i32 %51
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [15 x i8] c"/sys/class/net\00"
@.str.1 = [40 x i8] c"Unable to open /sys/class/net directory\00"
@.str.2 = [5 x i8] c"wlan\00"
@.str.3 = [28 x i8] c"/sys/class/net/%s/operstate\00"
@.str.4 = [2 x i8] c"r\00"
@.str.5 = [30 x i8] c"Unable to open operstate file\00"
define i32 @has_wlan_interfaces() {
%1 = alloca [256 x i8]
%2 = alloca i32
%3 = tail call i32* @opendir(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %11, label %5
5: ; preds = %0
%6 = tail call %struct.dirent* @readdir(i32* %3)
%7 = icmp eq %struct.dirent* %6, null
br i1 %7, label %35, label %8
8: ; preds = %5
%9 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 0
%10 = bitcast i32* %2 to i8*
br label %13
11: ; preds = %0
%12 = tail call i32 @perror(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0))
br label %39
13: ; preds = %8, %32
%14 = phi %struct.dirent* [ %6, %8 ], [ %33, %32 ]
%15 = getelementptr inbounds %struct.dirent, %struct.dirent* %14, i64 0, i32 0
%16 = load i8*, i8** %15
%17 = tail call i32 @strncmp(i8* %16, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 4)
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %32
19: ; preds = %13
%20 = load i8*, i8** %15
%21 = call i32 @sprintf(i8* %9, i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i8* %20)
%22 = call i32* @fopen(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%23 = icmp eq i32* %22, null
br i1 %23, label %24, label %26
24: ; preds = %19
%25 = tail call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.5, i64 0, i64 0))
br label %32
26: ; preds = %19
%27 = call i32 @fread(i32* %2, i32 2, i32 1, i32* %22)
%28 = tail call i32 @fclose(i32* %22)
%29 = load i32, i32* %2
%30 = icmp eq i32 %29, 28789
br i1 %30, label %37, label %31
31: ; preds = %26
br label %32
32: ; preds = %31, %24, %13
%33 = tail call %struct.dirent* @readdir(i32* %3)
%34 = icmp eq %struct.dirent* %33, null
br i1 %34, label %35, label %13
35: ; preds = %32, %5
%36 = tail call i32 @closedir(i32* %3)
br label %39
37: ; preds = %26
%38 = tail call i32 @closedir(i32* %3)
br label %39
39: ; preds = %37, %35, %11
%40 = phi i32 [ 0, %35 ], [ -1, %11 ], [ 1, %37 ]
ret i32 %40
}