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

/* Variables and functions */
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ concat_path(char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
scalar_t__ strcmp(char *, char *);
char *strrchr(int /*<<< orphan*/, char);
int pfan_read_dir(char *path, char img_paths[8][1024], uint8_t effects[8][3]) {
  struct dirent *dp;
  DIR *dir = opendir(path);
  int n = 0;
  if (!dir)
    return -1;
  while (n < 8 && (dp = readdir(dir)) != NULL) {
    char *ext = strrchr(dp->d_name, '.');
    if (!ext)
      continue;
    if (strcmp(ext, ".pbm") == 0) {
      concat_path(img_paths[n], path, dp->d_name);
      ++n;
    }
  }
  closedir(dir);
  return n;
}


x86 assembly (clang, -O0):
<pfan_read_dir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    119f <pfan_read_dir+0x3f>
movl   $0xffffffff,-0x4(%rbp)
jmp    1253 <pfan_read_dir+0xf3>
jmp    11a4 <pfan_read_dir+0x44>
xor    %eax,%eax
cmpl   $0x8,-0x34(%rbp)
mov    %al,-0x41(%rbp)
jge    11ca <pfan_read_dir+0x6a>
mov    -0x30(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
setne  %al
mov    %al,-0x41(%rbp)
mov    -0x41(%rbp),%al
test   $0x1,%al
jne    11da <pfan_read_dir+0x7a>
jmp    1244 <pfan_read_dir+0xe4>
mov    -0x28(%rbp),%rax
mov    (%rax),%edi
mov    $0x2e,%esi
call   1050 <strrchr@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    11fe <pfan_read_dir+0x9e>
jmp    11a4 <pfan_read_dir+0x44>
mov    -0x40(%rbp),%rdi
lea    0xdf7(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
jne    123f <pfan_read_dir+0xdf>
mov    -0x18(%rbp),%rdi
movslq -0x34(%rbp),%rax
shl    $0xa,%rax
add    %rax,%rdi
mov    -0x10(%rbp),%rsi
mov    -0x28(%rbp),%rax
mov    (%rax),%edx
call   1030 <concat_path@plt>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    11a4 <pfan_read_dir+0x44>
mov    -0x30(%rbp),%rdi
call   1060 <closedir@plt>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pfan_read_dir>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %rdi,%r12
call   1040 <opendir@plt>
test   %rax,%rax
je     11ed <pfan_read_dir+0x8d>
mov    %rax,%r14
xor    %ebx,%ebx
lea    0xe79(%rip),%r13
jmp    1195 <pfan_read_dir+0x35>
nopl   0x0(%rax)
cmp    $0x8,%ebx
jge    11e3 <pfan_read_dir+0x83>
mov    %r14,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
je     11e3 <pfan_read_dir+0x83>
mov    %rax,%rbp
mov    (%rax),%edi
mov    $0x2e,%esi
call   1050 <strrchr@plt>
test   %rax,%rax
je     1190 <pfan_read_dir+0x30>
mov    %rax,%rdi
mov    %r13,%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
jne    1190 <pfan_read_dir+0x30>
movslq %ebx,%rbx
mov    %rbx,%rdi
shl    $0xa,%rdi
add    %r15,%rdi
mov    0x0(%rbp),%edx
mov    %r12,%rsi
call   1030 <concat_path@plt>
add    $0x1,%ebx
jmp    1190 <pfan_read_dir+0x30>
mov    %r14,%rdi
call   1060 <closedir@plt>
jmp    11f2 <pfan_read_dir+0x92>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%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".pbm\00"
define i32 @pfan_read_dir(i8* %0, [1024 x i8]* %1, [3 x i32]* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca [1024 x i8]*
%7 = alloca [3 x i32]*
%8 = alloca %struct.dirent*
%9 = alloca i32*
%10 = alloca i32
%11 = alloca i8*
store i8* %0, i8** %5
store [1024 x i8]* %1, [1024 x i8]** %6
store [3 x i32]* %2, [3 x i32]** %7
%12 = load i8*, i8** %5
%13 = call i32* @opendir(i8* %12)
store i32* %13, i32** %9
store i32 0, i32* %10
%14 = load i32*, i32** %9
%15 = icmp ne i32* %14, null
br i1 %15, label %17, label %16
16: ; preds = %3
store i32 -1, i32* %4
br label %57
17: ; preds = %3
br label %18
18: ; preds = %52, %34, %17
%19 = load i32, i32* %10
%20 = icmp slt i32 %19, 8
br i1 %20, label %21, label %25
21: ; preds = %18
%22 = load i32*, i32** %9
%23 = call %struct.dirent* @readdir(i32* %22)
store %struct.dirent* %23, %struct.dirent** %8
%24 = icmp ne %struct.dirent* %23, null
br label %25
25: ; preds = %21, %18
%26 = phi i1 [ false, %18 ], [ %24, %21 ]
br i1 %26, label %27, label %53
27: ; preds = %25
%28 = load %struct.dirent*, %struct.dirent** %8
%29 = getelementptr inbounds %struct.dirent, %struct.dirent* %28, i32 0, i32 0
%30 = load i32, i32* %29
%31 = call i8* @strrchr(i32 %30, i8 signext 46)
store i8* %31, i8** %11
%32 = load i8*, i8** %11
%33 = icmp ne i8* %32, null
br i1 %33, label %35, label %34
34: ; preds = %27
br label %18
35: ; preds = %27
%36 = load i8*, i8** %11
%37 = call i64 @strcmp(i8* %36, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%38 = icmp eq i64 %37, 0
br i1 %38, label %39, label %52
39: ; preds = %35
%40 = load [1024 x i8]*, [1024 x i8]** %6
%41 = load i32, i32* %10
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds [1024 x i8], [1024 x i8]* %40, i64 %42
%44 = getelementptr inbounds [1024 x i8], [1024 x i8]* %43, i64 0, i64 0
%45 = load i8*, i8** %5
%46 = load %struct.dirent*, %struct.dirent** %8
%47 = getelementptr inbounds %struct.dirent, %struct.dirent* %46, i32 0, i32 0
%48 = load i32, i32* %47
%49 = call i32 @concat_path(i8* %44, i8* %45, i32 %48)
%50 = load i32, i32* %10
%51 = add nsw i32 %50, 1
store i32 %51, i32* %10
br label %52
52: ; preds = %39, %35
br label %18
53: ; preds = %25
%54 = load i32*, i32** %9
%55 = call i32 @closedir(i32* %54)
%56 = load i32, i32* %10
store i32 %56, i32* %4
br label %57
57: ; preds = %53, %16
%58 = load i32, i32* %4
ret i32 %58
}

LLVM IR (-O2):
%struct.dirent = type { i32 }
@.str = [5 x i8] c".pbm\00"
define i32 @pfan_read_dir(i8* %0, [1024 x i8]* %1, [3 x i32]* nocapture readnone %2) {
%4 = tail call i32* @opendir(i8* %0)
%5 = icmp eq i32* %4, null
br i1 %5, label %30, label %6
6: ; preds = %3, %24
%7 = phi i32 [ %25, %24 ], [ 0, %3 ]
%8 = tail call %struct.dirent* @readdir(i32* %4)
%9 = icmp eq %struct.dirent* %8, null
br i1 %9, label %27, label %10
10: ; preds = %6
%11 = getelementptr inbounds %struct.dirent, %struct.dirent* %8, i64 0, i32 0
%12 = load i32, i32* %11
%13 = tail call i8* @strrchr(i32 %12, i8 signext 46)
%14 = icmp eq i8* %13, null
br i1 %14, label %24, label %15
15: ; preds = %10
%16 = tail call i64 @strcmp(i8* %13, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %24
18: ; preds = %15
%19 = sext i32 %7 to i64
%20 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 %19, i64 0
%21 = load i32, i32* %11
%22 = tail call i32 @concat_path(i8* %20, i8* %0, i32 %21)
%23 = add nsw i32 %7, 1
br label %24
24: ; preds = %15, %18, %10
%25 = phi i32 [ %7, %10 ], [ %23, %18 ], [ %7, %15 ]
%26 = icmp slt i32 %25, 8
br i1 %26, label %6, label %27
27: ; preds = %24, %6
%28 = phi i32 [ %25, %24 ], [ %7, %6 ]
%29 = tail call i32 @closedir(i32* %4)
br label %30
30: ; preds = %3, %27
%31 = phi i32 [ %28, %27 ], [ -1, %3 ]
ret i32 %31
}