#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*/ DIR;

/* Variables and functions */
scalar_t__ DT_DIR;
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ printf(char *, char *, char *, int);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ strcat(char *, char *);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
void mylistdir(char *path) {
  DIR *dir;
  struct dirent *entry;
  char newpath[1024];
  dir = opendir(path);
  int len;
  if (dir == 0) {
    return;
  }
  while ((entry = readdir(dir))) {
    printf("%s/%s D_TYPE = %d\n", path, entry->d_name, entry->d_type);
    if (entry->d_type == DT_DIR) {
      if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) {
        strcpy(newpath, path);
        len = strlen(newpath);
        if (newpath[len - 1] != '/')
          strcat(newpath, "/");
        strcat(newpath, entry->d_name);
        mylistdir(newpath);
      }
    }
  }
  closedir(dir);
}


x86 assembly (clang, -O0):
<mylistdir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11bc <mylistdir+0x2c>
jmp    12cb <mylistdir+0x13b>
jmp    11c1 <mylistdir+0x31>
mov    -0x10(%rbp),%rdi
call   1090 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     12c2 <mylistdir+0x132>
mov    -0x8(%rbp),%rsi
mov    -0x18(%rbp),%rax
mov    (%rax),%rdx
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%ecx
lea    0xe0f(%rip),%rdi
call   1060 <printf@plt>
mov    -0x18(%rbp),%rax
movslq 0x8(%rax),%rax
mov    0x2ddb(%rip),%rcx
cmp    (%rcx),%rax
jne    12bd <mylistdir+0x12d>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
lea    0xdf8(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
je     12b8 <mylistdir+0x128>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
lea    0xdda(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
je     12b8 <mylistdir+0x128>
lea    -0x420(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1030 <strcpy@plt>
lea    -0x420(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x424(%rbp)
mov    -0x424(%rbp),%eax
sub    $0x1,%eax
cltq
movsbl -0x420(%rbp,%rax,1),%eax
ff
cmp    $0x2f,%eax
je     1299 <mylistdir+0x109>
lea    -0x420(%rbp),%rdi
lea    0xd82(%rip),%rsi
call   10a0 <strcat@plt>
lea    -0x420(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
call   10a0 <strcat@plt>
lea    -0x420(%rbp),%rdi
call   10b0 <mylistdir@plt>
jmp    12bd <mylistdir+0x12d>
jmp    11c1 <mylistdir+0x31>
mov    -0x10(%rbp),%rdi
call   1070 <closedir@plt>
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mylistdir>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x408,%rsp
mov    %rdi,%rbx
call   1040 <opendir@plt>
test   %rax,%rax
je     1283 <mylistdir+0xf3>
mov    %rax,%r14
mov    %rax,%rdi
call   1090 <readdir@plt>
test   %rax,%rax
je     127b <mylistdir+0xeb>
mov    %rax,%r13
lea    0xe30(%rip),%r15
mov    0x2e09(%rip),%r12
mov    %rsp,%rbp
jmp    1200 <mylistdir+0x70>
mov    0x0(%r13),%rsi
mov    %rbp,%rdi
call   10a0 <strcat@plt>
mov    %rbp,%rdi
call   10b0 <mylistdir@plt>
mov    %r14,%rdi
call   1090 <readdir@plt>
mov    %rax,%r13
test   %rax,%rax
je     127b <mylistdir+0xeb>
mov    0x0(%r13),%rdx
mov    0x8(%r13),%ecx
mov    %r15,%rdi
mov    %rbx,%rsi
call   1060 <printf@plt>
movslq 0x8(%r13),%rax
cmp    %rax,(%r12)
jne    11f0 <mylistdir+0x60>
mov    0x0(%r13),%rdi
lea    0xdec(%rip),%rsi
call   1080 <strcmp@plt>
test   %rax,%rax
je     11f0 <mylistdir+0x60>
mov    0x0(%r13),%rdi
lea    0xdd6(%rip),%rsi
call   1080 <strcmp@plt>
test   %rax,%rax
je     11f0 <mylistdir+0x60>
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1030 <strcpy@plt>
mov    %rbp,%rdi
call   1050 <strlen@plt>
cltq
cmpb   $0x2f,-0x1(%rax,%rbp,1)
je     11dc <mylistdir+0x4c>
mov    %rbp,%rdi
lea    0xda5(%rip),%rsi
call   10a0 <strcat@plt>
jmp    11dc <mylistdir+0x4c>
mov    %r14,%rdi
call   1070 <closedir@plt>
add    $0x408,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i8*, i32 }
@.str = [19 x i8] c"%s/%s D_TYPE = %d\0A\00"
@DT_DIR = global i64 0
@.str.1 = [2 x i8] c".\00"
@.str.2 = [3 x i8] c"..\00"
@.str.3 = [2 x i8] c"/\00"
define void @mylistdir(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca %struct.dirent*
%5 = alloca [1024 x i8]
%6 = alloca i32
store i8* %0, i8** %2
%7 = load i8*, i8** %2
%8 = call i32* @opendir(i8* %7)
store i32* %8, i32** %3
%9 = load i32*, i32** %3
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %12
11: ; preds = %1
br label %72
12: ; preds = %1
br label %13
13: ; preds = %68, %12
%14 = load i32*, i32** %3
%15 = call %struct.dirent* @readdir(i32* %14)
store %struct.dirent* %15, %struct.dirent** %4
%16 = icmp ne %struct.dirent* %15, null
br i1 %16, label %17, label %69
17: ; preds = %13
%18 = load i8*, i8** %2
%19 = load %struct.dirent*, %struct.dirent** %4
%20 = getelementptr inbounds %struct.dirent, %struct.dirent* %19, i32 0, i32 0
%21 = load i8*, i8** %20
%22 = load %struct.dirent*, %struct.dirent** %4
%23 = getelementptr inbounds %struct.dirent, %struct.dirent* %22, i32 0, i32 1
%24 = load i32, i32* %23
%25 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %18, i8* %21, i32 %24)
%26 = load %struct.dirent*, %struct.dirent** %4
%27 = getelementptr inbounds %struct.dirent, %struct.dirent* %26, i32 0, i32 1
%28 = load i32, i32* %27
%29 = sext i32 %28 to i64
%30 = load i64, i64* @DT_DIR
%31 = icmp eq i64 %29, %30
br i1 %31, label %32, label %68
32: ; preds = %17
%33 = load %struct.dirent*, %struct.dirent** %4
%34 = getelementptr inbounds %struct.dirent, %struct.dirent* %33, i32 0, i32 0
%35 = load i8*, i8** %34
%36 = call i64 @strcmp(i8* %35, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%37 = icmp ne i64 %36, 0
br i1 %37, label %38, label %67
38: ; preds = %32
%39 = load %struct.dirent*, %struct.dirent** %4
%40 = getelementptr inbounds %struct.dirent, %struct.dirent* %39, i32 0, i32 0
%41 = load i8*, i8** %40
%42 = call i64 @strcmp(i8* %41, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%43 = icmp ne i64 %42, 0
br i1 %43, label %44, label %67
44: ; preds = %38
%45 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%46 = load i8*, i8** %2
%47 = call i32 @strcpy(i8* %45, i8* %46)
%48 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%49 = call i32 @strlen(i8* %48)
store i32 %49, i32* %6
%50 = load i32, i32* %6
%51 = sub nsw i32 %50, 1
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 %52
%54 = load i8, i8* %53
%55 = sext i8 %54 to i32
%56 = icmp ne i32 %55, 47
br i1 %56, label %57, label %60
57: ; preds = %44
%58 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%59 = call i32 @strcat(i8* %58, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
br label %60
60: ; preds = %57, %44
%61 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%62 = load %struct.dirent*, %struct.dirent** %4
%63 = getelementptr inbounds %struct.dirent, %struct.dirent* %62, i32 0, i32 0
%64 = load i8*, i8** %63
%65 = call i32 @strcat(i8* %61, i8* %64)
%66 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
br label %67
67: ; preds = %60, %38, %32
br label %68
68: ; preds = %67, %17
br label %13
69: ; preds = %13
%70 = load i32*, i32** %3
%71 = call i32 @closedir(i32* %70)
br label %72
72: ; preds = %69, %11
ret void
}

LLVM IR (-O2):
%struct.dirent = type { i8*, i32 }
@.str = [19 x i8] c"%s/%s D_TYPE = %d\0A\00"
@DT_DIR = global i64 0
@.str.1 = [2 x i8] c".\00"
@.str.2 = [3 x i8] c"..\00"
@.str.3 = [2 x i8] c"/\00"
define void @mylistdir(i8* %0) {
%2 = alloca [1024 x i8]
%3 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%4 = tail call i32* @opendir(i8* %0)
%5 = icmp eq i32* %4, null
br i1 %5, label %46, label %6
6: ; preds = %1
%7 = call %struct.dirent* @readdir(i32* %4)
%8 = icmp eq %struct.dirent* %7, null
br i1 %8, label %44, label %9
9: ; preds = %6, %41
%10 = phi %struct.dirent* [ %42, %41 ], [ %7, %6 ]
%11 = getelementptr inbounds %struct.dirent, %struct.dirent* %10, i64 0, i32 0
%12 = load i8*, i8** %11
%13 = getelementptr inbounds %struct.dirent, %struct.dirent* %10, i64 0, i32 1
%14 = load i32, i32* %13
%15 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %12, i32 %14)
%16 = load i32, i32* %13
%17 = sext i32 %16 to i64
%18 = load i64, i64* @DT_DIR
%19 = icmp eq i64 %18, %17
br i1 %19, label %20, label %41
20: ; preds = %9
%21 = load i8*, i8** %11
%22 = call i64 @strcmp(i8* %21, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%23 = icmp eq i64 %22, 0
br i1 %23, label %41, label %24
24: ; preds = %20
%25 = load i8*, i8** %11
%26 = call i64 @strcmp(i8* %25, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%27 = icmp eq i64 %26, 0
br i1 %27, label %41, label %28
28: ; preds = %24
%29 = call i32 @strcpy(i8* %3, i8* %0)
%30 = call i32 @strlen(i8* %3)
%31 = add nsw i32 %30, -1
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 %32
%34 = load i8, i8* %33
%35 = icmp eq i8 %34, 47
br i1 %35, label %38, label %36
36: ; preds = %28
%37 = call i32 @strcat(i8* %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
br label %38
38: ; preds = %36, %28
%39 = load i8*, i8** %11
%40 = call i32 @strcat(i8* %3, i8* %39)
br label %41
41: ; preds = %20, %24, %38, %9
%42 = call %struct.dirent* @readdir(i32* %4)
%43 = icmp eq %struct.dirent* %42, null
br i1 %43, label %44, label %9
44: ; preds = %41, %6
%45 = call i32 @closedir(i32* %4)
br label %46
46: ; preds = %1, %44
ret void
}