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

/* Variables and functions */
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ print_file_details(struct stat, char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, char *, char *);
scalar_t__ stat(char *, struct stat *);
void print_dir_details(char dir[]) {
  struct dirent *pDirent;
  DIR *pDir;
  pDir = opendir(dir);
  if (pDir != NULL) {
    while ((pDirent = readdir(pDir)) != NULL) {
      char filepath[1024];
      sprintf(filepath, "%s/%s", dir, pDirent->d_name);
      struct stat file_stats;
      if (stat(filepath, &file_stats) < 0) {
        break;
      }
      print_file_details(file_stats, pDirent->d_name);
    }
  }
  closedir(pDir);
}


x86 assembly (clang, -O0):
<print_dir_details>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11ff <print_dir_details+0x9f>
jmp    118c <print_dir_details+0x2c>
mov    -0x18(%rbp),%rdi
call   1070 <readdir@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     11fa <print_dir_details+0x9a>
lea    -0x420(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    -0x10(%rbp),%rax
mov    (%rax),%rcx
lea    0xe44(%rip),%rsi
call   1080 <sprintf@plt>
lea    -0x420(%rbp),%rdi
lea    -0x428(%rbp),%rsi
call   1060 <stat@plt>
cmp    $0x0,%rax
jge    11e3 <print_dir_details+0x83>
jmp    11fa <print_dir_details+0x9a>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
mov    -0x428(%rbp),%edi
call   1050 <print_file_details@plt>
jmp    118c <print_dir_details+0x2c>
jmp    11ff <print_dir_details+0x9f>
mov    -0x18(%rbp),%rdi
call   1040 <closedir@plt>
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<print_dir_details>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
mov    %rdi,%r14
call   1030 <opendir@plt>
mov    %rax,%rbx
test   %rax,%rax
je     11ef <print_dir_details+0x8f>
mov    %rbx,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
je     11ef <print_dir_details+0x8f>
mov    %rax,%rbp
lea    0xe68(%rip),%r15
lea    0x10(%rsp),%r12
lea    0x8(%rsp),%r13
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    0x0(%rbp),%rcx
mov    %r12,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
call   1080 <sprintf@plt>
mov    %r12,%rdi
mov    %r13,%rsi
call   1060 <stat@plt>
test   %rax,%rax
js     11ef <print_dir_details+0x8f>
mov    0x0(%rbp),%rsi
mov    0x8(%rsp),%edi
call   1050 <print_file_details@plt>
mov    %rbx,%rdi
call   1070 <readdir@plt>
mov    %rax,%rbp
test   %rax,%rax
jne    11b0 <print_dir_details+0x50>
mov    %rbx,%rdi
call   1040 <closedir@plt>
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 = [6 x i8] c"%s/%s\00"
define void @print_dir_details(i8* %0) {
%2 = alloca i8*
%3 = alloca %struct.dirent*
%4 = alloca i32*
%5 = alloca [1024 x i8]
%6 = alloca %struct.stat
store i8* %0, i8** %2
%7 = load i8*, i8** %2
%8 = call i32* @opendir(i8* %7)
store i32* %8, i32** %4
%9 = load i32*, i32** %4
%10 = icmp ne i32* %9, null
br i1 %10, label %11, label %35
11: ; preds = %1
br label %12
12: ; preds = %27, %11
%13 = load i32*, i32** %4
%14 = call %struct.dirent* @readdir(i32* %13)
store %struct.dirent* %14, %struct.dirent** %3
%15 = icmp ne %struct.dirent* %14, null
br i1 %15, label %16, label %34
16: ; preds = %12
%17 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%18 = load i8*, i8** %2
%19 = load %struct.dirent*, %struct.dirent** %3
%20 = getelementptr inbounds %struct.dirent, %struct.dirent* %19, i32 0, i32 0
%21 = load i8*, i8** %20
%22 = call i32 @sprintf(i8* %17, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %18, i8* %21)
%23 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%24 = call i64 @stat(i8* %23, %struct.stat* %6)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %16
br label %34
27: ; preds = %16
%28 = load %struct.dirent*, %struct.dirent** %3
%29 = getelementptr inbounds %struct.dirent, %struct.dirent* %28, i32 0, i32 0
%30 = load i8*, i8** %29
%31 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%32 = load i32, i32* %31
%33 = call i32 @print_file_details(i32 %32, i8* %30)
br label %12
34: ; preds = %26, %12
br label %35
35: ; preds = %34, %1
%36 = load i32*, i32** %4
%37 = call i32 @closedir(i32* %36)
ret void
}

LLVM IR (-O2):
%struct.stat = type { i32 }
%struct.dirent = type { i8* }
@.str = [6 x i8] c"%s/%s\00"
define void @print_dir_details(i8* %0) {
%2 = alloca [1024 x i8]
%3 = alloca %struct.stat
%4 = tail call i32* @opendir(i8* %0)
%5 = icmp eq i32* %4, null
br i1 %5, label %27, label %6
6: ; preds = %1
%7 = call %struct.dirent* @readdir(i32* %4)
%8 = icmp eq %struct.dirent* %7, null
br i1 %8, label %27, label %9
9: ; preds = %6
%10 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%11 = bitcast %struct.stat* %3 to i8*
%12 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
br label %13
13: ; preds = %9, %21
%14 = phi %struct.dirent* [ %7, %9 ], [ %25, %21 ]
%15 = getelementptr inbounds %struct.dirent, %struct.dirent* %14, i64 0, i32 0
%16 = load i8*, i8** %15
%17 = call i32 @sprintf(i8* %10, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %16)
%18 = call i64 @stat(i8* %10, %struct.stat* %3)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %13
br label %27
21: ; preds = %13
%22 = load i8*, i8** %15
%23 = load i32, i32* %12
%24 = call i32 @print_file_details(i32 %23, i8* %22)
%25 = call %struct.dirent* @readdir(i32* %4)
%26 = icmp eq %struct.dirent* %25, null
br i1 %26, label %27, label %13
27: ; preds = %21, %6, %20, %1
%28 = call i32 @closedir(i32* %4)
ret void
}