#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;
  int d_type;
};
typedef int /*<<< orphan*/ FILE;
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
int DT_DIR;
int /*<<< orphan*/ EXIT_FAILURE;
int PATH_MAX;
scalar_t__ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ printf(char *, char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int snprintf(char *, int, char *, char *, char const *);
int /*<<< orphan*/ *stderr;
scalar_t__ strcmp(char const *, char *);
char *strerror(int /*<<< orphan*/);
void list_dir(char *file_name, char *dir_name) {
  DIR *d;
  FILE *fp;
  fp = fopen(file_name, "a");
  d = opendir(dir_name);
  if (!d) {
    fprintf(stderr, "Cannot open directory '%s': %s\n", dir_name,
            strerror(errno));
    exit(EXIT_FAILURE);
  }
  while (1) {
    struct dirent *entry;
    const char *d_name;
    entry = readdir(d);
    if (!entry) {
      break;
    }
    d_name = entry->d_name;
    fprintf(fp, "%s/%s\n", dir_name, d_name);
    if (entry->d_type & DT_DIR) {
      if (strcmp(d_name, "..") != 0 && strcmp(d_name, ".") != 0) {
        int path_length;
        char path[PATH_MAX];
        path_length = snprintf(path, PATH_MAX, "%s/%s", dir_name, d_name);
        printf("%s\n", path);
        if (path_length >= PATH_MAX) {
          fprintf(stderr, "Path length has got too long.\n");
          exit(EXIT_FAILURE);
        }
        list_dir(file_name, path);
      }
    }
  }
  if (closedir(d)) {
    fprintf(stderr, "Could not close '%s': %s\n", dir_name, strerror(errno));
    exit(EXIT_FAILURE);
  }
}


x86 assembly (clang, -O0):
<list_dir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe35(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1237 <list_dir+0x87>
mov    0x2de5(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x58(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    0x2dbf(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
mov    -0x58(%rbp),%rdi
mov    -0x50(%rbp),%rdx
mov    %rax,%rcx
lea    0xde0(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    0x2d90(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
jmp    123c <list_dir+0x8c>
mov    -0x18(%rbp),%rdi
call   1090 <readdir@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1259 <list_dir+0xa9>
jmp    137d <list_dir+0x1cd>
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x30(%rbp),%rcx
lea    0xdab(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    -0x28(%rbp),%rax
mov    0x8(%rax),%eax
mov    0x2d44(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1378 <list_dir+0x1c8>
mov    -0x30(%rbp),%rdi
lea    0xd87(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
je     1373 <list_dir+0x1c3>
mov    -0x30(%rbp),%rdi
lea    0xd6e(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
je     1373 <list_dir+0x1c3>
mov    0x2d1e(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x40(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x60(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x48(%rbp)
mov    0x2cf2(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%rcx
mov    -0x30(%rbp),%r8
lea    0xd1d(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0x60(%rbp),%rsi
mov    %eax,-0x34(%rbp)
lea    0xd45(%rip),%rdi
call   1040 <printf@plt>
mov    -0x34(%rbp),%eax
mov    0x2cbf(%rip),%rcx
cmp    (%rcx),%eax
jl     135f <list_dir+0x1af>
mov    0x2c98(%rip),%rax
mov    (%rax),%rdi
lea    0xce8(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    0x2c68(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    -0x60(%rbp),%rsi
mov    -0x8(%rbp),%rdi
call   10a0 <list_dir@plt>
mov    -0x40(%rbp),%rax
mov    %rax,%rsp
jmp    1378 <list_dir+0x1c8>
jmp    123c <list_dir+0x8c>
mov    -0x18(%rbp),%rdi
call   1060 <closedir@plt>
cmp    $0x0,%rax
je     13db <list_dir+0x22b>
mov    0x2c41(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x70(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    0x2c1b(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
mov    -0x70(%rbp),%rdi
mov    -0x68(%rbp),%rdx
mov    %rax,%rcx
lea    0xc8b(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    0x2bec(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<list_dir>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r12
lea    0xe25(%rip),%rsi
mov    %rdi,-0x38(%rbp)
call   10b0 <fopen@plt>
mov    %rax,%r15
mov    %r12,%rdi
call   1030 <opendir@plt>
test   %rax,%rax
je     1330 <list_dir+0x170>
mov    %rax,%rbx
mov    %rax,%rdi
call   1090 <readdir@plt>
test   %rax,%rax
je     1314 <list_dir+0x154>
mov    %rax,%r13
mov    %rbx,-0x30(%rbp)
jmp    1247 <list_dir+0x87>
cs nopw 0x0(%rax,%rax,1)
nop
mov    -0x38(%rbp),%rdi
mov    %rbx,%rsi
call   10a0 <list_dir@plt>
mov    %r13,%rsp
mov    -0x30(%rbp),%rbx
mov    %rbx,%rdi
call   1090 <readdir@plt>
mov    %rax,%r13
test   %rax,%rax
je     1314 <list_dir+0x154>
mov    0x0(%r13),%r14
mov    %r15,%rdi
lea    0xdcd(%rip),%rsi
mov    %r12,%rdx
mov    %r14,%rcx
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    0x2d67(%rip),%rax
mov    (%rax),%eax
test   %eax,0x8(%r13)
je     1233 <list_dir+0x73>
mov    %r14,%rdi
lea    0xdae(%rip),%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
je     1233 <list_dir+0x73>
mov    %r14,%rdi
lea    0xd9b(%rip),%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
je     1233 <list_dir+0x73>
mov    %rsp,%r13
mov    0x2d4d(%rip),%rcx
mov    (%rcx),%eax
mov    %rcx,%rdx
mov    %r12,%rcx
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    (%rdx),%esi
mov    %rbx,%rdi
lea    0xd64(%rip),%rdx
mov    %r14,%r8
call   1050 <snprintf@plt>
mov    %eax,%r14d
lea    0xd8d(%rip),%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
mov    0x2d07(%rip),%rax
cmp    (%rax),%r14d
jl     1220 <list_dir+0x60>
mov    0x2cdf(%rip),%rax
mov    (%rax),%rcx
lea    0xd2f(%rip),%rdi
mov    $0x1e,%esi
mov    $0x1,%edx
call   10d0 <fwrite@plt>
jmp    1380 <list_dir+0x1c0>
mov    %rbx,%rdi
call   1060 <closedir@plt>
test   %rax,%rax
jne    1351 <list_dir+0x191>
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2ca1(%rip),%rax
mov    (%rax),%rbx
mov    0x2c87(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
lea    0xcb3(%rip),%rsi
jmp    1370 <list_dir+0x1b0>
mov    0x2c80(%rip),%rax
mov    (%rax),%rbx
mov    0x2c66(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
lea    0xce1(%rip),%rsi
mov    %rbx,%rdi
mov    %r12,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    0x2c39(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
%struct.dirent = type { i8*, i32 }
@.str = [2 x i8] c"a\00"
@stderr = global i32* null
@.str.1 = [32 x i8] c"Cannot open directory '%s': %s\0A\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@.str.2 = [7 x i8] c"%s/%s\0A\00"
@DT_DIR = global i32 0
@.str.3 = [3 x i8] c"..\00"
@.str.4 = [2 x i8] c".\00"
@PATH_MAX = global i32 0
@.str.5 = [6 x i8] c"%s/%s\00"
@.str.6 = [4 x i8] c"%s\0A\00"
@.str.7 = [31 x i8] c"Path length has got too long.\0A\00"
@.str.8 = [26 x i8] c"Could not close '%s': %s\0A\00"
define void @list_dir(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca %struct.dirent*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i64
store i8* %0, i8** %3
store i8* %1, i8** %4
%12 = load i8*, i8** %3
%13 = call i32* @fopen(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %13, i32** %6
%14 = load i8*, i8** %4
%15 = call i32* @opendir(i8* %14)
store i32* %15, i32** %5
%16 = load i32*, i32** %5
%17 = icmp ne i32* %16, null
br i1 %17, label %26, label %18
18: ; preds = %2
%19 = load i32*, i32** @stderr
%20 = load i8*, i8** %4
%21 = load i32, i32* @errno
%22 = call i8* @strerror(i32 %21)
%23 = call i32 (i32*, i8*, ...) @fprintf(i32* %19, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %20, i8* %22)
%24 = load i32, i32* @EXIT_FAILURE
%25 = call i32 @exit(i32 %24)
unreachable
26: ; preds = %2
br label %27
27: ; preds = %26, %77
%28 = load i32*, i32** %5
%29 = call %struct.dirent* @readdir(i32* %28)
store %struct.dirent* %29, %struct.dirent** %7
%30 = load %struct.dirent*, %struct.dirent** %7
%31 = icmp ne %struct.dirent* %30, null
br i1 %31, label %33, label %32
32: ; preds = %27
br label %78
33: ; preds = %27
%34 = load %struct.dirent*, %struct.dirent** %7
%35 = getelementptr inbounds %struct.dirent, %struct.dirent* %34, i32 0, i32 0
%36 = load i8*, i8** %35
store i8* %36, i8** %8
%37 = load i32*, i32** %6
%38 = load i8*, i8** %4
%39 = load i8*, i8** %8
%40 = call i32 (i32*, i8*, ...) @fprintf(i32* %37, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* %38, i8* %39)
%41 = load %struct.dirent*, %struct.dirent** %7
%42 = getelementptr inbounds %struct.dirent, %struct.dirent* %41, i32 0, i32 1
%43 = load i32, i32* %42
%44 = load i32, i32* @DT_DIR
%45 = and i32 %43, %44
%46 = icmp ne i32 %45, 0
br i1 %46, label %47, label %77
47: ; preds = %33
%48 = load i8*, i8** %8
%49 = call i64 @strcmp(i8* %48, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%50 = icmp ne i64 %49, 0
br i1 %50, label %51, label %76
51: ; preds = %47
%52 = load i8*, i8** %8
%53 = call i64 @strcmp(i8* %52, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%54 = icmp ne i64 %53, 0
br i1 %54, label %55, label %76
55: ; preds = %51
%56 = load i32, i32* @PATH_MAX
%57 = zext i32 %56 to i64
%58 = call i8* @llvm.stacksave()
store i8* %58, i8** %10
%59 = alloca i8, i64 %57
store i64 %57, i64* %11
%60 = load i32, i32* @PATH_MAX
%61 = load i8*, i8** %4
%62 = load i8*, i8** %8
%63 = call i32 @snprintf(i8* %59, i32 %60, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i8* %61, i8* %62)
store i32 %63, i32* %9
%64 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i8* %59)
%65 = load i32, i32* %9
%66 = load i32, i32* @PATH_MAX
%67 = icmp sge i32 %65, %66
br i1 %67, label %68, label %73
68: ; preds = %55
%69 = load i32*, i32** @stderr
%70 = call i32 (i32*, i8*, ...) @fprintf(i32* %69, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.7, i64 0, i64 0))
%71 = load i32, i32* @EXIT_FAILURE
%72 = call i32 @exit(i32 %71)
unreachable
73: ; preds = %55
%74 = load i8*, i8** %3
%75 = load i8*, i8** %10
br label %76
76: ; preds = %73, %51, %47
br label %77
77: ; preds = %76, %33
br label %27
78: ; preds = %32
%79 = load i32*, i32** %5
%80 = call i64 @closedir(i32* %79)
%81 = icmp ne i64 %80, 0
br i1 %81, label %82, label %90
82: ; preds = %78
%83 = load i32*, i32** @stderr
%84 = load i8*, i8** %4
%85 = load i32, i32* @errno
%86 = call i8* @strerror(i32 %85)
%87 = call i32 (i32*, i8*, ...) @fprintf(i32* %83, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.8, i64 0, i64 0), i8* %84, i8* %86)
%88 = load i32, i32* @EXIT_FAILURE
%89 = call i32 @exit(i32 %88)
unreachable
90: ; preds = %78
ret void
}

LLVM IR (-O2):
%struct.dirent = type { i8*, i32 }
@.str = [2 x i8] c"a\00"
@stderr = global i32* null
@.str.1 = [32 x i8] c"Cannot open directory '%s': %s\0A\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@.str.2 = [7 x i8] c"%s/%s\0A\00"
@DT_DIR = global i32 0
@.str.3 = [3 x i8] c"..\00"
@.str.4 = [2 x i8] c".\00"
@PATH_MAX = global i32 0
@.str.5 = [6 x i8] c"%s/%s\00"
@.str.6 = [4 x i8] c"%s\0A\00"
@.str.7 = [31 x i8] c"Path length has got too long.\0A\00"
@.str.8 = [26 x i8] c"Could not close '%s': %s\0A\00"
define void @list_dir(i8* nocapture %0, i8* %1) {
%3 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%4 = tail call i32* @opendir(i8* %1)
%5 = icmp eq i32* %4, null
br i1 %5, label %9, label %6
6: ; preds = %2
%7 = call %struct.dirent* @readdir(i32* %4)
%8 = icmp eq %struct.dirent* %7, null
br i1 %8, label %51, label %16
9: ; preds = %2
%10 = load i32*, i32** @stderr
%11 = load i32, i32* @errno
%12 = tail call i8* @strerror(i32 %11)
%13 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %10, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %1, i8* %12)
%14 = load i32, i32* @EXIT_FAILURE
%15 = tail call i32 @exit(i32 %14)
unreachable
16: ; preds = %6, %48
%17 = phi %struct.dirent* [ %49, %48 ], [ %7, %6 ]
%18 = getelementptr inbounds %struct.dirent, %struct.dirent* %17, i64 0, i32 0
%19 = load i8*, i8** %18
%20 = call i32 (i32*, i8*, ...) @fprintf(i32* %3, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* %1, i8* %19)
%21 = getelementptr inbounds %struct.dirent, %struct.dirent* %17, i64 0, i32 1
%22 = load i32, i32* %21
%23 = load i32, i32* @DT_DIR
%24 = and i32 %23, %22
%25 = icmp eq i32 %24, 0
br i1 %25, label %48, label %26
26: ; preds = %16
%27 = call i64 @strcmp(i8* %19, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%28 = icmp eq i64 %27, 0
br i1 %28, label %48, label %29
29: ; preds = %26
%30 = call i64 @strcmp(i8* %19, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%31 = icmp eq i64 %30, 0
br i1 %31, label %48, label %32
32: ; preds = %29
%33 = load i32, i32* @PATH_MAX
%34 = zext i32 %33 to i64
%35 = call i8* @llvm.stacksave()
%36 = alloca i8, i64 %34
%37 = load i32, i32* @PATH_MAX
%38 = call i32 @snprintf(i8* %36, i32 %37, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i8* %1, i8* %19)
%39 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i8* %36)
%40 = load i32, i32* @PATH_MAX
%41 = icmp slt i32 %38, %40
br i1 %41, label %47, label %42
42: ; preds = %32
%43 = load i32*, i32** @stderr
%44 = call i64 @fwrite(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.7, i64 0, i64 0), i64 30, i64 1, i32* %43)
%45 = load i32, i32* @EXIT_FAILURE
%46 = call i32 @exit(i32 %45)
unreachable
47: ; preds = %32
br label %48
48: ; preds = %16, %47, %29, %26
%49 = call %struct.dirent* @readdir(i32* %4)
%50 = icmp eq %struct.dirent* %49, null
br i1 %50, label %51, label %16
51: ; preds = %48, %6
%52 = call i64 @closedir(i32* %4)
%53 = icmp eq i64 %52, 0
br i1 %53, label %61, label %54
54: ; preds = %51
%55 = load i32*, i32** @stderr
%56 = load i32, i32* @errno
%57 = call i8* @strerror(i32 %56)
%58 = call i32 (i32*, i8*, ...) @fprintf(i32* %55, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.8, i64 0, i64 0), i8* %1, i8* %57)
%59 = load i32, i32* @EXIT_FAILURE
%60 = call i32 @exit(i32 %59)
unreachable
61: ; preds = %51
ret void
}