#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 */
scalar_t__ ENOENT;
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
scalar_t__ errno;
int /*<<< orphan*/ logger(int, scalar_t__, char *, char const *);
int /*<<< orphan*/ *opendir(char const *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ strcmp(int /*<<< orphan*/, char *);
int dir_empty(const char *dir) {
  DIR *dp;
  struct dirent *ep;
  int ret = 1;
  dp = opendir(dir);
  if (dp == NULL) {
    if (errno == ENOENT)
      return 1;
    logger(-1, errno, "Can't opendir %s", dir);
    return -1;
  }
  while ((ep = readdir(dp))) {
    if (!strcmp(ep->d_name, "."))
      continue;
    if (!strcmp(ep->d_name, ".."))
      continue;
    ret = 0;
    break;
  }
  closedir(dp);
  return ret;
}


x86 assembly (clang, -O0):
<dir_empty>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x1,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11cc <dir_empty+0x7c>
mov    0x2e5e(%rip),%rax
mov    (%rax),%rax
mov    0x2e44(%rip),%rcx
cmp    (%rcx),%rax
jne    11a1 <dir_empty+0x51>
movl   $0x1,-0x4(%rbp)
jmp    123e <dir_empty+0xee>
mov    0x2e38(%rip),%rax
mov    (%rax),%rsi
mov    -0x10(%rbp),%rcx
mov    $0xffffffff,%edi
lea    0xe45(%rip),%rdx
call   1060 <logger@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    123e <dir_empty+0xee>
jmp    11d1 <dir_empty+0x81>
mov    -0x18(%rbp),%rdi
call   1070 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     122f <dir_empty+0xdf>
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
lea    0xe1d(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%eax
jne    1208 <dir_empty+0xb8>
jmp    11d1 <dir_empty+0x81>
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
lea    0xdfc(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%eax
jne    1228 <dir_empty+0xd8>
jmp    11d1 <dir_empty+0x81>
movl   $0x0,-0x24(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <closedir@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dir_empty>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdi,%rbp
call   1030 <opendir@plt>
test   %rax,%rax
je     11cb <dir_empty+0x7b>
mov    %rax,%rbx
mov    %rax,%rdi
call   1070 <readdir@plt>
mov    $0x1,%r14d
test   %rax,%rax
je     11c1 <dir_empty+0x71>
mov    %rax,%rbp
lea    0xe8d(%rip),%r12
lea    0xe85(%rip),%r15
jmp    11a0 <dir_empty+0x50>
xchg   %ax,%ax
mov    %rbx,%rdi
call   1070 <readdir@plt>
mov    %rax,%rbp
test   %rax,%rax
je     11c1 <dir_empty+0x71>
mov    0x0(%rbp),%edi
mov    %r12,%rsi
call   1050 <strcmp@plt>
test   %eax,%eax
je     1190 <dir_empty+0x40>
mov    0x0(%rbp),%edi
mov    %r15,%rsi
call   1050 <strcmp@plt>
test   %eax,%eax
je     1190 <dir_empty+0x40>
xor    %r14d,%r14d
mov    %rbx,%rdi
call   1040 <closedir@plt>
jmp    1201 <dir_empty+0xb1>
mov    0x2e0e(%rip),%rax
mov    (%rax),%rsi
mov    0x2df4(%rip),%rax
mov    $0x1,%r14d
cmp    (%rax),%rsi
je     1201 <dir_empty+0xb1>
lea    0xe12(%rip),%rdx
mov    $0xffffffff,%r14d
mov    $0xffffffff,%edi
mov    %rbp,%rcx
call   1060 <logger@plt>
mov    %r14d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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