#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 /*<<< orphan*/ DIR;

/* Variables and functions */
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ *dir_exists(char *);
int /*<<< orphan*/ sprintf(char *, char *, int, char *);
int wifi_where(char *dev) {
  char buf[1024];
  DIR *fd;
  for (int i = 0; i < 10; i++) {
    sprintf(buf, "/sys/kernel/debug/ieee80211/phy%i/netdev:%s/stations", i,
            dev);
    if ((fd = dir_exists(buf)) != NULL) {
      closedir(fd);
      return i;
    }
  }
  return -1;
}


x86 assembly (clang, -O0):
<wifi_where>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x41c(%rbp)
cmpl   $0xa,-0x41c(%rbp)
jge    11c3 <wifi_where+0x93>
lea    -0x410(%rbp),%rdi
mov    -0x41c(%rbp),%edx
mov    -0x10(%rbp),%rcx
lea    0xe92(%rip),%rsi
call   1050 <sprintf@plt>
lea    -0x410(%rbp),%rdi
call   1030 <dir_exists@plt>
mov    %rax,-0x418(%rbp)
cmp    $0x0,%rax
je     11aa <wifi_where+0x7a>
mov    -0x418(%rbp),%rdi
call   1040 <closedir@plt>
mov    -0x41c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11ca <wifi_where+0x9a>
jmp    11af <wifi_where+0x7f>
mov    -0x41c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x41c(%rbp)
jmp    1149 <wifi_where+0x19>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wifi_where>:
push   %rbp
push   %r14
push   %rbx
sub    $0x400,%rsp
mov    %rdi,%r14
lea    0xebb(%rip),%rsi
mov    %rsp,%rbx
xor    %ebp,%ebp
mov    %rbx,%rdi
xor    %edx,%edx
mov    %r14,%rcx
call   1050 <sprintf@plt>
mov    %rbx,%rdi
call   1030 <dir_exists@plt>
test   %rax,%rax
jne    130c <wifi_where+0x1dc>
lea    0xe91(%rip),%rsi
mov    %rsp,%rbx
mov    $0x1,%ebp
mov    %rbx,%rdi
mov    $0x1,%edx
mov    %r14,%rcx
call   1050 <sprintf@plt>
mov    %rbx,%rdi
call   1030 <dir_exists@plt>
test   %rax,%rax
jne    130c <wifi_where+0x1dc>
lea    0xe61(%rip),%rsi
mov    %rsp,%rbx
mov    $0x2,%ebp
mov    %rbx,%rdi
mov    $0x2,%edx
mov    %r14,%rcx
call   1050 <sprintf@plt>
mov    %rbx,%rdi
call   1030 <dir_exists@plt>
test   %rax,%rax
jne    130c <wifi_where+0x1dc>
lea    0xe31(%rip),%rsi
mov    %rsp,%rbx
mov    $0x3,%ebp
mov    %rbx,%rdi
mov    $0x3,%edx
mov    %r14,%rcx
call   1050 <sprintf@plt>
mov    %rbx,%rdi
call   1030 <dir_exists@plt>
test   %rax,%rax
jne    130c <wifi_where+0x1dc>
lea    0xe01(%rip),%rsi
mov    %rsp,%rbx
mov    $0x4,%ebp
mov    %rbx,%rdi
mov    $0x4,%edx
mov    %r14,%rcx
call   1050 <sprintf@plt>
mov    %rbx,%rdi
call   1030 <dir_exists@plt>
test   %rax,%rax
jne    130c <wifi_where+0x1dc>
lea    0xdd1(%rip),%rsi
mov    %rsp,%rbx
mov    $0x5,%ebp
mov    %rbx,%rdi
mov    $0x5,%edx
mov    %r14,%rcx
call   1050 <sprintf@plt>
mov    %rbx,%rdi
call   1030 <dir_exists@plt>
test   %rax,%rax
jne    130c <wifi_where+0x1dc>
lea    0xda1(%rip),%rsi
mov    %rsp,%rbx
mov    $0x6,%ebp
mov    %rbx,%rdi
mov    $0x6,%edx
mov    %r14,%rcx
call   1050 <sprintf@plt>
mov    %rbx,%rdi
call   1030 <dir_exists@plt>
test   %rax,%rax
jne    130c <wifi_where+0x1dc>
lea    0xd71(%rip),%rsi
mov    %rsp,%rbx
mov    $0x7,%ebp
mov    %rbx,%rdi
mov    $0x7,%edx
mov    %r14,%rcx
call   1050 <sprintf@plt>
mov    %rbx,%rdi
call   1030 <dir_exists@plt>
test   %rax,%rax
jne    130c <wifi_where+0x1dc>
lea    0xd45(%rip),%rsi
mov    %rsp,%rbx
mov    $0x8,%ebp
mov    %rbx,%rdi
mov    $0x8,%edx
mov    %r14,%rcx
call   1050 <sprintf@plt>
mov    %rbx,%rdi
call   1030 <dir_exists@plt>
test   %rax,%rax
jne    130c <wifi_where+0x1dc>
lea    0xd19(%rip),%rsi
mov    %rsp,%rbx
mov    $0x9,%ebp
mov    %rbx,%rdi
mov    $0x9,%edx
mov    %r14,%rcx
call   1050 <sprintf@plt>
mov    %rbx,%rdi
call   1030 <dir_exists@plt>
test   %rax,%rax
je     1322 <wifi_where+0x1f2>
mov    %rax,%rdi
call   1040 <closedir@plt>
mov    %ebp,%eax
add    $0x400,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    $0xffffffff,%ebp
jmp    1314 <wifi_where+0x1e4>

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

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