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

/* Variables and functions */
scalar_t__ S_ISDIR(int /*<<< orphan*/);
scalar_t__ S_ISREG(int /*<<< orphan*/);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, ...);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ printf(char *, ...);
struct dirent *readdir(int /*<<< orphan*/ *);
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int main(int argc, char **argv) {
  DIR *dp;
  struct dirent *dirp;
  struct stat sb;
  if (argc != 2) {
    fprintf(stderr, "usage: %s dir_name\n", argv[0]);
    exit(1);
  }
  if ((dp = opendir(argv[1])) == NULL) {
    fprintf(stderr, "can't open '%s'\n", argv[1]);
    exit(1);
  }
  while ((dirp = readdir(dp)) != NULL) {
    if (stat(dirp->d_name, &sb) < 0) {
      fprintf(stderr, "Can't stat %s: %s\n", dirp->d_name, strerror(errno));
    }
    printf("%s\t", dirp->d_name);
    if (S_ISREG(sb.st_mode))
      printf("regular file\n");
    else if (S_ISDIR(sb.st_mode))
      printf("directory\n");
    else
      printf("unknown\n");
  }
  closedir(dp);
  return (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)
cmpl   $0x2,-0x8(%rbp)
je     11e8 <main+0x48>
mov    0x2e19(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe29(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    122c <main+0x8c>
mov    0x2dd6(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xdf9(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
jmp    1231 <main+0x91>
mov    -0x18(%rbp),%rdi
call   10a0 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     1316 <main+0x176>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
lea    -0x28(%rbp),%rsi
call   1080 <stat@plt>
cmp    $0x0,%rax
jge    129f <main+0xff>
mov    0x2d77(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
mov    0x2d58(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rdx
mov    %rax,%rcx
lea    0xd8d(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%rsi
lea    0xd8b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x28(%rbp),%edi
call   1090 <S_ISREG@plt>
cmp    $0x0,%rax
je     12d9 <main+0x139>
lea    0xd6f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1311 <main+0x171>
mov    -0x28(%rbp),%edi
call   1050 <S_ISDIR@plt>
cmp    $0x0,%rax
je     12fe <main+0x15e>
lea    0xd58(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    130c <main+0x16c>
lea    0xd50(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1311 <main+0x171>
jmp    1231 <main+0x91>
mov    -0x18(%rbp),%rdi
call   1060 <closedir@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbx
cmp    $0x2,%edi
jne    12de <main+0x12e>
mov    0x8(%rbx),%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     12f3 <main+0x143>
mov    %rax,%r13
mov    %rax,%rdi
call   10b0 <readdir@plt>
test   %rax,%rax
je     12c5 <main+0x115>
mov    %rax,%rbx
lea    0x8(%rsp),%r15
lea    0xe39(%rip),%r12
lea    0xe48(%rip),%r14
mov    %r13,0x10(%rsp)
jmp    1229 <main+0x79>
nopl   (%rax)
call   1030 <puts@plt>
mov    %r13,%rdi
call   10b0 <readdir@plt>
mov    %rax,%rbx
test   %rax,%rax
je     12c5 <main+0x115>
mov    (%rbx),%rdi
mov    %r15,%rsi
call   1090 <stat@plt>
test   %rax,%rax
jns    1280 <main+0xd0>
mov    0x2da0(%rip),%rax
mov    (%rax),%ebp
mov    %r14,%r13
mov    %r12,%r14
mov    %r15,%r12
mov    (%rbx),%r15
mov    0x2d83(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
mov    %ebp,%edi
lea    0xdc0(%rip),%rsi
mov    %r15,%rdx
mov    %r12,%r15
mov    %r14,%r12
mov    %r13,%r14
mov    0x10(%rsp),%r13
mov    %rax,%rcx
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    (%rbx),%rsi
mov    %r12,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x8(%rsp),%edi
call   10a0 <S_ISREG@plt>
mov    %r14,%rdi
test   %rax,%rax
jne    1210 <main+0x60>
mov    0x8(%rsp),%edi
call   1060 <S_ISDIR@plt>
test   %rax,%rax
lea    0xd8f(%rip),%rdi
lea    0xd80(%rip),%rax
cmove  %rax,%rdi
jmp    1210 <main+0x60>
mov    %r13,%rdi
call   1070 <closedir@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2cfb(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xd0f(%rip),%rsi
jmp    1307 <main+0x157>
mov    0x2ce6(%rip),%rax
mov    (%rax),%edi
mov    0x8(%rbx),%rdx
lea    0xd0d(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
%struct.dirent = type { i8* }
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [20 x i8] c"usage: %s dir_name\0A\00"
@.str.1 = [17 x i8] c"can't open '%s'\0A\00"
@.str.2 = [19 x i8] c"Can't stat %s: %s\0A\00"
@errno = global i32 0
@.str.3 = [4 x i8] c"%s\09\00"
@.str.4 = [14 x i8] c"regular file\0A\00"
@.str.5 = [11 x i8] c"directory\0A\00"
@.str.6 = [9 x i8] c"unknown\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 %struct.stat
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp ne i32 %9, 2
br i1 %10, label %11, label %18
11: ; preds = %2
%12 = load i32, i32* @stderr
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 0
%15 = load i8*, i8** %14
%16 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %15)
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %2
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 1
%21 = load i8*, i8** %20
%22 = call i32* @opendir(i8* %21)
store i32* %22, i32** %6
%23 = icmp eq i32* %22, null
br i1 %23, label %24, label %31
24: ; preds = %18
%25 = load i32, i32* @stderr
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 1
%28 = load i8*, i8** %27
%29 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %28)
%30 = call i32 @exit(i32 1)
unreachable
31: ; preds = %18
br label %32
32: ; preds = %71, %31
%33 = load i32*, i32** %6
%34 = call %struct.dirent* @readdir(i32* %33)
store %struct.dirent* %34, %struct.dirent** %7
%35 = icmp ne %struct.dirent* %34, null
br i1 %35, label %36, label %72
36: ; preds = %32
%37 = load %struct.dirent*, %struct.dirent** %7
%38 = getelementptr inbounds %struct.dirent, %struct.dirent* %37, i32 0, i32 0
%39 = load i8*, i8** %38
%40 = call i64 @stat(i8* %39, %struct.stat* %8)
%41 = icmp slt i64 %40, 0
br i1 %41, label %42, label %50
42: ; preds = %36
%43 = load i32, i32* @stderr
%44 = load %struct.dirent*, %struct.dirent** %7
%45 = getelementptr inbounds %struct.dirent, %struct.dirent* %44, i32 0, i32 0
%46 = load i8*, i8** %45
%47 = load i32, i32* @errno
%48 = call i8* @strerror(i32 %47)
%49 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %43, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %46, i8* %48)
br label %50
50: ; preds = %42, %36
%51 = load %struct.dirent*, %struct.dirent** %7
%52 = getelementptr inbounds %struct.dirent, %struct.dirent* %51, i32 0, i32 0
%53 = load i8*, i8** %52
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %53)
%55 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%56 = load i32, i32* %55
%57 = call i64 @S_ISREG(i32 %56)
%58 = icmp ne i64 %57, 0
br i1 %58, label %59, label %61
59: ; preds = %50
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
br label %71
61: ; preds = %50
%62 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%63 = load i32, i32* %62
%64 = call i64 @S_ISDIR(i32 %63)
%65 = icmp ne i64 %64, 0
br i1 %65, label %66, label %68
66: ; preds = %61
%67 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0))
br label %70
68: ; preds = %61
%69 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0))
br label %70
70: ; preds = %68, %66
br label %71
71: ; preds = %70, %59
br label %32
72: ; preds = %32
%73 = load i32*, i32** %6
%74 = call i32 @closedir(i32* %73)
ret i32 0
}

LLVM IR (-O2):
%struct.stat = type { i32 }
%struct.dirent = type { i8* }
@stderr = global i32 0
@.str = [20 x i8] c"usage: %s dir_name\0A\00"
@.str.1 = [17 x i8] c"can't open '%s'\0A\00"
@.str.2 = [19 x i8] c"Can't stat %s: %s\0A\00"
@errno = global i32 0
@.str.3 = [4 x i8] c"%s\09\00"
@str = [8 x i8] c"unknown\00"
@str.7 = [10 x i8] c"directory\00"
@str.8 = [13 x i8] c"regular file\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = icmp eq i32 %0, 2
br i1 %5, label %11, label %6
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = load i8*, i8** %1
%9 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %8)
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %2
%12 = getelementptr inbounds i8*, i8** %1, i64 1
%13 = load i8*, i8** %12
%14 = tail call i32* @opendir(i8* %13)
%15 = icmp eq i32* %14, null
br i1 %15, label %21, label %16
16: ; preds = %11
%17 = call %struct.dirent* @readdir(i32* %14)
%18 = icmp eq %struct.dirent* %17, null
br i1 %18, label %54, label %19
19: ; preds = %16
%20 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
br label %26
21: ; preds = %11
%22 = load i32, i32* @stderr
%23 = load i8*, i8** %12
%24 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %23)
%25 = tail call i32 @exit(i32 1)
unreachable
26: ; preds = %19, %49
%27 = phi %struct.dirent* [ %17, %19 ], [ %52, %49 ]
%28 = getelementptr inbounds %struct.dirent, %struct.dirent* %27, i64 0, i32 0
%29 = load i8*, i8** %28
%30 = call i64 @stat(i8* %29, %struct.stat* %3)
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %38
32: ; preds = %26
%33 = load i32, i32* @stderr
%34 = load i8*, i8** %28
%35 = load i32, i32* @errno
%36 = call i8* @strerror(i32 %35)
%37 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %34, i8* %36)
br label %38
38: ; preds = %32, %26
%39 = load i8*, i8** %28
%40 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %39)
%41 = load i32, i32* %20
%42 = call i64 @S_ISREG(i32 %41)
%43 = icmp eq i64 %42, 0
br i1 %43, label %44, label %49
44: ; preds = %38
%45 = load i32, i32* %20
%46 = call i64 @S_ISDIR(i32 %45)
%47 = icmp eq i64 %46, 0
%48 = select i1 %47, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @str, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @str.7, i64 0, i64 0)
br label %49
49: ; preds = %44, %38
%50 = phi i8* [ getelementptr inbounds ([13 x i8], [13 x i8]* @str.8, i64 0, i64 0), %38 ], [ %48, %44 ]
%51 = call i32 @puts(i8* dereferenceable(1) %50)
%52 = call %struct.dirent* @readdir(i32* %14)
%53 = icmp eq %struct.dirent* %52, null
br i1 %53, label %54, label %26
54: ; preds = %49, %16
%55 = call i32 @closedir(i32* %14)
ret i32 0
}