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

/* Variables and functions */
int closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
scalar_t__ iioutils_check_suffix(char *, char *);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *, char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
int /*<<< orphan*/ stderr;
int write_sysfs_int(char *, char *, int);
void enable_events(char *dev_dir, int enable) {
  const struct dirent *ent;
  char evdir[256];
  int ret;
  DIR *dp;
  snprintf(evdir, sizeof(evdir), "%s/events", dev_dir);
  evdir[sizeof(evdir) - 1] = '\0';
  dp = opendir(evdir);
  if (!dp) {
    fprintf(stderr, "Enabling/disabling events: can't open %s\n", evdir);
    return;
  }
  while (ent = readdir(dp), ent) {
    if (iioutils_check_suffix(ent->d_name, "_en")) {
      printf("%sabling: %s\n", enable ? "En" : "Dis", ent->d_name);
      ret = write_sysfs_int(ent->d_name, evdir, enable);
      if (ret < 0)
        fprintf(stderr, "Failed to enable/disable %s\n", ent->d_name);
    }
  }
  if (closedir(dp) == -1) {
    perror("Enabling/disabling channels: "
           "Failed to close directory");
    return;
  }
}


x86 assembly (clang, -O0):
<enable_events>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    -0x120(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    $0x100,%esi
lea    0xe47(%rip),%rdx
call   1050 <snprintf@plt>
movb   $0x0,-0x21(%rbp)
lea    -0x120(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x130(%rbp)
cmpq   $0x0,-0x130(%rbp)
jne    1204 <enable_events+0x74>
mov    0x2df6(%rip),%rax
mov    (%rax),%edi
lea    -0x120(%rbp),%rdx
lea    0xe10(%rip),%rsi
call   1070 <fprintf@plt>
jmp    12dc <enable_events+0x14c>
jmp    1209 <enable_events+0x79>
mov    -0x130(%rbp),%rdi
call   1090 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     12bb <enable_events+0x12b>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
lea    0xe02(%rip),%rsi
call   1080 <iioutils_check_suffix@plt>
cmp    $0x0,%rax
je     12b6 <enable_events+0x126>
mov    -0xc(%rbp),%ecx
lea    0xdfe(%rip),%rsi
lea    0xdf4(%rip),%rax
cmp    $0x0,%ecx
cmovne %rax,%rsi
mov    -0x18(%rbp),%rax
mov    (%rax),%rdx
lea    0xdd1(%rip),%rdi
call   1040 <printf@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
lea    -0x120(%rbp),%rsi
mov    -0xc(%rbp),%edx
call   10b0 <write_sysfs_int@plt>
mov    %eax,-0x124(%rbp)
cmpl   $0x0,-0x124(%rbp)
jge    12b1 <enable_events+0x121>
mov    0x2d44(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rax
mov    (%rax),%rdx
lea    0xda1(%rip),%rsi
call   1070 <fprintf@plt>
jmp    12b6 <enable_events+0x126>
jmp    1209 <enable_events+0x79>
mov    -0x130(%rbp),%rdi
call   1060 <closedir@plt>
cmp    $0xffffffff,%eax
jne    12dc <enable_events+0x14c>
lea    0xd93(%rip),%rdi
call   10a0 <perror@plt>
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<enable_events>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x108,%rsp
mov    %esi,%r14d
mov    %rdi,%rcx
lea    0xe52(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x100,%esi
call   1050 <snprintf@plt>
movb   $0x0,0xff(%rsp)
mov    %rbx,%rdi
call   1030 <opendir@plt>
test   %rax,%rax
je     128d <enable_events+0xfd>
mov    %rax,%rbx
mov    %rax,%rdi
call   1090 <readdir@plt>
test   %rax,%rax
je     1272 <enable_events+0xe2>
mov    %rax,%rbp
test   %r14d,%r14d
lea    0xe51(%rip),%rax
lea    0xe47(%rip),%r13
cmove  %rax,%r13
lea    0xe2a(%rip),%r15
mov    %rsp,%r12
jmp    1220 <enable_events+0x90>
nop
mov    %rbx,%rdi
call   1090 <readdir@plt>
mov    %rax,%rbp
test   %rax,%rax
je     1272 <enable_events+0xe2>
mov    0x0(%rbp),%rdi
mov    %r15,%rsi
call   1080 <iioutils_check_suffix@plt>
test   %rax,%rax
je     1210 <enable_events+0x80>
mov    0x0(%rbp),%rdx
lea    0xdfc(%rip),%rdi
mov    %r13,%rsi
call   1040 <printf@plt>
mov    0x0(%rbp),%rdi
mov    %r12,%rsi
mov    %r14d,%edx
call   10b0 <write_sysfs_int@plt>
test   %eax,%eax
jns    1210 <enable_events+0x80>
mov    0x2d82(%rip),%rax
mov    (%rax),%edi
mov    0x0(%rbp),%rdx
lea    0xde2(%rip),%rsi
call   1070 <fprintf@plt>
jmp    1210 <enable_events+0x80>
mov    %rbx,%rdi
call   1060 <closedir@plt>
cmp    $0xffffffff,%eax
jne    12a5 <enable_events+0x115>
lea    0xde4(%rip),%rdi
call   10a0 <perror@plt>
jmp    12a5 <enable_events+0x115>
mov    0x2d4c(%rip),%rax
mov    (%rax),%edi
lea    0xd6d(%rip),%rsi
mov    %rsp,%rdx
call   1070 <fprintf@plt>
add    $0x108,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i8* }
@.str = [10 x i8] c"%s/events\00"
@stderr = global i32 0
@.str.1 = [42 x i8] c"Enabling/disabling events: can't open %s\0A\00"
@.str.2 = [4 x i8] c"_en\00"
@.str.3 = [14 x i8] c"%sabling: %s\0A\00"
@.str.4 = [3 x i8] c"En\00"
@.str.5 = [4 x i8] c"Dis\00"
@.str.6 = [29 x i8] c"Failed to enable/disable %s\0A\00"
@.str.7 = [55 x i8] c"Enabling/disabling channels: Failed to close directory\00"
define void @enable_events(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.dirent*
%6 = alloca [256 x i8]
%7 = alloca i32
%8 = alloca i32*
store i8* %0, i8** %3
store i32 %1, i32* %4
%9 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%10 = load i8*, i8** %3
%11 = call i32 @snprintf(i8* %9, i32 256, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %10)
%12 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 255
store i8 0, i8* %12
%13 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%14 = call i32* @opendir(i8* %13)
store i32* %14, i32** %8
%15 = load i32*, i32** %8
%16 = icmp ne i32* %15, null
br i1 %16, label %21, label %17
17: ; preds = %2
%18 = load i32, i32* @stderr
%19 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%20 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0), i8* %19)
br label %64
21: ; preds = %2
br label %22
22: ; preds = %57, %21
%23 = load i32*, i32** %8
%24 = call %struct.dirent* @readdir(i32* %23)
store %struct.dirent* %24, %struct.dirent** %5
%25 = load %struct.dirent*, %struct.dirent** %5
%26 = icmp ne %struct.dirent* %25, null
br i1 %26, label %27, label %58
27: ; preds = %22
%28 = load %struct.dirent*, %struct.dirent** %5
%29 = getelementptr inbounds %struct.dirent, %struct.dirent* %28, i32 0, i32 0
%30 = load i8*, i8** %29
%31 = call i64 @iioutils_check_suffix(i8* %30, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %57
33: ; preds = %27
%34 = load i32, i32* %4
%35 = icmp ne i32 %34, 0
%36 = zext i1 %35 to i64
%37 = select i1 %35, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0)
%38 = load %struct.dirent*, %struct.dirent** %5
%39 = getelementptr inbounds %struct.dirent, %struct.dirent* %38, i32 0, i32 0
%40 = load i8*, i8** %39
%41 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i8* %37, i8* %40)
%42 = load %struct.dirent*, %struct.dirent** %5
%43 = getelementptr inbounds %struct.dirent, %struct.dirent* %42, i32 0, i32 0
%44 = load i8*, i8** %43
%45 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%46 = load i32, i32* %4
%47 = call i32 @write_sysfs_int(i8* %44, i8* %45, i32 %46)
store i32 %47, i32* %7
%48 = load i32, i32* %7
%49 = icmp slt i32 %48, 0
br i1 %49, label %50, label %56
50: ; preds = %33
%51 = load i32, i32* @stderr
%52 = load %struct.dirent*, %struct.dirent** %5
%53 = getelementptr inbounds %struct.dirent, %struct.dirent* %52, i32 0, i32 0
%54 = load i8*, i8** %53
%55 = call i32 @fprintf(i32 %51, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.6, i64 0, i64 0), i8* %54)
br label %56
56: ; preds = %50, %33
br label %57
57: ; preds = %56, %27
br label %22
58: ; preds = %22
%59 = load i32*, i32** %8
%60 = call i32 @closedir(i32* %59)
%61 = icmp eq i32 %60, -1
br i1 %61, label %62, label %64
62: ; preds = %58
%63 = call i32 @perror(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.7, i64 0, i64 0))
br label %64
64: ; preds = %17, %62, %58
ret void
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [10 x i8] c"%s/events\00"
@stderr = global i32 0
@.str.1 = [42 x i8] c"Enabling/disabling events: can't open %s\0A\00"
@.str.2 = [4 x i8] c"_en\00"
@.str.3 = [14 x i8] c"%sabling: %s\0A\00"
@.str.4 = [3 x i8] c"En\00"
@.str.5 = [4 x i8] c"Dis\00"
@.str.6 = [29 x i8] c"Failed to enable/disable %s\0A\00"
@.str.7 = [55 x i8] c"Enabling/disabling channels: Failed to close directory\00"
define void @enable_events(i8* %0, i32 %1) {
%3 = alloca [256 x i8]
%4 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%5 = call i32 @snprintf(i8* %4, i32 256, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %0)
%6 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 255
store i8 0, i8* %6
%7 = call i32* @opendir(i8* %4)
%8 = icmp eq i32* %7, null
br i1 %8, label %15, label %9
9: ; preds = %2
%10 = call %struct.dirent* @readdir(i32* %7)
%11 = icmp eq %struct.dirent* %10, null
br i1 %11, label %37, label %12
12: ; preds = %9
%13 = icmp eq i32 %1, 0
%14 = select i1 %13, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0)
br label %18
15: ; preds = %2
%16 = load i32, i32* @stderr
%17 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0), i8* %4)
br label %42
18: ; preds = %12, %34
%19 = phi %struct.dirent* [ %10, %12 ], [ %35, %34 ]
%20 = getelementptr inbounds %struct.dirent, %struct.dirent* %19, i64 0, i32 0
%21 = load i8*, i8** %20
%22 = call i64 @iioutils_check_suffix(i8* %21, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%23 = icmp eq i64 %22, 0
br i1 %23, label %34, label %24
24: ; preds = %18
%25 = load i8*, i8** %20
%26 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i8* %14, i8* %25)
%27 = load i8*, i8** %20
%28 = call i32 @write_sysfs_int(i8* %27, i8* %4, i32 %1)
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %34
30: ; preds = %24
%31 = load i32, i32* @stderr
%32 = load i8*, i8** %20
%33 = call i32 @fprintf(i32 %31, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.6, i64 0, i64 0), i8* %32)
br label %34
34: ; preds = %24, %30, %18
%35 = call %struct.dirent* @readdir(i32* %7)
%36 = icmp eq %struct.dirent* %35, null
br i1 %36, label %37, label %18
37: ; preds = %34, %9
%38 = call i32 @closedir(i32* %7)
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %42
40: ; preds = %37
%41 = call i32 @perror(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.7, i64 0, i64 0))
br label %42
42: ; preds = %37, %40, %15
ret void
}