#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 queue {
  int dummy;
};
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*/ checked_closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ checked_lstat(char *, struct stat *);
char *checked_malloc(int);
int /*<<< orphan*/ *checked_opendir(char *);
struct dirent *checked_readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ enqueue(struct queue *, char *);
int /*<<< orphan*/ fprintf_ts(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ free(char *);
int snprintf(char *, int, char *, char *, char *);
int /*<<< orphan*/ stderr;
scalar_t__ strcmp(char *, char *);
int strlen(char *);
void traverse(char *path, struct queue *q) {
  DIR *d = checked_opendir(path);
  struct dirent *entry;
  struct stat stat;
  int p_size;
  if (!d) {
    fprintf_ts(stderr, "Cannot open directory %s\n", path);
    return;
  }
  p_size = strlen(path);
  while ((entry = checked_readdir(d))) {
    int extra;
    int full;
    int len;
    char *new_path;
    if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {
      continue;
    }
    extra = strlen(entry->d_name);
    full = p_size + extra + 2;
    new_path = checked_malloc(sizeof(char) * full);
    len = snprintf(new_path, full, "%s/%s", path, entry->d_name);
    new_path[len] = 0;
    checked_lstat(new_path, &stat);
    if (S_ISDIR(stat.st_mode)) {
      traverse(new_path, q);
      free(new_path);
      continue;
    }
    if (S_ISREG(stat.st_mode)) {
      enqueue(q, new_path);
    }
  }
  checked_closedir(d);
}


x86 assembly (clang, -O0):
<traverse>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   10f0 <checked_opendir@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1226 <traverse+0x46>
mov    0x2dd1(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xde4(%rip),%rsi
call   1040 <fprintf_ts@plt>
jmp    134a <traverse+0x16a>
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x18(%rbp),%rdi
call   1070 <checked_readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     1341 <traverse+0x161>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
lea    0xdc4(%rip),%rsi
call   10b0 <strcmp@plt>
cmp    $0x0,%rax
je     1283 <traverse+0xa3>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
lea    0xda6(%rip),%rsi
call   10b0 <strcmp@plt>
cmp    $0x0,%rax
jne    1288 <traverse+0xa8>
jmp    1232 <traverse+0x52>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x30(%rbp)
mov    -0x2c(%rbp),%eax
add    -0x30(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x34(%rbp)
movslq -0x34(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   10a0 <checked_malloc@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
mov    -0x34(%rbp),%esi
mov    -0x8(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    (%rax),%r8
lea    0xd4e(%rip),%rdx
call   1080 <snprintf@plt>
mov    %eax,-0x38(%rbp)
mov    -0x40(%rbp),%rax
movslq -0x38(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x40(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   10c0 <checked_lstat@plt>
mov    -0x28(%rbp),%edi
call   1060 <S_ISDIR@plt>
cmp    $0x0,%rax
je     131d <traverse+0x13d>
mov    -0x40(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1090 <traverse@plt>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
jmp    1232 <traverse+0x52>
mov    -0x28(%rbp),%edi
call   10d0 <S_ISREG@plt>
cmp    $0x0,%rax
je     133c <traverse+0x15c>
mov    -0x10(%rbp),%rdi
mov    -0x40(%rbp),%rsi
call   1100 <enqueue@plt>
jmp    1232 <traverse+0x52>
mov    -0x18(%rbp),%rdi
call   10e0 <checked_closedir@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<traverse>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,0x10(%rsp)
mov    %rdi,%r15
call   10f0 <checked_opendir@plt>
test   %rax,%rax
je     1322 <traverse+0x142>
mov    %rax,%rbx
mov    %r15,%rdi
call   1050 <strlen@plt>
mov    %rax,0x8(%rsp)
mov    %rbx,%rdi
call   1070 <checked_readdir@plt>
test   %rax,%rax
je     130b <traverse+0x12b>
mov    %rax,%rbp
mov    0x8(%rsp),%rax
add    $0x2,%eax
mov    %rax,0x8(%rsp)
lea    0xddf(%rip),%r13
jmp    1269 <traverse+0x89>
xchg   %ax,%ax
mov    %r14,%rdi
mov    0x10(%rsp),%rsi
call   1090 <traverse@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   1070 <checked_readdir@plt>
mov    %rax,%rbp
test   %rax,%rax
je     130b <traverse+0x12b>
mov    0x0(%rbp),%rdi
mov    %r13,%rsi
call   10b0 <strcmp@plt>
test   %rax,%rax
je     1255 <traverse+0x75>
mov    0x0(%rbp),%rdi
lea    0xd95(%rip),%rsi
call   10b0 <strcmp@plt>
test   %rax,%rax
je     1255 <traverse+0x75>
mov    0x0(%rbp),%rdi
call   1050 <strlen@plt>
mov    0x8(%rsp),%rcx
lea    (%rax,%rcx,1),%r12d
mov    %r12d,%edi
call   10a0 <checked_malloc@plt>
mov    %rax,%r14
mov    0x0(%rbp),%r8
mov    %rax,%rdi
mov    %r12d,%esi
lea    0xd60(%rip),%rdx
mov    %r15,%rcx
call   1080 <snprintf@plt>
cltq
movb   $0x0,(%r14,%rax,1)
mov    %r14,%rdi
mov    %rsp,%rsi
call   10c0 <checked_lstat@plt>
mov    (%rsp),%edi
call   1060 <S_ISDIR@plt>
test   %rax,%rax
jne    1240 <traverse+0x60>
mov    (%rsp),%edi
call   10d0 <S_ISREG@plt>
test   %rax,%rax
je     1255 <traverse+0x75>
mov    0x10(%rsp),%rdi
mov    %r14,%rsi
call   1100 <enqueue@plt>
jmp    1255 <traverse+0x75>
mov    %rbx,%rdi
call   10e0 <checked_closedir@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2cb7(%rip),%rax
mov    (%rax),%edi
lea    0xcce(%rip),%rsi
mov    %r15,%rdx
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <fprintf_ts@plt>

LLVM IR (-O0):
%struct.queue = type { i32 }
%struct.dirent = type { i8* }
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [26 x i8] c"Cannot open directory %s\0A\00"
@.str.1 = [2 x i8] c".\00"
@.str.2 = [3 x i8] c"..\00"
@.str.3 = [6 x i8] c"%s/%s\00"
define void @traverse(i8* %0, %struct.queue* %1) {
%3 = alloca i8*
%4 = alloca %struct.queue*
%5 = alloca i32*
%6 = alloca %struct.dirent*
%7 = alloca %struct.stat
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i8*
store i8* %0, i8** %3
store %struct.queue* %1, %struct.queue** %4
%13 = load i8*, i8** %3
%14 = call i32* @checked_opendir(i8* %13)
store i32* %14, i32** %5
%15 = load i32*, i32** %5
%16 = icmp ne i32* %15, null
br i1 %16, label %21, label %17
17: ; preds = %2
%18 = load i32, i32* @stderr
%19 = load i8*, i8** %3
%20 = call i32 @fprintf_ts(i32 %18, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %19)
br label %90
21: ; preds = %2
%22 = load i8*, i8** %3
%23 = call i32 @strlen(i8* %22)
store i32 %23, i32* %8
br label %24
24: ; preds = %86, %72, %40, %21
%25 = load i32*, i32** %5
%26 = call %struct.dirent* @checked_readdir(i32* %25)
store %struct.dirent* %26, %struct.dirent** %6
%27 = icmp ne %struct.dirent* %26, null
br i1 %27, label %28, label %87
28: ; preds = %24
%29 = load %struct.dirent*, %struct.dirent** %6
%30 = getelementptr inbounds %struct.dirent, %struct.dirent* %29, i32 0, i32 0
%31 = load i8*, i8** %30
%32 = call i64 @strcmp(i8* %31, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%33 = icmp eq i64 %32, 0
br i1 %33, label %40, label %34
34: ; preds = %28
%35 = load %struct.dirent*, %struct.dirent** %6
%36 = getelementptr inbounds %struct.dirent, %struct.dirent* %35, i32 0, i32 0
%37 = load i8*, i8** %36
%38 = call i64 @strcmp(i8* %37, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%39 = icmp eq i64 %38, 0
br i1 %39, label %40, label %41
40: ; preds = %34, %28
br label %24
41: ; preds = %34
%42 = load %struct.dirent*, %struct.dirent** %6
%43 = getelementptr inbounds %struct.dirent, %struct.dirent* %42, i32 0, i32 0
%44 = load i8*, i8** %43
%45 = call i32 @strlen(i8* %44)
store i32 %45, i32* %9
%46 = load i32, i32* %8
%47 = load i32, i32* %9
%48 = add nsw i32 %46, %47
%49 = add nsw i32 %48, 2
store i32 %49, i32* %10
%50 = load i32, i32* %10
%51 = sext i32 %50 to i64
%52 = mul i64 1, %51
%53 = trunc i64 %52 to i32
%54 = call i8* @checked_malloc(i32 %53)
store i8* %54, i8** %12
%55 = load i8*, i8** %12
%56 = load i32, i32* %10
%57 = load i8*, i8** %3
%58 = load %struct.dirent*, %struct.dirent** %6
%59 = getelementptr inbounds %struct.dirent, %struct.dirent* %58, i32 0, i32 0
%60 = load i8*, i8** %59
%61 = call i32 @snprintf(i8* %55, i32 %56, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* %57, i8* %60)
store i32 %61, i32* %11
%62 = load i8*, i8** %12
%63 = load i32, i32* %11
%64 = sext i32 %63 to i64
%65 = getelementptr inbounds i8, i8* %62, i64 %64
store i8 0, i8* %65
%66 = load i8*, i8** %12
%67 = call i32 @checked_lstat(i8* %66, %struct.stat* %7)
%68 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%69 = load i32, i32* %68
%70 = call i64 @S_ISDIR(i32 %69)
%71 = icmp ne i64 %70, 0
br i1 %71, label %72, label %77
72: ; preds = %41
%73 = load i8*, i8** %12
%74 = load %struct.queue*, %struct.queue** %4
%75 = load i8*, i8** %12
%76 = call i32 @free(i8* %75)
br label %24
77: ; preds = %41
%78 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%79 = load i32, i32* %78
%80 = call i64 @S_ISREG(i32 %79)
%81 = icmp ne i64 %80, 0
br i1 %81, label %82, label %86
82: ; preds = %77
%83 = load %struct.queue*, %struct.queue** %4
%84 = load i8*, i8** %12
%85 = call i32 @enqueue(%struct.queue* %83, i8* %84)
br label %86
86: ; preds = %82, %77
br label %24
87: ; preds = %24
%88 = load i32*, i32** %5
%89 = call i32 @checked_closedir(i32* %88)
br label %90
90: ; preds = %87, %17
ret void
}

LLVM IR (-O2):
%struct.queue = type { i32 }
%struct.stat = type { i32 }
%struct.dirent = type { i8* }
@stderr = global i32 0
@.str = [26 x i8] c"Cannot open directory %s\0A\00"
@.str.1 = [2 x i8] c".\00"
@.str.2 = [3 x i8] c"..\00"
@.str.3 = [6 x i8] c"%s/%s\00"
define void @traverse(i8* %0, %struct.queue* %1) {
%3 = alloca %struct.stat
%4 = tail call i32* @checked_opendir(i8* %0)
%5 = bitcast %struct.stat* %3 to i8*
%6 = icmp eq i32* %4, null
br i1 %6, label %7, label %10
7: ; preds = %2
%8 = load i32, i32* @stderr
%9 = tail call i32 @fprintf_ts(i32 %8, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %53
10: ; preds = %2
%11 = tail call i32 @strlen(i8* %0)
%12 = call %struct.dirent* @checked_readdir(i32* %4)
%13 = icmp eq %struct.dirent* %12, null
br i1 %13, label %51, label %14
14: ; preds = %10
%15 = add i32 %11, 2
%16 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
br label %17
17: ; preds = %14, %48
%18 = phi %struct.dirent* [ %12, %14 ], [ %49, %48 ]
%19 = getelementptr inbounds %struct.dirent, %struct.dirent* %18, i64 0, i32 0
%20 = load i8*, i8** %19
%21 = call i64 @strcmp(i8* %20, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%22 = icmp eq i64 %21, 0
br i1 %22, label %48, label %23
23: ; preds = %17
%24 = load i8*, i8** %19
%25 = call i64 @strcmp(i8* %24, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%26 = icmp eq i64 %25, 0
br i1 %26, label %48, label %27
27: ; preds = %23
%28 = load i8*, i8** %19
%29 = call i32 @strlen(i8* %28)
%30 = add i32 %15, %29
%31 = call i8* @checked_malloc(i32 %30)
%32 = load i8*, i8** %19
%33 = call i32 @snprintf(i8* %31, i32 %30, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* %0, i8* %32)
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %31, i64 %34
store i8 0, i8* %35
%36 = call i32 @checked_lstat(i8* %31, %struct.stat* %3)
%37 = load i32, i32* %16
%38 = call i64 @S_ISDIR(i32 %37)
%39 = icmp eq i64 %38, 0
br i1 %39, label %42, label %40
40: ; preds = %27
%41 = call i32 @free(i8* %31)
br label %48
42: ; preds = %27
%43 = load i32, i32* %16
%44 = call i64 @S_ISREG(i32 %43)
%45 = icmp eq i64 %44, 0
br i1 %45, label %48, label %46
46: ; preds = %42
%47 = call i32 @enqueue(%struct.queue* %1, i8* %31)
br label %48
48: ; preds = %42, %46, %17, %23, %40
%49 = call %struct.dirent* @checked_readdir(i32* %4)
%50 = icmp eq %struct.dirent* %49, null
br i1 %50, label %51, label %17
51: ; preds = %48, %10
%52 = call i32 @checked_closedir(i32* %4)
br label %53
53: ; preds = %51, %7
ret void
}