#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*/ R_OK;
char *StrApp(char **, char *, int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ access(char *, int /*<<< orphan*/);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ strcmp(int /*<<< orphan*/, char *);
char *find_acad_proc_file(void) {
  DIR *dir;
  char *basedir = "/proc/acpi/ac_adapter/";
  struct dirent *entry;
  dir = opendir(basedir);
  if (!dir)
    return NULL;
  while ((entry = readdir(dir))) {
    char *result = NULL;
    char *temp1, *temp2, *temp3;
    if (!strcmp(entry->d_name, "."))
      continue;
    if (!strcmp(entry->d_name, ".."))
      continue;
    temp1 =
        StrApp((char **)NULL, basedir, entry->d_name, "/state", (char *)NULL);
    temp2 =
        StrApp((char **)NULL, basedir, entry->d_name, "/status", (char *)NULL);
    temp3 =
        StrApp((char **)NULL, basedir, entry->d_name, "/stats", (char *)NULL);
    if (!access(temp1, R_OK)) {
      result = temp1;
      free(temp2);
      free(temp3);
    } else if (!access(temp2, R_OK)) {
      result = temp2;
      free(temp1);
      free(temp3);
    } else if (!access(temp3, R_OK)) {
      result = temp3;
      free(temp1);
      free(temp2);
    }
    if (result) {
      closedir(dir);
      return result;
    }
    free(temp1);
    free(temp2);
    free(temp3);
  }
  closedir(dir);
  return NULL;
}


x86 assembly (clang, -O0):
<find_acad_proc_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
lea    0xe81(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11a8 <find_acad_proc_file+0x38>
movq   $0x0,-0x8(%rbp)
jmp    1377 <find_acad_proc_file+0x207>
jmp    11ad <find_acad_proc_file+0x3d>
mov    -0x10(%rbp),%rdi
call   1070 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     1366 <find_acad_proc_file+0x1f6>
movq   $0x0,-0x28(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
lea    0xe3f(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%eax
jne    11ec <find_acad_proc_file+0x7c>
jmp    11ad <find_acad_proc_file+0x3d>
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
lea    0xe1e(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%eax
jne    120c <find_acad_proc_file+0x9c>
jmp    11ad <find_acad_proc_file+0x3d>
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%r8d
lea    0xdf8(%rip),%rcx
mov    %r8,%rdi
call   1090 <StrApp@plt>
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%r8d
lea    0xddd(%rip),%rcx
mov    %r8,%rdi
call   1090 <StrApp@plt>
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%r8d
lea    0xdc3(%rip),%rcx
mov    %r8,%rdi
call   1090 <StrApp@plt>
mov    %rax,-0x40(%rbp)
mov    -0x30(%rbp),%rdi
mov    0x2d63(%rip),%rax
mov    (%rax),%esi
call   1080 <access@plt>
cmp    $0x0,%eax
jne    12ac <find_acad_proc_file+0x13c>
mov    -0x30(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
jmp    1325 <find_acad_proc_file+0x1b5>
mov    -0x38(%rbp),%rdi
mov    0x2d29(%rip),%rax
mov    (%rax),%esi
call   1080 <access@plt>
cmp    $0x0,%eax
jne    12e6 <find_acad_proc_file+0x176>
mov    -0x38(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
jmp    1320 <find_acad_proc_file+0x1b0>
mov    -0x40(%rbp),%rdi
mov    0x2cef(%rip),%rax
mov    (%rax),%esi
call   1080 <access@plt>
cmp    $0x0,%eax
jne    131b <find_acad_proc_file+0x1ab>
mov    -0x40(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
jmp    1320 <find_acad_proc_file+0x1b0>
jmp    1325 <find_acad_proc_file+0x1b5>
cmpq   $0x0,-0x28(%rbp)
je     1346 <find_acad_proc_file+0x1d6>
mov    -0x10(%rbp),%rdi
call   1050 <closedir@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1377 <find_acad_proc_file+0x207>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
jmp    11ad <find_acad_proc_file+0x3d>
mov    -0x10(%rbp),%rdi
call   1050 <closedir@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_acad_proc_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
lea    0xe7e(%rip),%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     12e2 <find_acad_proc_file+0x172>
mov    %rax,%r14
mov    %rax,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
je     12d6 <find_acad_proc_file+0x166>
mov    %rax,%rbx
lea    0xe6a(%rip),%rbp
jmp    11f9 <find_acad_proc_file+0x89>
call   1030 <free@plt>
mov    %r15,%rdi
call   1030 <free@plt>
test   %rbx,%rbx
jne    12d8 <find_acad_proc_file+0x168>
mov    %rbp,%rdi
call   1030 <free@plt>
mov    %r13,%rdi
call   1030 <free@plt>
mov    %r12,%rdi
call   1030 <free@plt>
lea    0xe33(%rip),%rbp
mov    %r14,%rdi
call   1070 <readdir@plt>
mov    %rax,%rbx
test   %rax,%rax
je     12d6 <find_acad_proc_file+0x166>
mov    (%rbx),%edi
mov    %rbp,%rsi
call   1060 <strcmp@plt>
test   %eax,%eax
je     11e5 <find_acad_proc_file+0x75>
mov    (%rbx),%edi
lea    0xe07(%rip),%rsi
call   1060 <strcmp@plt>
test   %eax,%eax
je     11e5 <find_acad_proc_file+0x75>
mov    (%rbx),%edx
xor    %edi,%edi
lea    0xddc(%rip),%r15
mov    %r15,%rsi
lea    0xdec(%rip),%rcx
xor    %r8d,%r8d
call   1090 <StrApp@plt>
mov    %rax,%rbp
mov    (%rbx),%edx
xor    %edi,%edi
mov    %r15,%rsi
lea    0xdda(%rip),%rcx
xor    %r8d,%r8d
call   1090 <StrApp@plt>
mov    %rax,%r13
mov    (%rbx),%edx
xor    %edi,%edi
mov    %r15,%rsi
lea    0xdc9(%rip),%rcx
xor    %r8d,%r8d
call   1090 <StrApp@plt>
mov    %rax,%r12
mov    0x2d6e(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   1080 <access@plt>
mov    %r13,%rdi
mov    %r12,%r15
mov    %rbp,%rbx
test   %eax,%eax
je     11b0 <find_acad_proc_file+0x40>
mov    0x2d4c(%rip),%rax
mov    (%rax),%esi
mov    %r13,%rdi
call   1080 <access@plt>
mov    %rbp,%rdi
mov    %r12,%r15
mov    %r13,%rbx
test   %eax,%eax
je     11b0 <find_acad_proc_file+0x40>
mov    0x2d2a(%rip),%rax
mov    (%rax),%esi
mov    %r12,%rdi
call   1080 <access@plt>
mov    %rbp,%rdi
mov    %r13,%r15
mov    %r12,%rbx
test   %eax,%eax
jne    11c6 <find_acad_proc_file+0x56>
jmp    11b0 <find_acad_proc_file+0x40>
xor    %ebx,%ebx
mov    %r14,%rdi
call   1050 <closedir@plt>
jmp    12e4 <find_acad_proc_file+0x174>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i32 }
@.str = [23 x i8] c"/proc/acpi/ac_adapter/\00"
@.str.1 = [2 x i8] c".\00"
@.str.2 = [3 x i8] c"..\00"
@.str.3 = [7 x i8] c"/state\00"
@.str.4 = [8 x i8] c"/status\00"
@.str.5 = [7 x i8] c"/stats\00"
@R_OK = global i32 0
define i8* @find_acad_proc_file() {
%1 = alloca i8*
%2 = alloca i32*
%3 = alloca i8*
%4 = alloca %struct.dirent*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
store i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8** %3
%9 = load i8*, i8** %3
%10 = call i32* @opendir(i8* %9)
store i32* %10, i32** %2
%11 = load i32*, i32** %2
%12 = icmp ne i32* %11, null
br i1 %12, label %14, label %13
13: ; preds = %0
store i8* null, i8** %1
br label %100
14: ; preds = %0
br label %15
15: ; preds = %90, %32, %25, %14
%16 = load i32*, i32** %2
%17 = call %struct.dirent* @readdir(i32* %16)
store %struct.dirent* %17, %struct.dirent** %4
%18 = icmp ne %struct.dirent* %17, null
br i1 %18, label %19, label %97
19: ; preds = %15
store i8* null, i8** %5
%20 = load %struct.dirent*, %struct.dirent** %4
%21 = getelementptr inbounds %struct.dirent, %struct.dirent* %20, i32 0, i32 0
%22 = load i32, i32* %21
%23 = call i32 @strcmp(i32 %22, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%24 = icmp ne i32 %23, 0
br i1 %24, label %26, label %25
25: ; preds = %19
br label %15
26: ; preds = %19
%27 = load %struct.dirent*, %struct.dirent** %4
%28 = getelementptr inbounds %struct.dirent, %struct.dirent* %27, i32 0, i32 0
%29 = load i32, i32* %28
%30 = call i32 @strcmp(i32 %29, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%31 = icmp ne i32 %30, 0
br i1 %31, label %33, label %32
32: ; preds = %26
br label %15
33: ; preds = %26
%34 = load i8*, i8** %3
%35 = load %struct.dirent*, %struct.dirent** %4
%36 = getelementptr inbounds %struct.dirent, %struct.dirent* %35, i32 0, i32 0
%37 = load i32, i32* %36
%38 = call i8* @StrApp(i8** null, i8* %34, i32 %37, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i8* null)
store i8* %38, i8** %6
%39 = load i8*, i8** %3
%40 = load %struct.dirent*, %struct.dirent** %4
%41 = getelementptr inbounds %struct.dirent, %struct.dirent* %40, i32 0, i32 0
%42 = load i32, i32* %41
%43 = call i8* @StrApp(i8** null, i8* %39, i32 %42, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i8* null)
store i8* %43, i8** %7
%44 = load i8*, i8** %3
%45 = load %struct.dirent*, %struct.dirent** %4
%46 = getelementptr inbounds %struct.dirent, %struct.dirent* %45, i32 0, i32 0
%47 = load i32, i32* %46
%48 = call i8* @StrApp(i8** null, i8* %44, i32 %47, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0), i8* null)
store i8* %48, i8** %8
%49 = load i8*, i8** %6
%50 = load i32, i32* @R_OK
%51 = call i32 @access(i8* %49, i32 %50)
%52 = icmp ne i32 %51, 0
br i1 %52, label %59, label %53
53: ; preds = %33
%54 = load i8*, i8** %6
store i8* %54, i8** %5
%55 = load i8*, i8** %7
%56 = call i32 @free(i8* %55)
%57 = load i8*, i8** %8
%58 = call i32 @free(i8* %57)
br label %83
59: ; preds = %33
%60 = load i8*, i8** %7
%61 = load i32, i32* @R_OK
%62 = call i32 @access(i8* %60, i32 %61)
%63 = icmp ne i32 %62, 0
br i1 %63, label %70, label %64
64: ; preds = %59
%65 = load i8*, i8** %7
store i8* %65, i8** %5
%66 = load i8*, i8** %6
%67 = call i32 @free(i8* %66)
%68 = load i8*, i8** %8
%69 = call i32 @free(i8* %68)
br label %82
70: ; preds = %59
%71 = load i8*, i8** %8
%72 = load i32, i32* @R_OK
%73 = call i32 @access(i8* %71, i32 %72)
%74 = icmp ne i32 %73, 0
br i1 %74, label %81, label %75
75: ; preds = %70
%76 = load i8*, i8** %8
store i8* %76, i8** %5
%77 = load i8*, i8** %6
%78 = call i32 @free(i8* %77)
%79 = load i8*, i8** %7
%80 = call i32 @free(i8* %79)
br label %81
81: ; preds = %75, %70
br label %82
82: ; preds = %81, %64
br label %83
83: ; preds = %82, %53
%84 = load i8*, i8** %5
%85 = icmp ne i8* %84, null
br i1 %85, label %86, label %90
86: ; preds = %83
%87 = load i32*, i32** %2
%88 = call i32 @closedir(i32* %87)
%89 = load i8*, i8** %5
store i8* %89, i8** %1
br label %100
90: ; preds = %83
%91 = load i8*, i8** %6
%92 = call i32 @free(i8* %91)
%93 = load i8*, i8** %7
%94 = call i32 @free(i8* %93)
%95 = load i8*, i8** %8
%96 = call i32 @free(i8* %95)
br label %15
97: ; preds = %15
%98 = load i32*, i32** %2
%99 = call i32 @closedir(i32* %98)
store i8* null, i8** %1
br label %100
100: ; preds = %97, %86, %13
%101 = load i8*, i8** %1
ret i8* %101
}

LLVM IR (-O2):
%struct.dirent = type { i32 }
@.str = [23 x i8] c"/proc/acpi/ac_adapter/\00"
@.str.1 = [2 x i8] c".\00"
@.str.2 = [3 x i8] c"..\00"
@.str.3 = [7 x i8] c"/state\00"
@.str.4 = [8 x i8] c"/status\00"
@.str.5 = [7 x i8] c"/stats\00"
@R_OK = global i32 0
define i8* @find_acad_proc_file() {
%1 = tail call i32* @opendir(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%2 = icmp eq i32* %1, null
br i1 %2, label %51, label %3
3: ; preds = %0
%4 = tail call %struct.dirent* @readdir(i32* %1)
%5 = icmp eq %struct.dirent* %4, null
br i1 %5, label %48, label %6
6: ; preds = %3, %45
%7 = phi %struct.dirent* [ %46, %45 ], [ %4, %3 ]
%8 = getelementptr inbounds %struct.dirent, %struct.dirent* %7, i64 0, i32 0
%9 = load i32, i32* %8
%10 = tail call i32 @strcmp(i32 %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%11 = icmp eq i32 %10, 0
br i1 %11, label %45, label %12
12: ; preds = %6
%13 = load i32, i32* %8
%14 = tail call i32 @strcmp(i32 %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%15 = icmp eq i32 %14, 0
br i1 %15, label %45, label %16
16: ; preds = %12
%17 = load i32, i32* %8
%18 = tail call i8* @StrApp(i8** null, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %17, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i8* null)
%19 = load i32, i32* %8
%20 = tail call i8* @StrApp(i8** null, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %19, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i8* null)
%21 = load i32, i32* %8
%22 = tail call i8* @StrApp(i8** null, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %21, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0), i8* null)
%23 = load i32, i32* @R_OK
%24 = tail call i32 @access(i8* %18, i32 %23)
%25 = icmp eq i32 %24, 0
br i1 %25, label %34, label %26
26: ; preds = %16
%27 = load i32, i32* @R_OK
%28 = tail call i32 @access(i8* %20, i32 %27)
%29 = icmp eq i32 %28, 0
br i1 %29, label %34, label %30
30: ; preds = %26
%31 = load i32, i32* @R_OK
%32 = tail call i32 @access(i8* %22, i32 %31)
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %41
34: ; preds = %30, %26, %16
%35 = phi i8* [ %20, %16 ], [ %18, %26 ], [ %18, %30 ]
%36 = phi i8* [ %22, %16 ], [ %22, %26 ], [ %20, %30 ]
%37 = phi i8* [ %18, %16 ], [ %20, %26 ], [ %22, %30 ]
%38 = tail call i32 @free(i8* %35)
%39 = tail call i32 @free(i8* %36)
%40 = icmp eq i8* %37, null
br i1 %40, label %41, label %48
41: ; preds = %30, %34
%42 = tail call i32 @free(i8* %18)
%43 = tail call i32 @free(i8* %20)
%44 = tail call i32 @free(i8* %22)
br label %45
45: ; preds = %12, %6, %41
%46 = tail call %struct.dirent* @readdir(i32* %1)
%47 = icmp eq %struct.dirent* %46, null
br i1 %47, label %48, label %6
48: ; preds = %45, %34, %3
%49 = phi i8* [ null, %3 ], [ %37, %34 ], [ null, %45 ]
%50 = tail call i32 @closedir(i32* %1)
br label %51
51: ; preds = %48, %0
%52 = phi i8* [ null, %0 ], [ %49, %48 ]
ret i8* %52
}