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

/* Variables and functions */
int /*<<< orphan*/ PATH_MAX;
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ *opendir(char const *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ realpath(int /*<<< orphan*/ *, char *);
scalar_t__ strcmp(char *, int /*<<< orphan*/ *);
int strlen(int /*<<< orphan*/ *);
void read_dir(const char *directory, void (*file_action)(const char *)) {
  uint length;
  struct dirent *dirent;
  DIR *dir;
  dir = opendir(directory);
  if (dir != NULL) {
    while ((dirent = readdir(dir)) != NULL) {
      length = strlen(dirent->d_name);
      if (length >= 4) {
        if (strcmp(".bmp", &(dirent->d_name[length - 4])) == 0) {
          char path[PATH_MAX + 1];
          realpath(dirent->d_name, path);
          file_action(path);
        }
      }
    }
    closedir(dir);
  }
}


x86 assembly (clang, -O0):
<read_dir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1245 <read_dir+0xe5>
jmp    118d <read_dir+0x2d>
mov    -0x28(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     123c <read_dir+0xdc>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x4,-0x14(%rbp)
jl     1237 <read_dir+0xd7>
mov    -0x20(%rbp),%rax
mov    (%rax),%rsi
mov    -0x14(%rbp),%eax
sub    $0x4,%eax
cltq
shl    $0x2,%rax
add    %rax,%rsi
lea    0xe26(%rip),%rdi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    1232 <read_dir+0xd2>
mov    0x2e00(%rip),%rax
mov    (%rax),%eax
add    $0x1,%eax
mov    %rsp,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rsi
sub    %rcx,%rsi
mov    %rsi,-0x40(%rbp)
mov    %rsi,%rsp
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
call   1070 <realpath@plt>
mov    -0x40(%rbp),%rdi
call   *-0x10(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
jmp    1237 <read_dir+0xd7>
jmp    118d <read_dir+0x2d>
mov    -0x28(%rbp),%rdi
call   1050 <closedir@plt>
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_dir>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,-0x30(%rbp)
call   1030 <opendir@plt>
test   %rax,%rax
je     1215 <read_dir+0xb5>
mov    %rax,%r12
mov    %rax,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
je     120d <read_dir+0xad>
mov    %rax,%rbx
lea    0xe62(%rip),%r15
jmp    11b0 <read_dir+0x50>
mov    %r12,%rdi
call   1080 <readdir@plt>
mov    %rax,%rbx
test   %rax,%rax
je     120d <read_dir+0xad>
mov    (%rbx),%rdi
call   1040 <strlen@plt>
cmp    $0x4,%eax
jl     11a0 <read_dir+0x40>
add    $0xfffffffc,%eax
shl    $0x2,%rax
add    (%rbx),%rax
mov    %r15,%rdi
mov    %rax,%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
jne    11a0 <read_dir+0x40>
mov    %rsp,%r14
mov    0x2e0f(%rip),%rax
mov    (%rax),%eax
add    $0x1,%eax
mov    %rsp,%r13
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r13
mov    %r13,%rsp
mov    (%rbx),%rdi
mov    %r13,%rsi
call   1070 <realpath@plt>
mov    %r13,%rdi
call   *-0x30(%rbp)
mov    %r14,%rsp
jmp    11a0 <read_dir+0x40>
mov    %r12,%rdi
call   1050 <closedir@plt>
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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