#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*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int Ftestdirempty(char *dirname) {
  int n = 0;
  struct dirent *d;
  DIR *dir = opendir(dirname);
  if (dir == NULL)
    return 1;
  while ((d = readdir(dir)) != NULL)
    if (++n > 2)
      break;
  closedir(dir);
  if (n <= 2)
    return 1;
  else
    return 0;
}


x86 assembly (clang, -O0):
<Ftestdirempty>:
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,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1167 <Ftestdirempty+0x37>
movl   $0x1,-0x4(%rbp)
jmp    11c5 <Ftestdirempty+0x95>
jmp    116c <Ftestdirempty+0x3c>
mov    -0x28(%rbp),%rdi
call   1050 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     119f <Ftestdirempty+0x6f>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
cmp    $0x2,%eax
jle    119a <Ftestdirempty+0x6a>
jmp    119f <Ftestdirempty+0x6f>
jmp    116c <Ftestdirempty+0x3c>
mov    -0x28(%rbp),%rdi
call   1040 <closedir@plt>
cmpl   $0x2,-0x14(%rbp)
jg     11be <Ftestdirempty+0x8e>
movl   $0x1,-0x4(%rbp)
jmp    11c5 <Ftestdirempty+0x95>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<Ftestdirempty>:
push   %rbp
push   %rbx
push   %rax
call   1030 <opendir@plt>
test   %rax,%rax
je     1178 <Ftestdirempty+0x48>
mov    %rax,%rbx
mov    %rax,%rdi
call   1050 <readdir@plt>
test   %rax,%rax
je     1169 <Ftestdirempty+0x39>
mov    %rbx,%rdi
call   1050 <readdir@plt>
test   %rax,%rax
je     1169 <Ftestdirempty+0x39>
mov    %rbx,%rdi
call   1050 <readdir@plt>
xor    %ebp,%ebp
test   %rax,%rax
jne    116e <Ftestdirempty+0x3e>
mov    $0x1,%ebp
mov    %rbx,%rdi
call   1040 <closedir@plt>
jmp    117d <Ftestdirempty+0x4d>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i32 }
define i32 @Ftestdirempty(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 = load i32*, i32** %6
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %12
11: ; preds = %1
store i32 1, i32* %2
br label %30
12: ; preds = %1
br label %13
13: ; preds = %22, %12
%14 = load i32*, i32** %6
%15 = call %struct.dirent* @readdir(i32* %14)
store %struct.dirent* %15, %struct.dirent** %5
%16 = icmp ne %struct.dirent* %15, null
br i1 %16, label %17, label %23
17: ; preds = %13
%18 = load i32, i32* %4
%19 = add nsw i32 %18, 1
store i32 %19, i32* %4
%20 = icmp sgt i32 %19, 2
br i1 %20, label %21, label %22
21: ; preds = %17
br label %23
22: ; preds = %17
br label %13
23: ; preds = %21, %13
%24 = load i32*, i32** %6
%25 = call i32 @closedir(i32* %24)
%26 = load i32, i32* %4
%27 = icmp sle i32 %26, 2
br i1 %27, label %28, label %29
28: ; preds = %23
store i32 1, i32* %2
br label %30
29: ; preds = %23
store i32 0, i32* %2
br label %30
30: ; preds = %29, %28, %11
%31 = load i32, i32* %2
ret i32 %31
}

LLVM IR (-O2):
%struct.dirent = type { i32 }
define i32 @Ftestdirempty(i8* nocapture %0) {
%2 = tail call i32* @opendir(i8* %0)
%3 = icmp eq i32* %2, null
br i1 %3, label %17, label %4
4: ; preds = %1
%5 = tail call %struct.dirent* @readdir(i32* %2)
%6 = icmp eq %struct.dirent* %5, null
br i1 %6, label %13, label %7
7: ; preds = %4
%8 = tail call %struct.dirent* @readdir(i32* %2)
%9 = icmp eq %struct.dirent* %8, null
br i1 %9, label %13, label %10
10: ; preds = %7
%11 = tail call %struct.dirent* @readdir(i32* %2)
%12 = icmp eq %struct.dirent* %11, null
br i1 %12, label %13, label %14
13: ; preds = %10, %7, %4
br label %14
14: ; preds = %10, %13
%15 = phi i32 [ 1, %13 ], [ 0, %10 ]
%16 = tail call i32 @closedir(i32* %2)
br label %17
17: ; preds = %14, %1
%18 = phi i32 [ 1, %1 ], [ %15, %14 ]
ret i32 %18
}