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

/* Variables and functions */
scalar_t__ DT_DIR;
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ strcat(char *, char *);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
int /*<<< orphan*/ trio_printf(char *, char *, char *, scalar_t__);
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))) {
    trio_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   1050 <opendir@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11bc <mylistdir_+0x2c>
jmp    12cc <mylistdir_+0x13c>
jmp    11c1 <mylistdir_+0x31>
mov    -0x10(%rbp),%rdi
call   1090 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     12c3 <mylistdir_+0x133>
mov    -0x8(%rbp),%rsi
mov    -0x18(%rbp),%rax
mov    (%rax),%rdx
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rcx
lea    0xe0e(%rip),%rdi
call   1040 <trio_printf@plt>
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rax
mov    0x2dda(%rip),%rcx
cmp    (%rcx),%rax
jne    12be <mylistdir_+0x12e>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
lea    0xdf7(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
je     12b9 <mylistdir_+0x129>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
lea    0xdd9(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
je     12b9 <mylistdir_+0x129>
lea    -0x420(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1030 <strcpy@plt>
lea    -0x420(%rbp),%rdi
call   1060 <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     129a <mylistdir_+0x10a>
lea    -0x420(%rbp),%rdi
lea    0xd81(%rip),%rsi
call   10b0 <strcat@plt>
lea    -0x420(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
call   10b0 <strcat@plt>
lea    -0x420(%rbp),%rdi
call   10a0 <mylistdir_@plt>
jmp    12be <mylistdir_+0x12e>
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   1050 <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   10b0 <strcat@plt>
mov    %rbp,%rdi
call   10a0 <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),%rcx
mov    %r15,%rdi
mov    %rbx,%rsi
call   1040 <trio_printf@plt>
mov    0x8(%r13),%rax
cmp    (%r12),%rax
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   1060 <strlen@plt>
cltq
cmpb   $0x2f,-0x1(%rax,%rbp,1)
je     11dc <mylistdir_+0x4c>
mov    %rbp,%rdi
lea    0xda5(%rip),%rsi
call   10b0 <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*, i64 }
@.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 %71
12: ; preds = %1
br label %13
13: ; preds = %67, %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 %68
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 i64, i64* %23
%25 = call i32 @trio_printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %18, i8* %21, i64 %24)
%26 = load %struct.dirent*, %struct.dirent** %4
%27 = getelementptr inbounds %struct.dirent, %struct.dirent* %26, i32 0, i32 1
%28 = load i64, i64* %27
%29 = load i64, i64* @DT_DIR
%30 = icmp eq i64 %28, %29
br i1 %30, label %31, label %67
31: ; preds = %17
%32 = load %struct.dirent*, %struct.dirent** %4
%33 = getelementptr inbounds %struct.dirent, %struct.dirent* %32, i32 0, i32 0
%34 = load i8*, i8** %33
%35 = call i64 @strcmp(i8* %34, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%36 = icmp ne i64 %35, 0
br i1 %36, label %37, label %66
37: ; preds = %31
%38 = load %struct.dirent*, %struct.dirent** %4
%39 = getelementptr inbounds %struct.dirent, %struct.dirent* %38, i32 0, i32 0
%40 = load i8*, i8** %39
%41 = call i64 @strcmp(i8* %40, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%42 = icmp ne i64 %41, 0
br i1 %42, label %43, label %66
43: ; preds = %37
%44 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%45 = load i8*, i8** %2
%46 = call i32 @strcpy(i8* %44, i8* %45)
%47 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%48 = call i32 @strlen(i8* %47)
store i32 %48, i32* %6
%49 = load i32, i32* %6
%50 = sub nsw i32 %49, 1
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 %51
%53 = load i8, i8* %52
%54 = sext i8 %53 to i32
%55 = icmp ne i32 %54, 47
br i1 %55, label %56, label %59
56: ; preds = %43
%57 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%58 = call i32 @strcat(i8* %57, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
br label %59
59: ; preds = %56, %43
%60 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%61 = load %struct.dirent*, %struct.dirent** %4
%62 = getelementptr inbounds %struct.dirent, %struct.dirent* %61, i32 0, i32 0
%63 = load i8*, i8** %62
%64 = call i32 @strcat(i8* %60, i8* %63)
%65 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
br label %66
66: ; preds = %59, %37, %31
br label %67
67: ; preds = %66, %17
br label %13
68: ; preds = %13
%69 = load i32*, i32** %3
%70 = call i32 @closedir(i32* %69)
br label %71
71: ; preds = %68, %11
ret void
}

LLVM IR (-O2):
%struct.dirent = type { i8*, i64 }
@.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 %45, label %6
6: ; preds = %1
%7 = call %struct.dirent* @readdir(i32* %4)
%8 = icmp eq %struct.dirent* %7, null
br i1 %8, label %43, label %9
9: ; preds = %6, %40
%10 = phi %struct.dirent* [ %41, %40 ], [ %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 i64, i64* %13
%15 = call i32 @trio_printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %12, i64 %14)
%16 = load i64, i64* %13
%17 = load i64, i64* @DT_DIR
%18 = icmp eq i64 %16, %17
br i1 %18, label %19, label %40
19: ; preds = %9
%20 = load i8*, i8** %11
%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 %40, label %23
23: ; preds = %19
%24 = load i8*, i8** %11
%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 %40, label %27
27: ; preds = %23
%28 = call i32 @strcpy(i8* %3, i8* %0)
%29 = call i32 @strlen(i8* %3)
%30 = add nsw i32 %29, -1
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 %31
%33 = load i8, i8* %32
%34 = icmp eq i8 %33, 47
br i1 %34, label %37, label %35
35: ; preds = %27
%36 = call i32 @strcat(i8* %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
br label %37
37: ; preds = %35, %27
%38 = load i8*, i8** %11
%39 = call i32 @strcat(i8* %3, i8* %38)
br label %40
40: ; preds = %19, %23, %37, %9
%41 = call %struct.dirent* @readdir(i32* %4)
%42 = icmp eq %struct.dirent* %41, null
br i1 %42, label %43, label %9
43: ; preds = %40, %6
%44 = call i32 @closedir(i32* %4)
br label %45
45: ; preds = %1, %43
ret void
}