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

/* Variables and functions */
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
scalar_t__ extractPak(char *, int /*<<< orphan*/);
int /*<<< orphan*/ *opendir(char const *);
int /*<<< orphan*/ perror(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *, char *);
scalar_t__ strcasecmp(char *, char *);
scalar_t__ strcmp(char *, char *);
char *strrchr(char *, char);
int extractPakInDir(const char *path) {
  DIR *dir;
  struct dirent *ent;
  char *ext;
  char filepath[1024];
  int total = 0;
  dir = opendir(path);
  if (dir == NULL) {
    perror("opendir");
    return -1;
  }
  while ((ent = readdir(dir)) != NULL) {
    if (strcmp(ent->d_name, "..") == 0 || strcmp(ent->d_name, ".") == 0)
      continue;
    ext = strrchr(ent->d_name, '.');
    if (ext != NULL) {
      if (strcasecmp(ext, ".PAK") == 0) {
        snprintf(filepath, sizeof(filepath), "%s/%s", path, ent->d_name);
        total += extractPak(filepath, 0);
      }
    }
  }
  closedir(dir);
  return total;
}


x86 assembly (clang, -O0):
<extractPakInDir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x434(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11d9 <extractPakInDir+0x49>
lea    0xe38(%rip),%rdi
call   10b0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ce <extractPakInDir+0x13e>
jmp    11de <extractPakInDir+0x4e>
mov    -0x18(%rbp),%rdi
call   10a0 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     12bc <extractPakInDir+0x12c>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
lea    0xe05(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%rax
je     122f <extractPakInDir+0x9f>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
lea    0xde9(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%rax
jne    1234 <extractPakInDir+0xa4>
jmp    11de <extractPakInDir+0x4e>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
mov    $0x2e,%esi
call   1070 <strrchr@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     12b7 <extractPakInDir+0x127>
mov    -0x28(%rbp),%rdi
lea    0xdac(%rip),%rsi
call   1030 <strcasecmp@plt>
cmp    $0x0,%rax
jne    12b2 <extractPakInDir+0x122>
lea    -0x430(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    (%rax),%r8
mov    $0x400,%esi
lea    0xd84(%rip),%rdx
call   1060 <snprintf@plt>
lea    -0x430(%rbp),%rdi
xor    %esi,%esi
call   1050 <extractPak@plt>
mov    %rax,%rcx
movslq -0x434(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x434(%rbp)
jmp    12b7 <extractPakInDir+0x127>
jmp    11de <extractPakInDir+0x4e>
mov    -0x18(%rbp),%rdi
call   1080 <closedir@plt>
mov    -0x434(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<extractPakInDir>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x408,%rsp
mov    %rdi,%r14
call   1040 <opendir@plt>
test   %rax,%rax
je     1288 <extractPakInDir+0xf8>
mov    %rax,%rbx
mov    %rax,%rdi
call   10a0 <readdir@plt>
test   %rax,%rax
je     1268 <extractPakInDir+0xd8>
mov    %rax,%r13
xor    %r15d,%r15d
lea    0xe35(%rip),%rbp
lea    0xe2f(%rip),%r12
jmp    11f0 <extractPakInDir+0x60>
nopl   0x0(%rax)
mov    %rbx,%rdi
call   10a0 <readdir@plt>
mov    %rax,%r13
test   %rax,%rax
je     126b <extractPakInDir+0xdb>
mov    0x0(%r13),%rdi
mov    %rbp,%rsi
call   1090 <strcmp@plt>
test   %rax,%rax
je     11e0 <extractPakInDir+0x50>
mov    0x0(%r13),%rdi
mov    %r12,%rsi
call   1090 <strcmp@plt>
test   %rax,%rax
je     11e0 <extractPakInDir+0x50>
mov    0x0(%r13),%r13
mov    %r13,%rdi
mov    $0x2e,%esi
call   1070 <strrchr@plt>
test   %rax,%rax
je     11e0 <extractPakInDir+0x50>
mov    %rax,%rdi
lea    0xdd9(%rip),%rsi
call   1030 <strcasecmp@plt>
test   %rax,%rax
jne    11e0 <extractPakInDir+0x50>
mov    %rsp,%rdi
mov    $0x400,%esi
lea    0xdc5(%rip),%rdx
mov    %r14,%rcx
mov    %r13,%r8
call   1060 <snprintf@plt>
mov    %rsp,%rdi
xor    %esi,%esi
call   1050 <extractPak@plt>
add    %eax,%r15d
jmp    11e0 <extractPakInDir+0x50>
xor    %r15d,%r15d
mov    %rbx,%rdi
call   1080 <closedir@plt>
mov    %r15d,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd71(%rip),%rdi
call   10b0 <perror@plt>
mov    $0xffffffff,%r15d
jmp    1273 <extractPakInDir+0xe3>

LLVM IR (-O0):
%struct.dirent = type { i8* }
@.str = [8 x i8] c"opendir\00"
@.str.1 = [3 x i8] c"..\00"
@.str.2 = [2 x i8] c".\00"
@.str.3 = [5 x i8] c".PAK\00"
@.str.4 = [6 x i8] c"%s/%s\00"
define i32 @extractPakInDir(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca %struct.dirent*
%6 = alloca i8*
%7 = alloca [1024 x i8]
%8 = alloca i32
store i8* %0, i8** %3
store i32 0, i32* %8
%9 = load i8*, i8** %3
%10 = call i32* @opendir(i8* %9)
store i32* %10, i32** %4
%11 = load i32*, i32** %4
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %15
13: ; preds = %1
%14 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %63
15: ; preds = %1
br label %16
16: ; preds = %58, %32, %15
%17 = load i32*, i32** %4
%18 = call %struct.dirent* @readdir(i32* %17)
store %struct.dirent* %18, %struct.dirent** %5
%19 = icmp ne %struct.dirent* %18, null
br i1 %19, label %20, label %59
20: ; preds = %16
%21 = load %struct.dirent*, %struct.dirent** %5
%22 = getelementptr inbounds %struct.dirent, %struct.dirent* %21, i32 0, i32 0
%23 = load i8*, i8** %22
%24 = call i64 @strcmp(i8* %23, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%25 = icmp eq i64 %24, 0
br i1 %25, label %32, label %26
26: ; preds = %20
%27 = load %struct.dirent*, %struct.dirent** %5
%28 = getelementptr inbounds %struct.dirent, %struct.dirent* %27, i32 0, i32 0
%29 = load i8*, i8** %28
%30 = call i64 @strcmp(i8* %29, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%31 = icmp eq i64 %30, 0
br i1 %31, label %32, label %33
32: ; preds = %26, %20
br label %16
33: ; preds = %26
%34 = load %struct.dirent*, %struct.dirent** %5
%35 = getelementptr inbounds %struct.dirent, %struct.dirent* %34, i32 0, i32 0
%36 = load i8*, i8** %35
%37 = call i8* @strrchr(i8* %36, i8 signext 46)
store i8* %37, i8** %6
%38 = load i8*, i8** %6
%39 = icmp ne i8* %38, null
br i1 %39, label %40, label %58
40: ; preds = %33
%41 = load i8*, i8** %6
%42 = call i64 @strcasecmp(i8* %41, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%43 = icmp eq i64 %42, 0
br i1 %43, label %44, label %57
44: ; preds = %40
%45 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%46 = load i8*, i8** %3
%47 = load %struct.dirent*, %struct.dirent** %5
%48 = getelementptr inbounds %struct.dirent, %struct.dirent* %47, i32 0, i32 0
%49 = load i8*, i8** %48
%50 = call i32 @snprintf(i8* %45, i32 1024, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i8* %46, i8* %49)
%51 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%52 = call i64 @extractPak(i8* %51, i32 0)
%53 = load i32, i32* %8
%54 = sext i32 %53 to i64
%55 = add nsw i64 %54, %52
%56 = trunc i64 %55 to i32
store i32 %56, i32* %8
br label %57
57: ; preds = %44, %40
br label %58
58: ; preds = %57, %33
br label %16
59: ; preds = %16
%60 = load i32*, i32** %4
%61 = call i32 @closedir(i32* %60)
%62 = load i32, i32* %8
store i32 %62, i32* %2
br label %63
63: ; preds = %59, %13
%64 = load i32, i32* %2
ret i32 %64
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [8 x i8] c"opendir\00"
@.str.1 = [3 x i8] c"..\00"
@.str.2 = [2 x i8] c".\00"
@.str.3 = [5 x i8] c".PAK\00"
@.str.4 = [6 x i8] c"%s/%s\00"
define i32 @extractPakInDir(i8* %0) {
%2 = alloca [1024 x i8]
%3 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%4 = tail call i32* @opendir(i8* %0)
%5 = icmp eq i32* %4, null
br i1 %5, label %9, label %6
6: ; preds = %1
%7 = call %struct.dirent* @readdir(i32* %4)
%8 = icmp eq %struct.dirent* %7, null
br i1 %8, label %43, label %40
9: ; preds = %1
%10 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
br label %46
11: ; preds = %40, %21
%12 = phi %struct.dirent* [ %41, %40 ], [ %22, %21 ]
%13 = getelementptr inbounds %struct.dirent, %struct.dirent* %12, i64 0, i32 0
%14 = load i8*, i8** %13
%15 = call i64 @strcmp(i8* %14, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%16 = icmp eq i64 %15, 0
br i1 %16, label %21, label %17
17: ; preds = %11
%18 = load i8*, i8** %13
%19 = call i64 @strcmp(i8* %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %17, %11
%22 = call %struct.dirent* @readdir(i32* %4)
%23 = icmp eq %struct.dirent* %22, null
br i1 %23, label %43, label %11
24: ; preds = %17
%25 = load i8*, i8** %13
%26 = call i8* @strrchr(i8* dereferenceable(1) %25, i8 signext 46)
%27 = icmp eq i8* %26, null
br i1 %27, label %36, label %28
28: ; preds = %24
%29 = call i64 @strcasecmp(i8* %26, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%30 = icmp eq i64 %29, 0
br i1 %30, label %31, label %36
31: ; preds = %28
%32 = call i32 @snprintf(i8* %3, i32 1024, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i8* %0, i8* %25)
%33 = call i64 @extractPak(i8* %3, i32 0)
%34 = trunc i64 %33 to i32
%35 = add i32 %42, %34
br label %36
36: ; preds = %28, %31, %24
%37 = phi i32 [ %35, %31 ], [ %42, %28 ], [ %42, %24 ]
%38 = call %struct.dirent* @readdir(i32* %4)
%39 = icmp eq %struct.dirent* %38, null
br i1 %39, label %43, label %40
40: ; preds = %6, %36
%41 = phi %struct.dirent* [ %38, %36 ], [ %7, %6 ]
%42 = phi i32 [ %37, %36 ], [ 0, %6 ]
br label %11
43: ; preds = %36, %21, %6
%44 = phi i32 [ 0, %6 ], [ %42, %21 ], [ %37, %36 ]
%45 = call i32 @closedir(i32* %4)
br label %46
46: ; preds = %43, %9
%47 = phi i32 [ -1, %9 ], [ %44, %43 ]
ret i32 %47
}