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

/* Variables and functions */
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ err(int, char *, char *);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ printf(char *, ...);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ strcmp(char *, char *);
void listSheet(char *dr) {
  DIR *d;
  struct dirent *dir;
  d = opendir(dr);
  if (!d)
    err(1, "listSheet: %s", dr);
  printf("Available cheatsheets:\n");
  while ((dir = readdir(d)) != NULL)
    if (!strcmp(dir->d_name, ".") || !(strcmp(dir->d_name, "..")))
      continue;
    else
      printf("%s\n", dir->d_name);
  (void)closedir(d);
  return;
}


x86 assembly (clang, -O0):
<listSheet>:
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)
jne    1199 <listSheet+0x39>
mov    -0x8(%rbp),%rdx
mov    $0x1,%edi
lea    0xe6c(%rip),%rsi
call   1080 <err@plt>
lea    0xe6e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1070 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     1215 <listSheet+0xb5>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
lea    0xe5b(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%eax
je     11f6 <listSheet+0x96>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
lea    0xe3e(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%eax
jne    11fb <listSheet+0x9b>
jmp    11a7 <listSheet+0x47>
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
lea    0xe20(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11a7 <listSheet+0x47>
mov    -0x10(%rbp),%rdi
call   1050 <closedir@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<listSheet>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rbx
call   1040 <opendir@plt>
mov    %rax,%r12
test   %rax,%rax
jne    118c <listSheet+0x2c>
lea    0xe81(%rip),%rsi
mov    $0x1,%edi
mov    %rbx,%rdx
call   1080 <err@plt>
lea    0xe7e(%rip),%rdi
call   1030 <puts@plt>
mov    %r12,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
je     11f8 <listSheet+0x98>
lea    0xe63(%rip),%r14
lea    0xe5b(%rip),%r15
jmp    11cd <listSheet+0x6d>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r12,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
je     11f8 <listSheet+0x98>
mov    (%rax),%rbx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1060 <strcmp@plt>
test   %eax,%eax
je     11c0 <listSheet+0x60>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1060 <strcmp@plt>
test   %eax,%eax
je     11c0 <listSheet+0x60>
mov    %rbx,%rdi
call   1030 <puts@plt>
jmp    11c0 <listSheet+0x60>
mov    %r12,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1050 <closedir@plt>

LLVM IR (-O0):
%struct.dirent = type { i8* }
@.str = [14 x i8] c"listSheet: %s\00"
@.str.1 = [24 x i8] c"Available cheatsheets:\0A\00"
@.str.2 = [2 x i8] c".\00"
@.str.3 = [3 x i8] c"..\00"
@.str.4 = [4 x i8] c"%s\0A\00"
define void @listSheet(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 %12, label %9
9: ; preds = %1
%10 = load i8*, i8** %2
%11 = call i32 @err(i32 1, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %10)
br label %12
12: ; preds = %9, %1
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
br label %14
14: ; preds = %36, %30, %12
%15 = load i32*, i32** %3
%16 = call %struct.dirent* @readdir(i32* %15)
store %struct.dirent* %16, %struct.dirent** %4
%17 = icmp ne %struct.dirent* %16, null
br i1 %17, label %18, label %37
18: ; preds = %14
%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 = call i32 @strcmp(i8* %21, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%23 = icmp ne i32 %22, 0
br i1 %23, label %24, label %30
24: ; preds = %18
%25 = load %struct.dirent*, %struct.dirent** %4
%26 = getelementptr inbounds %struct.dirent, %struct.dirent* %25, i32 0, i32 0
%27 = load i8*, i8** %26
%28 = call i32 @strcmp(i8* %27, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%29 = icmp ne i32 %28, 0
br i1 %29, label %31, label %30
30: ; preds = %24, %18
br label %14
31: ; preds = %24
%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 i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* %34)
br label %36
36: ; preds = %31
br label %14
37: ; preds = %14
%38 = load i32*, i32** %3
%39 = call i32 @closedir(i32* %38)
ret void
}

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