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

/* Variables and functions */
int EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
struct dirent *readdir(int /*<<< orphan*/ *);
scalar_t__ strcmp(char *, char *);
int discovery() {
  DIR *dir;
  struct dirent *ent;
  bool comma = false;
  if ((dir = opendir("/sys/kernel/debug/ieee80211/")) != NULL) {
    printf("{\"data\":[");
    while ((ent = readdir(dir)) != NULL) {
      if (strcmp(".", ent->d_name) && strcmp("..", ent->d_name)) {
        if (comma)
          printf(",");
        printf("{\"{#PHY}\":\"%s\"}", ent->d_name);
        comma = true;
      }
    }
    printf("]}\n");
    closedir(dir);
  } else {
    perror("");
    return EXIT_FAILURE;
  }
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<discovery>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x1c(%rbp)
lea    0xe8a(%rip),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     123d <discovery+0xdd>
lea    0xe8d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1070 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     1221 <discovery+0xc1>
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
lea    0xe6c(%rip),%rdi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
je     121c <discovery+0xbc>
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
lea    0xe4e(%rip),%rdi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
je     121c <discovery+0xbc>
cmpl   $0x0,-0x1c(%rbp)
je     1200 <discovery+0xa0>
lea    0xe31(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
lea    0xe1e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x1c(%rbp)
jmp    1197 <discovery+0x37>
lea    0xe14(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1050 <closedir@plt>
jmp    125a <discovery+0xfa>
lea    0xdfb(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d90(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1266 <discovery+0x106>
mov    0x2d77(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<discovery>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
lea    0xe6e(%rip),%rdi
call   1050 <opendir@plt>
test   %rax,%rax
je     125e <discovery+0xde>
mov    %rax,%r13
lea    0xe73(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r13,%rdi
call   1090 <readdir@plt>
test   %rax,%rax
je     1232 <discovery+0xb2>
mov    %rax,%rbx
lea    0xe60(%rip),%r14
lea    0xe58(%rip),%r15
lea    0xe54(%rip),%r12
xor    %ebp,%ebp
jmp    1202 <discovery+0x82>
nopw   0x0(%rax,%rax,1)
mov    (%rbx),%rsi
mov    %r12,%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x1,%ebp
mov    %r13,%rdi
call   1090 <readdir@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1232 <discovery+0xb2>
mov    (%rbx),%rsi
mov    %r14,%rdi
call   1080 <strcmp@plt>
test   %rax,%rax
je     11f2 <discovery+0x72>
mov    (%rbx),%rsi
mov    %r15,%rdi
call   1080 <strcmp@plt>
test   %rax,%rax
je     11f2 <discovery+0x72>
test   %ebp,%ebp
je     11e0 <discovery+0x60>
mov    $0x2c,%edi
call   1030 <putchar@plt>
jmp    11e0 <discovery+0x60>
lea    0xe01(%rip),%rdi
call   1040 <puts@plt>
mov    %r13,%rdi
call   1070 <closedir@plt>
mov    0x2d8b(%rip),%rax
mov    (%rax),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdc4(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2d6f(%rip),%rax
jmp    124d <discovery+0xcd>

LLVM IR (-O0):
%struct.dirent = type { i8* }
@.str = [29 x i8] c"/sys/kernel/debug/ieee80211/\00"
@.str.1 = [10 x i8] c"{\22data\22:[\00"
@.str.2 = [2 x i8] c".\00"
@.str.3 = [3 x i8] c"..\00"
@.str.4 = [2 x i8] c",\00"
@.str.5 = [16 x i8] c"{\22{#PHY}\22:\22%s\22}\00"
@.str.6 = [4 x i8] c"]}\0A\00"
@.str.7 = [1 x i8] zeroinitializer
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @discovery() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca %struct.dirent*
%4 = alloca i32
store i32 0, i32* %4
%5 = call i32* @opendir(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
store i32* %5, i32** %2
%6 = icmp ne i32* %5, null
br i1 %6, label %7, label %40
7: ; preds = %0
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
br label %9
9: ; preds = %35, %7
%10 = load i32*, i32** %2
%11 = call %struct.dirent* @readdir(i32* %10)
store %struct.dirent* %11, %struct.dirent** %3
%12 = icmp ne %struct.dirent* %11, null
br i1 %12, label %13, label %36
13: ; preds = %9
%14 = load %struct.dirent*, %struct.dirent** %3
%15 = getelementptr inbounds %struct.dirent, %struct.dirent* %14, i32 0, i32 0
%16 = load i8*, i8** %15
%17 = call i64 @strcmp(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i8* %16)
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %35
19: ; preds = %13
%20 = load %struct.dirent*, %struct.dirent** %3
%21 = getelementptr inbounds %struct.dirent, %struct.dirent* %20, i32 0, i32 0
%22 = load i8*, i8** %21
%23 = call i64 @strcmp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %22)
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %35
25: ; preds = %19
%26 = load i32, i32* %4
%27 = icmp ne i32 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %25
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
br label %30
30: ; preds = %28, %25
%31 = load %struct.dirent*, %struct.dirent** %3
%32 = getelementptr inbounds %struct.dirent, %struct.dirent* %31, i32 0, i32 0
%33 = load i8*, i8** %32
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0), i8* %33)
store i32 1, i32* %4
br label %35
35: ; preds = %30, %19, %13
br label %9
36: ; preds = %9
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0))
%38 = load i32*, i32** %2
%39 = call i32 @closedir(i32* %38)
br label %43
40: ; preds = %0
%41 = call i32 @perror(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.7, i64 0, i64 0))
%42 = load i32, i32* @EXIT_FAILURE
store i32 %42, i32* %1
br label %45
43: ; preds = %36
%44 = load i32, i32* @EXIT_SUCCESS
store i32 %44, i32* %1
br label %45
45: ; preds = %43, %40
%46 = load i32, i32* %1
ret i32 %46
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [29 x i8] c"/sys/kernel/debug/ieee80211/\00"
@.str.1 = [10 x i8] c"{\22data\22:[\00"
@.str.2 = [2 x i8] c".\00"
@.str.3 = [3 x i8] c"..\00"
@.str.5 = [16 x i8] c"{\22{#PHY}\22:\22%s\22}\00"
@.str.7 = [1 x i8] zeroinitializer
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
@str = [3 x i8] c"]}\00"
define i32 @discovery() {
%1 = tail call i32* @opendir(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
%2 = icmp eq i32* %1, null
br i1 %2, label %32, label %3
3: ; preds = %0
%4 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%5 = tail call %struct.dirent* @readdir(i32* %1)
%6 = icmp eq %struct.dirent* %5, null
br i1 %6, label %29, label %7
7: ; preds = %3, %25
%8 = phi %struct.dirent* [ %27, %25 ], [ %5, %3 ]
%9 = phi i32 [ %26, %25 ], [ 0, %3 ]
%10 = getelementptr inbounds %struct.dirent, %struct.dirent* %8, i64 0, i32 0
%11 = load i8*, i8** %10
%12 = tail call i64 @strcmp(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i8* %11)
%13 = icmp eq i64 %12, 0
br i1 %13, label %25, label %14
14: ; preds = %7
%15 = load i8*, i8** %10
%16 = tail call i64 @strcmp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %15)
%17 = icmp eq i64 %16, 0
br i1 %17, label %25, label %18
18: ; preds = %14
%19 = icmp eq i32 %9, 0
br i1 %19, label %22, label %20
20: ; preds = %18
%21 = tail call i32 @putchar(i32 44)
br label %22
22: ; preds = %20, %18
%23 = load i8*, i8** %10
%24 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0), i8* %23)
br label %25
25: ; preds = %22, %14, %7
%26 = phi i32 [ 1, %22 ], [ %9, %14 ], [ %9, %7 ]
%27 = tail call %struct.dirent* @readdir(i32* %1)
%28 = icmp eq %struct.dirent* %27, null
br i1 %28, label %29, label %7
29: ; preds = %25, %3
%30 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @str, i64 0, i64 0))
%31 = tail call i32 @closedir(i32* %1)
br label %34
32: ; preds = %0
%33 = tail call i32 @perror(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.7, i64 0, i64 0))
br label %34
34: ; preds = %29, %32
%35 = phi i32* [ @EXIT_SUCCESS, %29 ], [ @EXIT_FAILURE, %32 ]
%36 = load i32, i32* %35
ret i32 %36
}