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

/* Variables and functions */
int /*<<< orphan*/ PATH_MAX;
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
char *get_file_type(char *);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ printf(char *, char const *, char *);
struct dirent *readdir(int /*<<< orphan*/ *);
size_t strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
int main(int argc, char *argv[]) {
  char *dir_path;
  DIR *dir;
  struct dirent *entry;
  char entry_path[PATH_MAX + 1];
  size_t path_len;
  if (argc >= 2) {
    dir_path = argv[1];
  } else {
    dir_path = ".";
  }
  strncpy(entry_path, dir_path, sizeof(entry_path));
  path_len = strlen(dir_path);
  if (entry_path[path_len - 1] != '/') {
    entry_path[path_len] = '/';
    entry_path[path_len + 1] = '\0';
    ++path_len;
  }
  dir = opendir(dir_path);
  while ((entry = readdir(dir)) != NULL) {
    const char *type;
    strncpy(entry_path + path_len, entry->d_name,
            sizeof(entry_path) - path_len);
    type = get_file_type(entry_path);
    printf("%-18s: %s\n", type, entry_path);
  }
  closedir(dir);
  return 0;
}


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)
mov    0x2e63(%rip),%rax
mov    (%rax),%eax
add    $0x1,%eax
mov    %rax,-0x58(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x50(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x38(%rbp)
cmpl   $0x2,-0x8(%rbp)
jl     11d4 <main+0x64>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
jmp    11df <main+0x6f>
lea    0xe25(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0x50(%rbp),%rdi
mov    -0x58(%rbp),%rax
mov    -0x18(%rbp),%rsi
mov    %eax,%edx
call   1030 <strncpy@plt>
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,%rcx
mov    -0x50(%rbp),%rax
mov    %rcx,-0x40(%rbp)
mov    -0x40(%rbp),%rcx
sub    $0x1,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2f,%eax
je     123c <main+0xcc>
mov    -0x50(%rbp),%rax
mov    -0x40(%rbp),%rcx
movb   $0x2f,(%rax,%rcx,1)
mov    -0x40(%rbp),%rcx
movb   $0x0,0x1(%rax,%rcx,1)
mov    -0x40(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x40(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1090 <readdir@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     12a4 <main+0x134>
mov    -0x50(%rbp),%rdi
mov    -0x58(%rbp),%rax
add    -0x40(%rbp),%rdi
mov    -0x28(%rbp),%rcx
mov    (%rcx),%rsi
sub    -0x40(%rbp),%rax
mov    %eax,%edx
call   1030 <strncpy@plt>
mov    -0x50(%rbp),%rdi
call   1080 <get_file_type@plt>
mov    -0x50(%rbp),%rdx
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rsi
lea    0xd68(%rip),%rdi
call   1060 <printf@plt>
jmp    1249 <main+0xd9>
mov    -0x20(%rbp),%rdi
call   1070 <closedir@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    0x2e6b(%rip),%rax
mov    (%rax),%r14d
add    $0x1,%r14d
mov    %rsp,%rbx
lea    0xf(%r14),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
cmp    $0x2,%edi
jl     11a8 <main+0x38>
mov    0x8(%rsi),%r15
jmp    11af <main+0x3f>
lea    0xe51(%rip),%r15
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r14d,%edx
call   1030 <strncpy@plt>
mov    %r15,%rdi
call   1050 <strlen@plt>
mov    %rax,%r13
cmpb   $0x2f,-0x1(%rax,%rbx,1)
je     11da <main+0x6a>
movw   $0x2f,(%rbx,%r13,1)
add    $0x1,%r13
mov    %r15,%rdi
call   1040 <opendir@plt>
mov    %rax,%r15
mov    %rax,%rdi
call   1090 <readdir@plt>
test   %rax,%rax
je     1231 <main+0xc1>
lea    (%rbx,%r13,1),%r12
sub    %r13d,%r14d
lea    0xe02(%rip),%r13
mov    (%rax),%rsi
mov    %r12,%rdi
mov    %r14d,%edx
call   1030 <strncpy@plt>
mov    %rbx,%rdi
call   1080 <get_file_type@plt>
mov    %r13,%rdi
mov    %rax,%rsi
mov    %rbx,%rdx
call   1060 <printf@plt>
mov    %r15,%rdi
call   1090 <readdir@plt>
test   %rax,%rax
jne    1200 <main+0x90>
mov    %r15,%rdi
call   1070 <closedir@plt>
xor    %eax,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i8* }
@PATH_MAX = global i32 0
@.str = [2 x i8] c".\00"
@.str.1 = [11 x i8] c"%-18s: %s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca %struct.dirent*
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i64
%12 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i32, i32* @PATH_MAX
%14 = add nsw i32 %13, 1
%15 = zext i32 %14 to i64
%16 = call i8* @llvm.stacksave()
store i8* %16, i8** %9
%17 = alloca i8, i64 %15
store i64 %15, i64* %10
%18 = load i32, i32* %4
%19 = icmp sge i32 %18, 2
br i1 %19, label %20, label %24
20: ; preds = %2
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 1
%23 = load i8*, i8** %22
store i8* %23, i8** %6
br label %25
24: ; preds = %2
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %6
br label %25
25: ; preds = %24, %20
%26 = load i8*, i8** %6
%27 = trunc i64 %15 to i32
%28 = call i32 @strncpy(i8* %17, i8* %26, i32 %27)
%29 = load i8*, i8** %6
%30 = call i64 @strlen(i8* %29)
store i64 %30, i64* %11
%31 = load i64, i64* %11
%32 = sub i64 %31, 1
%33 = getelementptr inbounds i8, i8* %17, i64 %32
%34 = load i8, i8* %33
%35 = sext i8 %34 to i32
%36 = icmp ne i32 %35, 47
br i1 %36, label %37, label %45
37: ; preds = %25
%38 = load i64, i64* %11
%39 = getelementptr inbounds i8, i8* %17, i64 %38
store i8 47, i8* %39
%40 = load i64, i64* %11
%41 = add i64 %40, 1
%42 = getelementptr inbounds i8, i8* %17, i64 %41
store i8 0, i8* %42
%43 = load i64, i64* %11
%44 = add i64 %43, 1
store i64 %44, i64* %11
br label %45
45: ; preds = %37, %25
%46 = load i8*, i8** %6
%47 = call i32* @opendir(i8* %46)
store i32* %47, i32** %7
br label %48
48: ; preds = %52, %45
%49 = load i32*, i32** %7
%50 = call %struct.dirent* @readdir(i32* %49)
store %struct.dirent* %50, %struct.dirent** %8
%51 = icmp ne %struct.dirent* %50, null
br i1 %51, label %52, label %65
52: ; preds = %48
%53 = load i64, i64* %11
%54 = getelementptr inbounds i8, i8* %17, i64 %53
%55 = load %struct.dirent*, %struct.dirent** %8
%56 = getelementptr inbounds %struct.dirent, %struct.dirent* %55, i32 0, i32 0
%57 = load i8*, i8** %56
%58 = load i64, i64* %11
%59 = sub i64 %15, %58
%60 = trunc i64 %59 to i32
%61 = call i32 @strncpy(i8* %54, i8* %57, i32 %60)
%62 = call i8* @get_file_type(i8* %17)
store i8* %62, i8** %12
%63 = load i8*, i8** %12
%64 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* %63, i8* %17)
br label %48
65: ; preds = %48
%66 = load i32*, i32** %7
%67 = call i32 @closedir(i32* %66)
store i32 0, i32* %3
%68 = load i8*, i8** %9
%69 = load i32, i32* %3
ret i32 %69
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@PATH_MAX = global i32 0
@.str = [2 x i8] c".\00"
@.str.1 = [11 x i8] c"%-18s: %s\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = load i32, i32* @PATH_MAX
%4 = add nsw i32 %3, 1
%5 = zext i32 %4 to i64
%6 = alloca i8, i64 %5
%7 = icmp sgt i32 %0, 1
br i1 %7, label %8, label %11
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
br label %11
11: ; preds = %2, %8
%12 = phi i8* [ %10, %8 ], [ getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), %2 ]
%13 = call i32 @strncpy(i8* %6, i8* %12, i32 %4)
%14 = call i64 @strlen(i8* dereferenceable(1) %12)
%15 = add i64 %14, -1
%16 = getelementptr inbounds i8, i8* %6, i64 %15
%17 = load i8, i8* %16
%18 = icmp eq i8 %17, 47
br i1 %18, label %23, label %19
19: ; preds = %11
%20 = getelementptr inbounds i8, i8* %6, i64 %14
store i8 47, i8* %20
%21 = add i64 %14, 1
%22 = getelementptr inbounds i8, i8* %6, i64 %21
store i8 0, i8* %22
br label %23
23: ; preds = %19, %11
%24 = phi i64 [ %21, %19 ], [ %14, %11 ]
%25 = call i32* @opendir(i8* %12)
%26 = call %struct.dirent* @readdir(i32* %25)
%27 = icmp eq %struct.dirent* %26, null
br i1 %27, label %41, label %28
28: ; preds = %23
%29 = getelementptr inbounds i8, i8* %6, i64 %24
%30 = trunc i64 %24 to i32
%31 = sub i32 %4, %30
br label %32
32: ; preds = %28, %32
%33 = phi %struct.dirent* [ %26, %28 ], [ %39, %32 ]
%34 = getelementptr inbounds %struct.dirent, %struct.dirent* %33, i64 0, i32 0
%35 = load i8*, i8** %34
%36 = call i32 @strncpy(i8* %29, i8* %35, i32 %31)
%37 = call i8* @get_file_type(i8* %6)
%38 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* %37, i8* %6)
%39 = call %struct.dirent* @readdir(i32* %25)
%40 = icmp eq %struct.dirent* %39, null
br i1 %40, label %41, label %32
41: ; preds = %32, %23
%42 = call i32 @closedir(i32* %25)
ret i32 0
}