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

/* Variables and functions */
#define DT_DIR 130
#define DT_LNK 129
#define DT_REG 128
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *, char *);
int /*<<< orphan*/ *opendir(char const *);
int /*<<< orphan*/ printf(char *, char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
void list_directory(const char *dirname) {
  DIR *dir = opendir(dirname);
  if (!dir) {
    fprintf(stderr, "Cannot open %s (%s)\n", dirname, strerror(errno));
    exit(EXIT_FAILURE);
  }
  struct dirent *ent;
  while ((ent = readdir(dir)) != NULL) {
    switch (ent->d_type) {
    case DT_REG:
      printf("%s\n", ent->d_name);
      break;
    case DT_DIR:
      printf("%s/\n", ent->d_name);
      break;
    case DT_LNK:
      printf("%s@\n", ent->d_name);
      break;
    default:
      printf("%s*\n", ent->d_name);
    }
  }
  closedir(dir);
}


x86 assembly (clang, -O0):
<list_directory>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11da <list_directory+0x6a>
mov    0x2e45(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    0x2e29(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%rdx
mov    %rax,%rcx
lea    0xe39(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2dfd(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
jmp    11df <list_directory+0x6f>
mov    -0x10(%rbp),%rdi
call   1070 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     1299 <list_directory+0x129>
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
sub    $0x80,%eax
je     1235 <list_directory+0xc5>
jmp    120f <list_directory+0x9f>
mov    -0x28(%rbp),%eax
sub    $0x81,%eax
je     1267 <list_directory+0xf7>
jmp    1222 <list_directory+0xb2>
mov    -0x28(%rbp),%eax
sub    $0x82,%eax
je     124e <list_directory+0xde>
jmp    1280 <list_directory+0x110>
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xdd1(%rip),%rdi
call   1040 <printf@plt>
jmp    1294 <list_directory+0x124>
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xdbc(%rip),%rdi
call   1040 <printf@plt>
jmp    1294 <list_directory+0x124>
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xda8(%rip),%rdi
call   1040 <printf@plt>
jmp    1294 <list_directory+0x124>
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xd94(%rip),%rdi
call   1040 <printf@plt>
jmp    11df <list_directory+0x6f>
mov    -0x10(%rbp),%rdi
call   1050 <closedir@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<list_directory>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
call   1030 <opendir@plt>
test   %rax,%rax
je     11ec <list_directory+0x7c>
mov    %rax,%rbx
mov    %rax,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
je     11df <list_directory+0x6f>
lea    0xe8a(%rip),%r14
lea    0xe88(%rip),%r15
jmp    11c6 <list_directory+0x56>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    0x8(%rax),%rsi
call   1040 <printf@plt>
mov    %rbx,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
je     11df <list_directory+0x6f>
mov    (%rax),%ecx
add    $0xffffff80,%ecx
mov    %r14,%rdi
cmp    $0x2,%ecx
ja     11b0 <list_directory+0x40>
movslq %ecx,%rcx
movslq (%r15,%rcx,4),%rdi
add    %r15,%rdi
jmp    11b0 <list_directory+0x40>
mov    %rbx,%rdi
pop    %rbx
pop    %r14
pop    %r15
jmp    1050 <closedir@plt>
mov    0x2ded(%rip),%rax
mov    (%rax),%ebx
mov    0x2ddc(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xdf6(%rip),%rsi
mov    %ebx,%edi
mov    %r14,%rdx
mov    %rax,%rcx
call   1060 <fprintf@plt>
mov    0x2db2(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.dirent = type { i32, i8* }
@stderr = global i32 0
@.str = [21 x i8] c"Cannot open %s (%s)\0A\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@.str.1 = [4 x i8] c"%s\0A\00"
@.str.2 = [5 x i8] c"%s/\0A\00"
@.str.3 = [5 x i8] c"%s@\0A\00"
@.str.4 = [5 x i8] c"%s*\0A\00"
define void @list_directory(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca %struct.dirent*
store i8* %0, i8** %2
%5 = load i8*, i8** %2
%6 = call i32* @opendir(i8* %5)
store i32* %6, i32** %3
%7 = load i32*, i32** %3
%8 = icmp ne i32* %7, null
br i1 %8, label %17, label %9
9: ; preds = %1
%10 = load i32, i32* @stderr
%11 = load i8*, i8** %2
%12 = load i32, i32* @errno
%13 = call i8* @strerror(i32 %12)
%14 = call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %11, i8* %13)
%15 = load i32, i32* @EXIT_FAILURE
%16 = call i32 @exit(i32 %15)
unreachable
17: ; preds = %1
br label %18
18: ; preds = %46, %17
%19 = load i32*, i32** %3
%20 = call %struct.dirent* @readdir(i32* %19)
store %struct.dirent* %20, %struct.dirent** %4
%21 = icmp ne %struct.dirent* %20, null
br i1 %21, label %22, label %47
22: ; preds = %18
%23 = load %struct.dirent*, %struct.dirent** %4
%24 = getelementptr inbounds %struct.dirent, %struct.dirent* %23, i32 0, i32 0
%25 = load i32, i32* %24
switch i32 %25, label %41 [
i32 128, label %26
i32 130, label %31
i32 129, label %36
]
26: ; preds = %22
%27 = load %struct.dirent*, %struct.dirent** %4
%28 = getelementptr inbounds %struct.dirent, %struct.dirent* %27, i32 0, i32 1
%29 = load i8*, i8** %28
%30 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %29)
br label %46
31: ; preds = %22
%32 = load %struct.dirent*, %struct.dirent** %4
%33 = getelementptr inbounds %struct.dirent, %struct.dirent* %32, i32 0, i32 1
%34 = load i8*, i8** %33
%35 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* %34)
br label %46
36: ; preds = %22
%37 = load %struct.dirent*, %struct.dirent** %4
%38 = getelementptr inbounds %struct.dirent, %struct.dirent* %37, i32 0, i32 1
%39 = load i8*, i8** %38
%40 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8* %39)
br label %46
41: ; preds = %22
%42 = load %struct.dirent*, %struct.dirent** %4
%43 = getelementptr inbounds %struct.dirent, %struct.dirent* %42, i32 0, i32 1
%44 = load i8*, i8** %43
%45 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i8* %44)
br label %46
46: ; preds = %41, %36, %31, %26
br label %18
47: ; preds = %18
%48 = load i32*, i32** %3
%49 = call i32 @closedir(i32* %48)
ret void
}

LLVM IR (-O2):
%struct.dirent = type { i32, i8* }
@stderr = global i32 0
@.str = [21 x i8] c"Cannot open %s (%s)\0A\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@.str.1 = [4 x i8] c"%s\0A\00"
@.str.2 = [5 x i8] c"%s/\0A\00"
@.str.3 = [5 x i8] c"%s@\0A\00"
@.str.4 = [5 x i8] c"%s*\0A\00"
@reltable.list_directory = [3 x i32] [i32 trunc (i64 sub (i64 ptrtoint ([4 x i8]* @.str.1 to i64), i64 ptrtoint ([3 x i32]* @reltable.list_directory to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([5 x i8]* @.str.3 to i64), i64 ptrtoint ([3 x i32]* @reltable.list_directory to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([5 x i8]* @.str.2 to i64), i64 ptrtoint ([3 x i32]* @reltable.list_directory to i64)) to i32)]
define void @list_directory(i8* %0) {
%2 = tail call i32* @opendir(i8* %0)
%3 = icmp eq i32* %2, null
br i1 %3, label %7, label %4
4: ; preds = %1
%5 = tail call %struct.dirent* @readdir(i32* %2)
%6 = icmp eq %struct.dirent* %5, null
br i1 %6, label %31, label %14
7: ; preds = %1
%8 = load i32, i32* @stderr
%9 = load i32, i32* @errno
%10 = tail call i8* @strerror(i32 %9)
%11 = tail call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %10)
%12 = load i32, i32* @EXIT_FAILURE
%13 = tail call i32 @exit(i32 %12)
unreachable
14: ; preds = %4, %24
%15 = phi %struct.dirent* [ %29, %24 ], [ %5, %4 ]
%16 = getelementptr inbounds %struct.dirent, %struct.dirent* %15, i64 0, i32 0
%17 = load i32, i32* %16
%18 = add i32 %17, -128
%19 = icmp ult i32 %18, 3
br i1 %19, label %20, label %24
20: ; preds = %14
%21 = sext i32 %18 to i64
%22 = shl i64 %21, 2
%23 = call i8* @llvm.load.relative.i64(i8* bitcast ([3 x i32]* @reltable.list_directory to i8*), i64 %22)
br label %24
24: ; preds = %14, %20
%25 = phi i8* [ %23, %20 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), %14 ]
%26 = getelementptr inbounds %struct.dirent, %struct.dirent* %15, i64 0, i32 1
%27 = load i8*, i8** %26
%28 = tail call i32 @printf(i8* %25, i8* %27)
%29 = tail call %struct.dirent* @readdir(i32* %2)
%30 = icmp eq %struct.dirent* %29, null
br i1 %30, label %31, label %14
31: ; preds = %24, %4
%32 = tail call i32 @closedir(i32* %2)
ret void
}