#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*/ basename(char *);
int /*<<< orphan*/ lstat(char *, struct stat *);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, char *, char *);
int /*<<< orphan*/ strcmp(char *, int /*<<< orphan*/);
int list_dir(char *path) {
  DIR *dir = NULL;
  struct dirent *ptr;
  struct stat st;
  char file[1024];
  dir = opendir(path);
  if (!dir) {
    perror(path);
    return 0;
  }
  while ((ptr = readdir(dir)) != NULL) {
    if (!strcmp(".", basename(ptr->d_name)) ||
        !strcmp("..", basename(ptr->d_name))) {
      continue;
    }
    sprintf(file, "%s/%s", path, ptr->d_name);
    lstat(file, &st);
    if (S_ISDIR(st.st_mode)) {
      printf("dir:%s\n", file);
      list_dir(file);
    } else if (S_ISREG(st.st_mode)) {
      printf("file:%s\n", file);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<list_dir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11f4 <list_dir+0x44>
mov    -0x10(%rbp),%rdi
call   10c0 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    12f5 <list_dir+0x145>
jmp    11f9 <list_dir+0x49>
mov    -0x18(%rbp),%rdi
call   10a0 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     12ee <list_dir+0x13e>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
call   1080 <basename@plt>
mov    %eax,%esi
lea    0xddc(%rip),%rdi
call   1070 <strcmp@plt>
cmp    $0x0,%eax
je     1256 <list_dir+0xa6>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
call   1080 <basename@plt>
mov    %eax,%esi
lea    0xdb8(%rip),%rdi
call   1070 <strcmp@plt>
cmp    $0x0,%eax
jne    125b <list_dir+0xab>
jmp    11f9 <list_dir+0x49>
lea    -0x430(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x20(%rbp),%rax
mov    (%rax),%rcx
lea    0xd8f(%rip),%rsi
call   10d0 <sprintf@plt>
lea    -0x430(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1060 <lstat@plt>
mov    -0x28(%rbp),%edi
call   1050 <S_ISDIR@plt>
cmp    $0x0,%rax
je     12bf <list_dir+0x10f>
lea    -0x430(%rbp),%rsi
lea    0xd60(%rip),%rdi
call   1040 <printf@plt>
lea    -0x430(%rbp),%rdi
call   10b0 <list_dir@plt>
jmp    12e9 <list_dir+0x139>
mov    -0x28(%rbp),%edi
call   1090 <S_ISREG@plt>
cmp    $0x0,%rax
je     12e4 <list_dir+0x134>
lea    -0x430(%rbp),%rsi
lea    0xd32(%rip),%rdi
call   1040 <printf@plt>
jmp    12e9 <list_dir+0x139>
jmp    11f9 <list_dir+0x49>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<list_dir>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
mov    %rdi,%r14
call   1030 <opendir@plt>
test   %rax,%rax
je     12b8 <list_dir+0x108>
mov    %rax,%rbx
mov    %rax,%rdi
call   10a0 <readdir@plt>
test   %rax,%rax
je     12c0 <list_dir+0x110>
mov    %rax,%r13
lea    0xe11(%rip),%r15
lea    0x10(%rsp),%r12
jmp    122b <list_dir+0x7b>
nopw   0x0(%rax,%rax,1)
lea    0xe02(%rip),%rdi
mov    %r12,%rsi
call   1040 <printf@plt>
mov    %r12,%rdi
call   10b0 <list_dir@plt>
mov    %rbx,%rdi
call   10a0 <readdir@plt>
mov    %rax,%r13
test   %rax,%rax
je     12c0 <list_dir+0x110>
mov    0x0(%r13),%rdi
call   1080 <basename@plt>
mov    %r15,%rdi
mov    %eax,%esi
call   1070 <strcmp@plt>
test   %eax,%eax
je     1217 <list_dir+0x67>
mov    0x0(%r13),%rdi
call   1080 <basename@plt>
lea    0xdae(%rip),%rdi
mov    %eax,%esi
call   1070 <strcmp@plt>
test   %eax,%eax
je     1217 <list_dir+0x67>
mov    0x0(%r13),%rcx
mov    %r12,%rdi
lea    0xd98(%rip),%rsi
mov    %r14,%rdx
call   10d0 <sprintf@plt>
mov    %r12,%rdi
lea    0x8(%rsp),%rsi
call   1060 <lstat@plt>
mov    0x8(%rsp),%ebp
mov    %ebp,%edi
call   1050 <S_ISDIR@plt>
test   %rax,%rax
jne    1200 <list_dir+0x50>
mov    %ebp,%edi
call   1090 <S_ISREG@plt>
test   %rax,%rax
je     1217 <list_dir+0x67>
lea    0xd66(%rip),%rdi
mov    %r12,%rsi
call   1040 <printf@plt>
jmp    1217 <list_dir+0x67>
mov    %r14,%rdi
call   10c0 <perror@plt>
xor    %eax,%eax
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i8* }
%struct.stat = type { i32 }
@.str = [2 x i8] c".\00"
@.str.1 = [3 x i8] c"..\00"
@.str.2 = [6 x i8] c"%s/%s\00"
@.str.3 = [8 x i8] c"dir:%s\0A\00"
@.str.4 = [9 x i8] c"file:%s\0A\00"
define i32 @list_dir(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca %struct.dirent*
%6 = alloca %struct.stat
%7 = alloca [1024 x i8]
store i8* %0, i8** %3
store i32* null, i32** %4
%8 = load i8*, i8** %3
%9 = call i32* @opendir(i8* %8)
store i32* %9, i32** %4
%10 = load i32*, i32** %4
%11 = icmp ne i32* %10, null
br i1 %11, label %15, label %12
12: ; preds = %1
%13 = load i8*, i8** %3
%14 = call i32 @perror(i8* %13)
store i32 0, i32* %2
br label %64
15: ; preds = %1
br label %16
16: ; preds = %62, %34, %15
%17 = load i32*, i32** %4
%18 = call %struct.dirent* @readdir(i32* %17)
store %struct.dirent* %18, %struct.dirent** %5
%19 = icmp ne %struct.dirent* %18, null
br i1 %19, label %20, label %63
20: ; preds = %16
%21 = load %struct.dirent*, %struct.dirent** %5
%22 = getelementptr inbounds %struct.dirent, %struct.dirent* %21, i32 0, i32 0
%23 = load i8*, i8** %22
%24 = call i32 @basename(i8* %23)
%25 = call i32 @strcmp(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 %24)
%26 = icmp ne i32 %25, 0
br i1 %26, label %27, label %34
27: ; preds = %20
%28 = load %struct.dirent*, %struct.dirent** %5
%29 = getelementptr inbounds %struct.dirent, %struct.dirent* %28, i32 0, i32 0
%30 = load i8*, i8** %29
%31 = call i32 @basename(i8* %30)
%32 = call i32 @strcmp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %31)
%33 = icmp ne i32 %32, 0
br i1 %33, label %35, label %34
34: ; preds = %27, %20
br label %16
35: ; preds = %27
%36 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%37 = load i8*, i8** %3
%38 = load %struct.dirent*, %struct.dirent** %5
%39 = getelementptr inbounds %struct.dirent, %struct.dirent* %38, i32 0, i32 0
%40 = load i8*, i8** %39
%41 = call i32 @sprintf(i8* %36, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %37, i8* %40)
%42 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%43 = call i32 @lstat(i8* %42, %struct.stat* %6)
%44 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%45 = load i32, i32* %44
%46 = call i64 @S_ISDIR(i32 %45)
%47 = icmp ne i64 %46, 0
br i1 %47, label %48, label %53
48: ; preds = %35
%49 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%50 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8* %49)
%51 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%52 = call i32 @list_dir(i8* %51)
br label %62
53: ; preds = %35
%54 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%55 = load i32, i32* %54
%56 = call i64 @S_ISREG(i32 %55)
%57 = icmp ne i64 %56, 0
br i1 %57, label %58, label %61
58: ; preds = %53
%59 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%60 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i8* %59)
br label %61
61: ; preds = %58, %53
br label %62
62: ; preds = %61, %48
br label %16
63: ; preds = %16
store i32 0, i32* %2
br label %64
64: ; preds = %63, %12
%65 = load i32, i32* %2
ret i32 %65
}

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