#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 */
typedef struct TYPE_3__ TYPE_1__;

/* Type definitions */
struct TYPE_3__ {
  int gl_pathc;
  char **gl_pathv;
};
typedef TYPE_1__ glob_t;

/* Variables and functions */
int GLOB_MARK;
int GLOB_NOESCAPE;
int GLOB_ONLYDIR;
int /*<<< orphan*/ glob(char *, int, int /*<<< orphan*/ *, TYPE_1__ *);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *);
int strlen(char const *);
void foreachdir(const char *dir, int (*cb)(const char *)) {
  char globdir[256];
  glob_t gl;
  int j;
  if (dir[strlen(dir) - 1] == '/')
    snprintf(globdir, 256, "%s*", dir);
  else
    snprintf(globdir, 256, "%s/*", dir);
  if (!glob(globdir, GLOB_NOESCAPE | GLOB_MARK | GLOB_ONLYDIR, NULL, &gl))
    for (j = 0; j < gl.gl_pathc; j++)
      foreachdir(gl.gl_pathv[j], cb);
  cb(dir);
}


x86 assembly (clang, -O0):
<foreachdir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x130(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x130(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2f,%eax
jne    11a4 <foreachdir+0x64>
lea    -0x110(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    $0x100,%esi
lea    0xe66(%rip),%rdx
call   1040 <snprintf@plt>
jmp    11c0 <foreachdir+0x80>
lea    -0x110(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    $0x100,%esi
lea    0xe49(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x110(%rbp),%rdi
mov    0x2e12(%rip),%rax
mov    (%rax),%esi
mov    0x2e01(%rip),%rax
or     (%rax),%esi
mov    0x2e18(%rip),%rax
or     (%rax),%esi
xor    %eax,%eax
mov    %eax,%edx
lea    -0x120(%rbp),%rcx
call   1060 <glob@plt>
cmp    $0x0,%eax
jne    124b <foreachdir+0x10b>
movl   $0x0,-0x124(%rbp)
mov    -0x124(%rbp),%eax
cmp    -0x120(%rbp),%eax
jge    1246 <foreachdir+0x106>
mov    -0x118(%rbp),%rax
movslq -0x124(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
mov    -0x10(%rbp),%rsi
call   1050 <foreachdir@plt>
mov    -0x124(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x124(%rbp)
jmp    1205 <foreachdir+0xc5>
jmp    124b <foreachdir+0x10b>
mov    -0x10(%rbp),%rax
mov    -0x8(%rbp),%rdi
call   *%rax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<foreachdir>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x118,%rsp
mov    %rsi,%r12
mov    %rdi,%r14
call   1030 <strlen@plt>
cltq
cmpb   $0x2f,-0x1(%rax,%r14,1)
lea    0xe98(%rip),%rax
lea    0xe95(%rip),%rdx
cmove  %rax,%rdx
lea    0x10(%rsp),%r15
mov    %r15,%rdi
mov    $0x100,%esi
mov    %r14,%rcx
call   1040 <snprintf@plt>
mov    0x2e51(%rip),%rax
mov    0x2e42(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e57(%rip),%rax
or     (%rax),%esi
mov    %rsp,%rcx
mov    %r15,%rdi
xor    %edx,%edx
call   1060 <glob@plt>
test   %eax,%eax
jne    11de <foreachdir+0x9e>
cmpl   $0x0,(%rsp)
jle    11de <foreachdir+0x9e>
xor    %ebx,%ebx
nopl   0x0(%rax)
mov    0x8(%rsp),%rax
mov    (%rax,%rbx,8),%rdi
mov    %r12,%rsi
call   1050 <foreachdir@plt>
add    $0x1,%rbx
movslq (%rsp),%rax
cmp    %rax,%rbx
jl     11c0 <foreachdir+0x80>
mov    %r14,%rdi
call   *%r12
add    $0x118,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.TYPE_3__ = type { i32, i8** }
@.str = [4 x i8] c"%s*\00"
@.str.1 = [5 x i8] c"%s/*\00"
@GLOB_NOESCAPE = global i32 0
@GLOB_MARK = global i32 0
@GLOB_ONLYDIR = global i32 0
define void @foreachdir(i8* %0, i32 (i8*)* %1) {
%3 = alloca i8*
%4 = alloca i32 (i8*)*
%5 = alloca [256 x i8]
%6 = alloca %struct.TYPE_3__
%7 = alloca i32
store i8* %0, i8** %3
store i32 (i8*)* %1, i32 (i8*)** %4
%8 = load i8*, i8** %3
%9 = load i8*, i8** %3
%10 = call i32 @strlen(i8* %9)
%11 = sub nsw i32 %10, 1
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds i8, i8* %8, i64 %12
%14 = load i8, i8* %13
%15 = sext i8 %14 to i32
%16 = icmp eq i32 %15, 47
br i1 %16, label %17, label %21
17: ; preds = %2
%18 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%19 = load i8*, i8** %3
%20 = call i32 @snprintf(i8* %18, i32 256, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %19)
br label %25
21: ; preds = %2
%22 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%23 = load i8*, i8** %3
%24 = call i32 @snprintf(i8* %22, i32 256, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %23)
br label %25
25: ; preds = %21, %17
%26 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%27 = load i32, i32* @GLOB_NOESCAPE
%28 = load i32, i32* @GLOB_MARK
%29 = or i32 %27, %28
%30 = load i32, i32* @GLOB_ONLYDIR
%31 = or i32 %29, %30
%32 = call i32 @glob(i8* %26, i32 %31, i32* null, %struct.TYPE_3__* %6)
%33 = icmp ne i32 %32, 0
br i1 %33, label %52, label %34
34: ; preds = %25
store i32 0, i32* %7
br label %35
35: ; preds = %48, %34
%36 = load i32, i32* %7
%37 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %6, i32 0, i32 0
%38 = load i32, i32* %37
%39 = icmp slt i32 %36, %38
br i1 %39, label %40, label %51
40: ; preds = %35
%41 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %6, i32 0, i32 1
%42 = load i8**, i8*** %41
%43 = load i32, i32* %7
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i8*, i8** %42, i64 %44
%46 = load i8*, i8** %45
%47 = load i32 (i8*)*, i32 (i8*)** %4
br label %48
48: ; preds = %40
%49 = load i32, i32* %7
%50 = add nsw i32 %49, 1
store i32 %50, i32* %7
br label %35
51: ; preds = %35
br label %52
52: ; preds = %51, %25
%53 = load i32 (i8*)*, i32 (i8*)** %4
%54 = load i8*, i8** %3
%55 = call i32 %53(i8* %54)
ret void
}

LLVM IR (-O2):
%struct.TYPE_3__ = type { i32, i8** }
@.str = [4 x i8] c"%s*\00"
@.str.1 = [5 x i8] c"%s/*\00"
@GLOB_NOESCAPE = global i32 0
@GLOB_MARK = global i32 0
@GLOB_ONLYDIR = global i32 0
define void @foreachdir(i8* %0, i32 (i8*)* nocapture %1) {
%3 = alloca [256 x i8]
%4 = alloca %struct.TYPE_3__
%5 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%6 = bitcast %struct.TYPE_3__* %4 to i8*
%7 = tail call i32 @strlen(i8* %0)
%8 = add nsw i32 %7, -1
%9 = sext i32 %8 to i64
%10 = getelementptr inbounds i8, i8* %0, i64 %9
%11 = load i8, i8* %10
%12 = icmp eq i8 %11, 47
%13 = select i1 %12, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0)
%14 = call i32 @snprintf(i8* %5, i32 256, i8* %13, i8* %0)
%15 = load i32, i32* @GLOB_NOESCAPE
%16 = load i32, i32* @GLOB_MARK
%17 = or i32 %16, %15
%18 = load i32, i32* @GLOB_ONLYDIR
%19 = or i32 %17, %18
%20 = call i32 @glob(i8* %5, i32 %19, i32* null, %struct.TYPE_3__* %4)
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %37
22: ; preds = %2
%23 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %4, i64 0, i32 0
%24 = load i32, i32* %23
%25 = icmp sgt i32 %24, 0
br i1 %25, label %26, label %37
26: ; preds = %22
%27 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %4, i64 0, i32 1
br label %28
28: ; preds = %26, %28
%29 = phi i64 [ 0, %26 ], [ %33, %28 ]
%30 = load i8**, i8*** %27
%31 = getelementptr inbounds i8*, i8** %30, i64 %29
%32 = load i8*, i8** %31
%33 = add nuw nsw i64 %29, 1
%34 = load i32, i32* %23
%35 = sext i32 %34 to i64
%36 = icmp slt i64 %33, %35
br i1 %36, label %28, label %37
37: ; preds = %28, %22, %2
%38 = call i32 %1(i8* %0)
ret void
}