#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 stat {
  int /*<<< orphan*/ st_mode;
};
struct dirent {
  char const *d_name;
};
typedef int /*<<< orphan*/ mode_t;
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
int /*<<< orphan*/ S_ISDIR(int /*<<< orphan*/);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int errno;
int lstat(char const *, struct stat *);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ strcmp(char const *, char *);
int for_each_subdir(int (*func)(const char *, void *), void *private_data) {
  struct dirent *entry;
  int ret;
  DIR *dir = opendir(".");
  if (!dir)
    return -((errno) | (1 << 30));
  while ((entry = readdir(dir))) {
    mode_t m;
    struct stat s;
    if (!strcmp(entry->d_name, "."))
      continue;
    if (!strcmp(entry->d_name, ".."))
      continue;
    ret = lstat(entry->d_name, &s) == -1;
    if (ret == -1) {
      ret = -((errno) | (1 << 30));
      goto out;
    }
    m = s.st_mode;
    if (!S_ISDIR(m))
      continue;
    ret = func(entry->d_name, private_data);
    if (ret < 0)
      goto out;
  }
  ret = 1;
out:
  closedir(dir);
  return ret;
}


x86 assembly (clang, -O0):
<for_each_subdir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    0xe8a(%rip),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11a6 <for_each_subdir+0x46>
mov    0x2e4e(%rip),%rax
mov    (%rax),%ecx
or     $0x40000000,%ecx
xor    %eax,%eax
sub    %ecx,%eax
mov    %eax,-0x4(%rbp)
jmp    12a1 <for_each_subdir+0x141>
jmp    11ab <for_each_subdir+0x4b>
mov    -0x30(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     128b <for_each_subdir+0x12b>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
lea    0xe31(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%eax
jne    11e3 <for_each_subdir+0x83>
jmp    11ab <for_each_subdir+0x4b>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
lea    0xe0f(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%eax
jne    1204 <for_each_subdir+0xa4>
jmp    11ab <for_each_subdir+0x4b>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
lea    -0x38(%rbp),%rsi
call   1060 <lstat@plt>
cmp    $0xffffffff,%eax
sete   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    1247 <for_each_subdir+0xe7>
mov    0x2dad(%rip),%rax
mov    (%rax),%ecx
or     $0x40000000,%ecx
xor    %eax,%eax
sub    %ecx,%eax
mov    %eax,-0x24(%rbp)
jmp    1292 <for_each_subdir+0x132>
mov    -0x38(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%edi
call   1040 <S_ISDIR@plt>
cmp    $0x0,%eax
jne    1263 <for_each_subdir+0x103>
jmp    11ab <for_each_subdir+0x4b>
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    (%rcx),%rdi
mov    -0x18(%rbp),%rsi
call   *%rax
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1286 <for_each_subdir+0x126>
jmp    1292 <for_each_subdir+0x132>
jmp    11ab <for_each_subdir+0x4b>
movl   $0x1,-0x24(%rbp)
mov    -0x30(%rbp),%rdi
call   1050 <closedir@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<for_each_subdir>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,0x10(%rsp)
mov    %rdi,%r12
lea    0xe84(%rip),%rdi
call   1030 <opendir@plt>
test   %rax,%rax
je     121e <for_each_subdir+0xbe>
mov    %rax,%rbx
mov    %rax,%rdi
call   1080 <readdir@plt>
mov    $0x1,%ebp
test   %rax,%rax
je     1235 <for_each_subdir+0xd5>
mov    %rax,%r14
lea    0xe53(%rip),%rbp
lea    0xe4b(%rip),%r13
jmp    11d0 <for_each_subdir+0x70>
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1080 <readdir@plt>
mov    %rax,%r14
test   %rax,%rax
je     1230 <for_each_subdir+0xd0>
mov    (%r14),%r15
mov    %r15,%rdi
mov    %rbp,%rsi
call   1070 <strcmp@plt>
test   %eax,%eax
je     11c0 <for_each_subdir+0x60>
mov    %r15,%rdi
mov    %r13,%rsi
call   1070 <strcmp@plt>
test   %eax,%eax
je     11c0 <for_each_subdir+0x60>
mov    %r15,%rdi
lea    0x8(%rsp),%rsi
call   1060 <lstat@plt>
mov    0x8(%rsp),%edi
call   1040 <S_ISDIR@plt>
test   %eax,%eax
je     11c0 <for_each_subdir+0x60>
mov    (%r14),%rdi
mov    0x10(%rsp),%rsi
call   *%r12
test   %eax,%eax
jns    11c0 <for_each_subdir+0x60>
mov    %eax,%ebp
jmp    1235 <for_each_subdir+0xd5>
mov    0x2dbb(%rip),%rax
mov    $0x40000000,%ebp
or     (%rax),%ebp
neg    %ebp
jmp    123d <for_each_subdir+0xdd>
mov    $0x1,%ebp
mov    %rbx,%rdi
call   1050 <closedir@plt>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i8* }
%struct.stat = type { i32 }
@.str = [2 x i8] c".\00"
@errno = global i32 0
@.str.1 = [3 x i8] c"..\00"
define i32 @for_each_subdir(i32 (i8*, i8*)* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32 (i8*, i8*)*
%5 = alloca i8*
%6 = alloca %struct.dirent*
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i32
%10 = alloca %struct.stat
store i32 (i8*, i8*)* %0, i32 (i8*, i8*)** %4
store i8* %1, i8** %5
%11 = call i32* @opendir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %8
%12 = load i32*, i32** %8
%13 = icmp ne i32* %12, null
br i1 %13, label %18, label %14
14: ; preds = %2
%15 = load i32, i32* @errno
%16 = or i32 %15, 1073741824
%17 = sub nsw i32 0, %16
store i32 %17, i32* %3
br label %73
18: ; preds = %2
br label %19
19: ; preds = %67, %56, %36, %29, %18
%20 = load i32*, i32** %8
%21 = call %struct.dirent* @readdir(i32* %20)
store %struct.dirent* %21, %struct.dirent** %6
%22 = icmp ne %struct.dirent* %21, null
br i1 %22, label %23, label %68
23: ; preds = %19
%24 = load %struct.dirent*, %struct.dirent** %6
%25 = getelementptr inbounds %struct.dirent, %struct.dirent* %24, i32 0, i32 0
%26 = load i8*, i8** %25
%27 = call i32 @strcmp(i8* %26, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%28 = icmp ne i32 %27, 0
br i1 %28, label %30, label %29
29: ; preds = %23
br label %19
30: ; preds = %23
%31 = load %struct.dirent*, %struct.dirent** %6
%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
br label %19
37: ; preds = %30
%38 = load %struct.dirent*, %struct.dirent** %6
%39 = getelementptr inbounds %struct.dirent, %struct.dirent* %38, i32 0, i32 0
%40 = load i8*, i8** %39
%41 = call i32 @lstat(i8* %40, %struct.stat* %10)
%42 = icmp eq i32 %41, -1
%43 = zext i1 %42 to i32
store i32 %43, i32* %7
%44 = load i32, i32* %7
%45 = icmp eq i32 %44, -1
br i1 %45, label %46, label %50
46: ; preds = %37
%47 = load i32, i32* @errno
%48 = or i32 %47, 1073741824
%49 = sub nsw i32 0, %48
store i32 %49, i32* %7
br label %69
50: ; preds = %37
%51 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%52 = load i32, i32* %51
store i32 %52, i32* %9
%53 = load i32, i32* %9
%54 = call i32 @S_ISDIR(i32 %53)
%55 = icmp ne i32 %54, 0
br i1 %55, label %57, label %56
56: ; preds = %50
br label %19
57: ; preds = %50
%58 = load i32 (i8*, i8*)*, i32 (i8*, i8*)** %4
%59 = load %struct.dirent*, %struct.dirent** %6
%60 = getelementptr inbounds %struct.dirent, %struct.dirent* %59, i32 0, i32 0
%61 = load i8*, i8** %60
%62 = load i8*, i8** %5
%63 = call i32 %58(i8* %61, i8* %62)
store i32 %63, i32* %7
%64 = load i32, i32* %7
%65 = icmp slt i32 %64, 0
br i1 %65, label %66, label %67
66: ; preds = %57
br label %69
67: ; preds = %57
br label %19
68: ; preds = %19
store i32 1, i32* %7
br label %69
69: ; preds = %68, %66, %46
%70 = load i32*, i32** %8
%71 = call i32 @closedir(i32* %70)
%72 = load i32, i32* %7
store i32 %72, i32* %3
br label %73
73: ; preds = %69, %14
%74 = load i32, i32* %3
ret i32 %74
}

LLVM IR (-O2):
%struct.stat = type { i32 }
%struct.dirent = type { i8* }
@.str = [2 x i8] c".\00"
@errno = global i32 0
@.str.1 = [3 x i8] c"..\00"
define i32 @for_each_subdir(i32 (i8*, i8*)* nocapture %0, i8* %1) {
%3 = alloca %struct.stat
%4 = tail call i32* @opendir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %12, label %6
6: ; preds = %2
%7 = tail call %struct.dirent* @readdir(i32* %4)
%8 = icmp eq %struct.dirent* %7, null
br i1 %8, label %38, label %9
9: ; preds = %6
%10 = bitcast %struct.stat* %3 to i8*
%11 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
br label %16
12: ; preds = %2
%13 = load i32, i32* @errno
%14 = or i32 %13, 1073741824
%15 = sub nsw i32 0, %14
br label %41
16: ; preds = %9, %35
%17 = phi %struct.dirent* [ %7, %9 ], [ %36, %35 ]
%18 = getelementptr inbounds %struct.dirent, %struct.dirent* %17, i64 0, i32 0
%19 = load i8*, i8** %18
%20 = tail call i32 @strcmp(i8* dereferenceable(1) %19, i8* dereferenceable(2) getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%21 = icmp eq i32 %20, 0
br i1 %21, label %35, label %22
22: ; preds = %16
%23 = tail call i32 @strcmp(i8* dereferenceable(1) %19, i8* dereferenceable(3) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%24 = icmp eq i32 %23, 0
br i1 %24, label %35, label %25
25: ; preds = %22
%26 = call i32 @lstat(i8* %19, %struct.stat* %3)
%27 = load i32, i32* %11
%28 = tail call i32 @S_ISDIR(i32 %27)
%29 = icmp eq i32 %28, 0
br i1 %29, label %35, label %30
30: ; preds = %25
%31 = load i8*, i8** %18
%32 = tail call i32 %0(i8* %31, i8* %1)
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %35
34: ; preds = %30
br label %38
35: ; preds = %30, %25, %22, %16
%36 = tail call %struct.dirent* @readdir(i32* %4)
%37 = icmp eq %struct.dirent* %36, null
br i1 %37, label %38, label %16
38: ; preds = %35, %6, %34
%39 = phi i32 [ %32, %34 ], [ 1, %6 ], [ 1, %35 ]
%40 = tail call i32 @closedir(i32* %4)
br label %41
41: ; preds = %38, %12
%42 = phi i32 [ %39, %38 ], [ %15, %12 ]
ret i32 %42
}