#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 */
scalar_t__ S_ISDIR(int /*<<< orphan*/);
scalar_t__ S_ISREG(int /*<<< orphan*/);
int /*<<< orphan*/ chdir(char const *);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ lstat(char *, struct stat *);
int /*<<< orphan*/ *opendir(char const *);
int /*<<< orphan*/ perror(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ scan_file(char const *, char *);
scalar_t__ strcmp(char *, char *);
void scan_dir(const char *keyword, const char *dir_name, int hierarchy) {
  DIR *dir = NULL;
  ;
  struct dirent *dp = NULL;
  struct stat statbuf;
  int space_num = hierarchy * 2;
  hierarchy++;
  if ((dir = opendir(dir_name)) == NULL) {
    perror("opendir");
    exit(-1);
  }
  chdir(dir_name);
  for (dp = readdir(dir); dp != NULL; dp = readdir(dir)) {
    if (strcmp(".", dp->d_name) == 0 || strcmp("..", dp->d_name) == 0) {
      continue;
    }
    lstat(dp->d_name, &statbuf);
    if (S_ISDIR(statbuf.st_mode)) {
      scan_dir(keyword, dp->d_name, hierarchy);
    }
    if (S_ISREG(statbuf.st_mode)) {
      scan_file(keyword, dp->d_name);
    }
  }
  chdir("..");
  closedir(dir);
}


x86 assembly (clang, -O0):
<scan_dir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x14(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x34(%rbp)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1222 <scan_dir+0x62>
lea    0xded(%rip),%rdi
call   10c0 <perror@plt>
mov    $0xffffffff,%edi
call   10d0 <exit@plt>
mov    -0x10(%rbp),%rdi
call   1050 <chdir@plt>
mov    -0x20(%rbp),%rdi
call   10b0 <readdir@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     12f0 <scan_dir+0x130>
mov    -0x28(%rbp),%rax
mov    (%rax),%rsi
lea    0xdb8(%rip),%rdi
call   1090 <strcmp@plt>
cmp    $0x0,%rax
je     127d <scan_dir+0xbd>
mov    -0x28(%rbp),%rax
mov    (%rax),%rsi
lea    0xd9a(%rip),%rdi
call   1090 <strcmp@plt>
cmp    $0x0,%rax
jne    1282 <scan_dir+0xc2>
jmp    12de <scan_dir+0x11e>
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
lea    -0x30(%rbp),%rsi
call   1080 <lstat@plt>
mov    -0x30(%rbp),%edi
call   1060 <S_ISDIR@plt>
cmp    $0x0,%rax
je     12b7 <scan_dir+0xf7>
mov    -0x8(%rbp),%rdi
mov    -0x28(%rbp),%rax
mov    (%rax),%rsi
mov    -0x14(%rbp),%edx
call   1030 <scan_dir@plt>
mov    -0x30(%rbp),%edi
call   10a0 <S_ISREG@plt>
cmp    $0x0,%rax
je     12d9 <scan_dir+0x119>
mov    -0x8(%rbp),%rdi
mov    -0x28(%rbp),%rax
mov    (%rax),%rsi
call   10e0 <scan_file@plt>
jmp    12de <scan_dir+0x11e>
mov    -0x20(%rbp),%rdi
call   10b0 <readdir@plt>
mov    %rax,-0x28(%rbp)
jmp    1238 <scan_dir+0x78>
lea    0xd11(%rip),%rdi
call   1050 <chdir@plt>
mov    -0x20(%rbp),%rdi
call   1070 <closedir@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<scan_dir>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edx,0xc(%rsp)
mov    %rsi,%rbp
mov    %rdi,%r15
mov    %rsi,%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     12bf <scan_dir+0xff>
mov    %rax,%rbx
mov    %rbp,%rdi
call   1050 <chdir@plt>
mov    %rbx,%rdi
call   10b0 <readdir@plt>
test   %rax,%rax
je     129d <scan_dir+0xdd>
mov    %rax,%r13
addl   $0x1,0xc(%rsp)
lea    0xdf5(%rip),%rbp
lea    0xded(%rip),%r12
jmp    1230 <scan_dir+0x70>
nopl   (%rax)
mov    %rbx,%rdi
call   10b0 <readdir@plt>
mov    %rax,%r13
test   %rax,%rax
je     129d <scan_dir+0xdd>
mov    0x0(%r13),%rsi
mov    %rbp,%rdi
call   1090 <strcmp@plt>
test   %rax,%rax
je     1220 <scan_dir+0x60>
mov    0x0(%r13),%rsi
mov    %r12,%rdi
call   1090 <strcmp@plt>
test   %rax,%rax
je     1220 <scan_dir+0x60>
mov    0x0(%r13),%rdi
lea    0x10(%rsp),%rsi
call   1080 <lstat@plt>
mov    0x10(%rsp),%r14d
mov    %r14d,%edi
call   1060 <S_ISDIR@plt>
test   %rax,%rax
je     1282 <scan_dir+0xc2>
mov    0x0(%r13),%rsi
mov    %r15,%rdi
mov    0xc(%rsp),%edx
call   1030 <scan_dir@plt>
mov    %r14d,%edi
call   10a0 <S_ISREG@plt>
test   %rax,%rax
je     1220 <scan_dir+0x60>
mov    0x0(%r13),%rsi
mov    %r15,%rdi
call   10e0 <scan_file@plt>
jmp    1220 <scan_dir+0x60>
lea    0xd64(%rip),%rdi
call   1050 <chdir@plt>
mov    %rbx,%rdi
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1070 <closedir@plt>
lea    0xd3a(%rip),%rdi
call   10c0 <perror@plt>
mov    $0xffffffff,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
%struct.dirent = type { i8* }
%struct.stat = type { i32 }
@.str = [8 x i8] c"opendir\00"
@.str.1 = [2 x i8] c".\00"
@.str.2 = [3 x i8] c"..\00"
define void @scan_dir(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32*
%8 = alloca %struct.dirent*
%9 = alloca %struct.stat
%10 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
store i32* null, i32** %7
store %struct.dirent* null, %struct.dirent** %8
%11 = load i32, i32* %6
%12 = mul nsw i32 %11, 2
store i32 %12, i32* %10
%13 = load i32, i32* %6
%14 = add nsw i32 %13, 1
store i32 %14, i32* %6
%15 = load i8*, i8** %5
%16 = call i32* @opendir(i8* %15)
store i32* %16, i32** %7
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %21
18: ; preds = %3
%19 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%20 = call i32 @exit(i32 -1)
unreachable
21: ; preds = %3
%22 = load i8*, i8** %5
%23 = call i32 @chdir(i8* %22)
%24 = load i32*, i32** %7
%25 = call %struct.dirent* @readdir(i32* %24)
store %struct.dirent* %25, %struct.dirent** %8
br label %26
26: ; preds = %69, %21
%27 = load %struct.dirent*, %struct.dirent** %8
%28 = icmp ne %struct.dirent* %27, null
br i1 %28, label %29, label %72
29: ; preds = %26
%30 = load %struct.dirent*, %struct.dirent** %8
%31 = getelementptr inbounds %struct.dirent, %struct.dirent* %30, i32 0, i32 0
%32 = load i8*, i8** %31
%33 = call i64 @strcmp(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8* %32)
%34 = icmp eq i64 %33, 0
br i1 %34, label %41, label %35
35: ; preds = %29
%36 = load %struct.dirent*, %struct.dirent** %8
%37 = getelementptr inbounds %struct.dirent, %struct.dirent* %36, i32 0, i32 0
%38 = load i8*, i8** %37
%39 = call i64 @strcmp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %38)
%40 = icmp eq i64 %39, 0
br i1 %40, label %41, label %42
41: ; preds = %35, %29
br label %69
42: ; preds = %35
%43 = load %struct.dirent*, %struct.dirent** %8
%44 = getelementptr inbounds %struct.dirent, %struct.dirent* %43, i32 0, i32 0
%45 = load i8*, i8** %44
%46 = call i32 @lstat(i8* %45, %struct.stat* %9)
%47 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%48 = load i32, i32* %47
%49 = call i64 @S_ISDIR(i32 %48)
%50 = icmp ne i64 %49, 0
br i1 %50, label %51, label %57
51: ; preds = %42
%52 = load i8*, i8** %4
%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 i32, i32* %6
br label %57
57: ; preds = %51, %42
%58 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%59 = load i32, i32* %58
%60 = call i64 @S_ISREG(i32 %59)
%61 = icmp ne i64 %60, 0
br i1 %61, label %62, label %68
62: ; preds = %57
%63 = load i8*, i8** %4
%64 = load %struct.dirent*, %struct.dirent** %8
%65 = getelementptr inbounds %struct.dirent, %struct.dirent* %64, i32 0, i32 0
%66 = load i8*, i8** %65
%67 = call i32 @scan_file(i8* %63, i8* %66)
br label %68
68: ; preds = %62, %57
br label %69
69: ; preds = %68, %41
%70 = load i32*, i32** %7
%71 = call %struct.dirent* @readdir(i32* %70)
store %struct.dirent* %71, %struct.dirent** %8
br label %26
72: ; preds = %26
%73 = call i32 @chdir(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%74 = load i32*, i32** %7
%75 = call i32 @closedir(i32* %74)
ret void
}

LLVM IR (-O2):
%struct.stat = type { i32 }
%struct.dirent = type { i8* }
@.str = [8 x i8] c"opendir\00"
@.str.1 = [2 x i8] c".\00"
@.str.2 = [3 x i8] c"..\00"
define void @scan_dir(i8* %0, i8* %1, i32 %2) {
%4 = alloca %struct.stat
%5 = bitcast %struct.stat* %4 to i8*
%6 = add nsw i32 %2, 1
%7 = tail call i32* @opendir(i8* %1)
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %12
9: ; preds = %3
%10 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%11 = tail call i32 @exit(i32 -1)
unreachable
12: ; preds = %3
%13 = tail call i32 @chdir(i8* %1)
%14 = tail call %struct.dirent* @readdir(i32* %7)
%15 = icmp eq %struct.dirent* %14, null
br i1 %15, label %45, label %16
16: ; preds = %12
%17 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
br label %18
18: ; preds = %16, %42
%19 = phi %struct.dirent* [ %14, %16 ], [ %43, %42 ]
%20 = getelementptr inbounds %struct.dirent, %struct.dirent* %19, i64 0, i32 0
%21 = load i8*, i8** %20
%22 = tail call i64 @strcmp(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8* %21)
%23 = icmp eq i64 %22, 0
br i1 %23, label %42, label %24
24: ; preds = %18
%25 = load i8*, i8** %20
%26 = tail call i64 @strcmp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %25)
%27 = icmp eq i64 %26, 0
br i1 %27, label %42, label %28
28: ; preds = %24
%29 = load i8*, i8** %20
%30 = call i32 @lstat(i8* %29, %struct.stat* %4)
%31 = load i32, i32* %17
%32 = tail call i64 @S_ISDIR(i32 %31)
%33 = icmp eq i64 %32, 0
br i1 %33, label %36, label %34
34: ; preds = %28
%35 = load i8*, i8** %20
tail call void @scan_dir(i8* %0, i8* %35, i32 %6)
br label %36
36: ; preds = %34, %28
%37 = tail call i64 @S_ISREG(i32 %31)
%38 = icmp eq i64 %37, 0
br i1 %38, label %42, label %39
39: ; preds = %36
%40 = load i8*, i8** %20
%41 = tail call i32 @scan_file(i8* %0, i8* %40)
br label %42
42: ; preds = %36, %39, %18, %24
%43 = tail call %struct.dirent* @readdir(i32* %7)
%44 = icmp eq %struct.dirent* %43, null
br i1 %44, label %45, label %18
45: ; preds = %42, %12
%46 = tail call i32 @chdir(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%47 = tail call i32 @closedir(i32* %7)
ret void
}