#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*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ printf(char *, char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int is_dir_empty(char *path) {
  int n = 0;
  struct dirent *d;
  DIR *dir;
  if (NULL == (dir = opendir(path))) {
    printf("Couldn't open %s directory!\n", path);
    exit(1);
  }
  while ((d = readdir(dir)) != NULL) {
    if (++n > 2)
      break;
  }
  closedir(dir);
  if (n <= 2)
    return 1;
  else
    return 0;
}


x86 assembly (clang, -O0):
<is_dir_empty>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,%rcx
mov    %rcx,-0x28(%rbp)
xor    %eax,%eax
cmp    %rcx,%rax
jne    1198 <is_dir_empty+0x48>
mov    -0x10(%rbp),%rsi
lea    0xe77(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
jmp    119d <is_dir_empty+0x4d>
mov    -0x28(%rbp),%rdi
call   1060 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     11d0 <is_dir_empty+0x80>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
cmp    $0x2,%eax
jle    11cb <is_dir_empty+0x7b>
jmp    11d0 <is_dir_empty+0x80>
jmp    119d <is_dir_empty+0x4d>
mov    -0x28(%rbp),%rdi
call   1050 <closedir@plt>
cmpl   $0x2,-0x14(%rbp)
jg     11ef <is_dir_empty+0x9f>
movl   $0x1,-0x4(%rbp)
jmp    11f6 <is_dir_empty+0xa6>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<is_dir_empty>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%r14
call   1030 <opendir@plt>
test   %rax,%rax
je     11a1 <is_dir_empty+0x51>
mov    %rax,%rbx
mov    %rax,%rdi
call   1060 <readdir@plt>
test   %rax,%rax
je     118d <is_dir_empty+0x3d>
mov    %rbx,%rdi
call   1060 <readdir@plt>
test   %rax,%rax
je     118d <is_dir_empty+0x3d>
mov    %rbx,%rdi
call   1060 <readdir@plt>
xor    %ebp,%ebp
test   %rax,%rax
jne    1192 <is_dir_empty+0x42>
mov    $0x1,%ebp
mov    %rbx,%rdi
call   1050 <closedir@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe58(%rip),%rdi
mov    %r14,%rsi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
%struct.dirent = type { i32 }
@.str = [29 x i8] c"Couldn't open %s directory!\0A\00"
define i32 @is_dir_empty(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* %4
%7 = load i8*, i8** %3
%8 = call i32* @opendir(i8* %7)
store i32* %8, i32** %6
%9 = icmp eq i32* null, %8
br i1 %9, label %10, label %14
10: ; preds = %1
%11 = load i8*, i8** %3
%12 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %11)
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %1
br label %15
15: ; preds = %24, %14
%16 = load i32*, i32** %6
%17 = call %struct.dirent* @readdir(i32* %16)
store %struct.dirent* %17, %struct.dirent** %5
%18 = icmp ne %struct.dirent* %17, null
br i1 %18, label %19, label %25
19: ; preds = %15
%20 = load i32, i32* %4
%21 = add nsw i32 %20, 1
store i32 %21, i32* %4
%22 = icmp sgt i32 %21, 2
br i1 %22, label %23, label %24
23: ; preds = %19
br label %25
24: ; preds = %19
br label %15
25: ; preds = %23, %15
%26 = load i32*, i32** %6
%27 = call i32 @closedir(i32* %26)
%28 = load i32, i32* %4
%29 = icmp sle i32 %28, 2
br i1 %29, label %30, label %31
30: ; preds = %25
store i32 1, i32* %2
br label %32
31: ; preds = %25
store i32 0, i32* %2
br label %32
32: ; preds = %31, %30
%33 = load i32, i32* %2
ret i32 %33
}

LLVM IR (-O2):
%struct.dirent = type { i32 }
@.str = [29 x i8] c"Couldn't open %s directory!\0A\00"
define i32 @is_dir_empty(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 %16, label %10
7: ; preds = %1
%8 = tail call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %0)
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %4
%11 = tail call %struct.dirent* @readdir(i32* %2)
%12 = icmp eq %struct.dirent* %11, null
br i1 %12, label %16, label %13
13: ; preds = %10
%14 = tail call %struct.dirent* @readdir(i32* %2)
%15 = icmp eq %struct.dirent* %14, null
br i1 %15, label %16, label %17
16: ; preds = %13, %10, %4
br label %17
17: ; preds = %13, %16
%18 = phi i32 [ 1, %16 ], [ 0, %13 ]
%19 = tail call i32 @closedir(i32* %2)
ret i32 %18
}