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

/* Variables and functions */
char const *bprintf(char *, int);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *, char *);
int /*<<< orphan*/ *opendir(char const *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcmp(int /*<<< orphan*/, char *);
char *strerror(int /*<<< orphan*/);
const char *num_files(const char *dir) {
  struct dirent *dp;
  DIR *fd;
  int num = 0;
  if ((fd = opendir(dir)) == NULL) {
    fprintf(stderr, "opendir '%s': %s\n", dir, strerror(errno));
    return NULL;
  }
  while ((dp = readdir(fd)) != NULL) {
    if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
      continue;
    num++;
  }
  closedir(fd);
  return bprintf("%d", num);
}


x86 assembly (clang, -O0):
<num_files>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11df <num_files+0x6f>
mov    0x2e3f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2e23(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rdx
mov    %rax,%rcx
lea    0xe33(%rip),%rsi
call   1070 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1260 <num_files+0xf0>
jmp    11e4 <num_files+0x74>
mov    -0x20(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     1244 <num_files+0xd4>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
lea    0xe0b(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%eax
je     1231 <num_files+0xc1>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
lea    0xdef(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%eax
jne    1236 <num_files+0xc6>
jmp    11e4 <num_files+0x74>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11e4 <num_files+0x74>
mov    -0x20(%rbp),%rdi
call   1050 <closedir@plt>
mov    -0x24(%rbp),%esi
lea    0xdbe(%rip),%rdi
call   1040 <bprintf@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<num_files>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdi,%rbp
call   1030 <opendir@plt>
test   %rax,%rax
je     11e4 <num_files+0x74>
mov    %rax,%rbx
mov    %rax,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
je     121a <num_files+0xaa>
mov    %rax,%rbp
xor    %r14d,%r14d
lea    0xe6d(%rip),%r12
lea    0xe65(%rip),%r15
jmp    11c0 <num_files+0x50>
nop
mov    %rbx,%rdi
call   1080 <readdir@plt>
mov    %rax,%rbp
test   %rax,%rax
je     121d <num_files+0xad>
mov    0x0(%rbp),%edi
mov    %r12,%rsi
call   1060 <strcmp@plt>
test   %eax,%eax
je     11b0 <num_files+0x40>
mov    0x0(%rbp),%edi
mov    %r15,%rsi
call   1060 <strcmp@plt>
test   %eax,%eax
je     11b0 <num_files+0x40>
add    $0x1,%r14d
jmp    11b0 <num_files+0x40>
mov    0x2df5(%rip),%rax
mov    (%rax),%ebx
mov    0x2de4(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xdfe(%rip),%rsi
mov    %ebx,%edi
mov    %rbp,%rdx
mov    %rax,%rcx
call   1070 <fprintf@plt>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %r14d,%r14d
mov    %rbx,%rdi
call   1050 <closedir@plt>
lea    0xde9(%rip),%rdi
mov    %r14d,%esi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <bprintf@plt>

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

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