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

/* Variables and functions */
scalar_t__ S_ISDIR(scalar_t__);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, long long, int /*<<< orphan*/, char *);
int readdir_r(int /*<<< orphan*/ *, struct dirent *, struct dirent **);
int stat(int /*<<< orphan*/, struct stat *);
int main(int argc, char **argv) {
  char *d_to_open = ".";
  DIR *dstream = opendir(d_to_open);
  if (dstream == NULL) {
    perror("opening directory failed");
    exit(2);
  }
  struct dirent d;
  struct dirent *result;
  for (;;) {
    int ret = readdir_r(dstream, &d, &result);
    if (ret != 0) {
      perror("readdir_r error");
      exit(3);
    }
    if (!result) {
      break;
    }
    struct stat mystat;
    if ((stat(d.d_name, &mystat) == -1)) {
      perror("stat");
      exit(4);
    }
    char *dir = "";
    if (S_ISDIR(mystat.st_mode))
      dir = "(Directory)";
    printf("mode: %llo, name: %s %s\n", (long long)mystat.st_mode, d.d_name,
           dir);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe73(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11bf <main+0x4f>
lea    0xe52(%rip),%rdi
call   1080 <perror@plt>
mov    $0x2,%edi
call   1090 <exit@plt>
jmp    11c4 <main+0x54>
mov    -0x20(%rbp),%rdi
lea    -0x28(%rbp),%rsi
lea    -0x30(%rbp),%rdx
call   1030 <readdir_r@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
je     11f8 <main+0x88>
lea    0xe32(%rip),%rdi
call   1080 <perror@plt>
mov    $0x3,%edi
call   1090 <exit@plt>
cmpq   $0x0,-0x30(%rbp)
jne    1208 <main+0x98>
jmp    1278 <main+0x108>
mov    -0x28(%rbp),%edi
lea    -0x40(%rbp),%rsi
call   1070 <stat@plt>
cmp    $0xffffffff,%eax
jne    1233 <main+0xc3>
lea    0xe07(%rip),%rdi
call   1080 <perror@plt>
mov    $0x4,%edi
call   1090 <exit@plt>
lea    0xe1a(%rip),%rax
mov    %rax,-0x48(%rbp)
mov    -0x40(%rbp),%rdi
call   1060 <S_ISDIR@plt>
cmp    $0x0,%rax
je     125c <main+0xec>
lea    0xdd8(%rip),%rax
mov    %rax,-0x48(%rbp)
mov    -0x40(%rbp),%rsi
mov    -0x28(%rbp),%edx
mov    -0x48(%rbp),%rcx
lea    0xdce(%rip),%rdi
call   1050 <printf@plt>
jmp    11c4 <main+0x54>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
lea    0xe7b(%rip),%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     125d <main+0xed>
mov    %rax,%rbx
mov    %rsp,%rsi
lea    0x10(%rsp),%rdx
mov    %rax,%rdi
call   1030 <readdir_r@plt>
test   %eax,%eax
jne    1220 <main+0xb0>
lea    0xea3(%rip),%rbp
lea    0xe78(%rip),%r14
lea    0xe7d(%rip),%r15
mov    %rsp,%r12
lea    0x10(%rsp),%r13
nopw   0x0(%rax,%rax,1)
cmpq   $0x0,0x10(%rsp)
je     1236 <main+0xc6>
mov    (%rsp),%edi
lea    0x8(%rsp),%rsi
call   1070 <stat@plt>
cmp    $0xffffffff,%eax
je     1247 <main+0xd7>
mov    0x8(%rsp),%rdi
call   1060 <S_ISDIR@plt>
test   %rax,%rax
mov    %r14,%rcx
cmove  %rbp,%rcx
mov    0x8(%rsp),%rsi
mov    (%rsp),%edx
mov    %r15,%rdi
call   1050 <printf@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %r13,%rdx
call   1030 <readdir_r@plt>
test   %eax,%eax
je     11d0 <main+0x60>
lea    0xdf4(%rip),%rdi
call   1080 <perror@plt>
mov    $0x3,%edi
call   1090 <exit@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xddd(%rip),%rdi
call   1080 <perror@plt>
mov    $0x4,%edi
call   1090 <exit@plt>
lea    0xd9e(%rip),%rdi
call   1080 <perror@plt>
mov    $0x2,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.dirent = type { i32 }
%struct.stat = type { i64 }
@.str = [2 x i8] c".\00"
@.str.1 = [25 x i8] c"opening directory failed\00"
@.str.2 = [16 x i8] c"readdir_r error\00"
@.str.3 = [5 x i8] c"stat\00"
@.str.4 = [1 x i8] zeroinitializer
@.str.5 = [12 x i8] c"(Directory)\00"
@.str.6 = [25 x i8] c"mode: %llo, name: %s %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 %struct.dirent*
%10 = alloca i32
%11 = alloca %struct.stat
%12 = alloca i8*
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** %6
%13 = load i8*, i8** %6
%14 = call i32* @opendir(i8* %13)
store i32* %14, i32** %7
%15 = load i32*, i32** %7
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %20
17: ; preds = %2
%18 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
%19 = call i32 @exit(i32 2)
unreachable
20: ; preds = %2
br label %21
21: ; preds = %47, %20
%22 = load i32*, i32** %7
%23 = call i32 @readdir_r(i32* %22, %struct.dirent* %8, %struct.dirent** %9)
store i32 %23, i32* %10
%24 = load i32, i32* %10
%25 = icmp ne i32 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %21
%27 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
%28 = call i32 @exit(i32 3)
unreachable
29: ; preds = %21
%30 = load %struct.dirent*, %struct.dirent** %9
%31 = icmp ne %struct.dirent* %30, null
br i1 %31, label %33, label %32
32: ; preds = %29
br label %54
33: ; preds = %29
%34 = getelementptr inbounds %struct.dirent, %struct.dirent* %8, i32 0, i32 0
%35 = load i32, i32* %34
%36 = call i32 @stat(i32 %35, %struct.stat* %11)
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %41
38: ; preds = %33
%39 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%40 = call i32 @exit(i32 4)
unreachable
41: ; preds = %33
store i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.4, i64 0, i64 0), i8** %12
%42 = getelementptr inbounds %struct.stat, %struct.stat* %11, i32 0, i32 0
%43 = load i64, i64* %42
%44 = call i64 @S_ISDIR(i64 %43)
%45 = icmp ne i64 %44, 0
br i1 %45, label %46, label %47
46: ; preds = %41
store i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0), i8** %12
br label %47
47: ; preds = %46, %41
%48 = getelementptr inbounds %struct.stat, %struct.stat* %11, i32 0, i32 0
%49 = load i64, i64* %48
%50 = getelementptr inbounds %struct.dirent, %struct.dirent* %8, i32 0, i32 0
%51 = load i32, i32* %50
%52 = load i8*, i8** %12
%53 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.6, i64 0, i64 0), i64 %49, i32 %51, i8* %52)
br label %21
54: ; preds = %32
ret i32 0
}

