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

/* Variables and functions */
scalar_t__ DT_DIR;
int errno;
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ printf(char *, char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int main(int argc, char **argv) {
  char *name;
  if (argc == 2) {
    name = argv[1];
  } else {
    name = ".";
  }
  DIR *dir = opendir(name);
  if (!dir) {
    return errno;
  }
  while (1) {
    struct dirent *entry = readdir(dir);
    if (!entry) {
      return errno;
    }
    if (entry->d_type == DT_DIR) {
      printf("\e[32m%s\e[0m\n", entry->d_name);
    } else {
      printf("%s\n", entry->d_name);
    }
  }
}


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)
jne    1161 <main+0x31>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
jmp    116c <main+0x3c>
lea    0xe98(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1195 <main+0x65>
mov    0x2e4d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    120c <main+0xdc>
jmp    119a <main+0x6a>
mov    -0x20(%rbp),%rdi
call   1050 <readdir@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11c3 <main+0x93>
mov    0x2e1f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    120c <main+0xdc>
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
mov    0x2e0f(%rip),%rcx
cmp    (%rcx),%rax
jne    11f3 <main+0xc3>
mov    -0x28(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe19(%rip),%rdi
call   1040 <printf@plt>
jmp    1207 <main+0xd7>
mov    -0x28(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe0d(%rip),%rdi
call   1040 <printf@plt>
jmp    119a <main+0x6a>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
cmp    $0x2,%edi
jne    1143 <main+0x13>
mov    0x8(%rsi),%rdi
jmp    114a <main+0x1a>
lea    0xeb6(%rip),%rdi
call   1030 <opendir@plt>
test   %rax,%rax
je     11a3 <main+0x73>
mov    %rax,%rbx
mov    %rax,%rdi
call   1050 <readdir@plt>
test   %rax,%rax
je     11a3 <main+0x73>
mov    0x2e75(%rip),%r14
lea    0xe90(%rip),%r15
lea    0xe96(%rip),%r12
nopl   0x0(%rax)
mov    (%rax),%rcx
mov    0x8(%rax),%rsi
cmp    (%r14),%rcx
mov    %r12,%rdi
cmove  %r15,%rdi
call   1040 <printf@plt>
mov    %rbx,%rdi
call   1050 <readdir@plt>
test   %rax,%rax
jne    1180 <main+0x50>
mov    0x2e2e(%rip),%rax
mov    (%rax),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.dirent = type { i64, i8* }
@.str = [2 x i8] c".\00"
@errno = global i32 0
@DT_DIR = global i64 0
@.str.1 = [13 x i8] c"\1B[32m%s\1B[0m\0A\00"
@.str.2 = [4 x i8] c"%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*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp eq i32 %9, 2
br i1 %10, label %11, label %15
11: ; preds = %2
%12 = load i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i64 1
%14 = load i8*, i8** %13
store i8* %14, i8** %6
br label %16
15: ; preds = %2
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %6
br label %16
16: ; preds = %15, %11
%17 = load i8*, i8** %6
%18 = call i32* @opendir(i8* %17)
store i32* %18, i32** %7
%19 = load i32*, i32** %7
%20 = icmp ne i32* %19, null
br i1 %20, label %23, label %21
21: ; preds = %16
%22 = load i32, i32* @errno
store i32 %22, i32* %3
br label %48
23: ; preds = %16
br label %24
24: ; preds = %23, %47
%25 = load i32*, i32** %7
%26 = call %struct.dirent* @readdir(i32* %25)
store %struct.dirent* %26, %struct.dirent** %8
%27 = load %struct.dirent*, %struct.dirent** %8
%28 = icmp ne %struct.dirent* %27, null
br i1 %28, label %31, label %29
29: ; preds = %24
%30 = load i32, i32* @errno
store i32 %30, i32* %3
br label %48
31: ; preds = %24
%32 = load %struct.dirent*, %struct.dirent** %8
%33 = getelementptr inbounds %struct.dirent, %struct.dirent* %32, i32 0, i32 0
%34 = load i64, i64* %33
%35 = load i64, i64* @DT_DIR
%36 = icmp eq i64 %34, %35
br i1 %36, label %37, label %42
37: ; preds = %31
%38 = load %struct.dirent*, %struct.dirent** %8
%39 = getelementptr inbounds %struct.dirent, %struct.dirent* %38, i32 0, i32 1
%40 = load i8*, i8** %39
%41 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* %40)
br label %47
42: ; preds = %31
%43 = load %struct.dirent*, %struct.dirent** %8
%44 = getelementptr inbounds %struct.dirent, %struct.dirent* %43, i32 0, i32 1
%45 = load i8*, i8** %44
%46 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %45)
br label %47
47: ; preds = %42, %37
br label %24
48: ; preds = %29, %21
%49 = load i32, i32* %3
ret i32 %49
}

LLVM IR (-O2):
%struct.dirent = type { i64, i8* }
@.str = [2 x i8] c".\00"
@errno = global i32 0
@DT_DIR = global i64 0
@.str.1 = [13 x i8] c"\1B[32m%s\1B[0m\0A\00"
@.str.2 = [4 x i8] c"%s\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 2
br i1 %3, label %4, label %7
4: ; preds = %2
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
br label %7
7: ; preds = %2, %4
%8 = phi i8* [ %6, %4 ], [ getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), %2 ]
%9 = tail call i32* @opendir(i8* %8)
%10 = icmp eq i32* %9, null
br i1 %10, label %26, label %11
11: ; preds = %7
%12 = tail call %struct.dirent* @readdir(i32* %9)
%13 = icmp eq %struct.dirent* %12, null
br i1 %13, label %26, label %14
14: ; preds = %11, %14
%15 = phi %struct.dirent* [ %24, %14 ], [ %12, %11 ]
%16 = getelementptr inbounds %struct.dirent, %struct.dirent* %15, i64 0, i32 0
%17 = load i64, i64* %16
%18 = load i64, i64* @DT_DIR
%19 = icmp eq i64 %17, %18
%20 = getelementptr inbounds %struct.dirent, %struct.dirent* %15, i64 0, i32 1
%21 = load i8*, i8** %20
%22 = select i1 %19, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0)
%23 = tail call i32 @printf(i8* %22, i8* %21)
%24 = tail call %struct.dirent* @readdir(i32* %9)
%25 = icmp eq %struct.dirent* %24, null
br i1 %25, label %26, label %14
26: ; preds = %14, %11, %7
%27 = load i32, i32* @errno
ret i32 %27
}