#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*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ strcmp(char *, char *);
int test_if_dir_empty(char *dir) {
  struct dirent *ent = NULL;
  DIR *pDir;
  int i = 0;
  pDir = opendir(dir);
  if (pDir != NULL) {
    while (NULL != (ent = readdir(pDir))) {
      if (ent->d_name[0] == '.')
        continue;
      if (!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, ".."))
        continue;
      i++;
    }
    closedir(pDir);
  }
  return (i == 0) ? 1 : 0;
}


x86 assembly (clang, -O0):
<test_if_dir_empty>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11ff <test_if_dir_empty+0xbf>
jmp    1178 <test_if_dir_empty+0x38>
mov    -0x18(%rbp),%rdi
call   1060 <readdir@plt>
mov    %rax,%rcx
mov    %rcx,-0x10(%rbp)
xor    %eax,%eax
cmp    %rcx,%rax
je     11f6 <test_if_dir_empty+0xb6>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
jne    11ab <test_if_dir_empty+0x6b>
jmp    1178 <test_if_dir_empty+0x38>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
lea    0xe48(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%eax
je     11e3 <test_if_dir_empty+0xa3>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
lea    0xe2b(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%eax
jne    11e8 <test_if_dir_empty+0xa8>
jmp    1178 <test_if_dir_empty+0x38>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1178 <test_if_dir_empty+0x38>
mov    -0x18(%rbp),%rdi
call   1040 <closedir@plt>
mov    -0x1c(%rbp),%edx
xor    %eax,%eax
mov    $0x1,%ecx
cmp    $0x0,%edx
cmove  %ecx,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_if_dir_empty>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
call   1030 <opendir@plt>
test   %rax,%rax
je     11c2 <test_if_dir_empty+0x82>
mov    %rax,%r12
mov    %rax,%rdi
call   1060 <readdir@plt>
xor    %ebp,%ebp
test   %rax,%rax
je     11b8 <test_if_dir_empty+0x78>
lea    0xe96(%rip),%r14
lea    0xe8e(%rip),%r15
jmp    118d <test_if_dir_empty+0x4d>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r12,%rdi
call   1060 <readdir@plt>
test   %rax,%rax
je     11b8 <test_if_dir_empty+0x78>
mov    (%rax),%rbx
cmpb   $0x2e,(%rbx)
je     1180 <test_if_dir_empty+0x40>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1050 <strcmp@plt>
test   %eax,%eax
je     1180 <test_if_dir_empty+0x40>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1050 <strcmp@plt>
test   %eax,%eax
je     1180 <test_if_dir_empty+0x40>
add    $0x1,%ebp
jmp    1180 <test_if_dir_empty+0x40>
mov    %r12,%rdi
call   1040 <closedir@plt>
jmp    11c4 <test_if_dir_empty+0x84>
xor    %ebp,%ebp
xor    %eax,%eax
test   %ebp,%ebp
sete   %al
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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