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

/* Variables and functions */
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ file_process(int /*<<< orphan*/, char *);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ perror(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
void directory_process(char *directory) {
  DIR *dir;
  if ((dir = opendir(directory)) == NULL) {
    perror(directory);
    return;
  }
  struct dirent *entry;
  while ((entry = readdir(dir)) != NULL) {
    file_process(entry->d_name, directory);
  }
  closedir(dir);
}


x86 assembly (clang, -O0):
<directory_process>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    1181 <directory_process+0x31>
mov    -0x8(%rbp),%rdi
call   1060 <perror@plt>
jmp    11ba <directory_process+0x6a>
jmp    1186 <directory_process+0x36>
mov    -0x10(%rbp),%rdi
call   1050 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     11b1 <directory_process+0x61>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rsi
call   1070 <file_process@plt>
jmp    1186 <directory_process+0x36>
mov    -0x10(%rbp),%rdi
call   1040 <closedir@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<directory_process>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
call   1030 <opendir@plt>
test   %rax,%rax
je     1198 <directory_process+0x48>
mov    %rax,%rbx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rbx,%rdi
call   1050 <readdir@plt>
test   %rax,%rax
je     1189 <directory_process+0x39>
mov    (%rax),%edi
mov    %r14,%rsi
call   1070 <file_process@plt>
jmp    1170 <directory_process+0x20>
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1040 <closedir@plt>
mov    %r14,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1060 <perror@plt>

LLVM IR (-O0):
%struct.dirent = type { i32 }
define void @directory_process(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca %struct.dirent*
store i8* %0, i8** %2
%5 = load i8*, i8** %2
%6 = call i32* @opendir(i8* %5)
store i32* %6, i32** %3
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %11
8: ; preds = %1
%9 = load i8*, i8** %2
%10 = call i32 @perror(i8* %9)
br label %25
11: ; preds = %1
br label %12
12: ; preds = %16, %11
%13 = load i32*, i32** %3
%14 = call %struct.dirent* @readdir(i32* %13)
store %struct.dirent* %14, %struct.dirent** %4
%15 = icmp ne %struct.dirent* %14, null
br i1 %15, label %16, label %22
16: ; preds = %12
%17 = load %struct.dirent*, %struct.dirent** %4
%18 = getelementptr inbounds %struct.dirent, %struct.dirent* %17, i32 0, i32 0
%19 = load i32, i32* %18
%20 = load i8*, i8** %2
%21 = call i32 @file_process(i32 %19, i8* %20)
br label %12
22: ; preds = %12
%23 = load i32*, i32** %3
%24 = call i32 @closedir(i32* %23)
br label %25
25: ; preds = %22, %8
ret void
}

LLVM IR (-O2):
%struct.dirent = type { i32 }
define void @directory_process(i8* %0) {
%2 = tail call i32* @opendir(i8* %0)
%3 = icmp eq i32* %2, null
br i1 %3, label %7, label %4
4: ; preds = %1
%5 = tail call %struct.dirent* @readdir(i32* %2)
%6 = icmp eq %struct.dirent* %5, null
br i1 %6, label %16, label %9
7: ; preds = %1
%8 = tail call i32 @perror(i8* %0)
br label %18
9: ; preds = %4, %9
%10 = phi %struct.dirent* [ %14, %9 ], [ %5, %4 ]
%11 = getelementptr inbounds %struct.dirent, %struct.dirent* %10, i64 0, i32 0
%12 = load i32, i32* %11
%13 = tail call i32 @file_process(i32 %12, i8* %0)
%14 = tail call %struct.dirent* @readdir(i32* %2)
%15 = icmp eq %struct.dirent* %14, null
br i1 %15, label %16, label %9
16: ; preds = %9, %4
%17 = tail call i32 @closedir(i32* %2)
br label %18
18: ; preds = %16, %7
ret void
}