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

/* Variables and functions */
int EXIT_SUCCESS;
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ printf(char *, ...);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ scanf(char *, char *);
int main(int argc, char **argv) {
  DIR *current_directory_ptr;
  struct dirent *next_entry_ptr;
  char open[60];
  printf("Please enter a directory to open:");
  scanf(" %s", open);
  current_directory_ptr = opendir(open);
  next_entry_ptr = readdir(current_directory_ptr);
  while (next_entry_ptr != NULL) {
    printf("File has inode number %d and is called %s\n",
           (int)next_entry_ptr->d_ino, next_entry_ptr->d_name);
    next_entry_ptr = readdir(current_directory_ptr);
  }
  closedir(current_directory_ptr);
  return (EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe93(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x60(%rbp),%rsi
lea    0xea3(%rip),%rdi
call   1070 <scanf@plt>
lea    -0x60(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11da <main+0x8a>
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%rax
mov    %eax,%esi
mov    -0x20(%rbp),%rax
mov    (%rax),%rdx
lea    0xe65(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1060 <readdir@plt>
mov    %rax,-0x20(%rbp)
jmp    119e <main+0x4e>
mov    -0x18(%rbp),%rdi
call   1050 <closedir@plt>
mov    0x2df6(%rip),%rax
mov    (%rax),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x48,%rsp
lea    0xea2(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xeb6(%rip),%rdi
mov    %rsp,%rbx
mov    %rbx,%rsi
call   1070 <scanf@plt>
mov    %rbx,%rdi
call   1030 <opendir@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1060 <readdir@plt>
test   %rax,%rax
je     11bd <main+0x6d>
lea    0xe90(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
mov    0x8(%rax),%esi
mov    (%rax),%rdx
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %rbx,%rdi
call   1060 <readdir@plt>
test   %rax,%rax
jne    11a0 <main+0x50>
mov    %rbx,%rdi
call   1050 <closedir@plt>
mov    0x2e14(%rip),%rax
mov    (%rax),%eax
add    $0x48,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.dirent = type { i8*, i64 }
@.str = [34 x i8] c"Please enter a directory to open:\00"
@.str.1 = [4 x i8] c" %s\00"
@.str.2 = [43 x i8] c"File has inode number %d and is called %s\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca %struct.dirent*
%8 = alloca [60 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0))
%10 = getelementptr inbounds [60 x i8], [60 x i8]* %8, i64 0, i64 0
%11 = call i32 @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %10)
%12 = getelementptr inbounds [60 x i8], [60 x i8]* %8, i64 0, i64 0
%13 = call i32* @opendir(i8* %12)
store i32* %13, i32** %6
%14 = load i32*, i32** %6
%15 = call %struct.dirent* @readdir(i32* %14)
store %struct.dirent* %15, %struct.dirent** %7
br label %16
16: ; preds = %19, %2
%17 = load %struct.dirent*, %struct.dirent** %7
%18 = icmp ne %struct.dirent* %17, null
br i1 %18, label %19, label %30
19: ; preds = %16
%20 = load %struct.dirent*, %struct.dirent** %7
%21 = getelementptr inbounds %struct.dirent, %struct.dirent* %20, i32 0, i32 1
%22 = load i64, i64* %21
%23 = trunc i64 %22 to i32
%24 = load %struct.dirent*, %struct.dirent** %7
%25 = getelementptr inbounds %struct.dirent, %struct.dirent* %24, i32 0, i32 0
%26 = load i8*, i8** %25
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0), i32 %23, i8* %26)
%28 = load i32*, i32** %6
%29 = call %struct.dirent* @readdir(i32* %28)
store %struct.dirent* %29, %struct.dirent** %7
br label %16
30: ; preds = %16
%31 = load i32*, i32** %6
%32 = call i32 @closedir(i32* %31)
%33 = load i32, i32* @EXIT_SUCCESS
ret i32 %33
}

LLVM IR (-O2):
%struct.dirent = type { i8*, i64 }
@.str = [34 x i8] c"Please enter a directory to open:\00"
@.str.1 = [4 x i8] c" %s\00"
@.str.2 = [43 x i8] c"File has inode number %d and is called %s\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [60 x i8]
%4 = getelementptr inbounds [60 x i8], [60 x i8]* %3, i64 0, i64 0
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0))
%6 = call i32 @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %4)
%7 = call i32* @opendir(i8* %4)
%8 = call %struct.dirent* @readdir(i32* %7)
%9 = icmp eq %struct.dirent* %8, null
br i1 %9, label %20, label %10
10: ; preds = %2, %10
%11 = phi %struct.dirent* [ %18, %10 ], [ %8, %2 ]
%12 = getelementptr inbounds %struct.dirent, %struct.dirent* %11, i64 0, i32 1
%13 = load i64, i64* %12
%14 = trunc i64 %13 to i32
%15 = getelementptr inbounds %struct.dirent, %struct.dirent* %11, i64 0, i32 0
%16 = load i8*, i8** %15
%17 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0), i32 %14, i8* %16)
%18 = call %struct.dirent* @readdir(i32* %7)
%19 = icmp eq %struct.dirent* %18, null
br i1 %19, label %20, label %10
20: ; preds = %10, %2
%21 = call i32 @closedir(i32* %7)
%22 = load i32, i32* @EXIT_SUCCESS
ret i32 %22
}