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

/* Variables and functions */
int EIO;
int ENOENT;
int closedir(int /*<<< orphan*/ *);
int errno;
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);
int enable_disable_all_channels(char *dev_dir_name, int enable) {
  const struct dirent *ent;
  char scanelemdir[256];
  DIR *dp;
  int ret;
  snprintf(scanelemdir, sizeof(scanelemdir), "%s/scan_elements", dev_dir_name);
  scanelemdir[sizeof(scanelemdir) - 1] = '\0';
  dp = opendir(scanelemdir);
  if (!dp) {
    fprintf(stderr, "Enabling/disabling channels: can't open %s\n",
            scanelemdir);
    return -EIO;
  }
  ret = -ENOENT;
  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, scanelemdir, 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 -errno;
  }
  return 0;
}


x86 assembly (clang, -O0):
<enable_disable_all_channels>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
lea    -0x120(%rbp),%rdi
mov    -0x10(%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,-0x128(%rbp)
cmpq   $0x0,-0x128(%rbp)
jne    1212 <enable_disable_all_channels+0x82>
mov    0x2dee(%rip),%rax
mov    (%rax),%edi
lea    -0x120(%rbp),%rdx
lea    0xe17(%rip),%rsi
call   1070 <fprintf@plt>
xor    %eax,%eax
mov    0x2dd8(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1310 <enable_disable_all_channels+0x180>
xor    %eax,%eax
mov    0x2da5(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x12c(%rbp)
mov    -0x128(%rbp),%rdi
call   1090 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     12d5 <enable_disable_all_channels+0x145>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
lea    0xdf1(%rip),%rsi
call   1080 <iioutils_check_suffix@plt>
cmp    $0x0,%rax
je     12d0 <enable_disable_all_channels+0x140>
mov    -0x14(%rbp),%ecx
lea    0xded(%rip),%rsi
lea    0xde3(%rip),%rax
cmp    $0x0,%ecx
cmovne %rax,%rsi
mov    -0x20(%rbp),%rax
mov    (%rax),%rdx
lea    0xdc0(%rip),%rdi
call   1040 <printf@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
lea    -0x120(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   10b0 <write_sysfs_int@plt>
mov    %eax,-0x12c(%rbp)
cmpl   $0x0,-0x12c(%rbp)
jge    12cb <enable_disable_all_channels+0x13b>
mov    0x2d22(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rax
mov    (%rax),%rdx
lea    0xd90(%rip),%rsi
call   1070 <fprintf@plt>
jmp    12d0 <enable_disable_all_channels+0x140>
jmp    1223 <enable_disable_all_channels+0x93>
mov    -0x128(%rbp),%rdi
call   1060 <closedir@plt>
cmp    $0xffffffff,%eax
jne    1309 <enable_disable_all_channels+0x179>
lea    0xd82(%rip),%rdi
call   10a0 <perror@plt>
xor    %eax,%eax
mov    0x2cd1(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1310 <enable_disable_all_channels+0x180>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<enable_disable_all_channels>:
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     1298 <enable_disable_all_channels+0x108>
mov    %rax,%rbx
mov    %rax,%rdi
call   1090 <readdir@plt>
test   %rax,%rax
je     1272 <enable_disable_all_channels+0xe2>
mov    %rax,%rbp
test   %r14d,%r14d
lea    0xe5a(%rip),%rax
lea    0xe50(%rip),%r13
cmove  %rax,%r13
lea    0xe33(%rip),%r15
mov    %rsp,%r12
jmp    1220 <enable_disable_all_channels+0x90>
nop
mov    %rbx,%rdi
call   1090 <readdir@plt>
mov    %rax,%rbp
test   %rax,%rax
je     1272 <enable_disable_all_channels+0xe2>
mov    0x0(%rbp),%rdi
mov    %r15,%rsi
call   1080 <iioutils_check_suffix@plt>
test   %rax,%rax
je     1210 <enable_disable_all_channels+0x80>
mov    0x0(%rbp),%rdx
lea    0xe05(%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_disable_all_channels+0x80>
mov    0x2d7a(%rip),%rax
mov    (%rax),%edi
mov    0x0(%rbp),%rdx
lea    0xdeb(%rip),%rsi
call   1070 <fprintf@plt>
jmp    1210 <enable_disable_all_channels+0x80>
mov    %rbx,%rdi
call   1060 <closedir@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0xffffffff,%ecx
jne    12bb <enable_disable_all_channels+0x12b>
lea    0xde9(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2d3a(%rip),%rcx
jmp    12b7 <enable_disable_all_channels+0x127>
mov    0x2d39(%rip),%rax
mov    (%rax),%edi
lea    0xd69(%rip),%rsi
mov    %rsp,%rdx
call   1070 <fprintf@plt>
mov    0x2d29(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
add    $0x108,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [17 x i8] c"%s/scan_elements\00"
@stderr = global i32 0
@.str.1 = [44 x i8] c"Enabling/disabling channels: can't open %s\0A\00"
@EIO = global i32 0
@ENOENT = global i32 0
@.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"
@errno = global i32 0
define i32 @enable_disable_all_channels(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 ([17 x i8], [17 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 %39, 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 %20
15: ; preds = %2
%16 = load i32, i32* @stderr
%17 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0), i8* %4)
%18 = load i32, i32* @EIO
%19 = sub nsw i32 0, %18
br label %46
20: ; preds = %12, %36
%21 = phi %struct.dirent* [ %10, %12 ], [ %37, %36 ]
%22 = getelementptr inbounds %struct.dirent, %struct.dirent* %21, i64 0, i32 0
%23 = load i8*, i8** %22
%24 = call i64 @iioutils_check_suffix(i8* %23, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%25 = icmp eq i64 %24, 0
br i1 %25, label %36, label %26
26: ; preds = %20
%27 = load i8*, i8** %22
%28 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i8* %14, i8* %27)
%29 = load i8*, i8** %22
%30 = call i32 @write_sysfs_int(i8* %29, i8* %4, i32 %1)
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %36
32: ; preds = %26
%33 = load i32, i32* @stderr
%34 = load i8*, i8** %22
%35 = call i32 @fprintf(i32 %33, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.6, i64 0, i64 0), i8* %34)
br label %36
36: ; preds = %26, %32, %20
%37 = call %struct.dirent* @readdir(i32* %7)
%38 = icmp eq %struct.dirent* %37, null
br i1 %38, label %39, label %20
39: ; preds = %36, %9
%40 = call i32 @closedir(i32* %7)
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %46
42: ; preds = %39
%43 = call i32 @perror(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.7, i64 0, i64 0))
%44 = load i32, i32* @errno
%45 = sub nsw i32 0, %44
br label %46
46: ; preds = %39, %42, %15
%47 = phi i32 [ %45, %42 ], [ %19, %15 ], [ 0, %39 ]
ret i32 %47
}