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

/* Variables and functions */
scalar_t__ DT_DIR;
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ printHex(char *, int);
int /*<<< orphan*/ printf(char *, ...);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcmp(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
int main(int argc, char **argv) {
  if (argc > 2) {
    printf("Usage: ls [directory]\n");
    exit(1);
  }
  DIR *dir;
  struct dirent *direntry;
  char *path;
  if (argc == 1) {
    path = malloc(2);
    strcpy(path, ".");
  } else {
    argv++;
    path = malloc(strlen(*argv));
    strcpy(path, *argv);
  }
  dir = opendir(path);
  if (dir == NULL) {
    fprintf(stderr, "Failed to open directory %s\n", path);
    return -1;
  }
  while ((direntry = readdir(dir))) {
    if (!strcmp(direntry->d_name, "..") || !strcmp(direntry->d_name, "."))
      continue;
    if (direntry->d_type == DT_DIR)
      printf("dir:  %s\n", direntry->d_name);
    else
      printf("file: %s\n", direntry->d_name);
    printHex(direntry->d_name, -1);
    printf("\n");
  }
  closedir(dir);
  free(path);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jle    11f8 <main+0x38>
lea    0xe19(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
cmpl   $0x1,-0x8(%rbp)
jne    1225 <main+0x65>
mov    $0x2,%edi
call   10c0 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
lea    0xe1a(%rip),%rsi
call   1040 <strcpy@plt>
jmp    1258 <main+0x98>
mov    -0x10(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1060 <strlen@plt>
mov    %eax,%edi
call   10c0 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
call   1040 <strcpy@plt>
mov    -0x28(%rbp),%rdi
call   1050 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1295 <main+0xd5>
mov    0x2d69(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%rdx
lea    0xd93(%rip),%rsi
call   10a0 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1372 <main+0x1b2>
jmp    129a <main+0xda>
mov    -0x18(%rbp),%rdi
call   10b0 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     1359 <main+0x199>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
lea    0xd75(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%eax
je     12e9 <main+0x129>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
lea    0xd5a(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%eax
jne    12ee <main+0x12e>
jmp    129a <main+0xda>
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%rax
mov    0x2cdb(%rip),%rcx
cmp    (%rcx),%rax
jne    1320 <main+0x160>
mov    -0x20(%rbp),%rax
mov    (%rax),%rsi
lea    0xd23(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
jmp    1335 <main+0x175>
mov    -0x20(%rbp),%rax
mov    (%rax),%rsi
lea    0xd13(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
mov    $0xffffffff,%esi
call   10d0 <printHex@plt>
lea    0xcc8(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
jmp    129a <main+0xda>
mov    -0x18(%rbp),%rdi
call   1080 <closedir@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
cmp    $0x3,%edi
jge    132f <main+0x14f>
cmp    $0x1,%edi
jne    120f <main+0x2f>
mov    $0x2,%edi
call   10e0 <malloc@plt>
mov    %rax,%rbp
lea    0xe11(%rip),%rsi
jmp    1229 <main+0x49>
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
call   1080 <strlen@plt>
mov    %eax,%edi
call   10e0 <malloc@plt>
mov    %rax,%rbp
mov    0x8(%rbx),%rsi
mov    %rax,%rdi
call   1050 <strcpy@plt>
mov    %rbp,%rdi
call   1070 <opendir@plt>
test   %rax,%rax
je     1303 <main+0x123>
mov    %rax,%r13
mov    %rbp,(%rsp)
mov    %rax,%rdi
call   10d0 <readdir@plt>
test   %rax,%rax
je     12ee <main+0x10e>
mov    %rax,%rbx
lea    0xdb9(%rip),%r15
lea    0xdb3(%rip),%r12
lea    0xdae(%rip),%r14
jmp    12b1 <main+0xd1>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rbp,%rsi
xor    %eax,%eax
call   1090 <printf@plt>
mov    (%rbx),%rdi
mov    $0xffffffff,%esi
call   10f0 <printHex@plt>
mov    $0xa,%edi
call   1040 <putchar@plt>
mov    %r13,%rdi
call   10d0 <readdir@plt>
mov    %rax,%rbx
test   %rax,%rax
je     12ee <main+0x10e>
mov    (%rbx),%rbp
mov    %rbp,%rdi
mov    %r15,%rsi
call   10b0 <strcmp@plt>
test   %eax,%eax
je     12a1 <main+0xc1>
mov    %rbp,%rdi
mov    %r12,%rsi
call   10b0 <strcmp@plt>
test   %eax,%eax
je     12a1 <main+0xc1>
mov    0x8(%rbx),%rax
mov    %r14,%rdi
mov    0x2cf8(%rip),%rcx
cmp    (%rcx),%rax
je     1280 <main+0xa0>
lea    0xd3e(%rip),%rdi
jmp    1280 <main+0xa0>
mov    %r13,%rdi
call   10a0 <closedir@plt>
mov    (%rsp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
jmp    1320 <main+0x140>
mov    0x2cd6(%rip),%rax
mov    (%rax),%edi
lea    0xced(%rip),%rsi
mov    %rbp,%rdx
call   10c0 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xcfe(%rip),%rdi
call   1060 <puts@plt>
mov    $0x1,%edi
call   1100 <exit@plt>

LLVM IR (-O0):
%struct.dirent = type { i8*, i64 }
@.str = [23 x i8] c"Usage: ls [directory]\0A\00"
@.str.1 = [2 x i8] c".\00"
@stderr = global i32 0
@.str.2 = [29 x i8] c"Failed to open directory %s\0A\00"
@.str.3 = [3 x i8] c"..\00"
@DT_DIR = global i64 0
@.str.4 = [10 x i8] c"dir: %s\0A\00"
@.str.5 = [10 x i8] c"file: %s\0A\00"
@.str.6 = [2 x i8] c"\0A\00"
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 i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp sgt i32 %9, 2
br i1 %10, label %11, label %14
11: ; preds = %2
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %2
%15 = load i32, i32* %4
%16 = icmp eq i32 %15, 1
br i1 %16, label %17, label %21
17: ; preds = %14
%18 = call i8* @malloc(i32 2)
store i8* %18, i8** %8
%19 = load i8*, i8** %8
%20 = call i32 @strcpy(i8* %19, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %32
21: ; preds = %14
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i32 1
store i8** %23, i8*** %5
%24 = load i8**, i8*** %5
%25 = load i8*, i8** %24
%26 = call i32 @strlen(i8* %25)
%27 = call i8* @malloc(i32 %26)
store i8* %27, i8** %8
%28 = load i8*, i8** %8
%29 = load i8**, i8*** %5
%30 = load i8*, i8** %29
%31 = call i32 @strcpy(i8* %28, i8* %30)
br label %32
32: ; preds = %21, %17
%33 = load i8*, i8** %8
%34 = call i32* @opendir(i8* %33)
store i32* %34, i32** %6
%35 = load i32*, i32** %6
%36 = icmp eq i32* %35, null
br i1 %36, label %37, label %41
37: ; preds = %32
%38 = load i32, i32* @stderr
%39 = load i8*, i8** %8
%40 = call i32 @fprintf(i32 %38, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i8* %39)
store i32 -1, i32* %3
br label %86
41: ; preds = %32
br label %42
42: ; preds = %75, %58, %41
%43 = load i32*, i32** %6
%44 = call %struct.dirent* @readdir(i32* %43)
store %struct.dirent* %44, %struct.dirent** %7
%45 = icmp ne %struct.dirent* %44, null
br i1 %45, label %46, label %81
46: ; preds = %42
%47 = load %struct.dirent*, %struct.dirent** %7
%48 = getelementptr inbounds %struct.dirent, %struct.dirent* %47, i32 0, i32 0
%49 = load i8*, i8** %48
%50 = call i32 @strcmp(i8* %49, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%51 = icmp ne i32 %50, 0
br i1 %51, label %52, label %58
52: ; preds = %46
%53 = load %struct.dirent*, %struct.dirent** %7
%54 = getelementptr inbounds %struct.dirent, %struct.dirent* %53, i32 0, i32 0
%55 = load i8*, i8** %54
%56 = call i32 @strcmp(i8* %55, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%57 = icmp ne i32 %56, 0
br i1 %57, label %59, label %58
58: ; preds = %52, %46
br label %42
59: ; preds = %52
%60 = load %struct.dirent*, %struct.dirent** %7
%61 = getelementptr inbounds %struct.dirent, %struct.dirent* %60, i32 0, i32 1
%62 = load i64, i64* %61
%63 = load i64, i64* @DT_DIR
%64 = icmp eq i64 %62, %63
br i1 %64, label %65, label %70
65: ; preds = %59
%66 = load %struct.dirent*, %struct.dirent** %7
%67 = getelementptr inbounds %struct.dirent, %struct.dirent* %66, i32 0, i32 0
%68 = load i8*, i8** %67
%69 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i8* %68)
br label %75
70: ; preds = %59
%71 = load %struct.dirent*, %struct.dirent** %7
%72 = getelementptr inbounds %struct.dirent, %struct.dirent* %71, i32 0, i32 0
%73 = load i8*, i8** %72
%74 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i8* %73)
br label %75
75: ; preds = %70, %65
%76 = load %struct.dirent*, %struct.dirent** %7
%77 = getelementptr inbounds %struct.dirent, %struct.dirent* %76, i32 0, i32 0
%78 = load i8*, i8** %77
%79 = call i32 @printHex(i8* %78, i32 -1)
%80 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
br label %42
81: ; preds = %42
%82 = load i32*, i32** %6
%83 = call i32 @closedir(i32* %82)
%84 = load i8*, i8** %8
%85 = call i32 @free(i8* %84)
store i32 0, i32* %3
br label %86
86: ; preds = %81, %37
%87 = load i32, i32* %3
ret i32 %87
}

LLVM IR (-O2):
%struct.dirent = type { i8*, i64 }
@.str.1 = [2 x i8] c".\00"
@stderr = global i32 0
@.str.2 = [29 x i8] c"Failed to open directory %s\0A\00"
@.str.3 = [3 x i8] c"..\00"
@DT_DIR = global i64 0
@.str.4 = [10 x i8] c"dir: %s\0A\00"
@.str.5 = [10 x i8] c"file: %s\0A\00"
@str = [22 x i8] c"Usage: ls [directory]\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp sgt i32 %0, 2
br i1 %3, label %4, label %7
4: ; preds = %2
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
%6 = tail call i32 @exit(i32 1)
unreachable
7: ; preds = %2
%8 = icmp eq i32 %0, 1
br i1 %8, label %9, label %12
9: ; preds = %7
%10 = tail call dereferenceable_or_null(2) i8* @malloc(i32 2)
%11 = tail call i32 @strcpy(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %19
12: ; preds = %7
%13 = getelementptr inbounds i8*, i8** %1, i64 1
%14 = load i8*, i8** %13
%15 = tail call i32 @strlen(i8* %14)
%16 = tail call i8* @malloc(i32 %15)
%17 = load i8*, i8** %13
%18 = tail call i32 @strcpy(i8* %16, i8* %17)
br label %19
19: ; preds = %12, %9
%20 = phi i8* [ %10, %9 ], [ %16, %12 ]
%21 = tail call i32* @opendir(i8* %20)
%22 = icmp eq i32* %21, null
br i1 %22, label %26, label %23
23: ; preds = %19
%24 = tail call %struct.dirent* @readdir(i32* %21)
%25 = icmp eq %struct.dirent* %24, null
br i1 %25, label %51, label %29
26: ; preds = %19
%27 = load i32, i32* @stderr
%28 = tail call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i8* %20)
br label %54
29: ; preds = %23, %38
%30 = phi %struct.dirent* [ %39, %38 ], [ %24, %23 ]
%31 = getelementptr inbounds %struct.dirent, %struct.dirent* %30, i64 0, i32 0
%32 = load i8*, i8** %31
%33 = tail call i32 @strcmp(i8* dereferenceable(1) %32, i8* dereferenceable(3) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%34 = icmp eq i32 %33, 0
br i1 %34, label %38, label %35
35: ; preds = %29
%36 = tail call i32 @strcmp(i8* dereferenceable(1) %32, i8* dereferenceable(2) getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%37 = icmp eq i32 %36, 0
br i1 %37, label %38, label %41
38: ; preds = %29, %35, %41
%39 = tail call %struct.dirent* @readdir(i32* %21)
%40 = icmp eq %struct.dirent* %39, null
br i1 %40, label %51, label %29
41: ; preds = %35
%42 = getelementptr inbounds %struct.dirent, %struct.dirent* %30, i64 0, i32 1
%43 = load i64, i64* %42
%44 = load i64, i64* @DT_DIR
%45 = icmp eq i64 %43, %44
%46 = select i1 %45, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0)
%47 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) %46, i8* %32)
%48 = load i8*, i8** %31
%49 = tail call i32 @printHex(i8* %48, i32 -1)
%50 = tail call i32 @putchar(i32 10)
br label %38
51: ; preds = %38, %23
%52 = tail call i32 @closedir(i32* %21)
%53 = tail call i32 @free(i8* %20)
br label %54
54: ; preds = %51, %26
%55 = phi i32 [ -1, %26 ], [ 0, %51 ]
ret i32 %55
}