#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);
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, int print_flags) {
  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, print_flags);
  }
  closedir(dir);
  return 0;
}


x86 assembly (clang, -O0):
<scan_dir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    0x2e7a(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x50(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    11c6 <scan_dir+0x66>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    12a1 <scan_dir+0x141>
mov    -0x50(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x50(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x50(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x30(%rbp)
movb   $0x2f,(%rax)
mov    -0x38(%rbp),%rdi
call   1070 <readdir@plt>
mov    %rax,-0x40(%rbp)
cmp    $0x0,%rax
je     128a <scan_dir+0x12a>
mov    -0x40(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
jne    1269 <scan_dir+0x109>
mov    -0x40(%rbp),%rax
mov    (%rax),%rax
movsbl 0x1(%rax),%eax
cmp    $0x0,%eax
je     1264 <scan_dir+0x104>
mov    -0x40(%rbp),%rax
mov    (%rax),%rax
movsbl 0x1(%rax),%eax
cmp    $0x2e,%eax
jne    1269 <scan_dir+0x109>
mov    -0x40(%rbp),%rax
mov    (%rax),%rax
movsbl 0x2(%rax),%eax
cmp    $0x0,%eax
jne    1269 <scan_dir+0x109>
jmp    11fe <scan_dir+0x9e>
mov    -0x30(%rbp),%rdi
mov    -0x40(%rbp),%rax
mov    (%rax),%rsi
call   1030 <strcpy@plt>
mov    -0x50(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1080 <open_device@plt>
jmp    11fe <scan_dir+0x9e>
mov    -0x38(%rbp),%rdi
call   1060 <closedir@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
mov    -0x20(%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   %r12
push   %rbx
mov    %esi,%r14d
mov    %rdi,%r12
mov    0x2e78(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%r15
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r15
mov    %r15,%rsp
call   1040 <opendir@plt>
test   %rax,%rax
je     1218 <scan_dir+0xb8>
mov    %rax,%rbx
mov    %r15,%rdi
mov    %r12,%rsi
call   1030 <strcpy@plt>
mov    %r15,%rdi
call   1050 <strlen@plt>
movslq %eax,%r12
movb   $0x2f,(%r15,%r12,1)
mov    %rbx,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
je     120c <scan_dir+0xac>
add    %r15,%r12
add    $0x1,%r12
jmp    11f0 <scan_dir+0x90>
nopl   (%rax)
mov    %r12,%rdi
call   1030 <strcpy@plt>
mov    %r15,%rdi
mov    %r14d,%esi
call   1080 <open_device@plt>
mov    %rbx,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
je     120c <scan_dir+0xac>
mov    (%rax),%rsi
cmpb   $0x2e,(%rsi)
jne    11d0 <scan_dir+0x70>
movzbl 0x1(%rsi),%eax
test   %al,%al
je     11e3 <scan_dir+0x83>
cmp    $0x2e,%al
jne    11d0 <scan_dir+0x70>
cmpb   $0x0,0x2(%rsi)
jne    11d0 <scan_dir+0x70>
jmp    11e3 <scan_dir+0x83>
mov    %rbx,%rdi
call   1060 <closedir@plt>
xor    %eax,%eax
jmp    121d <scan_dir+0xbd>
mov    $0xffffffff,%eax
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
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, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i32*
%10 = alloca %struct.dirent*
%11 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%12 = load i32, i32* @PATH_MAX
%13 = zext i32 %12 to i64
%14 = call i8* @llvm.stacksave()
store i8* %14, i8** %6
%15 = alloca i8, i64 %13
store i64 %13, i64* %7
%16 = load i8*, i8** %4
%17 = call i32* @opendir(i8* %16)
store i32* %17, i32** %9
%18 = load i32*, i32** %9
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %21
20: ; preds = %2
store i32 -1, i32* %3
store i32 1, i32* %11
br label %77
21: ; preds = %2
%22 = load i8*, i8** %4
%23 = call i32 @strcpy(i8* %15, i8* %22)
%24 = call i32 @strlen(i8* %15)
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %15, i64 %25
store i8* %26, i8** %8
%27 = load i8*, i8** %8
%28 = getelementptr inbounds i8, i8* %27, i32 1
store i8* %28, i8** %8
store i8 47, i8* %27
br label %29
29: ; preds = %66, %65, %21
%30 = load i32*, i32** %9
%31 = call %struct.dirent* @readdir(i32* %30)
store %struct.dirent* %31, %struct.dirent** %10
%32 = icmp ne %struct.dirent* %31, null
br i1 %32, label %33, label %74
33: ; preds = %29
%34 = load %struct.dirent*, %struct.dirent** %10
%35 = getelementptr inbounds %struct.dirent, %struct.dirent* %34, i32 0, i32 0
%36 = load i8*, i8** %35
%37 = getelementptr inbounds i8, i8* %36, i64 0
%38 = load i8, i8* %37
%39 = sext i8 %38 to i32
%40 = icmp eq i32 %39, 46
br i1 %40, label %41, label %66
41: ; preds = %33
%42 = load %struct.dirent*, %struct.dirent** %10
%43 = getelementptr inbounds %struct.dirent, %struct.dirent* %42, i32 0, i32 0
%44 = load i8*, i8** %43
%45 = getelementptr inbounds i8, i8* %44, i64 1
%46 = load i8, i8* %45
%47 = sext i8 %46 to i32
%48 = icmp eq i32 %47, 0
br i1 %48, label %65, label %49
49: ; preds = %41
%50 = load %struct.dirent*, %struct.dirent** %10
%51 = getelementptr inbounds %struct.dirent, %struct.dirent* %50, i32 0, i32 0
%52 = load i8*, i8** %51
%53 = getelementptr inbounds i8, i8* %52, i64 1
%54 = load i8, i8* %53
%55 = sext i8 %54 to i32
%56 = icmp eq i32 %55, 46
br i1 %56, label %57, label %66
57: ; preds = %49
%58 = load %struct.dirent*, %struct.dirent** %10
%59 = getelementptr inbounds %struct.dirent, %struct.dirent* %58, i32 0, i32 0
%60 = load i8*, i8** %59
%61 = getelementptr inbounds i8, i8* %60, i64 2
%62 = load i8, i8* %61
%63 = sext i8 %62 to i32
%64 = icmp eq i32 %63, 0
br i1 %64, label %65, label %66
65: ; preds = %57, %41
br label %29
66: ; preds = %57, %49, %33
%67 = load i8*, i8** %8
%68 = load %struct.dirent*, %struct.dirent** %10
%69 = getelementptr inbounds %struct.dirent, %struct.dirent* %68, i32 0, i32 0
%70 = load i8*, i8** %69
%71 = call i32 @strcpy(i8* %67, i8* %70)
%72 = load i32, i32* %5
%73 = call i32 @open_device(i8* %15, i32 %72)
br label %29
74: ; preds = %29
%75 = load i32*, i32** %9
%76 = call i32 @closedir(i32* %75)
store i32 0, i32* %3
store i32 1, i32* %11
br label %77
77: ; preds = %74, %20
%78 = load i8*, i8** %6
%79 = load i32, i32* %3
ret i32 %79
}

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