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

/* Variables and functions */
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ *opendir(char const *);
int /*<<< orphan*/ perror(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int countDirEnts(char *path, int all) {
  DIR *dp = NULL;
  struct dirent *ent;
  int ents = 0;
  dp = opendir((const char *)path);
  if (dp != NULL) {
    ent = readdir(dp);
    while (ent != NULL) {
      if ((!all && ent->d_name[0] != '.') || all) {
        ents++;
      }
      ent = readdir(dp);
    }
    closedir(dp);
    return ents;
  } else {
    fprintf(stderr, "-feshell: ls: %s: ", path);
    perror("");
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<countDirEnts>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movq   $0x0,-0x20(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11f4 <countDirEnts+0xa4>
mov    -0x20(%rbp),%rdi
call   1060 <readdir@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     11e0 <countDirEnts+0x90>
cmpl   $0x0,-0x14(%rbp)
jne    11bb <countDirEnts+0x6b>
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
jne    11c5 <countDirEnts+0x75>
cmpl   $0x0,-0x14(%rbp)
je     11ce <countDirEnts+0x7e>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x20(%rbp),%rdi
call   1060 <readdir@plt>
mov    %rax,-0x28(%rbp)
jmp    1193 <countDirEnts+0x43>
mov    -0x20(%rbp),%rdi
call   1040 <closedir@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1220 <countDirEnts+0xd0>
mov    0x2de5(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xdf8(%rip),%rsi
call   1050 <fprintf@plt>
lea    0xdfe(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<countDirEnts>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%r14d
mov    %rdi,%rbp
call   1030 <opendir@plt>
test   %rax,%rax
je     11ae <countDirEnts+0x5e>
mov    %rax,%rbx
mov    %rax,%rdi
call   1060 <readdir@plt>
xor    %ebp,%ebp
jmp    118b <countDirEnts+0x3b>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
add    $0x1,%ebp
mov    %rbx,%rdi
call   1060 <readdir@plt>
test   %rax,%rax
je     119f <countDirEnts+0x4f>
test   %r14d,%r14d
jne    1180 <countDirEnts+0x30>
mov    (%rax),%rax
cmpb   $0x2e,(%rax)
jne    1180 <countDirEnts+0x30>
jmp    1183 <countDirEnts+0x33>
mov    %rbx,%rdi
call   1040 <closedir@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2e2b(%rip),%rax
mov    (%rax),%edi
lea    0xe42(%rip),%rsi
mov    %rbp,%rdx
call   1050 <fprintf@plt>
lea    0xe45(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%ebp
jmp    11a7 <countDirEnts+0x57>

LLVM IR (-O0):
%struct.dirent = type { i8* }
@stderr = global i32 0
@.str = [19 x i8] c"-feshell: ls: %s: \00"
@.str.1 = [1 x i8] zeroinitializer
define i32 @countDirEnts(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32*
%7 = alloca %struct.dirent*
%8 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32* null, i32** %6
store i32 0, i32* %8
%9 = load i8*, i8** %4
%10 = call i32* @opendir(i8* %9)
store i32* %10, i32** %6
%11 = load i32*, i32** %6
%12 = icmp ne i32* %11, null
br i1 %12, label %13, label %43
13: ; preds = %2
%14 = load i32*, i32** %6
%15 = call %struct.dirent* @readdir(i32* %14)
store %struct.dirent* %15, %struct.dirent** %7
br label %16
16: ; preds = %36, %13
%17 = load %struct.dirent*, %struct.dirent** %7
%18 = icmp ne %struct.dirent* %17, null
br i1 %18, label %19, label %39
19: ; preds = %16
%20 = load i32, i32* %5
%21 = icmp ne i32 %20, 0
br i1 %21, label %30, label %22
22: ; preds = %19
%23 = load %struct.dirent*, %struct.dirent** %7
%24 = getelementptr inbounds %struct.dirent, %struct.dirent* %23, i32 0, i32 0
%25 = load i8*, i8** %24
%26 = getelementptr inbounds i8, i8* %25, i64 0
%27 = load i8, i8* %26
%28 = sext i8 %27 to i32
%29 = icmp ne i32 %28, 46
br i1 %29, label %33, label %30
30: ; preds = %22, %19
%31 = load i32, i32* %5
%32 = icmp ne i32 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %30, %22
%34 = load i32, i32* %8
%35 = add nsw i32 %34, 1
store i32 %35, i32* %8
br label %36
36: ; preds = %33, %30
%37 = load i32*, i32** %6
%38 = call %struct.dirent* @readdir(i32* %37)
store %struct.dirent* %38, %struct.dirent** %7
br label %16
39: ; preds = %16
%40 = load i32*, i32** %6
%41 = call i32 @closedir(i32* %40)
%42 = load i32, i32* %8
store i32 %42, i32* %3
br label %48
43: ; preds = %2
%44 = load i32, i32* @stderr
%45 = load i8*, i8** %4
%46 = call i32 @fprintf(i32 %44, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %45)
%47 = call i32 @perror(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %48
48: ; preds = %43, %39
%49 = load i32, i32* %3
ret i32 %49
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@stderr = global i32 0
@.str = [19 x i8] c"-feshell: ls: %s: \00"
@.str.1 = [1 x i8] zeroinitializer
define i32 @countDirEnts(i8* %0, i32 %1) {
%3 = tail call i32* @opendir(i8* %0)
%4 = icmp eq i32* %3, null
br i1 %4, label %27, label %5
5: ; preds = %2
%6 = tail call %struct.dirent* @readdir(i32* %3)
%7 = icmp eq %struct.dirent* %6, null
br i1 %7, label %24, label %8
8: ; preds = %5
%9 = icmp eq i32 %1, 0
br label %10
10: ; preds = %8, %20
%11 = phi i32 [ 0, %8 ], [ %21, %20 ]
%12 = phi %struct.dirent* [ %6, %8 ], [ %22, %20 ]
br i1 %9, label %13, label %18
13: ; preds = %10
%14 = getelementptr inbounds %struct.dirent, %struct.dirent* %12, i64 0, i32 0
%15 = load i8*, i8** %14
%16 = load i8, i8* %15
%17 = icmp eq i8 %16, 46
br i1 %17, label %20, label %18
18: ; preds = %10, %13
%19 = add nsw i32 %11, 1
br label %20
20: ; preds = %13, %18
%21 = phi i32 [ %19, %18 ], [ %11, %13 ]
%22 = tail call %struct.dirent* @readdir(i32* %3)
%23 = icmp eq %struct.dirent* %22, null
br i1 %23, label %24, label %10
24: ; preds = %20, %5
%25 = phi i32 [ 0, %5 ], [ %21, %20 ]
%26 = tail call i32 @closedir(i32* %3)
br label %31
27: ; preds = %2
%28 = load i32, i32* @stderr
%29 = tail call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %0)
%30 = tail call i32 @perror(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0))
br label %31
31: ; preds = %27, %24
%32 = phi i32 [ %25, %24 ], [ -1, %27 ]
ret i32 %32
}