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

/* Variables and functions */
scalar_t__ DT_DIR;
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, char const *, char const *);
int /*<<< orphan*/ strcmp(char *, char const *);
scalar_t__ strlen(char const *);
char *xmalloc(scalar_t__);
int /*<<< orphan*/ *xopendir(char const *);
void apply(const char *path, void (*func)(const char *)) {
  DIR *d;
  struct dirent *e;
  char *newpath;
  d = xopendir(path);
  while ((e = readdir(d)) != NULL) {
    if (e->d_type == DT_DIR &&
        (!strcmp(".", e->d_name) || !strcmp("..", e->d_name))) {
      continue;
    }
    newpath = xmalloc(strlen(path) + strlen(e->d_name) + 2);
    sprintf(newpath, "%s/%s", path, e->d_name);
    func(newpath);
    free(newpath);
  }
  closedir(d);
}


x86 assembly (clang, -O0):
<apply>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1090 <xopendir@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     126c <apply+0xec>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    0x2e1e(%rip),%rcx
cmp    (%rcx),%rax
jne    120a <apply+0x8a>
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe27(%rip),%rdi
call   1070 <strcmp@plt>
cmp    $0x0,%eax
je     1205 <apply+0x85>
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe09(%rip),%rdi
call   1070 <strcmp@plt>
cmp    $0x0,%eax
jne    120a <apply+0x8a>
jmp    119d <apply+0x1d>
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%rdi
call   1040 <strlen@plt>
mov    -0x30(%rbp),%rdi
add    %rax,%rdi
add    $0x2,%rdi
call   1050 <xmalloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%rcx
lea    0xdb4(%rip),%rsi
call   10a0 <sprintf@plt>
mov    -0x10(%rbp),%rax
mov    -0x28(%rbp),%rdi
call   *%rax
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
jmp    119d <apply+0x1d>
mov    -0x18(%rbp),%rdi
call   1060 <closedir@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<apply>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
call   1090 <xopendir@plt>
mov    %rax,%r15
mov    %rax,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
je     1247 <apply+0xc7>
mov    %rax,%r13
jmp    1207 <apply+0x87>
nop
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %rax,%rbp
mov    %r12,%rdi
call   1040 <strlen@plt>
lea    (%rax,%rbp,1),%rdi
add    $0x2,%rdi
call   1050 <xmalloc@plt>
mov    %rax,%rbp
mov    0x8(%r13),%rcx
mov    %rax,%rdi
lea    0xe22(%rip),%rsi
mov    %rbx,%rdx
call   10a0 <sprintf@plt>
mov    %rbp,%rdi
call   *%r14
mov    %rbp,%rdi
call   1030 <free@plt>
mov    %r15,%rdi
call   1080 <readdir@plt>
mov    %rax,%r13
test   %rax,%rax
je     1247 <apply+0xc7>
mov    0x0(%r13),%rax
mov    0x8(%r13),%r12
mov    0x2dca(%rip),%rcx
cmp    (%rcx),%rax
jne    11b0 <apply+0x30>
lea    0xddf(%rip),%rdi
mov    %r12,%rsi
call   1070 <strcmp@plt>
test   %eax,%eax
je     11f7 <apply+0x77>
lea    0xdcb(%rip),%rdi
mov    %r12,%rsi
call   1070 <strcmp@plt>
test   %eax,%eax
jne    11b0 <apply+0x30>
jmp    11f7 <apply+0x77>
mov    %r15,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <closedir@plt>

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

LLVM IR (-O2):
%struct.dirent = type { i64, i8* }
@DT_DIR = global i64 0
@.str = [2 x i8] c".\00"
@.str.1 = [3 x i8] c"..\00"
@.str.2 = [6 x i8] c"%s/%s\00"
define void @apply(i8* %0, void (i8*)* nocapture %1) {
%3 = tail call i32* @xopendir(i8* %0)
%4 = tail call %struct.dirent* @readdir(i32* %3)
%5 = icmp eq %struct.dirent* %4, null
br i1 %5, label %33, label %6
6: ; preds = %2, %20
%7 = phi %struct.dirent* [ %21, %20 ], [ %4, %2 ]
%8 = getelementptr inbounds %struct.dirent, %struct.dirent* %7, i64 0, i32 0
%9 = load i64, i64* %8
%10 = load i64, i64* @DT_DIR
%11 = icmp eq i64 %9, %10
%12 = getelementptr inbounds %struct.dirent, %struct.dirent* %7, i64 0, i32 1
%13 = load i8*, i8** %12
br i1 %11, label %14, label %23
14: ; preds = %6
%15 = tail call i32 @strcmp(i8* dereferenceable(2) getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8* dereferenceable(1) %13)
%16 = icmp eq i32 %15, 0
br i1 %16, label %20, label %17
17: ; preds = %14
%18 = tail call i32 @strcmp(i8* dereferenceable(3) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* dereferenceable(1) %13)
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %14, %17, %23
%21 = tail call %struct.dirent* @readdir(i32* %3)
%22 = icmp eq %struct.dirent* %21, null
br i1 %22, label %33, label %6
23: ; preds = %6, %17
%24 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%25 = getelementptr inbounds %struct.dirent, %struct.dirent* %7, i64 0, i32 1
%26 = tail call i64 @strlen(i8* dereferenceable(1) %13)
%27 = add i64 %24, 2
%28 = add i64 %27, %26
%29 = tail call i8* @xmalloc(i64 %28)
%30 = load i8*, i8** %25
%31 = tail call i32 @sprintf(i8* dereferenceable(1) %29, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %0, i8* %30)
tail call void %1(i8* %29)
%32 = tail call i32 @free(i8* %29)
br label %20
33: ; preds = %20, %2
%34 = tail call i32 @closedir(i32* %3)
ret void
}