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

/* Variables and functions */
int DT_DIR;
int DT_LNK;
int PATH_MAX;
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, char *, ...);
int /*<<< orphan*/ lstat(char *, struct stat *);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ printf(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int snprintf(char *, int, char *, char *, char const *);
scalar_t__ strcmp(char const *, char *);
void list_dir(char *dir_name, FILE *f) {
  DIR *d;
  d = opendir(dir_name);
  if (!d) {
    printf("\nError. ");
    exit(1);
  }
  while (1) {
    struct dirent *entry;
    const char *d_name;
    struct stat status;
    entry = readdir(d);
    if (!entry) {
      break;
    }
    lstat(entry->d_name, &status);
    d_name = entry->d_name;
    if (!0) {
      if (!(entry->d_type & DT_DIR)) {
        fprintf(f, "%s/%s\n", dir_name, d_name);
      }
    } else {
      if (!(entry->d_type & DT_DIR)) {
        fprintf(f, "%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];
        if (!0)
          path_length = snprintf(path, PATH_MAX, "%s/%s", dir_name, d_name);
        else
          path_length = snprintf(path, PATH_MAX, "%s\\%s", dir_name, d_name);
        fprintf(f, "%s\n", path);
        if (path_length >= PATH_MAX) {
          printf("Error. Path length too long.\n");
          exit(1);
        }
        if ((!(entry->d_type & DT_LNK))) {
          list_dir(path, f);
        }
      }
    }
  }
  closedir(d);
}


x86 assembly (clang, -O0):
<list_dir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11de <list_dir+0x3e>
lea    0xe31(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
jmp    11e3 <list_dir+0x43>
mov    -0x18(%rbp),%rdi
call   10a0 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1200 <list_dir+0x60>
jmp    135c <list_dir+0x1bc>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
lea    -0x30(%rbp),%rsi
call   1070 <lstat@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%eax
mov    0x2daf(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
jne    124e <list_dir+0xae>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    -0x28(%rbp),%rcx
lea    0xdc2(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%eax
mov    0x2d7c(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1357 <list_dir+0x1b7>
mov    -0x28(%rbp),%rdi
lea    0xd9e(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
je     1352 <list_dir+0x1b2>
mov    -0x28(%rbp),%rdi
lea    0xd85(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
je     1352 <list_dir+0x1b2>
mov    0x2d4e(%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,-0x50(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x48(%rbp)
mov    0x2d22(%rip),%rax
mov    (%rax),%esi
mov    -0x8(%rbp),%rcx
mov    -0x28(%rbp),%r8
lea    0xd34(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0x50(%rbp),%rdx
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xd16(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    -0x34(%rbp),%eax
mov    0x2ce9(%rip),%rcx
cmp    (%rcx),%eax
jl     1325 <list_dir+0x185>
lea    0xd03(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%eax
mov    0x2c9d(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
jne    134b <list_dir+0x1ab>
mov    -0x50(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   10b0 <list_dir@plt>
mov    -0x40(%rbp),%rax
mov    %rax,%rsp
jmp    1357 <list_dir+0x1b7>
jmp    11e3 <list_dir+0x43>
mov    -0x18(%rbp),%rdi
call   1060 <closedir@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    $0x28,%rsp
mov    %rsi,-0x30(%rbp)
mov    %rdi,-0x40(%rbp)
call   1040 <opendir@plt>
test   %rax,%rax
je     132a <list_dir+0x17a>
mov    %rax,%r14
mov    %rax,%rdi
call   10b0 <readdir@plt>
test   %rax,%rax
je     12fd <list_dir+0x14d>
mov    %rax,%r13
lea    -0x38(%rbp),%r15
mov    0x2ddf(%rip),%rbx
mov    %r14,-0x48(%rbp)
jmp    1226 <list_dir+0x76>
nop
mov    %r15,%rsp
mov    -0x48(%rbp),%r14
lea    -0x38(%rbp),%r15
mov    0x2dc6(%rip),%rbx
mov    %r14,%rdi
call   10b0 <readdir@plt>
mov    %rax,%r13
test   %rax,%rax
je     12fd <list_dir+0x14d>
mov    0x0(%r13),%rdi
mov    %r15,%rsi
call   1080 <lstat@plt>
mov    0x0(%r13),%r12
mov    (%rbx),%eax
test   %eax,0x8(%r13)
jne    125f <list_dir+0xaf>
mov    -0x30(%rbp),%rdi
lea    0xdc0(%rip),%rsi
mov    -0x40(%rbp),%rdx
mov    %r12,%rcx
xor    %eax,%eax
call   10a0 <fprintf@plt>
mov    (%rbx),%eax
test   %eax,0x8(%r13)
je     1212 <list_dir+0x62>
mov    %r12,%rdi
lea    0xda7(%rip),%rsi
call   1090 <strcmp@plt>
test   %rax,%rax
je     1212 <list_dir+0x62>
mov    %r12,%rdi
lea    0xd94(%rip),%rsi
call   1090 <strcmp@plt>
test   %rax,%rax
je     1212 <list_dir+0x62>
mov    %rsp,%r15
mov    0x2d5f(%rip),%rbx
mov    (%rbx),%eax
mov    %rsp,%r14
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r14
mov    %r14,%rsp
mov    (%rbx),%esi
mov    %r14,%rdi
lea    0xd63(%rip),%rdx
mov    -0x40(%rbp),%rcx
mov    %r12,%r8
call   1060 <snprintf@plt>
mov    %eax,%r12d
mov    -0x30(%rbp),%rdi
lea    0xd42(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   10a0 <fprintf@plt>
cmp    (%rbx),%r12d
jge    1314 <list_dir+0x164>
mov    0x2cf0(%rip),%rax
mov    (%rax),%eax
test   %eax,0x8(%r13)
jne    1200 <list_dir+0x50>
mov    %r14,%rdi
mov    -0x30(%rbp),%rsi
call   10c0 <list_dir@plt>
jmp    1200 <list_dir+0x50>
mov    %r14,%rdi
call   1070 <closedir@plt>
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xcfe(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
lea    0xccf(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>

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

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