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

/* Variables and functions */
scalar_t__ DT_DIR;
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ stdout;
int print_dirlist(char *filename) {
  struct dirent *de;
  char c;
  DIR *d;
  fprintf(stdout, "<br><br>--------<pre>Directory listing for %s:\n\n",
          filename);
  d = opendir(filename);
  if (!d) {
    fprintf(stdout, "No such directory\n</pre>");
    return -1;
  }
  while (de = readdir(d)) {
    if (de->d_type == DT_DIR)
      fprintf(stdout, "%s/\n", de->d_name);
    else
      fprintf(stdout, "%s\n", de->d_name);
  }
  closedir(d);
  fprintf(stdout, "</pre>");
  return 0;
}


x86 assembly (clang, -O0):
<print_dirlist>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e85(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xea0(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11a2 <print_dirlist+0x62>
mov    0x2e52(%rip),%rax
mov    (%rax),%edi
lea    0xea2(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1244 <print_dirlist+0x104>
jmp    11a7 <print_dirlist+0x67>
mov    -0x28(%rbp),%rdi
call   1060 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     121d <print_dirlist+0xdd>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    0x2e14(%rip),%rcx
cmp    (%rcx),%rax
jne    11f9 <print_dirlist+0xb9>
mov    0x2dfc(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xe5d(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    1218 <print_dirlist+0xd8>
mov    0x2dd8(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xe3e(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    11a7 <print_dirlist+0x67>
mov    -0x28(%rbp),%rdi
call   1040 <closedir@plt>
mov    0x2dab(%rip),%rax
mov    (%rax),%edi
lea    0xe0d(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<print_dirlist>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdi,%rbx
mov    0x2e85(%rip),%r14
mov    (%r14),%edi
lea    0xea3(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %rbx,%rdi
call   1030 <opendir@plt>
test   %rax,%rax
je     11db <print_dirlist+0x9b>
mov    %rax,%rbx
mov    %rax,%rdi
call   1060 <readdir@plt>
test   %rax,%rax
je     11c8 <print_dirlist+0x88>
mov    0x2e55(%rip),%r15
lea    0xeb8(%rip),%r12
lea    0xeb6(%rip),%r13
nopl   0x0(%rax)
mov    (%rax),%rcx
cmp    (%r15),%rcx
mov    0x8(%rax),%rdx
mov    (%r14),%edi
mov    %r13,%rsi
cmove  %r12,%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %rbx,%rdi
call   1060 <readdir@plt>
test   %rax,%rax
jne    11a0 <print_dirlist+0x60>
mov    %rbx,%rdi
call   1040 <closedir@plt>
xor    %ebx,%ebx
lea    0xe6a(%rip),%rsi
jmp    11e7 <print_dirlist+0xa7>
mov    $0xffffffff,%ebx
lea    0xe4a(%rip),%rsi
mov    (%r14),%edi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %ebx,%eax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.dirent = type { i64, i8* }
@stdout = global i32 0
@.str = [49 x i8] c"<br><br>--------<pre>Directory listing for %s:\0A\0A\00"
@.str.1 = [25 x i8] c"No such directory\0A</pre>\00"
@DT_DIR = global i64 0
@.str.2 = [5 x i8] c"%s/\0A\00"
@.str.3 = [4 x i8] c"%s\0A\00"
@.str.4 = [7 x i8] c"</pre>\00"
define i32 @print_dirlist(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.dirent*
%5 = alloca i8
%6 = alloca i32*
store i8* %0, i8** %3
%7 = load i32, i32* @stdout
%8 = load i8*, i8** %3
%9 = call i32 (i32, i8*, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0), i8* %8)
%10 = load i8*, i8** %3
%11 = call i32* @opendir(i8* %10)
store i32* %11, i32** %6
%12 = load i32*, i32** %6
%13 = icmp ne i32* %12, null
br i1 %13, label %17, label %14
14: ; preds = %1
%15 = load i32, i32* @stdout
%16 = call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %46
17: ; preds = %1
br label %18
18: ; preds = %40, %17
%19 = load i32*, i32** %6
%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 %41
22: ; preds = %18
%23 = load %struct.dirent*, %struct.dirent** %4
%24 = getelementptr inbounds %struct.dirent, %struct.dirent* %23, i32 0, i32 0
%25 = load i64, i64* %24
%26 = load i64, i64* @DT_DIR
%27 = icmp eq i64 %25, %26
br i1 %27, label %28, label %34
28: ; preds = %22
%29 = load i32, i32* @stdout
%30 = load %struct.dirent*, %struct.dirent** %4
%31 = getelementptr inbounds %struct.dirent, %struct.dirent* %30, i32 0, i32 1
%32 = load i8*, i8** %31
%33 = call i32 (i32, i8*, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* %32)
br label %40
34: ; preds = %22
%35 = load i32, i32* @stdout
%36 = load %struct.dirent*, %struct.dirent** %4
%37 = getelementptr inbounds %struct.dirent, %struct.dirent* %36, i32 0, i32 1
%38 = load i8*, i8** %37
%39 = call i32 (i32, i8*, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %38)
br label %40
40: ; preds = %34, %28
br label %18
41: ; preds = %18
%42 = load i32*, i32** %6
%43 = call i32 @closedir(i32* %42)
%44 = load i32, i32* @stdout
%45 = call i32 (i32, i8*, ...) @fprintf(i32 %44, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
store i32 0, i32* %2
br label %46
46: ; preds = %41, %14
%47 = load i32, i32* %2
ret i32 %47
}

LLVM IR (-O2):
%struct.dirent = type { i64, i8* }
@stdout = global i32 0
@.str = [49 x i8] c"<br><br>--------<pre>Directory listing for %s:\0A\0A\00"
@.str.1 = [25 x i8] c"No such directory\0A</pre>\00"
@DT_DIR = global i64 0
@.str.2 = [5 x i8] c"%s/\0A\00"
@.str.3 = [4 x i8] c"%s\0A\00"
@.str.4 = [7 x i8] c"</pre>\00"
define i32 @print_dirlist(i8* %0) {
%2 = load i32, i32* @stdout
%3 = tail call i32 (i32, i8*, ...) @fprintf(i32 %2, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0), i8* %0)
%4 = tail call i32* @opendir(i8* %0)
%5 = icmp eq i32* %4, null
br i1 %5, label %24, label %6
6: ; preds = %1
%7 = tail call %struct.dirent* @readdir(i32* %4)
%8 = icmp eq %struct.dirent* %7, null
br i1 %8, label %22, label %9
9: ; preds = %6, %9
%10 = phi %struct.dirent* [ %20, %9 ], [ %7, %6 ]
%11 = getelementptr inbounds %struct.dirent, %struct.dirent* %10, i64 0, i32 0
%12 = load i64, i64* %11
%13 = load i64, i64* @DT_DIR
%14 = icmp eq i64 %12, %13
%15 = load i32, i32* @stdout
%16 = getelementptr inbounds %struct.dirent, %struct.dirent* %10, i64 0, i32 1
%17 = load i8*, i8** %16
%18 = select i1 %14, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0)
%19 = tail call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* %18, i8* %17)
%20 = tail call %struct.dirent* @readdir(i32* %4)
%21 = icmp eq %struct.dirent* %20, null
br i1 %21, label %22, label %9
22: ; preds = %9, %6
%23 = tail call i32 @closedir(i32* %4)
br label %24
24: ; preds = %1, %22
%25 = phi i8* [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), %22 ], [ getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), %1 ]
%26 = phi i32 [ 0, %22 ], [ -1, %1 ]
%27 = load i32, i32* @stdout
%28 = tail call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* %25)
ret i32 %26
}