#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 */
struct dirent {
  int /*<<< orphan*/ d_name;
};
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ *dir_exists(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, int, char *);
int /*<<< orphan*/ strcmp(int /*<<< orphan*/, char *);
int wifi_where(char *);
int count_stations(char *dev) {
  int cnt = 0;
  DIR *fd;
  struct dirent *in;
  char dir[1024];
  sprintf(dir, "/sys/kernel/debug/ieee80211/phy%i/netdev:%s/stations",
          wifi_where(dev), dev);
  if ((fd = dir_exists(dir)) == NULL)
    return -1;
  while ((in = readdir(fd))) {
    if (!strcmp(in->d_name, "."))
      continue;
    if (!strcmp(in->d_name, ".."))
      continue;
    cnt++;
  }
  closedir(fd);
  return cnt;
}


x86 assembly (clang, -O0):
<count_stations>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
lea    -0x430(%rbp),%rax
mov    %rax,-0x438(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <wifi_where@plt>
mov    -0x438(%rbp),%rdi
mov    %eax,%edx
mov    -0x10(%rbp),%rcx
lea    0xe5f(%rip),%rsi
call   1080 <sprintf@plt>
lea    -0x430(%rbp),%rdi
call   1030 <dir_exists@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11cc <count_stations+0x6c>
movl   $0xffffffff,-0x4(%rbp)
jmp    1245 <count_stations+0xe5>
jmp    11d1 <count_stations+0x71>
mov    -0x20(%rbp),%rdi
call   1070 <readdir@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     1236 <count_stations+0xd6>
mov    -0x28(%rbp),%rax
mov    (%rax),%edi
lea    0xe41(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%eax
jne    1208 <count_stations+0xa8>
jmp    11d1 <count_stations+0x71>
mov    -0x28(%rbp),%rax
mov    (%rax),%edi
lea    0xe20(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%eax
jne    1228 <count_stations+0xc8>
jmp    11d1 <count_stations+0x71>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11d1 <count_stations+0x71>
mov    -0x20(%rbp),%rdi
call   1050 <closedir@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<count_stations>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x400,%rsp
mov    %rdi,%rbx
call   1040 <wifi_where@plt>
lea    0xe82(%rip),%rsi
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    %eax,%edx
mov    %rbx,%rcx
call   1080 <sprintf@plt>
mov    %rbp,%rdi
call   1030 <dir_exists@plt>
test   %rax,%rax
je     11ec <count_stations+0x8c>
mov    %rax,%rbx
xor    %r14d,%r14d
lea    0xe8e(%rip),%r12
lea    0xe86(%rip),%r15
nop
mov    %rbx,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
je     11e2 <count_stations+0x82>
mov    %rax,%rbp
mov    (%rax),%edi
mov    %r12,%rsi
call   1060 <strcmp@plt>
test   %eax,%eax
je     11b0 <count_stations+0x50>
mov    0x0(%rbp),%edi
mov    %r15,%rsi
call   1060 <strcmp@plt>
cmp    $0x1,%eax
sbb    $0xffffffff,%r14d
jmp    11b0 <count_stations+0x50>
mov    %rbx,%rdi
call   1050 <closedir@plt>
jmp    11f2 <count_stations+0x92>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x400,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i32 }
@.str = [53 x i8] c"/sys/kernel/debug/ieee80211/phy%i/netdev:%s/stations\00"
@.str.1 = [2 x i8] c".\00"
@.str.2 = [3 x i8] c"..\00"
define i32 @count_stations(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32*
%6 = alloca %struct.dirent*
%7 = alloca [1024 x i8]
store i8* %0, i8** %3
store i32 0, i32* %4
%8 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%9 = load i8*, i8** %3
%10 = call i32 @wifi_where(i8* %9)
%11 = load i8*, i8** %3
%12 = call i32 @sprintf(i8* %8, i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str, i64 0, i64 0), i32 %10, i8* %11)
%13 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%14 = call i32* @dir_exists(i8* %13)
store i32* %14, i32** %5
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %17
16: ; preds = %1
store i32 -1, i32* %2
br label %43
17: ; preds = %1
br label %18
18: ; preds = %36, %35, %28, %17
%19 = load i32*, i32** %5
%20 = call %struct.dirent* @readdir(i32* %19)
store %struct.dirent* %20, %struct.dirent** %6
%21 = icmp ne %struct.dirent* %20, null
br i1 %21, label %22, label %39
22: ; preds = %18
%23 = load %struct.dirent*, %struct.dirent** %6
%24 = getelementptr inbounds %struct.dirent, %struct.dirent* %23, i32 0, i32 0
%25 = load i32, i32* %24
%26 = call i32 @strcmp(i32 %25, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%27 = icmp ne i32 %26, 0
br i1 %27, label %29, label %28
28: ; preds = %22
br label %18
29: ; preds = %22
%30 = load %struct.dirent*, %struct.dirent** %6
%31 = getelementptr inbounds %struct.dirent, %struct.dirent* %30, i32 0, i32 0
%32 = load i32, i32* %31
%33 = call i32 @strcmp(i32 %32, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%34 = icmp ne i32 %33, 0
br i1 %34, label %36, label %35
35: ; preds = %29
br label %18
36: ; preds = %29
%37 = load i32, i32* %4
%38 = add nsw i32 %37, 1
store i32 %38, i32* %4
br label %18
39: ; preds = %18
%40 = load i32*, i32** %5
%41 = call i32 @closedir(i32* %40)
%42 = load i32, i32* %4
store i32 %42, i32* %2
br label %43
43: ; preds = %39, %16
%44 = load i32, i32* %2
ret i32 %44
}

LLVM IR (-O2):
%struct.dirent = type { i32 }
@.str = [53 x i8] c"/sys/kernel/debug/ieee80211/phy%i/netdev:%s/stations\00"
@.str.1 = [2 x i8] c".\00"
@.str.2 = [3 x i8] c"..\00"
define i32 @count_stations(i8* %0) {
%2 = alloca [1024 x i8]
%3 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%4 = tail call i32 @wifi_where(i8* %0)
%5 = call i32 @sprintf(i8* %3, i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @.str, i64 0, i64 0), i32 %4, i8* %0)
%6 = call i32* @dir_exists(i8* %3)
%7 = icmp eq i32* %6, null
br i1 %7, label %27, label %8
8: ; preds = %1, %18
%9 = phi i32 [ %24, %18 ], [ 0, %1 ]
br label %10
10: ; preds = %8, %13
%11 = call %struct.dirent* @readdir(i32* %6)
%12 = icmp eq %struct.dirent* %11, null
br i1 %12, label %25, label %13
13: ; preds = %10
%14 = getelementptr inbounds %struct.dirent, %struct.dirent* %11, i64 0, i32 0
%15 = load i32, i32* %14
%16 = call i32 @strcmp(i32 %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%17 = icmp eq i32 %16, 0
br i1 %17, label %10, label %18
18: ; preds = %13
%19 = getelementptr inbounds %struct.dirent, %struct.dirent* %11, i64 0, i32 0
%20 = load i32, i32* %19
%21 = call i32 @strcmp(i32 %20, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%22 = icmp ne i32 %21, 0
%23 = zext i1 %22 to i32
%24 = add nuw nsw i32 %9, %23
br label %8
25: ; preds = %10
%26 = call i32 @closedir(i32* %6)
br label %27
27: ; preds = %1, %25
%28 = phi i32 [ %9, %25 ], [ -1, %1 ]
ret i32 %28
}