#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_ISREG(int /*<<< orphan*/);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ puts(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, char *, char *);
int /*<<< orphan*/ stat(char *, struct stat *);
void listDir(char *dir) {
  char tmpFileName[255];
  DIR *dp;
  struct dirent *ep;
  struct stat buf;
  dp = opendir(dir);
  if (dp != NULL) {
    while (ep = readdir(dp)) {
      sprintf(tmpFileName, "%s/%s", dir, ep->d_name);
      stat(tmpFileName, &buf);
      if (S_ISREG(buf.st_mode)) {
        puts(ep->d_name);
      }
    }
    closedir(dp);
  } else
    perror("Couldn't open the directory!\n");
}


x86 assembly (clang, -O0):
<listDir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x118(%rbp)
cmpq   $0x0,-0x118(%rbp)
je     123d <listDir+0xbd>
jmp    11b2 <listDir+0x32>
mov    -0x118(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,-0x120(%rbp)
cmp    $0x0,%rax
je     122c <listDir+0xac>
lea    -0x110(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    -0x120(%rbp),%rax
mov    (%rax),%rcx
lea    0xe15(%rip),%rsi
call   10a0 <sprintf@plt>
lea    -0x110(%rbp),%rdi
lea    -0x128(%rbp),%rsi
call   1060 <stat@plt>
mov    -0x128(%rbp),%edi
call   1070 <S_ISREG@plt>
cmp    $0x0,%rax
je     1227 <listDir+0xa7>
mov    -0x120(%rbp),%rax
mov    (%rax),%rdi
call   1030 <puts@plt>
jmp    11b2 <listDir+0x32>
mov    -0x118(%rbp),%rdi
call   1050 <closedir@plt>
jmp    1249 <listDir+0xc9>
lea    0xdc2(%rip),%rdi
call   1090 <perror@plt>
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<listDir>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x118,%rsp
mov    %rdi,%r14
call   1040 <opendir@plt>
test   %rax,%rax
je     122f <listDir+0xaf>
mov    %rax,%r12
mov    %rax,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
je     1216 <listDir+0x96>
mov    %rax,%rbp
lea    0xe44(%rip),%r15
lea    0x10(%rsp),%rbx
lea    0x8(%rsp),%r13
jmp    11e0 <listDir+0x60>
nopl   0x0(%rax,%rax,1)
mov    %r12,%rdi
call   1080 <readdir@plt>
mov    %rax,%rbp
test   %rax,%rax
je     1216 <listDir+0x96>
mov    0x0(%rbp),%rcx
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
call   10a0 <sprintf@plt>
mov    %rbx,%rdi
mov    %r13,%rsi
call   1060 <stat@plt>
mov    0x8(%rsp),%edi
call   1070 <S_ISREG@plt>
test   %rax,%rax
je     11d0 <listDir+0x50>
mov    0x0(%rbp),%rdi
call   1030 <puts@plt>
jmp    11d0 <listDir+0x50>
mov    %r12,%rdi
add    $0x118,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <closedir@plt>
lea    0xdd0(%rip),%rdi
add    $0x118,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1090 <perror@plt>

LLVM IR (-O0):
%struct.dirent = type { i8* }
%struct.stat = type { i32 }
@.str = [6 x i8] c"%s/%s\00"
@.str.1 = [30 x i8] c"Couldn't open the directory!\0A\00"
define void @listDir(i8* %0) {
%2 = alloca i8*
%3 = alloca [255 x i8]
%4 = alloca i32*
%5 = alloca %struct.dirent*
%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 %38
11: ; preds = %1
br label %12
12: ; preds = %34, %11
%13 = load i32*, i32** %4
%14 = call %struct.dirent* @readdir(i32* %13)
store %struct.dirent* %14, %struct.dirent** %5
%15 = icmp ne %struct.dirent* %14, null
br i1 %15, label %16, label %35
16: ; preds = %12
%17 = getelementptr inbounds [255 x i8], [255 x i8]* %3, i64 0, i64 0
%18 = load i8*, i8** %2
%19 = load %struct.dirent*, %struct.dirent** %5
%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 [255 x i8], [255 x i8]* %3, i64 0, i64 0
%24 = call i32 @stat(i8* %23, %struct.stat* %6)
%25 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%26 = load i32, i32* %25
%27 = call i64 @S_ISREG(i32 %26)
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %34
29: ; preds = %16
%30 = load %struct.dirent*, %struct.dirent** %5
%31 = getelementptr inbounds %struct.dirent, %struct.dirent* %30, i32 0, i32 0
%32 = load i8*, i8** %31
%33 = call i32 @puts(i8* %32)
br label %34
34: ; preds = %29, %16
br label %12
35: ; preds = %12
%36 = load i32*, i32** %4
%37 = call i32 @closedir(i32* %36)
br label %40
38: ; preds = %1
%39 = call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
br label %40
40: ; preds = %38, %35
ret void
}

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