#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 /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ printf(char *, char *, ...);
struct dirent *readdir(int /*<<< orphan*/ *);
scalar_t__ strcmp(char *, char *);
char *strerror(int /*<<< orphan*/);
int read_directory(char *d, char *f) {
  DIR *dir;
  int ret;
  struct dirent *file;
  int found = 0;
  int i;
  dir = opendir(d);
  if (dir == NULL) {
    printf("Can not open directory %s %s\n", strerror(errno));
    return -1;
  }
  ret = 0;
  while (1) {
    file = readdir(dir);
    if (file == NULL)
      break;
    if (strcmp(file->d_name, "..") == 0)
      continue;
    if (strcmp(file->d_name, ".") == 0)
      continue;
    if (f != NULL) {
      if (strcmp(file->d_name, f) == 0) {
        found = 1;
        continue;
      }
    }
    printf("%s found in directory %s\n", file->d_name, d);
    ret = -1;
    break;
  }
  closedir(dir);
  if (f != NULL) {
    if (found == 0)
      return -1;
  }
  return ret;
}


x86 assembly (clang, -O0):
<read_directory>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11ba <read_directory+0x5a>
mov    0x2e4a(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    %rax,%rsi
lea    0xe59(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ad <read_directory+0x14d>
movl   $0x0,-0x24(%rbp)
mov    -0x20(%rbp),%rdi
call   1070 <readdir@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11de <read_directory+0x7e>
jmp    1278 <read_directory+0x118>
mov    -0x30(%rbp),%rax
mov    (%rax),%rdi
lea    0xe32(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    1200 <read_directory+0xa0>
jmp    11c1 <read_directory+0x61>
mov    -0x30(%rbp),%rax
mov    (%rax),%rdi
lea    0xe11(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    1222 <read_directory+0xc2>
jmp    11c1 <read_directory+0x61>
cmpq   $0x0,-0x18(%rbp)
je     1258 <read_directory+0xf8>
mov    -0x30(%rbp),%rax
mov    (%rax),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    1253 <read_directory+0xf3>
movl   $0x1,-0x34(%rbp)
jmp    11c1 <read_directory+0x61>
jmp    1258 <read_directory+0xf8>
mov    -0x30(%rbp),%rax
mov    (%rax),%rsi
mov    -0x10(%rbp),%rdx
lea    0xdb7(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x24(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <closedir@plt>
cmpq   $0x0,-0x18(%rbp)
je     12a7 <read_directory+0x147>
cmpl   $0x0,-0x34(%rbp)
jne    12a2 <read_directory+0x142>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ad <read_directory+0x14d>
jmp    12a7 <read_directory+0x147>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_directory>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r14
mov    %rdi,%rbp
call   1030 <opendir@plt>
test   %rax,%rax
je     1205 <read_directory+0xa5>
mov    %rax,%rbx
mov    %rbp,0x10(%rsp)
mov    %r14,0x8(%rsp)
mov    $0x1,%cl
lea    0xe86(%rip),%r12
lea    0xe80(%rip),%r13
xor    %r15d,%r15d
mov    %ecx,%r14d
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rbx,%rdi
call   1070 <readdir@plt>
mov    %rax,%rbp
test   %rax,%rax
je     124b <read_directory+0xeb>
mov    0x0(%rbp),%rdi
mov    %r12,%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
je     11b0 <read_directory+0x50>
mov    0x0(%rbp),%rdi
mov    %r13,%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
je     11b0 <read_directory+0x50>
mov    0x8(%rsp),%rsi
test   %rsi,%rsi
je     122b <read_directory+0xcb>
mov    0x0(%rbp),%rdi
call   1060 <strcmp@plt>
xor    %ecx,%ecx
test   %rax,%rax
jne    122e <read_directory+0xce>
mov    %ecx,%r14d
jmp    11b0 <read_directory+0x50>
mov    0x2dd4(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xde6(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xffffffff,%eax
jmp    126a <read_directory+0x10a>
mov    $0x1,%r14b
mov    0x0(%rbp),%rsi
lea    0xde8(%rip),%rdi
mov    0x10(%rsp),%rdx
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xffffffff,%r15d
mov    %rbx,%rdi
call   1050 <closedir@plt>
test   $0x1,%r14b
mov    $0xffffffff,%eax
cmove  %r15d,%eax
cmpq   $0x0,0x8(%rsp)
cmove  %r15d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i8* }
@.str = [30 x i8] c"Can not open directory %s %s\0A\00"
@errno = global i32 0
@.str.1 = [3 x i8] c"..\00"
@.str.2 = [2 x i8] c".\00"
@.str.3 = [26 x i8] c"%s found in directory %s\0A\00"
define i32 @read_directory(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i32
%8 = alloca %struct.dirent*
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 0, i32* %9
%11 = load i8*, i8** %4
%12 = call i32* @opendir(i8* %11)
store i32* %12, i32** %6
%13 = load i32*, i32** %6
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %19
15: ; preds = %2
%16 = load i32, i32* @errno
%17 = call i8* @strerror(i32 %16)
%18 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %17)
store i32 -1, i32* %3
br label %70
19: ; preds = %2
store i32 0, i32* %7
br label %20
20: ; preds = %19, %32, %39, %50
%21 = load i32*, i32** %6
%22 = call %struct.dirent* @readdir(i32* %21)
store %struct.dirent* %22, %struct.dirent** %8
%23 = load %struct.dirent*, %struct.dirent** %8
%24 = icmp eq %struct.dirent* %23, null
br i1 %24, label %25, label %26
25: ; preds = %20
br label %58
26: ; preds = %20
%27 = load %struct.dirent*, %struct.dirent** %8
%28 = getelementptr inbounds %struct.dirent, %struct.dirent* %27, i32 0, i32 0
%29 = load i8*, i8** %28
%30 = call i64 @strcmp(i8* %29, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%31 = icmp eq i64 %30, 0
br i1 %31, label %32, label %33
32: ; preds = %26
br label %20
33: ; preds = %26
%34 = load %struct.dirent*, %struct.dirent** %8
%35 = getelementptr inbounds %struct.dirent, %struct.dirent* %34, i32 0, i32 0
%36 = load i8*, i8** %35
%37 = call i64 @strcmp(i8* %36, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%38 = icmp eq i64 %37, 0
br i1 %38, label %39, label %40
39: ; preds = %33
br label %20
40: ; preds = %33
%41 = load i8*, i8** %5
%42 = icmp ne i8* %41, null
br i1 %42, label %43, label %52
43: ; preds = %40
%44 = load %struct.dirent*, %struct.dirent** %8
%45 = getelementptr inbounds %struct.dirent, %struct.dirent* %44, i32 0, i32 0
%46 = load i8*, i8** %45
%47 = load i8*, i8** %5
%48 = call i64 @strcmp(i8* %46, i8* %47)
%49 = icmp eq i64 %48, 0
br i1 %49, label %50, label %51
50: ; preds = %43
store i32 1, i32* %9
br label %20
51: ; preds = %43
br label %52
52: ; preds = %51, %40
%53 = load %struct.dirent*, %struct.dirent** %8
%54 = getelementptr inbounds %struct.dirent, %struct.dirent* %53, i32 0, i32 0
%55 = load i8*, i8** %54
%56 = load i8*, i8** %4
%57 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i8* %55, i8* %56)
store i32 -1, i32* %7
br label %58
58: ; preds = %52, %25
%59 = load i32*, i32** %6
%60 = call i32 @closedir(i32* %59)
%61 = load i8*, i8** %5
%62 = icmp ne i8* %61, null
br i1 %62, label %63, label %68
63: ; preds = %58
%64 = load i32, i32* %9
%65 = icmp eq i32 %64, 0
br i1 %65, label %66, label %67
66: ; preds = %63
store i32 -1, i32* %3
br label %70
67: ; preds = %63
br label %68
68: ; preds = %67, %58
%69 = load i32, i32* %7
store i32 %69, i32* %3
br label %70
70: ; preds = %68, %66, %15
%71 = load i32, i32* %3
ret i32 %71
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [30 x i8] c"Can not open directory %s %s\0A\00"
@errno = global i32 0
@.str.1 = [3 x i8] c"..\00"
@.str.2 = [2 x i8] c".\00"
@.str.3 = [26 x i8] c"%s found in directory %s\0A\00"
define i32 @read_directory(i8* %0, i8* %1) {
%3 = tail call i32* @opendir(i8* %0)
%4 = icmp eq i32* %3, null
br i1 %4, label %7, label %5
5: ; preds = %2
%6 = icmp eq i8* %1, null
br label %29
7: ; preds = %2
%8 = load i32, i32* @errno
%9 = tail call i8* @strerror(i32 %8)
%10 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %9)
br label %44
11: ; preds = %29, %17
%12 = phi %struct.dirent* [ %18, %17 ], [ %31, %29 ]
%13 = getelementptr inbounds %struct.dirent, %struct.dirent* %12, i64 0, i32 0
%14 = load i8*, i8** %13
%15 = tail call i64 @strcmp(i8* %14, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %11, %20
%18 = tail call %struct.dirent* @readdir(i32* %3)
%19 = icmp eq %struct.dirent* %18, null
br i1 %19, label %37, label %11
20: ; preds = %11
%21 = load i8*, i8** %13
%22 = tail call i64 @strcmp(i8* %21, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%23 = icmp eq i64 %22, 0
br i1 %23, label %17, label %24
24: ; preds = %20
br i1 %6, label %33, label %25
25: ; preds = %24
%26 = load i8*, i8** %13
%27 = tail call i64 @strcmp(i8* %26, i8* %1)
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %33
29: ; preds = %5, %25
%30 = phi i1 [ true, %5 ], [ false, %25 ]
%31 = tail call %struct.dirent* @readdir(i32* %3)
%32 = icmp eq %struct.dirent* %31, null
br i1 %32, label %37, label %11
33: ; preds = %25, %24
%34 = phi i1 [ %30, %25 ], [ true, %24 ]
%35 = load i8*, i8** %13
%36 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i8* %35, i8* %0)
br label %37
37: ; preds = %29, %17, %33
%38 = phi i1 [ %34, %33 ], [ %30, %17 ], [ %30, %29 ]
%39 = phi i32 [ -1, %33 ], [ 0, %17 ], [ 0, %29 ]
%40 = tail call i32 @closedir(i32* %3)
%41 = icmp ne i8* %1, null
%42 = and i1 %41, %38
%43 = select i1 %42, i32 -1, i32 %39
br label %44
44: ; preds = %37, %7
%45 = phi i32 [ -1, %7 ], [ %43, %37 ]
ret i32 %45
}