#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*/ add_image(char *);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ *opendir(char const *);
int /*<<< orphan*/ perror(char const *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *, char *);
void walkdir(const char *path) {
  DIR *dp = opendir(path);
  if (!dp) {
    perror(path);
    return;
  }
  struct dirent *entry;
  while ((entry = readdir(dp))) {
    if (entry->d_name[0] == '.')
      continue;
    char filepath[257];
    snprintf(filepath, sizeof filepath, "%s/%s", path, entry->d_name);
    add_image(filepath);
  }
  closedir(dp);
}


x86 assembly (clang, -O0):
<walkdir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1195 <walkdir+0x35>
mov    -0x8(%rbp),%rdi
call   1080 <perror@plt>
jmp    1206 <walkdir+0xa6>
jmp    119a <walkdir+0x3a>
mov    -0x10(%rbp),%rdi
call   1060 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     11fd <walkdir+0x9d>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
jne    11c9 <walkdir+0x69>
jmp    119a <walkdir+0x3a>
lea    -0x120(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    (%rax),%r8
mov    $0x101,%esi
lea    0xe19(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x120(%rbp),%rdi
call   1070 <add_image@plt>
jmp    119a <walkdir+0x3a>
mov    -0x10(%rbp),%rdi
call   1050 <closedir@plt>
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<walkdir>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x108,%rsp
mov    %rdi,%r14
call   1030 <opendir@plt>
test   %rax,%rax
je     11ea <walkdir+0x8a>
mov    %rax,%rbx
mov    %rax,%rdi
call   1060 <readdir@plt>
test   %rax,%rax
je     11d3 <walkdir+0x73>
lea    0xe6e(%rip),%r15
mov    %rsp,%r12
jmp    11ad <walkdir+0x4d>
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1060 <readdir@plt>
test   %rax,%rax
je     11d3 <walkdir+0x73>
mov    (%rax),%r8
cmpb   $0x2e,(%r8)
je     11a0 <walkdir+0x40>
mov    %r12,%rdi
mov    $0x101,%esi
mov    %r15,%rdx
mov    %r14,%rcx
call   1040 <snprintf@plt>
mov    %r12,%rdi
call   1070 <add_image@plt>
jmp    11a0 <walkdir+0x40>
mov    %rbx,%rdi
call   1050 <closedir@plt>
add    $0x108,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    %r14,%rdi
add    $0x108,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1080 <perror@plt>

LLVM IR (-O0):
%struct.dirent = type { i8* }
@.str = [6 x i8] c"%s/%s\00"
define void @walkdir(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca %struct.dirent*
%5 = alloca [257 x i8]
store i8* %0, i8** %2
%6 = load i8*, i8** %2
%7 = call i32* @opendir(i8* %6)
store i32* %7, i32** %3
%8 = load i32*, i32** %3
%9 = icmp ne i32* %8, null
br i1 %9, label %13, label %10
10: ; preds = %1
%11 = load i8*, i8** %2
%12 = call i32 @perror(i8* %11)
br label %39
13: ; preds = %1
br label %14
14: ; preds = %27, %26, %13
%15 = load i32*, i32** %3
%16 = call %struct.dirent* @readdir(i32* %15)
store %struct.dirent* %16, %struct.dirent** %4
%17 = icmp ne %struct.dirent* %16, null
br i1 %17, label %18, label %36
18: ; preds = %14
%19 = load %struct.dirent*, %struct.dirent** %4
%20 = getelementptr inbounds %struct.dirent, %struct.dirent* %19, i32 0, i32 0
%21 = load i8*, i8** %20
%22 = getelementptr inbounds i8, i8* %21, i64 0
%23 = load i8, i8* %22
%24 = sext i8 %23 to i32
%25 = icmp eq i32 %24, 46
br i1 %25, label %26, label %27
26: ; preds = %18
br label %14
27: ; preds = %18
%28 = getelementptr inbounds [257 x i8], [257 x i8]* %5, i64 0, i64 0
%29 = load i8*, i8** %2
%30 = load %struct.dirent*, %struct.dirent** %4
%31 = getelementptr inbounds %struct.dirent, %struct.dirent* %30, i32 0, i32 0
%32 = load i8*, i8** %31
%33 = call i32 @snprintf(i8* %28, i32 257, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %29, i8* %32)
%34 = getelementptr inbounds [257 x i8], [257 x i8]* %5, i64 0, i64 0
%35 = call i32 @add_image(i8* %34)
br label %14
36: ; preds = %14
%37 = load i32*, i32** %3
%38 = call i32 @closedir(i32* %37)
br label %39
39: ; preds = %36, %10
ret void
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [6 x i8] c"%s/%s\00"
define void @walkdir(i8* %0) {
%2 = alloca [257 x i8]
%3 = tail call i32* @opendir(i8* %0)
%4 = icmp eq i32* %3, null
br i1 %4, label %10, label %5
5: ; preds = %1
%6 = call %struct.dirent* @readdir(i32* %3)
%7 = icmp eq %struct.dirent* %6, null
br i1 %7, label %24, label %8
8: ; preds = %5
%9 = getelementptr inbounds [257 x i8], [257 x i8]* %2, i64 0, i64 0
br label %12
10: ; preds = %1
%11 = tail call i32 @perror(i8* %0)
br label %26
12: ; preds = %8, %21
%13 = phi %struct.dirent* [ %6, %8 ], [ %22, %21 ]
%14 = getelementptr inbounds %struct.dirent, %struct.dirent* %13, i64 0, i32 0
%15 = load i8*, i8** %14
%16 = load i8, i8* %15
%17 = icmp eq i8 %16, 46
br i1 %17, label %21, label %18
18: ; preds = %12
%19 = call i32 @snprintf(i8* %9, i32 257, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %15)
%20 = call i32 @add_image(i8* %9)
br label %21
21: ; preds = %18, %12
%22 = call %struct.dirent* @readdir(i32* %3)
%23 = icmp eq %struct.dirent* %22, null
br i1 %23, label %24, label %12
24: ; preds = %21, %5
%25 = call i32 @closedir(i32* %3)
br label %26
26: ; preds = %24, %10
ret void
}