LLVM IR (-O2):
%struct.dirent = type { i32 }
%struct.stat = type { i64 }
@.str = [2 x i8] c".\00"
@.str.1 = [25 x i8] c"opening directory failed\00"
@.str.2 = [16 x i8] c"readdir_r error\00"
@.str.3 = [5 x i8] c"stat\00"
@.str.4 = [1 x i8] zeroinitializer
@.str.5 = [12 x i8] c"(Directory)\00"
@.str.6 = [25 x i8] c"mode: %llo, name: %s %s\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.dirent
%4 = alloca %struct.dirent*
%5 = alloca %struct.stat
%6 = tail call i32* @opendir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %11
8: ; preds = %2
%9 = tail call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
%10 = tail call i32 @exit(i32 2)
unreachable
11: ; preds = %2
%12 = bitcast %struct.dirent* %3 to i8*
%13 = bitcast %struct.dirent** %4 to i8*
%14 = call i32 @readdir_r(i32* %6, %struct.dirent* %3, %struct.dirent** %4)
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %20
16: ; preds = %11
%17 = bitcast %struct.stat* %5 to i8*
%18 = getelementptr inbounds %struct.dirent, %struct.dirent* %3, i64 0, i32 0
%19 = getelementptr inbounds %struct.stat, %struct.stat* %5, i64 0, i32 0
br label %23
20: ; preds = %33, %11
%21 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
%22 = call i32 @exit(i32 3)
unreachable
23: ; preds = %16, %33
%24 = load %struct.dirent*, %struct.dirent** %4
%25 = icmp eq %struct.dirent* %24, null
br i1 %25, label %43, label %26
26: ; preds = %23
%27 = load i32, i32* %18
%28 = call i32 @stat(i32 %27, %struct.stat* %5)
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %33
30: ; preds = %26
%31 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%32 = call i32 @exit(i32 4)
unreachable
33: ; preds = %26
%34 = load i64, i64* %19
%35 = call i64 @S_ISDIR(i64 %34)
%36 = icmp eq i64 %35, 0
%37 = select i1 %36, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0)
%38 = load i64, i64* %19
%39 = load i32, i32* %18
%40 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.6, i64 0, i64 0), i64 %38, i32 %39, i8* %37)
%41 = call i32 @readdir_r(i32* %6, %struct.dirent* %3, %struct.dirent** %4)
%42 = icmp eq i32 %41, 0
br i1 %42, label %23, label %20
43: ; preds = %23
ret i32 0
}