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

/* Variables and functions */
int /*<<< orphan*/ EINVAL;
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ *opendir(char const *);
struct dirent *readdir(int /*<<< orphan*/ *);
int count_files_in_dir(const char *dir_name) {
  DIR *dir;
  struct dirent *entry;
  int count = 0;
  if (!dir_name) {
    errno = EINVAL;
    return -1;
  }
  dir = opendir(dir_name);
  if (dir == NULL)
    return -1;
  while ((entry = readdir(dir)))
    count++;
  closedir(dir);
  return count;
}


x86 assembly (clang, -O0):
<count_files_in_dir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x24(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    116c <count_files_in_dir+0x3c>
mov    0x2e83(%rip),%rax
mov    (%rax),%ecx
mov    0x2e82(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11c9 <count_files_in_dir+0x99>
mov    -0x10(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1190 <count_files_in_dir+0x60>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c9 <count_files_in_dir+0x99>
jmp    1195 <count_files_in_dir+0x65>
mov    -0x18(%rbp),%rdi
call   1050 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     11ba <count_files_in_dir+0x8a>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1195 <count_files_in_dir+0x65>
mov    -0x18(%rbp),%rdi
call   1040 <closedir@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<count_files_in_dir>:
push   %r14
push   %rbx
push   %rax
test   %rdi,%rdi
je     116a <count_files_in_dir+0x3a>
call   1030 <opendir@plt>
test   %rax,%rax
je     117c <count_files_in_dir+0x4c>
mov    %rax,%r14
mov    $0xffffffff,%ebx
nopl   0x0(%rax,%rax,1)
mov    %r14,%rdi
call   1050 <readdir@plt>
add    $0x1,%ebx
test   %rax,%rax
jne    1150 <count_files_in_dir+0x20>
mov    %r14,%rdi
call   1040 <closedir@plt>
jmp    1181 <count_files_in_dir+0x51>
mov    0x2e67(%rip),%rax
mov    (%rax),%eax
mov    0x2e66(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.dirent = type { i32 }
@EINVAL = global i32 0
@errno = global i32 0
define i32 @count_files_in_dir(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca %struct.dirent*
%6 = alloca i32
store i8* %0, i8** %3
store i32 0, i32* %6
%7 = load i8*, i8** %3
%8 = icmp ne i8* %7, null
br i1 %8, label %11, label %9
9: ; preds = %1
%10 = load i32, i32* @EINVAL
store i32 %10, i32* @errno
store i32 -1, i32* %2
br label %29
11: ; preds = %1
%12 = load i8*, i8** %3
%13 = call i32* @opendir(i8* %12)
store i32* %13, i32** %4
%14 = load i32*, i32** %4
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %17
16: ; preds = %11
store i32 -1, i32* %2
br label %29
17: ; preds = %11
br label %18
18: ; preds = %22, %17
%19 = load i32*, i32** %4
%20 = call %struct.dirent* @readdir(i32* %19)
store %struct.dirent* %20, %struct.dirent** %5
%21 = icmp ne %struct.dirent* %20, null
br i1 %21, label %22, label %25
22: ; preds = %18
%23 = load i32, i32* %6
%24 = add nsw i32 %23, 1
store i32 %24, i32* %6
br label %18
25: ; preds = %18
%26 = load i32*, i32** %4
%27 = call i32 @closedir(i32* %26)
%28 = load i32, i32* %6
store i32 %28, i32* %2
br label %29
29: ; preds = %25, %16, %9
%30 = load i32, i32* %2
ret i32 %30
}

LLVM IR (-O2):
%struct.dirent = type { i32 }
@EINVAL = global i32 0
@errno = global i32 0
define i32 @count_files_in_dir(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %3, label %5
3: ; preds = %1
%4 = load i32, i32* @EINVAL
store i32 %4, i32* @errno
br label %15
5: ; preds = %1
%6 = tail call i32* @opendir(i8* %0)
%7 = icmp eq i32* %6, null
br i1 %7, label %15, label %8
8: ; preds = %5, %8
%9 = phi i32 [ %12, %8 ], [ 0, %5 ]
%10 = tail call %struct.dirent* @readdir(i32* %6)
%11 = icmp eq %struct.dirent* %10, null
%12 = add nuw nsw i32 %9, 1
br i1 %11, label %13, label %8
13: ; preds = %8
%14 = tail call i32 @closedir(i32* %6)
br label %15
15: ; preds = %5, %13, %3
%16 = phi i32 [ %9, %13 ], [ -1, %3 ], [ -1, %5 ]
ret i32 %16
}