#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 {
  char *d_name;
};
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
int /*<<< orphan*/ asprintf(char **, char *, char const *, char *);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
char *has_library_ext(char *, char *);
int /*<<< orphan*/ *opendir(char const *);
struct dirent *readdir(int /*<<< orphan*/ *);
int foreach_dir(const char *path, int (*func)(const char *filename, void *data),
                void *data) {
  DIR *dirp = 0;
  int errors = 0;
  dirp = opendir(path);
  if (dirp) {
    struct dirent *dp = 0;
    while ((dp = readdir(dirp)))
      if (dp->d_name[0] != '.') {
        char *fullname = NULL;
        asprintf(&fullname, "%s/%s", path, dp->d_name);
        char *ext = has_library_ext(fullname, ".so");
        if (ext)
          *ext = '\0';
        int rc = func(fullname, data);
        free(fullname);
        if (rc)
          ++errors;
      }
    closedir(dirp);
  } else
    ++errors;
  return errors;
}


x86 assembly (clang, -O0):
<foreach_dir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     125b <foreach_dir+0xfb>
movq   $0x0,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
call   1060 <readdir@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
je     124d <foreach_dir+0xed>
mov    -0x30(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
je     1248 <foreach_dir+0xe8>
movq   $0x0,-0x38(%rbp)
mov    -0x8(%rbp),%rdx
mov    -0x30(%rbp),%rax
mov    (%rax),%rcx
lea    -0x38(%rbp),%rdi
lea    0xe15(%rip),%rsi
call   1070 <asprintf@plt>
mov    -0x38(%rbp),%rdi
lea    0xe0b(%rip),%rsi
call   1080 <has_library_ext@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
je     1216 <foreach_dir+0xb6>
mov    -0x40(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rax
mov    -0x38(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   *%rax
mov    %eax,-0x44(%rbp)
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
cmpl   $0x0,-0x44(%rbp)
je     1243 <foreach_dir+0xe3>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1248 <foreach_dir+0xe8>
jmp    11a3 <foreach_dir+0x43>
mov    -0x20(%rbp),%rdi
call   1050 <closedir@plt>
jmp    1264 <foreach_dir+0x104>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<foreach_dir>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r12
call   1040 <opendir@plt>
test   %rax,%rax
je     1213 <foreach_dir+0xb3>
mov    %rax,%rbx
mov    %rax,%rdi
call   1060 <readdir@plt>
xor    %ebp,%ebp
jmp    11c5 <foreach_dir+0x65>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    (%rsp),%rdi
mov    %r14,%rsi
call   *%r15
mov    %eax,%r13d
mov    (%rsp),%rdi
call   1030 <free@plt>
cmp    $0x1,%r13d
sbb    $0xffffffff,%ebp
mov    %rbx,%rdi
call   1060 <readdir@plt>
test   %rax,%rax
je     1209 <foreach_dir+0xa9>
mov    (%rax),%rcx
cmpb   $0x2e,(%rcx)
je     11bd <foreach_dir+0x5d>
movq   $0x0,(%rsp)
mov    (%rax),%rcx
mov    %rsp,%rdi
lea    0xe19(%rip),%rsi
mov    %r12,%rdx
call   1070 <asprintf@plt>
mov    (%rsp),%rdi
lea    0xe0c(%rip),%rsi
call   1080 <has_library_ext@plt>
test   %rax,%rax
je     11a0 <foreach_dir+0x40>
movb   $0x0,(%rax)
jmp    11a0 <foreach_dir+0x40>
mov    %rbx,%rdi
call   1050 <closedir@plt>
jmp    1218 <foreach_dir+0xb8>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i8* }
@.str = [6 x i8] c"%s/%s\00"
@.str.1 = [4 x i8] c".so\00"
define i32 @foreach_dir(i8* %0, i32 (i8*, i8*)* %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i32 (i8*, i8*)*
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca i32
%9 = alloca %struct.dirent*
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i32
store i8* %0, i8** %4
store i32 (i8*, i8*)* %1, i32 (i8*, i8*)** %5
store i8* %2, i8** %6
store i32* null, i32** %7
store i32 0, i32* %8
%13 = load i8*, i8** %4
%14 = call i32* @opendir(i8* %13)
store i32* %14, i32** %7
%15 = load i32*, i32** %7
%16 = icmp ne i32* %15, null
br i1 %16, label %17, label %59
17: ; preds = %3
store %struct.dirent* null, %struct.dirent** %9
br label %18
18: ; preds = %55, %17
%19 = load i32*, i32** %7
%20 = call %struct.dirent* @readdir(i32* %19)
store %struct.dirent* %20, %struct.dirent** %9
%21 = icmp ne %struct.dirent* %20, null
br i1 %21, label %22, label %56
22: ; preds = %18
%23 = load %struct.dirent*, %struct.dirent** %9
%24 = getelementptr inbounds %struct.dirent, %struct.dirent* %23, i32 0, i32 0
%25 = load i8*, i8** %24
%26 = getelementptr inbounds i8, i8* %25, i64 0
%27 = load i8, i8* %26
%28 = sext i8 %27 to i32
%29 = icmp ne i32 %28, 46
br i1 %29, label %30, label %55
30: ; preds = %22
store i8* null, i8** %10
%31 = load i8*, i8** %4
%32 = load %struct.dirent*, %struct.dirent** %9
%33 = getelementptr inbounds %struct.dirent, %struct.dirent* %32, i32 0, i32 0
%34 = load i8*, i8** %33
%35 = call i32 @asprintf(i8** %10, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %31, i8* %34)
%36 = load i8*, i8** %10
%37 = call i8* @has_library_ext(i8* %36, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
store i8* %37, i8** %11
%38 = load i8*, i8** %11
%39 = icmp ne i8* %38, null
br i1 %39, label %40, label %42
40: ; preds = %30
%41 = load i8*, i8** %11
store i8 0, i8* %41
br label %42
42: ; preds = %40, %30
%43 = load i32 (i8*, i8*)*, i32 (i8*, i8*)** %5
%44 = load i8*, i8** %10
%45 = load i8*, i8** %6
%46 = call i32 %43(i8* %44, i8* %45)
store i32 %46, i32* %12
%47 = load i8*, i8** %10
%48 = call i32 @free(i8* %47)
%49 = load i32, i32* %12
%50 = icmp ne i32 %49, 0
br i1 %50, label %51, label %54
51: ; preds = %42
%52 = load i32, i32* %8
%53 = add nsw i32 %52, 1
store i32 %53, i32* %8
br label %54
54: ; preds = %51, %42
br label %55
55: ; preds = %54, %22
br label %18
56: ; preds = %18
%57 = load i32*, i32** %7
%58 = call i32 @closedir(i32* %57)
br label %62
59: ; preds = %3
%60 = load i32, i32* %8
%61 = add nsw i32 %60, 1
store i32 %61, i32* %8
br label %62
62: ; preds = %59, %56
%63 = load i32, i32* %8
ret i32 %63
}

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