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

/* Variables and functions */
int EXIT_FAILURE;
int EXIT_SUCCESS;
int S_IFDIR;
int S_IFREG;
scalar_t__ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(scalar_t__);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ perror(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int scan_file(char *, int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(char *, char *, char const *, char const *);
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcmp(char const *, char *);
int /*<<< orphan*/ strcpy(char *, char const *);
scalar_t__ strlen(char const *);
int process_path(const char *directory, const char *filename) {
  struct stat filestat;
  char *path;
  if ((!strcmp(filename, ".")) || (!strcmp(filename, "..")))
    return EXIT_FAILURE;
  path = (char *)malloc(strlen(directory) + strlen(filename) + 2);
  if (!strcmp(directory, ""))
    strcpy(path, filename);
  else
    sprintf(path, "%s/%s", directory, filename);
  if (stat(path, &filestat) < 0) {
    perror(path);
    free(path);
    return EXIT_FAILURE;
  }
  if (filestat.st_mode & S_IFREG) {
    int ret = scan_file(path, filestat.st_size);
    free(path);
    return ret;
  }
  if (filestat.st_mode & S_IFDIR) {
    struct dirent *entry;
    DIR *dir = opendir(path);
    if (dir == NULL) {
      fprintf(stderr, "%s: opendir failed\n", path);
      free(path);
      return EXIT_FAILURE;
    }
    while ((entry = readdir(dir)) != NULL)
      process_path(path, entry->d_name);
    if (closedir(dir) < 0) {
      fprintf(stderr, "%s: closedir failed\n", path);
      free(path);
      return EXIT_FAILURE;
    }
    free(path);
    return EXIT_SUCCESS;
  }
  free(path);
  return EXIT_FAILURE;
}


x86 assembly (clang, -O0):
<process_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe06(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%eax
je     1222 <process_path+0x42>
mov    -0x18(%rbp),%rdi
lea    0xdec(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%eax
jne    1233 <process_path+0x53>
mov    0x2da7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1418 <process_path+0x238>
mov    -0x10(%rbp),%rdi
call   1070 <strlen@plt>
mov    %rax,-0x48(%rbp)
mov    -0x18(%rbp),%rdi
call   1070 <strlen@plt>
mov    -0x48(%rbp),%rdi
add    %rax,%rdi
add    $0x2,%rdi
call   10d0 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xdc9(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%eax
jne    1288 <process_path+0xa8>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <strcpy@plt>
jmp    12a0 <process_path+0xc0>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
lea    0xd68(%rip),%rsi
call   10f0 <sprintf@plt>
mov    -0x28(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   10b0 <stat@plt>
cmp    $0x0,%rax
jge    12da <process_path+0xfa>
mov    -0x28(%rbp),%rdi
call   10e0 <perror@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    0x2d00(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1418 <process_path+0x238>
mov    -0x20(%rbp),%eax
mov    0x2d04(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1312 <process_path+0x132>
mov    -0x28(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   1100 <scan_file@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1418 <process_path+0x238>
mov    -0x20(%rbp),%eax
mov    0x2c9c(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1403 <process_path+0x223>
mov    -0x28(%rbp),%rdi
call   1060 <opendir@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    1372 <process_path+0x192>
mov    0x2c92(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%rdx
lea    0xcb6(%rip),%rsi
call   10a0 <fprintf@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    0x2c68(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1418 <process_path+0x238>
jmp    1377 <process_path+0x197>
mov    -0x40(%rbp),%rdi
call   10c0 <readdir@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
je     13a3 <process_path+0x1c3>
mov    -0x28(%rbp),%rdi
mov    -0x38(%rbp),%rax
mov    (%rax),%rsi
call   1050 <process_path@plt>
jmp    1377 <process_path+0x197>
mov    -0x40(%rbp),%rdi
call   1080 <closedir@plt>
cmp    $0x0,%rax
jge    13e9 <process_path+0x209>
mov    0x2c1b(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%rdx
lea    0xc53(%rip),%rsi
call   10a0 <fprintf@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    0x2bf1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1418 <process_path+0x238>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    0x2bcf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1418 <process_path+0x238>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    0x2bbd(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<process_path>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
mov    %rdi,%r15
lea    0xe0b(%rip),%rsi
mov    %rbx,%rdi
call   1090 <strcmp@plt>
test   %eax,%eax
je     1309 <process_path+0x129>
lea    0xdf3(%rip),%rsi
mov    %rbx,%rdi
call   1090 <strcmp@plt>
test   %eax,%eax
je     1309 <process_path+0x129>
mov    %r15,%rdi
call   1070 <strlen@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1070 <strlen@plt>
lea    (%r14,%rax,1),%rdi
add    $0x2,%rdi
call   10d0 <malloc@plt>
mov    %rax,%r14
cmpb   $0x0,(%r15)
je     125d <process_path+0x7d>
lea    0xdb6(%rip),%rsi
mov    %r14,%rdi
mov    %r15,%rdx
mov    %rbx,%rcx
call   10f0 <sprintf@plt>
jmp    1268 <process_path+0x88>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1040 <strcpy@plt>
lea    0x8(%rsp),%rsi
mov    %r14,%rdi
call   10b0 <stat@plt>
test   %rax,%rax
js     12f9 <process_path+0x119>
mov    0x8(%rsp),%eax
mov    0x2d5f(%rip),%rcx
test   %eax,(%rcx)
je     12a5 <process_path+0xc5>
mov    0xc(%rsp),%esi
mov    %r14,%rdi
call   1100 <scan_file@plt>
mov    %eax,%ebx
mov    %r14,%rdi
call   1030 <free@plt>
jmp    1312 <process_path+0x132>
mov    0x2d0c(%rip),%rcx
test   %eax,(%rcx)
je     1301 <process_path+0x121>
mov    %r14,%rdi
call   1060 <opendir@plt>
test   %rax,%rax
je     12f0 <process_path+0x110>
mov    %rax,%rbx
mov    %rbx,%rdi
call   10c0 <readdir@plt>
test   %rax,%rax
je     12da <process_path+0xfa>
mov    (%rax),%rsi
mov    %r14,%rdi
call   1050 <process_path@plt>
jmp    12c0 <process_path+0xe0>
mov    %rbx,%rdi
call   1080 <closedir@plt>
test   %rax,%rax
js     131e <process_path+0x13e>
mov    0x2cda(%rip),%rbx
jmp    133d <process_path+0x15d>
lea    0xd12(%rip),%rsi
jmp    1325 <process_path+0x145>
mov    %r14,%rdi
call   10e0 <perror@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    0x2cc0(%rip),%rax
mov    (%rax),%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xcf8(%rip),%rsi
mov    0x2cac(%rip),%rax
mov    (%rax),%edi
mov    %r14,%rdx
call   10a0 <fprintf@plt>
mov    0x2c93(%rip),%rbx
mov    %r14,%rdi
call   1030 <free@plt>
mov    (%rbx),%ebx
jmp    1312 <process_path+0x132>

LLVM IR (-O0):
%struct.stat = type { i32, i32 }
%struct.dirent = type { i8* }
@.str = [2 x i8] c".\00"
@.str.1 = [3 x i8] c"..\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [1 x i8] zeroinitializer
@.str.3 = [6 x i8] c"%s/%s\00"
@S_IFREG = global i32 0
@S_IFDIR = global i32 0
@stderr = global i32 0
@.str.4 = [20 x i8] c"%s: opendir failed\0A\00"
@.str.5 = [21 x i8] c"%s: closedir failed\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @process_path(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca %struct.stat
%7 = alloca i8*
%8 = alloca i32
%9 = alloca %struct.dirent*
%10 = alloca i32*
store i8* %0, i8** %4
store i8* %1, i8** %5
%11 = load i8*, i8** %5
%12 = call i32 @strcmp(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%13 = icmp ne i32 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %2
%15 = load i8*, i8** %5
%16 = call i32 @strcmp(i8* %15, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%17 = icmp ne i32 %16, 0
br i1 %17, label %20, label %18
18: ; preds = %14, %2
%19 = load i32, i32* @EXIT_FAILURE
store i32 %19, i32* %3
br label %113
20: ; preds = %14
%21 = load i8*, i8** %4
%22 = call i64 @strlen(i8* %21)
%23 = load i8*, i8** %5
%24 = call i64 @strlen(i8* %23)
%25 = add nsw i64 %22, %24
%26 = add nsw i64 %25, 2
%27 = call i64 @malloc(i64 %26)
%28 = inttoptr i64 %27 to i8*
store i8* %28, i8** %7
%29 = load i8*, i8** %4
%30 = call i32 @strcmp(i8* %29, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0))
%31 = icmp ne i32 %30, 0
br i1 %31, label %36, label %32
32: ; preds = %20
%33 = load i8*, i8** %7
%34 = load i8*, i8** %5
%35 = call i32 @strcpy(i8* %33, i8* %34)
br label %41
36: ; preds = %20
%37 = load i8*, i8** %7
%38 = load i8*, i8** %4
%39 = load i8*, i8** %5
%40 = call i32 @sprintf(i8* %37, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* %38, i8* %39)
br label %41
41: ; preds = %36, %32
%42 = load i8*, i8** %7
%43 = call i64 @stat(i8* %42, %struct.stat* %6)
%44 = icmp slt i64 %43, 0
br i1 %44, label %45, label %51
45: ; preds = %41
%46 = load i8*, i8** %7
%47 = call i32 @perror(i8* %46)
%48 = load i8*, i8** %7
%49 = call i32 @free(i8* %48)
%50 = load i32, i32* @EXIT_FAILURE
store i32 %50, i32* %3
br label %113
51: ; preds = %41
%52 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%53 = load i32, i32* %52
%54 = load i32, i32* @S_IFREG
%55 = and i32 %53, %54
%56 = icmp ne i32 %55, 0
br i1 %56, label %57, label %65
57: ; preds = %51
%58 = load i8*, i8** %7
%59 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 1
%60 = load i32, i32* %59
%61 = call i32 @scan_file(i8* %58, i32 %60)
store i32 %61, i32* %8
%62 = load i8*, i8** %7
%63 = call i32 @free(i8* %62)
%64 = load i32, i32* %8
store i32 %64, i32* %3
br label %113
65: ; preds = %51
%66 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%67 = load i32, i32* %66
%68 = load i32, i32* @S_IFDIR
%69 = and i32 %67, %68
%70 = icmp ne i32 %69, 0
br i1 %70, label %71, label %109
71: ; preds = %65
%72 = load i8*, i8** %7
%73 = call i32* @opendir(i8* %72)
store i32* %73, i32** %10
%74 = load i32*, i32** %10
%75 = icmp eq i32* %74, null
br i1 %75, label %76, label %83
76: ; preds = %71
%77 = load i32, i32* @stderr
%78 = load i8*, i8** %7
%79 = call i32 @fprintf(i32 %77, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i8* %78)
%80 = load i8*, i8** %7
%81 = call i32 @free(i8* %80)
%82 = load i32, i32* @EXIT_FAILURE
store i32 %82, i32* %3
br label %113
83: ; preds = %71
br label %84
84: ; preds = %88, %83
%85 = load i32*, i32** %10
%86 = call %struct.dirent* @readdir(i32* %85)
store %struct.dirent* %86, %struct.dirent** %9
%87 = icmp ne %struct.dirent* %86, null
br i1 %87, label %88, label %94
88: ; preds = %84
%89 = load i8*, i8** %7
%90 = load %struct.dirent*, %struct.dirent** %9
%91 = getelementptr inbounds %struct.dirent, %struct.dirent* %90, i32 0, i32 0
%92 = load i8*, i8** %91
%93 = call i32 @process_path(i8* %89, i8* %92)
br label %84
94: ; preds = %84
%95 = load i32*, i32** %10
%96 = call i64 @closedir(i32* %95)
%97 = icmp slt i64 %96, 0
br i1 %97, label %98, label %105
98: ; preds = %94
%99 = load i32, i32* @stderr
%100 = load i8*, i8** %7
%101 = call i32 @fprintf(i32 %99, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), i8* %100)
%102 = load i8*, i8** %7
%103 = call i32 @free(i8* %102)
%104 = load i32, i32* @EXIT_FAILURE
store i32 %104, i32* %3
br label %113
105: ; preds = %94
%106 = load i8*, i8** %7
%107 = call i32 @free(i8* %106)
%108 = load i32, i32* @EXIT_SUCCESS
store i32 %108, i32* %3
br label %113
109: ; preds = %65
%110 = load i8*, i8** %7
%111 = call i32 @free(i8* %110)
%112 = load i32, i32* @EXIT_FAILURE
store i32 %112, i32* %3
br label %113
113: ; preds = %109, %105, %98, %76, %57, %45, %18
%114 = load i32, i32* %3
ret i32 %114
}

LLVM IR (-O2):
%struct.stat = type { i32, i32 }
%struct.dirent = type { i8* }
@.str = [2 x i8] c".\00"
@.str.1 = [3 x i8] c"..\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [6 x i8] c"%s/%s\00"
@S_IFREG = global i32 0
@S_IFDIR = global i32 0
@stderr = global i32 0
@.str.4 = [20 x i8] c"%s: opendir failed\0A\00"
@.str.5 = [21 x i8] c"%s: closedir failed\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @process_path(i8* %0, i8* %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = tail call i32 @strcmp(i8* dereferenceable(1) %1, i8* dereferenceable(2) getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32 %5, 0
br i1 %6, label %10, label %7
7: ; preds = %2
%8 = tail call i32 @strcmp(i8* dereferenceable(1) %1, i8* dereferenceable(3) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %7, %2
%11 = load i32, i32* @EXIT_FAILURE
br label %74
12: ; preds = %7
%13 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%14 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%15 = add i64 %13, 2
%16 = add i64 %15, %14
%17 = tail call i64 @malloc(i64 %16)
%18 = inttoptr i64 %17 to i8*
%19 = load i8, i8* %0
%20 = icmp eq i8 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %12
%22 = tail call i32 @strcpy(i8* %18, i8* %1)
br label %25
23: ; preds = %12
%24 = tail call i32 @sprintf(i8* dereferenceable(1) %18, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* %0, i8* %1)
br label %25
25: ; preds = %23, %21
%26 = call i64 @stat(i8* %18, %struct.stat* %3)
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %32
28: ; preds = %25
%29 = call i32 @perror(i8* %18)
%30 = call i32 @free(i8* %18)
%31 = load i32, i32* @EXIT_FAILURE
br label %74
32: ; preds = %25
%33 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%34 = load i32, i32* %33
%35 = load i32, i32* @S_IFREG
%36 = and i32 %35, %34
%37 = icmp eq i32 %36, 0
br i1 %37, label %43, label %38
38: ; preds = %32
%39 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 1
%40 = load i32, i32* %39
%41 = call i32 @scan_file(i8* %18, i32 %40)
%42 = call i32 @free(i8* %18)
br label %74
43: ; preds = %32
%44 = load i32, i32* @S_IFDIR
%45 = and i32 %44, %34
%46 = icmp eq i32 %45, 0
br i1 %46, label %71, label %47
47: ; preds = %43
%48 = call i32* @opendir(i8* %18)
%49 = icmp eq i32* %48, null
br i1 %49, label %63, label %50
50: ; preds = %47
%51 = call %struct.dirent* @readdir(i32* %48)
%52 = icmp eq %struct.dirent* %51, null
br i1 %52, label %60, label %53
53: ; preds = %50, %53
%54 = phi %struct.dirent* [ %58, %53 ], [ %51, %50 ]
%55 = getelementptr inbounds %struct.dirent, %struct.dirent* %54, i64 0, i32 0
%56 = load i8*, i8** %55
%57 = call i32 @process_path(i8* %18, i8* %56)
%58 = call %struct.dirent* @readdir(i32* %48)
%59 = icmp eq %struct.dirent* %58, null
br i1 %59, label %60, label %53
60: ; preds = %53, %50
%61 = call i64 @closedir(i32* %48)
%62 = icmp slt i64 %61, 0
br i1 %62, label %63, label %67
63: ; preds = %60, %47
%64 = phi i8* [ getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), %47 ], [ getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), %60 ]
%65 = load i32, i32* @stderr
%66 = call i32 @fprintf(i32 %65, i8* %64, i8* %18)
br label %67
67: ; preds = %63, %60
%68 = phi i32* [ @EXIT_SUCCESS, %60 ], [ @EXIT_FAILURE, %63 ]
%69 = call i32 @free(i8* %18)
%70 = load i32, i32* %68
br label %74
71: ; preds = %43
%72 = call i32 @free(i8* %18)
%73 = load i32, i32* @EXIT_FAILURE
br label %74
74: ; preds = %71, %67, %38, %28, %10
%75 = phi i32 [ %31, %28 ], [ %41, %38 ], [ %70, %67 ], [ %73, %71 ], [ %11, %10 ]
ret i32 %75
}