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

/* Variables and functions */
int foreachdir_rec(char const *, struct stat *,
                   int (*)(char const *, struct stat const *));
scalar_t__ lstat(char const *, struct stat *);
int foreachdir(const char *path,
               int (*walk)(const char *, const struct stat *)) {
  struct stat st;
  if (lstat(path, &st))
    return walk(path, NULL);
  return foreachdir_rec(path, &st, walk);
}


x86 assembly (clang, -O0):
<foreachdir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1030 <lstat@plt>
cmp    $0x0,%rax
je     115d <foreachdir+0x3d>
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%rdi
xor    %ecx,%ecx
mov    %ecx,%esi
call   *%rax
mov    %eax,-0x4(%rbp)
jmp    1171 <foreachdir+0x51>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    -0x20(%rbp),%rsi
call   1040 <foreachdir_rec@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<foreachdir>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
mov    %rsp,%rsi
call   1030 <lstat@plt>
test   %rax,%rax
je     1148 <foreachdir+0x28>
mov    %rbx,%rdi
xor    %esi,%esi
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    *%rax
mov    %rsp,%rsi
mov    %rbx,%rdi
mov    %r14,%rdx
call   1040 <foreachdir_rec@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
define i32 @foreachdir(i8* %0, i32 (i8*, %struct.stat*)* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32 (i8*, %struct.stat*)*
%6 = alloca %struct.stat
store i8* %0, i8** %4
store i32 (i8*, %struct.stat*)* %1, i32 (i8*, %struct.stat*)** %5
%7 = load i8*, i8** %4
%8 = call i64 @lstat(i8* %7, %struct.stat* %6)
%9 = icmp ne i64 %8, 0
br i1 %9, label %10, label %14
10: ; preds = %2
%11 = load i32 (i8*, %struct.stat*)*, i32 (i8*, %struct.stat*)** %5
%12 = load i8*, i8** %4
%13 = call i32 %11(i8* %12, %struct.stat* null)
store i32 %13, i32* %3
br label %18
14: ; preds = %2
%15 = load i8*, i8** %4
%16 = load i32 (i8*, %struct.stat*)*, i32 (i8*, %struct.stat*)** %5
%17 = call i32 @foreachdir_rec(i8* %15, %struct.stat* %6, i32 (i8*, %struct.stat*)* %16)
store i32 %17, i32* %3
br label %18
18: ; preds = %14, %10
%19 = load i32, i32* %3
ret i32 %19
}

LLVM IR (-O2):
%struct.stat = type { i32 }
define i32 @foreachdir(i8* %0, i32 (i8*, %struct.stat*)* %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = call i64 @lstat(i8* %0, %struct.stat* %3)
%6 = icmp eq i64 %5, 0
br i1 %6, label %9, label %7
7: ; preds = %2
%8 = tail call i32 %1(i8* %0, %struct.stat* null)
br label %11
9: ; preds = %2
%10 = call i32 @foreachdir_rec(i8* %0, %struct.stat* %3, i32 (i8*, %struct.stat*)* %1)
br label %11
11: ; preds = %9, %7
%12 = phi i32 [ %8, %7 ], [ %10, %9 ]
ret i32 %12
}