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

/* Variables and functions */
scalar_t__ chdir(char *);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
struct dirent *readdir(int /*<<< orphan*/ *);
scalar_t__ stat(char *, struct stat *);
int main(int argc, char *argv[]) {
  DIR *dir;
  struct dirent *direntry;
  char *dirname = ".";
  struct stat info;
  if (argc > 1)
    dirname = argv[1];
  printf("\n **** DISPLAYING DIRECTORY %s ****\n\n", dirname);
  if (!(dir = opendir(dirname))) {
    perror(dirname);
    exit(errno);
  };
  if (chdir(dirname)) {
    perror(dirname);
    exit(errno);
  };
  printf("   MODE     UID    GID           SIZE\n\n");
  while ((direntry = readdir(dir))) {
    if (stat(direntry->d_name, &info)) {
      perror(" problems fstating the file:");
      exit(errno);
    };
    printf("%8o   %4d   %4d   %12d  %s\n", info.st_mode, info.st_uid,
           info.st_gid, (int)info.st_size, direntry->d_name);
  }
  closedir(dir);
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe63(%rip),%rax
mov    %rax,-0x28(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    11b7 <main+0x37>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rsi
lea    0xe40(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x28(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11f7 <main+0x77>
mov    -0x28(%rbp),%rdi
call   1090 <perror@plt>
mov    0x2df0(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x28(%rbp),%rdi
call   1040 <chdir@plt>
cmp    $0x0,%rax
je     1221 <main+0xa1>
mov    -0x28(%rbp),%rdi
call   1090 <perror@plt>
mov    0x2dc6(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
lea    0xe00(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     12a4 <main+0x124>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
lea    -0x40(%rbp),%rsi
call   1070 <stat@plt>
cmp    $0x0,%rax
je     127a <main+0xfa>
lea    0xde9(%rip),%rdi
call   1090 <perror@plt>
mov    0x2d6d(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x40(%rbp),%esi
mov    -0x3c(%rbp),%edx
mov    -0x38(%rbp),%ecx
mov    -0x30(%rbp),%rax
mov    %eax,%r8d
mov    -0x20(%rbp),%rax
mov    (%rax),%r9
lea    0xdd5(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    122f <main+0xaf>
mov    -0x18(%rbp),%rdi
call   1060 <closedir@plt>
xor    %edi,%edi
call   10a0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
cmp    $0x2,%edi
jl     11a6 <main+0x16>
mov    0x8(%rsi),%r14
jmp    11ad <main+0x1d>
lea    0xe53(%rip),%r14
lea    0xe4e(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r14,%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     125c <main+0xcc>
mov    %rax,%r12
mov    %r14,%rdi
call   1050 <chdir@plt>
test   %rax,%rax
jne    125c <main+0xcc>
lea    0xe77(%rip),%rdi
call   1030 <puts@plt>
mov    %r12,%rdi
call   1090 <readdir@plt>
test   %rax,%rax
je     124d <main+0xbd>
mov    %rax,%rbx
mov    %rsp,%r14
lea    0xe3c(%rip),%r15
nopl   0x0(%rax)
mov    (%rbx),%rdi
mov    %r14,%rsi
call   1080 <stat@plt>
test   %rax,%rax
jne    1272 <main+0xe2>
mov    (%rsp),%esi
mov    0x4(%rsp),%edx
mov    0x8(%rsp),%ecx
mov    0x10(%rsp),%r8d
mov    (%rbx),%r9
mov    %r15,%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r12,%rdi
call   1090 <readdir@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1210 <main+0x80>
mov    %r12,%rdi
call   1070 <closedir@plt>
xor    %edi,%edi
call   10b0 <exit@plt>
mov    %r14,%rdi
call   10a0 <perror@plt>
mov    0x2d75(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
lea    0xdaf(%rip),%rdi
jmp    125f <main+0xcf>

LLVM IR (-O0):
%struct.dirent = type { i8* }
%struct.stat = type { i32, i32, i32, i64 }
@.str = [2 x i8] c".\00"
@.str.1 = [38 x i8] c"\0A **** DISPLAYING DIRECTORY %s ****\0A\0A\00"
@errno = global i32 0
@.str.2 = [40 x i8] c" MODE UID GID SIZE\0A\0A\00"
@.str.3 = [29 x i8] c" problems fstating the file:\00"
@.str.4 = [28 x i8] c"%8o %4d %4d %12d %s\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*
%9 = 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** %8
%10 = load i32, i32* %4
%11 = icmp sgt i32 %10, 1
br i1 %11, label %12, label %16
12: ; preds = %2
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 1
%15 = load i8*, i8** %14
store i8* %15, i8** %8
br label %16
16: ; preds = %12, %2
%17 = load i8*, i8** %8
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i8* %17)
%19 = load i8*, i8** %8
%20 = call i32* @opendir(i8* %19)
store i32* %20, i32** %6
%21 = icmp ne i32* %20, null
br i1 %21, label %27, label %22
22: ; preds = %16
%23 = load i8*, i8** %8
%24 = call i32 @perror(i8* %23)
%25 = load i32, i32* @errno
%26 = call i32 @exit(i32 %25)
unreachable
27: ; preds = %16
%28 = load i8*, i8** %8
%29 = call i64 @chdir(i8* %28)
%30 = icmp ne i64 %29, 0
br i1 %30, label %31, label %36
31: ; preds = %27
%32 = load i8*, i8** %8
%33 = call i32 @perror(i8* %32)
%34 = load i32, i32* @errno
%35 = call i32 @exit(i32 %34)
unreachable
36: ; preds = %27
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0))
br label %38
38: ; preds = %52, %36
%39 = load i32*, i32** %6
%40 = call %struct.dirent* @readdir(i32* %39)
store %struct.dirent* %40, %struct.dirent** %7
%41 = icmp ne %struct.dirent* %40, null
br i1 %41, label %42, label %66
42: ; preds = %38
%43 = load %struct.dirent*, %struct.dirent** %7
%44 = getelementptr inbounds %struct.dirent, %struct.dirent* %43, i32 0, i32 0
%45 = load i8*, i8** %44
%46 = call i64 @stat(i8* %45, %struct.stat* %9)
%47 = icmp ne i64 %46, 0
br i1 %47, label %48, label %52
48: ; preds = %42
%49 = call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0))
%50 = load i32, i32* @errno
%51 = call i32 @exit(i32 %50)
unreachable
52: ; preds = %42
%53 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%54 = load i32, i32* %53
%55 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 1
%56 = load i32, i32* %55
%57 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 2
%58 = load i32, i32* %57
%59 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 3
%60 = load i64, i64* %59
%61 = trunc i64 %60 to i32
%62 = load %struct.dirent*, %struct.dirent** %7
%63 = getelementptr inbounds %struct.dirent, %struct.dirent* %62, i32 0, i32 0
%64 = load i8*, i8** %63
%65 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0), i32 %54, i32 %56, i32 %58, i32 %61, i8* %64)
br label %38
66: ; preds = %38
%67 = load i32*, i32** %6
%68 = call i32 @closedir(i32* %67)
%69 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
%struct.stat = type { i32, i32, i32, i64 }
%struct.dirent = type { i8* }
@.str = [2 x i8] c".\00"
@.str.1 = [38 x i8] c"\0A **** DISPLAYING DIRECTORY %s ****\0A\0A\00"
@errno = global i32 0
@.str.3 = [29 x i8] c" problems fstating the file:\00"
@.str.4 = [28 x i8] c"%8o %4d %4d %12d %s\0A\00"
@str = [39 x i8] c" MODE UID GID SIZE\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = icmp sgt i32 %0, 1
br i1 %5, label %6, label %9
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
br label %9
9: ; preds = %6, %2
%10 = phi i8* [ %8, %6 ], [ getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), %2 ]
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i8* %10)
%12 = tail call i32* @opendir(i8* %10)
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %18
14: ; preds = %9
%15 = tail call i32 @perror(i8* %10)
%16 = load i32, i32* @errno
%17 = tail call i32 @exit(i32 %16)
unreachable
18: ; preds = %9
%19 = tail call i64 @chdir(i8* %10)
%20 = icmp eq i64 %19, 0
br i1 %20, label %25, label %21
21: ; preds = %18
%22 = tail call i32 @perror(i8* %10)
%23 = load i32, i32* @errno
%24 = tail call i32 @exit(i32 %23)
unreachable
25: ; preds = %18
%26 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @str, i64 0, i64 0))
%27 = call %struct.dirent* @readdir(i32* %12)
%28 = icmp eq %struct.dirent* %27, null
br i1 %28, label %54, label %29
29: ; preds = %25
%30 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%31 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 1
%32 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 2
%33 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 3
br label %34
34: ; preds = %29, %44
%35 = phi %struct.dirent* [ %27, %29 ], [ %52, %44 ]
%36 = getelementptr inbounds %struct.dirent, %struct.dirent* %35, i64 0, i32 0
%37 = load i8*, i8** %36
%38 = call i64 @stat(i8* %37, %struct.stat* %3)
%39 = icmp eq i64 %38, 0
br i1 %39, label %44, label %40
40: ; preds = %34
%41 = call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0))
%42 = load i32, i32* @errno
%43 = call i32 @exit(i32 %42)
unreachable
44: ; preds = %34
%45 = load i32, i32* %30
%46 = load i32, i32* %31
%47 = load i32, i32* %32
%48 = load i64, i64* %33
%49 = trunc i64 %48 to i32
%50 = load i8*, i8** %36
%51 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0), i32 %45, i32 %46, i32 %47, i32 %49, i8* %50)
%52 = call %struct.dirent* @readdir(i32* %12)
%53 = icmp eq %struct.dirent* %52, null
br i1 %53, label %54, label %34
54: ; preds = %44, %25
%55 = call i32 @closedir(i32* %12)
%56 = call i32 @exit(i32 0)
unreachable
}