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

/* Variables and functions */
int /*<<< orphan*/ R_OK;
scalar_t__ S_ISDIR(int /*<<< orphan*/);
scalar_t__ S_ISREG(int /*<<< orphan*/);
scalar_t__ access(char *, int /*<<< orphan*/);
int /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
scalar_t__ lstat(char *, struct stat *);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ printf(char *, char *, char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ strcat(char *, char *);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
void readable(char *beg_path) {
  char new_path[4096];
  DIR *directory = opendir(beg_path);
  struct dirent *files;
  struct stat area, *s = &area;
  while ((files = readdir(directory)) != NULL) {
    if (lstat(files->d_name, s) == 0) {
      if (S_ISREG(s->st_mode)) {
        if ((access(files->d_name, R_OK)) == 0)
          printf("%s/%s\n", beg_path, files->d_name);
      } else if (S_ISDIR(s->st_mode)) {
        if ((strcmp(files->d_name, ".") != 0) &&
            (strcmp(files->d_name, "..") != 0)) {
          if (strcmp(beg_path, "/") == 0) {
            strcpy(new_path, beg_path);
            strcat(new_path, files->d_name);
          } else {
            strcpy(new_path, beg_path);
            strcat(new_path, "/");
            strcat(new_path, files->d_name);
          }
          if ((access(new_path, R_OK)) == 0) {
            chdir(new_path);
            readable(new_path);
            chdir(beg_path);
          } else
            continue;
        }
      }
    }
  }
  closedir(directory);
}


x86 assembly (clang, -O0):
<readable>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x1018(%rbp)
lea    -0x1028(%rbp),%rax
mov    %rax,-0x1030(%rbp)
mov    -0x1018(%rbp),%rdi
call   10d0 <readdir@plt>
mov    %rax,-0x1020(%rbp)
cmp    $0x0,%rax
je     13cc <readable+0x1fc>
mov    -0x1020(%rbp),%rax
mov    (%rax),%rdi
mov    -0x1030(%rbp),%rsi
call   10a0 <lstat@plt>
cmp    $0x0,%rax
jne    13c7 <readable+0x1f7>
mov    -0x1030(%rbp),%rax
mov    (%rax),%edi
call   10c0 <S_ISREG@plt>
cmp    $0x0,%rax
je     1293 <readable+0xc3>
mov    -0x1020(%rbp),%rax
mov    (%rax),%rdi
mov    0x2d7d(%rip),%rax
mov    (%rax),%esi
call   10e0 <access@plt>
cmp    $0x0,%rax
jne    128e <readable+0xbe>
mov    -0x8(%rbp),%rsi
mov    -0x1020(%rbp),%rax
mov    (%rax),%rdx
lea    0xd77(%rip),%rdi
call   1060 <printf@plt>
jmp    13c2 <readable+0x1f2>
mov    -0x1030(%rbp),%rax
mov    (%rax),%edi
call   1070 <S_ISDIR@plt>
cmp    $0x0,%rax
je     13bd <readable+0x1ed>
mov    -0x1020(%rbp),%rax
mov    (%rax),%rdi
lea    0xd4c(%rip),%rsi
call   10b0 <strcmp@plt>
cmp    $0x0,%rax
je     13b8 <readable+0x1e8>
mov    -0x1020(%rbp),%rax
mov    (%rax),%rdi
lea    0xd2b(%rip),%rsi
call   10b0 <strcmp@plt>
cmp    $0x0,%rax
je     13b8 <readable+0x1e8>
mov    -0x8(%rbp),%rdi
lea    0xd14(%rip),%rsi
call   10b0 <strcmp@plt>
cmp    $0x0,%rax
jne    1330 <readable+0x160>
lea    -0x1010(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1030 <strcpy@plt>
lea    -0x1010(%rbp),%rdi
mov    -0x1020(%rbp),%rax
mov    (%rax),%rsi
call   10f0 <strcat@plt>
jmp    1369 <readable+0x199>
lea    -0x1010(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1030 <strcpy@plt>
lea    -0x1010(%rbp),%rdi
lea    0xcbc(%rip),%rsi
call   10f0 <strcat@plt>
lea    -0x1010(%rbp),%rdi
mov    -0x1020(%rbp),%rax
mov    (%rax),%rsi
call   10f0 <strcat@plt>
lea    -0x1010(%rbp),%rdi
mov    0x2c69(%rip),%rax
mov    (%rax),%esi
call   10e0 <access@plt>
cmp    $0x0,%rax
jne    13ae <readable+0x1de>
lea    -0x1010(%rbp),%rdi
call   1050 <chdir@plt>
lea    -0x1010(%rbp),%rdi
call   1080 <readable@plt>
mov    -0x8(%rbp),%rdi
call   1050 <chdir@plt>
jmp    13b3 <readable+0x1e3>
jmp    11fd <readable+0x2d>
jmp    13b8 <readable+0x1e8>
jmp    13bd <readable+0x1ed>
jmp    13c2 <readable+0x1f2>
jmp    13c7 <readable+0x1f7>
jmp    11fd <readable+0x2d>
mov    -0x1018(%rbp),%rdi
call   1090 <closedir@plt>
add    $0x1030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readable>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1018,%rsp
mov    %rdi,%r14
call   1040 <opendir@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   10d0 <readdir@plt>
test   %rax,%rax
je     134a <readable+0x17a>
mov    %rax,%r12
mov    %rsp,%r15
lea    0xe00(%rip),%rbp
mov    %r14,0x8(%rsp)
jmp    1234 <readable+0x64>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   10d0 <readdir@plt>
mov    %rax,%r12
test   %rax,%rax
je     134a <readable+0x17a>
mov    (%r12),%rdi
mov    %r15,%rsi
call   10a0 <lstat@plt>
test   %rax,%rax
jne    1220 <readable+0x50>
mov    %rbp,%r13
mov    (%rsp),%ebp
mov    %ebp,%edi
call   10c0 <S_ISREG@plt>
test   %rax,%rax
je     1286 <readable+0xb6>
mov    (%r12),%rdi
mov    0x2d7e(%rip),%rax
mov    (%rax),%esi
call   10e0 <access@plt>
test   %rax,%rax
mov    %r13,%rbp
jne    1220 <readable+0x50>
mov    (%r12),%rdx
lea    0xd84(%rip),%rdi
mov    %r14,%rsi
call   1060 <printf@plt>
jmp    1220 <readable+0x50>
mov    %ebp,%edi
call   1070 <S_ISDIR@plt>
test   %rax,%rax
mov    %r13,%rbp
je     1220 <readable+0x50>
mov    (%r12),%rdi
lea    0xd68(%rip),%rsi
call   10b0 <strcmp@plt>
test   %rax,%rax
je     1220 <readable+0x50>
mov    (%r12),%rdi
lea    0xd4e(%rip),%rsi
call   10b0 <strcmp@plt>
test   %rax,%rax
je     1220 <readable+0x50>
mov    %r14,%rdi
mov    %rbp,%rsi
call   10b0 <strcmp@plt>
mov    %rax,%rbp
mov    %r14,%rsi
lea    0x10(%rsp),%r14
mov    %r14,%rdi
call   1030 <strcpy@plt>
test   %rbp,%rbp
mov    %r13,%rbp
je     12f8 <readable+0x128>
mov    %r14,%rdi
mov    %rbp,%rsi
call   10f0 <strcat@plt>
mov    (%r12),%rsi
mov    %r14,%rdi
call   10f0 <strcat@plt>
mov    0x2cd5(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   10e0 <access@plt>
test   %rax,%rax
mov    0x8(%rsp),%r14
jne    1220 <readable+0x50>
lea    0x10(%rsp),%r14
mov    %r14,%rdi
call   1050 <chdir@plt>
mov    %r14,%rdi
mov    0x8(%rsp),%r14
call   1080 <readable@plt>
mov    %r14,%rdi
call   1050 <chdir@plt>
jmp    1220 <readable+0x50>
mov    %rbx,%rdi
call   1090 <closedir@plt>
add    $0x1018,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.stat = type { i32 }
%struct.dirent = type { i8* }
@R_OK = global i32 0
@.str = [7 x i8] c"%s/%s\0A\00"
@.str.1 = [2 x i8] c".\00"
@.str.2 = [3 x i8] c"..\00"
@.str.3 = [2 x i8] c"/\00"
define void @readable(i8* %0) {
%2 = alloca [4096 x i8]
%3 = alloca %struct.stat
%4 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 0
%5 = tail call i32* @opendir(i8* %0)
%6 = bitcast %struct.stat* %3 to i8*
%7 = call %struct.dirent* @readdir(i32* %5)
%8 = icmp eq %struct.dirent* %7, null
br i1 %8, label %58, label %9
9: ; preds = %1
%10 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
br label %11
11: ; preds = %9, %55
%12 = phi %struct.dirent* [ %7, %9 ], [ %56, %55 ]
%13 = getelementptr inbounds %struct.dirent, %struct.dirent* %12, i64 0, i32 0
%14 = load i8*, i8** %13
%15 = call i64 @lstat(i8* %14, %struct.stat* %3)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %55
17: ; preds = %11
%18 = load i32, i32* %10
%19 = call i64 @S_ISREG(i32 %18)
%20 = icmp eq i64 %19, 0
br i1 %20, label %29, label %21
21: ; preds = %17
%22 = load i8*, i8** %13
%23 = load i32, i32* @R_OK
%24 = call i64 @access(i8* %22, i32 %23)
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %55
26: ; preds = %21
%27 = load i8*, i8** %13
%28 = call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %27)
br label %55
29: ; preds = %17
%30 = call i64 @S_ISDIR(i32 %18)
%31 = icmp eq i64 %30, 0
br i1 %31, label %55, label %32
32: ; preds = %29
%33 = load i8*, i8** %13
%34 = call i64 @strcmp(i8* %33, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%35 = icmp eq i64 %34, 0
br i1 %35, label %55, label %36
36: ; preds = %32
%37 = load i8*, i8** %13
%38 = call i64 @strcmp(i8* %37, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%39 = icmp eq i64 %38, 0
br i1 %39, label %55, label %40
40: ; preds = %36
%41 = call i64 @strcmp(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%42 = icmp eq i64 %41, 0
%43 = call i32 @strcpy(i8* %4, i8* %0)
br i1 %42, label %46, label %44
44: ; preds = %40
%45 = call i32 @strcat(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
br label %46
46: ; preds = %40, %44
%47 = load i8*, i8** %13
%48 = call i32 @strcat(i8* %4, i8* %47)
%49 = load i32, i32* @R_OK
%50 = call i64 @access(i8* %4, i32 %49)
%51 = icmp eq i64 %50, 0
br i1 %51, label %52, label %55
52: ; preds = %46
%53 = call i32 @chdir(i8* %4)
%54 = call i32 @chdir(i8* %0)
br label %55
55: ; preds = %11, %29, %52, %36, %32, %21, %26, %46
%56 = call %struct.dirent* @readdir(i32* %5)
%57 = icmp eq %struct.dirent* %56, null
br i1 %57, label %58, label %11
58: ; preds = %55, %1
%59 = call i32 @closedir(i32* %5)
ret void
}