#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 PATH_MAX;
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ open_device(char *);
int /*<<< orphan*/ *opendir(char const *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ strcpy(char *, char const *);
int strlen(char *);
int scan_dir(const char *dirname) {
  char devname[PATH_MAX];
  char *filename;
  DIR *dir;
  struct dirent *de;
  dir = opendir(dirname);
  if (dir == NULL)
    return -1;
  strcpy(devname, dirname);
  filename = devname + strlen(devname);
  *filename++ = '/';
  while ((de = readdir(dir))) {
    if (de->d_name[0] == '.' &&
        (de->d_name[1] == '\0' ||
         (de->d_name[1] == '.' && de->d_name[2] == '\0')))
      continue;
    strcpy(filename, de->d_name);
    open_device(devname);
  }
  closedir(dir);
  return 0;
}


x86 assembly (clang, -O0):
<scan_dir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e7d(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11c3 <scan_dir+0x63>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    129b <scan_dir+0x13b>
mov    -0x48(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x48(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x48(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x28(%rbp)
movb   $0x2f,(%rax)
mov    -0x30(%rbp),%rdi
call   1070 <readdir@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
je     1284 <scan_dir+0x124>
mov    -0x38(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
jne    1266 <scan_dir+0x106>
mov    -0x38(%rbp),%rax
mov    (%rax),%rax
movsbl 0x1(%rax),%eax
cmp    $0x0,%eax
je     1261 <scan_dir+0x101>
mov    -0x38(%rbp),%rax
mov    (%rax),%rax
movsbl 0x1(%rax),%eax
cmp    $0x2e,%eax
jne    1266 <scan_dir+0x106>
mov    -0x38(%rbp),%rax
mov    (%rax),%rax
movsbl 0x2(%rax),%eax
cmp    $0x0,%eax
jne    1266 <scan_dir+0x106>
jmp    11fb <scan_dir+0x9b>
mov    -0x28(%rbp),%rdi
mov    -0x38(%rbp),%rax
mov    (%rax),%rsi
call   1030 <strcpy@plt>
mov    -0x48(%rbp),%rdi
call   1080 <open_device@plt>
jmp    11fb <scan_dir+0x9b>
mov    -0x30(%rbp),%rdi
call   1060 <closedir@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<scan_dir>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r15
mov    0x2e7c(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%r14
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r14
mov    %r14,%rsp
call   1040 <opendir@plt>
test   %rax,%rax
je     1215 <scan_dir+0xb5>
mov    %rax,%rbx
mov    %r14,%rdi
mov    %r15,%rsi
call   1030 <strcpy@plt>
mov    %r14,%rdi
call   1050 <strlen@plt>
movslq %eax,%r15
movb   $0x2f,(%r14,%r15,1)
mov    %rbx,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
je     1209 <scan_dir+0xa9>
add    %r14,%r15
add    $0x1,%r15
jmp    11ed <scan_dir+0x8d>
nopl   0x0(%rax)
mov    %r15,%rdi
call   1030 <strcpy@plt>
mov    %r14,%rdi
call   1080 <open_device@plt>
mov    %rbx,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
je     1209 <scan_dir+0xa9>
mov    (%rax),%rsi
cmpb   $0x2e,(%rsi)
jne    11d0 <scan_dir+0x70>
movzbl 0x1(%rsi),%eax
test   %al,%al
je     11e0 <scan_dir+0x80>
cmp    $0x2e,%al
jne    11d0 <scan_dir+0x70>
cmpb   $0x0,0x2(%rsi)
jne    11d0 <scan_dir+0x70>
jmp    11e0 <scan_dir+0x80>
mov    %rbx,%rdi
call   1060 <closedir@plt>
xor    %eax,%eax
jmp    121a <scan_dir+0xba>
mov    $0xffffffff,%eax
lea    -0x18(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i8* }
@PATH_MAX = global i32 0
define i32 @scan_dir(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca %struct.dirent*
%9 = alloca i32
store i8* %0, i8** %3
%10 = load i32, i32* @PATH_MAX
%11 = zext i32 %10 to i64
%12 = call i8* @llvm.stacksave()
store i8* %12, i8** %4
%13 = alloca i8, i64 %11
store i64 %11, i64* %5
%14 = load i8*, i8** %3
%15 = call i32* @opendir(i8* %14)
store i32* %15, i32** %7
%16 = load i32*, i32** %7
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %19
18: ; preds = %1
store i32 -1, i32* %2
store i32 1, i32* %9
br label %74
19: ; preds = %1
%20 = load i8*, i8** %3
%21 = call i32 @strcpy(i8* %13, i8* %20)
%22 = call i32 @strlen(i8* %13)
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* %13, i64 %23
store i8* %24, i8** %6
%25 = load i8*, i8** %6
%26 = getelementptr inbounds i8, i8* %25, i32 1
store i8* %26, i8** %6
store i8 47, i8* %25
br label %27
27: ; preds = %64, %63, %19
%28 = load i32*, i32** %7
%29 = call %struct.dirent* @readdir(i32* %28)
store %struct.dirent* %29, %struct.dirent** %8
%30 = icmp ne %struct.dirent* %29, null
br i1 %30, label %31, label %71
31: ; preds = %27
%32 = load %struct.dirent*, %struct.dirent** %8
%33 = getelementptr inbounds %struct.dirent, %struct.dirent* %32, i32 0, i32 0
%34 = load i8*, i8** %33
%35 = getelementptr inbounds i8, i8* %34, i64 0
%36 = load i8, i8* %35
%37 = sext i8 %36 to i32
%38 = icmp eq i32 %37, 46
br i1 %38, label %39, label %64
39: ; preds = %31
%40 = load %struct.dirent*, %struct.dirent** %8
%41 = getelementptr inbounds %struct.dirent, %struct.dirent* %40, i32 0, i32 0
%42 = load i8*, i8** %41
%43 = getelementptr inbounds i8, i8* %42, i64 1
%44 = load i8, i8* %43
%45 = sext i8 %44 to i32
%46 = icmp eq i32 %45, 0
br i1 %46, label %63, label %47
47: ; preds = %39
%48 = load %struct.dirent*, %struct.dirent** %8
%49 = getelementptr inbounds %struct.dirent, %struct.dirent* %48, i32 0, i32 0
%50 = load i8*, i8** %49
%51 = getelementptr inbounds i8, i8* %50, i64 1
%52 = load i8, i8* %51
%53 = sext i8 %52 to i32
%54 = icmp eq i32 %53, 46
br i1 %54, label %55, label %64
55: ; preds = %47
%56 = load %struct.dirent*, %struct.dirent** %8
%57 = getelementptr inbounds %struct.dirent, %struct.dirent* %56, i32 0, i32 0
%58 = load i8*, i8** %57
%59 = getelementptr inbounds i8, i8* %58, i64 2
%60 = load i8, i8* %59
%61 = sext i8 %60 to i32
%62 = icmp eq i32 %61, 0
br i1 %62, label %63, label %64
63: ; preds = %55, %39
br label %27
64: ; preds = %55, %47, %31
%65 = load i8*, i8** %6
%66 = load %struct.dirent*, %struct.dirent** %8
%67 = getelementptr inbounds %struct.dirent, %struct.dirent* %66, i32 0, i32 0
%68 = load i8*, i8** %67
%69 = call i32 @strcpy(i8* %65, i8* %68)
%70 = call i32 @open_device(i8* %13)
br label %27
71: ; preds = %27
%72 = load i32*, i32** %7
%73 = call i32 @closedir(i32* %72)
store i32 0, i32* %2
store i32 1, i32* %9
br label %74
74: ; preds = %71, %18
%75 = load i8*, i8** %4
%76 = load i32, i32* %2
ret i32 %76
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@PATH_MAX = global i32 0
define i32 @scan_dir(i8* %0) {
%2 = load i32, i32* @PATH_MAX
%3 = zext i32 %2 to i64
%4 = alloca i8, i64 %3
%5 = tail call i32* @opendir(i8* %0)
%6 = icmp eq i32* %5, null
br i1 %6, label %36, label %7
7: ; preds = %1
%8 = call i32 @strcpy(i8* %4, i8* %0)
%9 = call i32 @strlen(i8* %4)
%10 = sext i32 %9 to i64
%11 = getelementptr inbounds i8, i8* %4, i64 %10
%12 = getelementptr inbounds i8, i8* %11, i64 1
store i8 47, i8* %11
%13 = call %struct.dirent* @readdir(i32* %5)
%14 = icmp eq %struct.dirent* %13, null
br i1 %14, label %34, label %15
15: ; preds = %7, %28
%16 = phi %struct.dirent* [ %29, %28 ], [ %13, %7 ]
%17 = getelementptr inbounds %struct.dirent, %struct.dirent* %16, i64 0, i32 0
%18 = load i8*, i8** %17
%19 = load i8, i8* %18
%20 = icmp eq i8 %19, 46
br i1 %20, label %21, label %31
21: ; preds = %15
%22 = getelementptr inbounds i8, i8* %18, i64 1
%23 = load i8, i8* %22
switch i8 %23, label %31 [
i8 0, label %28
i8 46, label %24
]
24: ; preds = %21
%25 = getelementptr inbounds i8, i8* %18, i64 2
%26 = load i8, i8* %25
%27 = icmp eq i8 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %24, %21, %31
%29 = call %struct.dirent* @readdir(i32* %5)
%30 = icmp eq %struct.dirent* %29, null
br i1 %30, label %34, label %15
31: ; preds = %21, %24, %15
%32 = call i32 @strcpy(i8* %12, i8* %18)
%33 = call i32 @open_device(i8* %4)
br label %28
34: ; preds = %28, %7
%35 = call i32 @closedir(i32* %5)
br label %36
36: ; preds = %1, %34
%37 = phi i32 [ 0, %34 ], [ -1, %1 ]
ret i32 %37
}