#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*/ *opendir(char const *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ strcmp(int /*<<< orphan*/, char *);
int /*<<< orphan*/ warn(char *, char const *);
const char *num_files(const char *path) {
  struct dirent *dp;
  DIR *fd;
  int num;
  if (!(fd = opendir(path))) {
    warn("opendir '%s':", path);
    return NULL;
  }
  num = 0;
  while ((dp = readdir(fd))) {
    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    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11a0 <num_files+0x40>
mov    -0x10(%rbp),%rsi
lea    0xe72(%rip),%rdi
call   1030 <warn@plt>
movq   $0x0,-0x8(%rbp)
jmp    1223 <num_files+0xc3>
movl   $0x0,-0x24(%rbp)
mov    -0x20(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     1207 <num_files+0xa7>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
lea    0xe44(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%eax
je     11f4 <num_files+0x94>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
lea    0xe28(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%eax
jne    11f9 <num_files+0x99>
jmp    11a7 <num_files+0x47>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11a7 <num_files+0x47>
mov    -0x20(%rbp),%rdi
call   1060 <closedir@plt>
mov    -0x24(%rbp),%esi
lea    0xdf7(%rip),%rdi
call   1050 <bprintf@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<num_files>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdi,%rbp
call   1040 <opendir@plt>
test   %rax,%rax
je     11d4 <num_files+0x74>
mov    %rax,%rbx
mov    %rax,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
je     11ee <num_files+0x8e>
mov    %rax,%rbp
xor    %r14d,%r14d
lea    0xe7d(%rip),%r12
lea    0xe75(%rip),%r15
jmp    11b0 <num_files+0x50>
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1080 <readdir@plt>
mov    %rax,%rbp
test   %rax,%rax
je     11f1 <num_files+0x91>
mov    0x0(%rbp),%edi
mov    %r12,%rsi
call   1070 <strcmp@plt>
test   %eax,%eax
je     11a0 <num_files+0x40>
mov    0x0(%rbp),%edi
mov    %r15,%rsi
call   1070 <strcmp@plt>
test   %eax,%eax
je     11a0 <num_files+0x40>
add    $0x1,%r14d
jmp    11a0 <num_files+0x40>
lea    0xe25(%rip),%rdi
mov    %rbp,%rsi
call   1030 <warn@plt>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %r14d,%r14d
mov    %rbx,%rdi
call   1060 <closedir@plt>
lea    0xe11(%rip),%rdi
mov    %r14d,%esi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <bprintf@plt>

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

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