#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;
};

/* Variables and functions */
int /*<<< orphan*/ alphasort;
int /*<<< orphan*/ perror(char *);
int scandir(char *, struct dirent ***, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ strcmp(int /*<<< orphan*/, char *);
int dir_empty(char *nam) {
  struct dirent **namelist;
  int n;
  n = scandir(nam, &namelist, 0, alphasort);
  if (n < 0)
    perror("scandir");
  while (n--)
    if (strcmp(namelist[n]->d_name, ".") && strcmp(namelist[n]->d_name, ".."))
      return 0;
  return 1;
}


x86 assembly (clang, -O0):
<dir_empty>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e99(%rip),%rax
mov    (%rax),%ecx
lea    -0x18(%rbp),%rsi
xor    %edx,%edx
call   1030 <scandir@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    116d <dir_empty+0x3d>
lea    0xe98(%rip),%rdi
call   1050 <perror@plt>
jmp    1172 <dir_empty+0x42>
mov    -0x1c(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x1c(%rbp)
cmp    $0x0,%eax
je     11df <dir_empty+0xaf>
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    (%rax),%edi
lea    0xe6e(%rip),%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%rax
je     11da <dir_empty+0xaa>
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    (%rax),%edi
lea    0xe49(%rip),%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%rax
je     11da <dir_empty+0xaa>
movl   $0x0,-0x4(%rbp)
jmp    11e6 <dir_empty+0xb6>
jmp    1172 <dir_empty+0x42>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dir_empty>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    0x2ea2(%rip),%rax
mov    (%rax),%ecx
mov    %rsp,%rsi
xor    %edx,%edx
call   1030 <scandir@plt>
mov    %eax,%ebp
test   %eax,%eax
js     11bb <dir_empty+0x8b>
je     11ab <dir_empty+0x7b>
movslq %ebp,%rbx
add    $0xffffffffffffffff,%rbx
lea    0xea9(%rip),%r14
lea    0xea1(%rip),%r15
jmp    1179 <dir_empty+0x49>
nopl   0x0(%rax)
add    $0xffffffffffffffff,%rbx
add    $0xffffffff,%ebp
je     11ab <dir_empty+0x7b>
mov    (%rsp),%rax
mov    (%rax,%rbx,8),%rax
mov    (%rax),%edi
mov    %r14,%rsi
call   1040 <strcmp@plt>
test   %rax,%rax
je     1170 <dir_empty+0x40>
mov    (%rsp),%rax
mov    (%rax,%rbx,8),%rax
mov    (%rax),%edi
mov    %r15,%rsi
call   1040 <strcmp@plt>
test   %rax,%rax
je     1170 <dir_empty+0x40>
xor    %eax,%eax
jmp    11b0 <dir_empty+0x80>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe3e(%rip),%rdi
call   1050 <perror@plt>
jmp    1152 <dir_empty+0x22>

LLVM IR (-O0):
%struct.dirent = type { i32 }
@alphasort = global i32 0
@.str = [8 x i8] c"scandir\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 %struct.dirent**
%5 = alloca i32
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = load i32, i32* @alphasort
%8 = call i32 @scandir(i8* %6, %struct.dirent*** %4, i32 0, i32 %7)
store i32 %8, i32* %5
%9 = load i32, i32* %5
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %1
%12 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
br label %13
13: ; preds = %11, %1
br label %14
14: ; preds = %39, %13
%15 = load i32, i32* %5
%16 = add nsw i32 %15, -1
store i32 %16, i32* %5
%17 = icmp ne i32 %15, 0
br i1 %17, label %18, label %40
18: ; preds = %14
%19 = load %struct.dirent**, %struct.dirent*** %4
%20 = load i32, i32* %5
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds %struct.dirent*, %struct.dirent** %19, i64 %21
%23 = load %struct.dirent*, %struct.dirent** %22
%24 = getelementptr inbounds %struct.dirent, %struct.dirent* %23, i32 0, i32 0
%25 = load i32, i32* %24
%26 = call i64 @strcmp(i32 %25, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %39
28: ; preds = %18
%29 = load %struct.dirent**, %struct.dirent*** %4
%30 = load i32, i32* %5
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds %struct.dirent*, %struct.dirent** %29, i64 %31
%33 = load %struct.dirent*, %struct.dirent** %32
%34 = getelementptr inbounds %struct.dirent, %struct.dirent* %33, i32 0, i32 0
%35 = load i32, i32* %34
%36 = call i64 @strcmp(i32 %35, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%37 = icmp ne i64 %36, 0
br i1 %37, label %38, label %39
38: ; preds = %28
store i32 0, i32* %2
br label %41
39: ; preds = %28, %18
br label %14
40: ; preds = %14
store i32 1, i32* %2
br label %41
41: ; preds = %40, %38
%42 = load i32, i32* %2
ret i32 %42
}

LLVM IR (-O2):
%struct.dirent = type { i32 }
@alphasort = global i32 0
@.str = [8 x i8] c"scandir\00"
@.str.1 = [2 x i8] c".\00"
@.str.2 = [3 x i8] c"..\00"
define i32 @dir_empty(i8* %0) {
%2 = alloca %struct.dirent**
%3 = bitcast %struct.dirent*** %2 to i8*
%4 = load i32, i32* @alphasort
%5 = call i32 @scandir(i8* %0, %struct.dirent*** %2, i32 0, i32 %4)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %1
%8 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
br label %11
9: ; preds = %1
%10 = icmp eq i32 %5, 0
br i1 %10, label %34, label %11
11: ; preds = %7, %9
%12 = sext i32 %5 to i64
br label %13
13: ; preds = %11, %31
%14 = phi i64 [ %12, %11 ], [ %15, %31 ]
%15 = add nsw i64 %14, -1
%16 = load %struct.dirent**, %struct.dirent*** %2
%17 = getelementptr inbounds %struct.dirent*, %struct.dirent** %16, i64 %15
%18 = load %struct.dirent*, %struct.dirent** %17
%19 = getelementptr inbounds %struct.dirent, %struct.dirent* %18, i64 0, i32 0
%20 = load i32, i32* %19
%21 = call i64 @strcmp(i32 %20, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%22 = icmp eq i64 %21, 0
br i1 %22, label %31, label %23
23: ; preds = %13
%24 = load %struct.dirent**, %struct.dirent*** %2
%25 = getelementptr inbounds %struct.dirent*, %struct.dirent** %24, i64 %15
%26 = load %struct.dirent*, %struct.dirent** %25
%27 = getelementptr inbounds %struct.dirent, %struct.dirent* %26, i64 0, i32 0
%28 = load i32, i32* %27
%29 = call i64 @strcmp(i32 %28, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%30 = icmp eq i64 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %23, %13
%32 = trunc i64 %15 to i32
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %13
34: ; preds = %23, %31, %9
%35 = phi i32 [ 1, %9 ], [ 1, %31 ], [ 0, %23 ]
ret i32 %35
}