#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 */
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
scalar_t__ strcmp(char *, int /*<<< orphan*/);
int numdiritems(char *testdir) {
  DIR *dp;
  int items = 0;
  struct dirent *de;
  dp = opendir(testdir);
  while ((de = readdir(dp))) {
    if ((strcmp(".", de->d_name) == 0) || (strcmp("..", de->d_name) == 0))
      continue;
    items++;
  }
  closedir(dp);
  return items;
}


x86 assembly (clang, -O0):
<numdiritems>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     11c2 <numdiritems+0x82>
mov    -0x20(%rbp),%rax
mov    (%rax),%esi
lea    0xe7d(%rip),%rdi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
je     11af <numdiritems+0x6f>
mov    -0x20(%rbp),%rax
mov    (%rax),%esi
lea    0xe60(%rip),%rdi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
jne    11b4 <numdiritems+0x74>
jmp    1160 <numdiritems+0x20>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1160 <numdiritems+0x20>
mov    -0x10(%rbp),%rdi
call   1040 <closedir@plt>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<numdiritems>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
call   1030 <opendir@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1060 <readdir@plt>
test   %rax,%rax
je     11b6 <numdiritems+0x76>
mov    %rax,%rbp
xor    %r14d,%r14d
lea    0xe97(%rip),%r12
lea    0xe8f(%rip),%r15
jmp    1190 <numdiritems+0x50>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rbx,%rdi
call   1060 <readdir@plt>
mov    %rax,%rbp
test   %rax,%rax
je     11b9 <numdiritems+0x79>
mov    0x0(%rbp),%esi
mov    %r12,%rdi
call   1050 <strcmp@plt>
test   %rax,%rax
je     1180 <numdiritems+0x40>
mov    0x0(%rbp),%esi
mov    %r15,%rdi
call   1050 <strcmp@plt>
test   %rax,%rax
je     1180 <numdiritems+0x40>
add    $0x1,%r14d
jmp    1180 <numdiritems+0x40>
xor    %r14d,%r14d
mov    %rbx,%rdi
call   1040 <closedir@plt>
mov    %r14d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i32 }
@.str = [2 x i8] c".\00"
@.str.1 = [3 x i8] c"..\00"
define i32 @numdiritems(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i32
%5 = alloca %struct.dirent*
store i8* %0, i8** %2
store i32 0, i32* %4
%6 = load i8*, i8** %2
%7 = call i32* @opendir(i8* %6)
store i32* %7, i32** %3
br label %8
8: ; preds = %25, %24, %1
%9 = load i32*, i32** %3
%10 = call %struct.dirent* @readdir(i32* %9)
store %struct.dirent* %10, %struct.dirent** %5
%11 = icmp ne %struct.dirent* %10, null
br i1 %11, label %12, label %28
12: ; preds = %8
%13 = load %struct.dirent*, %struct.dirent** %5
%14 = getelementptr inbounds %struct.dirent, %struct.dirent* %13, i32 0, i32 0
%15 = load i32, i32* %14
%16 = call i64 @strcmp(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 %15)
%17 = icmp eq i64 %16, 0
br i1 %17, label %24, label %18
18: ; preds = %12
%19 = load %struct.dirent*, %struct.dirent** %5
%20 = getelementptr inbounds %struct.dirent, %struct.dirent* %19, i32 0, i32 0
%21 = load i32, i32* %20
%22 = call i64 @strcmp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %21)
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %18, %12
br label %8
25: ; preds = %18
%26 = load i32, i32* %4
%27 = add nsw i32 %26, 1
store i32 %27, i32* %4
br label %8
28: ; preds = %8
%29 = load i32*, i32** %3
%30 = call i32 @closedir(i32* %29)
%31 = load i32, i32* %4
ret i32 %31
}

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