#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 const *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*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ *opendir(char const *);
int /*<<< orphan*/ printf(char *, char const *);
struct dirent *readdir(int /*<<< orphan*/ *);
void list_directory(const char *dirname) {
  DIR *dir;
  struct dirent *ent;
  dir = opendir(dirname);
  if (dir != NULL) {
    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);
  } else {
    printf("Cannot open directory %s\n", dirname);
    exit(EXIT_FAILURE);
  }
}


x86 assembly (clang, -O0):
<list_directory>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1241 <list_directory+0xf1>
jmp    1179 <list_directory+0x29>
mov    -0x10(%rbp),%rdi
call   1060 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     1233 <list_directory+0xe3>
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
sub    $0x80,%eax
je     11cf <list_directory+0x7f>
jmp    11a9 <list_directory+0x59>
mov    -0x1c(%rbp),%eax
sub    $0x81,%eax
je     1201 <list_directory+0xb1>
jmp    11bc <list_directory+0x6c>
mov    -0x1c(%rbp),%eax
sub    $0x82,%eax
je     11e8 <list_directory+0x98>
jmp    121a <list_directory+0xca>
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe47(%rip),%rdi
call   1040 <printf@plt>
jmp    122e <list_directory+0xde>
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe09(%rip),%rdi
call   1040 <printf@plt>
jmp    122e <list_directory+0xde>
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xdf5(%rip),%rdi
call   1040 <printf@plt>
jmp    122e <list_directory+0xde>
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xde1(%rip),%rdi
call   1040 <printf@plt>
jmp    1179 <list_directory+0x29>
mov    -0x10(%rbp),%rdi
call   1050 <closedir@plt>
jmp    125f <list_directory+0x10f>
mov    -0x8(%rbp),%rsi
lea    0xdc3(%rip),%rdi
call   1040 <printf@plt>
mov    0x2d88(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
add    $0x20,%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     11cc <list_directory+0x7c>
mov    %rax,%rbx
mov    %rax,%rdi
call   1060 <readdir@plt>
test   %rax,%rax
je     11bf <list_directory+0x6f>
lea    0xe91(%rip),%r14
lea    0xeac(%rip),%r15
jmp    11a6 <list_directory+0x56>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    0x8(%rax),%rsi
call   1040 <printf@plt>
mov    %rbx,%rdi
call   1060 <readdir@plt>
test   %rax,%rax
je     11bf <list_directory+0x6f>
mov    (%rax),%ecx
add    $0xffffff80,%ecx
mov    %r14,%rdi
cmp    $0x2,%ecx
ja     1190 <list_directory+0x40>
movslq %ecx,%rcx
movslq (%r15,%rcx,4),%rdi
add    %r15,%rdi
jmp    1190 <list_directory+0x40>
mov    %rbx,%rdi
pop    %rbx
pop    %r14
pop    %r15
jmp    1050 <closedir@plt>
lea    0xe3c(%rip),%rdi
mov    %r14,%rsi
call   1040 <printf@plt>
mov    0x2dfe(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

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

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