#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 stat {
  int st_uid;
  int /*<<< orphan*/ st_mode;
  int /*<<< orphan*/ st_size;
  int /*<<< orphan*/ st_gid;
};
struct dirent {
  char *d_name;
};
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int getperms(int /*<<< orphan*/, char *);
scalar_t__ gettype(int /*<<< orphan*/);
int lstat(char *, struct stat *);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ printf(char *, ...);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
int main(int argc, char **argv) {
  DIR *dirp;
  char *path = "/";
  char *t;
  struct dirent *dirent;
  char pbuf[10];
  char pstr[256];
  int ix;
  if (argc == 2) {
    path = argv[1];
  }
  dirp = opendir(path);
  if (!dirp) {
    fprintf(stderr, "\ncould not open %s\n", path);
    return 0;
  }
  while ((dirent = readdir(dirp))) {
    int rc;
    struct stat sb;
    strncpy(pstr, path, 256);
    ix = strlen(path);
    if (pstr[ix - 1] != '/') {
      pstr[ix] = '/';
      ix++;
    }
    strncpy(&pstr[ix], dirent->d_name, 256 - ix);
    rc = lstat(pstr, &sb);
    if (rc < 0) {
      printf("failed to to stat:%s:\n", dirent->d_name);
    } else {
      printf("%c%s %8d %8d %16lld %s\n", (char)gettype(sb.st_mode),
             getperms(sb.st_mode, pbuf), sb.st_uid, sb.st_gid, sb.st_size,
             dirent->d_name);
      fflush(stdout);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x170,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe40(%rip),%rax
mov    %rax,-0x20(%rbp)
cmpl   $0x2,-0x8(%rbp)
jne    11da <main+0x3a>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1217 <main+0x77>
mov    0x2de7(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
lea    0xdfc(%rip),%rsi
call   1080 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    136b <main+0x1cb>
jmp    121c <main+0x7c>
mov    -0x18(%rbp),%rdi
call   10b0 <readdir@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
je     1364 <main+0x1c4>
lea    -0x140(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    $0x100,%edx
call   1030 <strncpy@plt>
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x144(%rbp)
mov    -0x144(%rbp),%eax
sub    $0x1,%eax
cltq
movsbl -0x140(%rbp,%rax,1),%eax
ff
cmp    $0x2f,%eax
je     1291 <main+0xf1>
movslq -0x144(%rbp),%rax
movb   $0x2f,-0x140(%rbp,%rax,1)
2f
mov    -0x144(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x144(%rbp)
movslq -0x144(%rbp),%rax
lea    -0x140(%rbp),%rdi
add    %rax,%rdi
mov    -0x30(%rbp),%rax
mov    (%rax),%rsi
mov    $0x100,%edx
sub    -0x144(%rbp),%edx
call   1030 <strncpy@plt>
lea    -0x140(%rbp),%rdi
lea    -0x158(%rbp),%rsi
call   1070 <lstat@plt>
mov    %eax,-0x148(%rbp)
cmpl   $0x0,-0x148(%rbp)
jge    12f9 <main+0x159>
mov    -0x30(%rbp),%rax
mov    (%rax),%rsi
lea    0xd29(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    135f <main+0x1bf>
mov    -0x154(%rbp),%edi
call   10a0 <gettype@plt>
movsbl %al,%eax
mov    %eax,-0x15c(%rbp)
mov    -0x154(%rbp),%edi
lea    -0x3a(%rbp),%rsi
call   1090 <getperms@plt>
mov    -0x15c(%rbp),%esi
mov    %eax,%edx
mov    -0x158(%rbp),%ecx
mov    -0x14c(%rbp),%r8d
mov    -0x150(%rbp),%r9d
mov    -0x30(%rbp),%rax
mov    (%rax),%rax
lea    0xce7(%rip),%rdi
mov    %rax,(%rsp)
mov    $0x0,%al
call   1060 <printf@plt>
mov    0x2c80(%rip),%rax
mov    (%rax),%edi
call   10c0 <fflush@plt>
jmp    121c <main+0x7c>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x170,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x138,%rsp
cmp    $0x2,%edi
jne    11bc <main+0x1c>
mov    0x8(%rsi),%rbx
jmp    11c3 <main+0x23>
lea    0xe3d(%rip),%rbx
mov    %rbx,%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     12e4 <main+0x144>
mov    %rax,%r14
mov    %rax,%rdi
call   10b0 <readdir@plt>
test   %rax,%rax
je     12fc <main+0x15c>
mov    %rax,%rbp
lea    0x30(%rsp),%r15
jmp    1294 <main+0xf4>
cs nopw 0x0(%rax,%rax,1)
nop
cltq
lea    (%rsp,%rax,1),%rdi
add    $0x30,%rdi
mov    0x0(%rbp),%rsi
mov    $0x100,%edx
sub    %eax,%edx
call   1030 <strncpy@plt>
mov    %r15,%rdi
lea    0x10(%rsp),%rsi
call   1070 <lstat@plt>
test   %eax,%eax
js     12d0 <main+0x130>
mov    0x14(%rsp),%r13d
mov    %r13d,%edi
call   10a0 <gettype@plt>
movsbl %al,%r12d
mov    %r13d,%edi
lea    0x26(%rsp),%rsi
call   1090 <getperms@plt>
mov    0x1c(%rsp),%r8d
mov    0x10(%rsp),%ecx
mov    0x18(%rsp),%r9d
mov    0x0(%rbp),%rdx
mov    %rdx,(%rsp)
lea    0xdc3(%rip),%rdi
mov    %r12d,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2d5b(%rip),%rax
mov    (%rax),%edi
call   10c0 <fflush@plt>
mov    %r14,%rdi
call   10b0 <readdir@plt>
mov    %rax,%rbp
test   %rax,%rax
je     12fc <main+0x15c>
mov    %r15,%rdi
mov    %rbx,%rsi
mov    $0x100,%edx
call   1030 <strncpy@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
movslq %eax,%rcx
cmpb   $0x2f,-0x1(%rcx,%r15,1)
je     1200 <main+0x60>
movb   $0x2f,0x30(%rsp,%rcx,1)
add    $0x1,%ecx
mov    %ecx,%eax
jmp    1200 <main+0x60>
nopw   0x0(%rax,%rax,1)
mov    0x0(%rbp),%rsi
lea    0xd3b(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    1284 <main+0xe4>
mov    0x2cf5(%rip),%rax
mov    (%rax),%edi
lea    0xd0e(%rip),%rsi
mov    %rbx,%rdx
call   1080 <fprintf@plt>
xor    %eax,%eax
add    $0x138,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i8* }
%struct.stat = type { i32, i32, i32, i32 }
@.str = [2 x i8] c"/\00"
@stderr = global i32 0
@.str.1 = [20 x i8] c"\0Acould not open %s\0A\00"
@.str.2 = [23 x i8] c"failed to to stat:%s:\0A\00"
@.str.3 = [24 x i8] c"%c%s %8d %8d %16lld %s\0A\00"
@stdout = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca %struct.dirent*
%10 = alloca [10 x i8]
%11 = alloca [256 x i8]
%12 = alloca i32
%13 = alloca i32
%14 = alloca %struct.stat
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %7
%15 = load i32, i32* %4
%16 = icmp eq i32 %15, 2
br i1 %16, label %17, label %21
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
store i8* %20, i8** %7
br label %21
21: ; preds = %17, %2
%22 = load i8*, i8** %7
%23 = call i32* @opendir(i8* %22)
store i32* %23, i32** %6
%24 = load i32*, i32** %6
%25 = icmp ne i32* %24, null
br i1 %25, label %30, label %26
26: ; preds = %21
%27 = load i32, i32* @stderr
%28 = load i8*, i8** %7
%29 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %28)
store i32 0, i32* %3
br label %97
30: ; preds = %21
br label %31
31: ; preds = %95, %30
%32 = load i32*, i32** %6
%33 = call %struct.dirent* @readdir(i32* %32)
store %struct.dirent* %33, %struct.dirent** %9
%34 = icmp ne %struct.dirent* %33, null
br i1 %34, label %35, label %96
35: ; preds = %31
%36 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%37 = load i8*, i8** %7
%38 = call i32 @strncpy(i8* %36, i8* %37, i32 256)
%39 = load i8*, i8** %7
%40 = call i32 @strlen(i8* %39)
store i32 %40, i32* %12
%41 = load i32, i32* %12
%42 = sub nsw i32 %41, 1
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 %43
%45 = load i8, i8* %44
%46 = sext i8 %45 to i32
%47 = icmp ne i32 %46, 47
br i1 %47, label %48, label %54
48: ; preds = %35
%49 = load i32, i32* %12
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 %50
store i8 47, i8* %51
%52 = load i32, i32* %12
%53 = add nsw i32 %52, 1
store i32 %53, i32* %12
br label %54
54: ; preds = %48, %35
%55 = load i32, i32* %12
%56 = sext i32 %55 to i64
%57 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 %56
%58 = load %struct.dirent*, %struct.dirent** %9
%59 = getelementptr inbounds %struct.dirent, %struct.dirent* %58, i32 0, i32 0
%60 = load i8*, i8** %59
%61 = load i32, i32* %12
%62 = sub nsw i32 256, %61
%63 = call i32 @strncpy(i8* %57, i8* %60, i32 %62)
%64 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%65 = call i32 @lstat(i8* %64, %struct.stat* %14)
store i32 %65, i32* %13
%66 = load i32, i32* %13
%67 = icmp slt i32 %66, 0
br i1 %67, label %68, label %73
68: ; preds = %54
%69 = load %struct.dirent*, %struct.dirent** %9
%70 = getelementptr inbounds %struct.dirent, %struct.dirent* %69, i32 0, i32 0
%71 = load i8*, i8** %70
%72 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %71)
br label %95
73: ; preds = %54
%74 = getelementptr inbounds %struct.stat, %struct.stat* %14, i32 0, i32 1
%75 = load i32, i32* %74
%76 = call i64 @gettype(i32 %75)
%77 = trunc i64 %76 to i8
%78 = sext i8 %77 to i32
%79 = getelementptr inbounds %struct.stat, %struct.stat* %14, i32 0, i32 1
%80 = load i32, i32* %79
%81 = getelementptr inbounds [10 x i8], [10 x i8]* %10, i64 0, i64 0
%82 = call i32 @getperms(i32 %80, i8* %81)
%83 = getelementptr inbounds %struct.stat, %struct.stat* %14, i32 0, i32 0
%84 = load i32, i32* %83
%85 = getelementptr inbounds %struct.stat, %struct.stat* %14, i32 0, i32 3
%86 = load i32, i32* %85
%87 = getelementptr inbounds %struct.stat, %struct.stat* %14, i32 0, i32 2
%88 = load i32, i32* %87
%89 = load %struct.dirent*, %struct.dirent** %9
%90 = getelementptr inbounds %struct.dirent, %struct.dirent* %89, i32 0, i32 0
%91 = load i8*, i8** %90
%92 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i32 %78, i32 %82, i32 %84, i32 %86, i32 %88, i8* %91)
%93 = load i32, i32* @stdout
%94 = call i32 @fflush(i32 %93)
br label %95
95: ; preds = %73, %68
br label %31
96: ; preds = %31
store i32 0, i32* %3
br label %97
97: ; preds = %96, %26
%98 = load i32, i32* %3
ret i32 %98
}

LLVM IR (-O2):
%struct.stat = type { i32, i32, i32, i32 }
%struct.dirent = type { i8* }
@.str = [2 x i8] c"/\00"
@stderr = global i32 0
@.str.1 = [20 x i8] c"\0Acould not open %s\0A\00"
@.str.2 = [23 x i8] c"failed to to stat:%s:\0A\00"
@.str.3 = [24 x i8] c"%c%s %8d %8d %16lld %s\0A\00"
@stdout = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [10 x i8]
%4 = alloca [256 x i8]
%5 = alloca %struct.stat
%6 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 0
%7 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%8 = icmp eq i32 %0, 2
br i1 %8, label %9, label %12
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
br label %12
12: ; preds = %9, %2
%13 = phi i8* [ %11, %9 ], [ getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), %2 ]
%14 = tail call i32* @opendir(i8* %13)
%15 = icmp eq i32* %14, null
br i1 %15, label %25, label %16
16: ; preds = %12
%17 = call %struct.dirent* @readdir(i32* %14)
%18 = icmp eq %struct.dirent* %17, null
br i1 %18, label %71, label %19
19: ; preds = %16
%20 = bitcast %struct.stat* %5 to i8*
%21 = getelementptr inbounds %struct.stat, %struct.stat* %5, i64 0, i32 1
%22 = getelementptr inbounds %struct.stat, %struct.stat* %5, i64 0, i32 0
%23 = getelementptr inbounds %struct.stat, %struct.stat* %5, i64 0, i32 3
%24 = getelementptr inbounds %struct.stat, %struct.stat* %5, i64 0, i32 2
br label %28
25: ; preds = %12
%26 = load i32, i32* @stderr
%27 = tail call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %13)
br label %71
28: ; preds = %19, %68
%29 = phi %struct.dirent* [ %17, %19 ], [ %69, %68 ]
%30 = call i32 @strncpy(i8* %7, i8* %13, i32 256)
%31 = call i32 @strlen(i8* %13)
%32 = add nsw i32 %31, -1
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 %33
%35 = load i8, i8* %34
%36 = icmp eq i8 %35, 47
br i1 %36, label %41, label %37
37: ; preds = %28
%38 = sext i32 %31 to i64
%39 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 %38
store i8 47, i8* %39
%40 = add nsw i32 %31, 1
br label %41
41: ; preds = %37, %28
%42 = phi i32 [ %40, %37 ], [ %31, %28 ]
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 %43
%45 = getelementptr inbounds %struct.dirent, %struct.dirent* %29, i64 0, i32 0
%46 = load i8*, i8** %45
%47 = sub nsw i32 256, %42
%48 = call i32 @strncpy(i8* %44, i8* %46, i32 %47)
%49 = call i32 @lstat(i8* %7, %struct.stat* %5)
%50 = icmp slt i32 %49, 0
br i1 %50, label %51, label %54
51: ; preds = %41
%52 = load i8*, i8** %45
%53 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %52)
br label %68
54: ; preds = %41
%55 = load i32, i32* %21
%56 = call i64 @gettype(i32 %55)
%57 = trunc i64 %56 to i32
%58 = shl i32 %57, 24
%59 = ashr exact i32 %58, 24
%60 = call i32 @getperms(i32 %55, i8* %6)
%61 = load i32, i32* %22
%62 = load i32, i32* %23
%63 = load i32, i32* %24
%64 = load i8*, i8** %45
%65 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i32 %59, i32 %60, i32 %61, i32 %62, i32 %63, i8* %64)
%66 = load i32, i32* @stdout
%67 = call i32 @fflush(i32 %66)
br label %68
68: ; preds = %54, %51
%69 = call %struct.dirent* @readdir(i32* %14)
%70 = icmp eq %struct.dirent* %69, null
br i1 %70, label %71, label %28
71: ; preds = %68, %16, %25
ret i32 0
}