#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 stat {
  int /*<<< orphan*/ st_mode;
};
struct patchpool {
  int dummy;
};
struct dirent {
  char *d_name;
};
typedef int /*<<< orphan*/ fullname;
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
int /*<<< orphan*/ S_ISDIR(int /*<<< orphan*/);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ lstat(char *, struct stat *);
int /*<<< orphan*/ *opendir(char const *);
int /*<<< orphan*/ patchpool_add(struct patchpool *, int /*<<< orphan*/);
struct dirent *readdir(int /*<<< orphan*/ *);
scalar_t__ strcasecmp(char const *, char *);
int /*<<< orphan*/ strdup(char *);
int /*<<< orphan*/ strncat(char *, char *, int);
int /*<<< orphan*/ strncpy(char *, char const *, int);
char *strrchr(char *, char);
void patchpool_add_files(struct patchpool *pp, const char *folder,
                         const char **extensions) {
  DIR *d;
  struct dirent *entry;
  struct stat s;
  char fullname[384];
  char *c;
  int i;
  d = opendir(folder);
  if (!d)
    return;
  while ((entry = readdir(d))) {
    if (entry->d_name[0] == '.')
      continue;
    strncpy(fullname, folder, sizeof(fullname));
    strncat(fullname, entry->d_name, sizeof(fullname));
    lstat(fullname, &s);
    if (!S_ISDIR(s.st_mode)) {
      c = strrchr(entry->d_name, '.');
      if (c != NULL) {
        i = 0;
        while (extensions[i] != NULL) {
          if (strcasecmp(extensions[i], c + 1) == 0) {
            patchpool_add(pp, strdup(entry->d_name));
            break;
          }
          i++;
        }
      }
    }
  }
  closedir(d);
}


x86 assembly (clang, -O0):
<patchpool_add_files>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1d0,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <opendir@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11e4 <patchpool_add_files+0x34>
jmp    132b <patchpool_add_files+0x17b>
jmp    11e9 <patchpool_add_files+0x39>
mov    -0x20(%rbp),%rdi
call   10c0 <readdir@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     1322 <patchpool_add_files+0x172>
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
jne    1218 <patchpool_add_files+0x68>
jmp    11e9 <patchpool_add_files+0x39>
lea    -0x1b0(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0x180,%edx
call   1040 <strncpy@plt>
lea    -0x1b0(%rbp),%rdi
mov    -0x28(%rbp),%rax
mov    (%rax),%rsi
mov    $0x180,%edx
call   1090 <strncat@plt>
lea    -0x1b0(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   10b0 <lstat@plt>
mov    -0x30(%rbp),%edi
call   1060 <S_ISDIR@plt>
cmp    $0x0,%eax
jne    131d <patchpool_add_files+0x16d>
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
mov    $0x2e,%esi
call   1070 <strrchr@plt>
mov    %rax,-0x1b8(%rbp)
cmpq   $0x0,-0x1b8(%rbp)
je     1318 <patchpool_add_files+0x168>
movl   $0x0,-0x1bc(%rbp)
mov    -0x18(%rbp),%rax
movslq -0x1bc(%rbp),%rcx
cmpq   $0x0,(%rax,%rcx,8)
je     1313 <patchpool_add_files+0x163>
mov    -0x18(%rbp),%rax
movslq -0x1bc(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
mov    -0x1b8(%rbp),%rsi
add    $0x1,%rsi
call   1030 <strcasecmp@plt>
cmp    $0x0,%rax
jne    12ff <patchpool_add_files+0x14f>
mov    -0x8(%rbp),%rax
mov    %rax,-0x1c8(%rbp)
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
call   10d0 <strdup@plt>
mov    -0x1c8(%rbp),%rdi
mov    %eax,%esi
call   1080 <patchpool_add@plt>
jmp    1313 <patchpool_add_files+0x163>
mov    -0x1bc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1bc(%rbp)
jmp    1296 <patchpool_add_files+0xe6>
jmp    1318 <patchpool_add_files+0x168>
jmp    131d <patchpool_add_files+0x16d>
jmp    11e9 <patchpool_add_files+0x39>
mov    -0x20(%rbp),%rdi
call   10a0 <closedir@plt>
add    $0x1d0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<patchpool_add_files>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1a8,%rsp
mov    %rdx,0x8(%rsp)
mov    %rsi,%r12
mov    %rdi,0x18(%rsp)
mov    %rsi,%rdi
call   1050 <opendir@plt>
test   %rax,%rax
je     12da <patchpool_add_files+0x12a>
mov    %rax,%rbx
mov    %rax,%rdi
call   10c0 <readdir@plt>
test   %rax,%rax
je     12d2 <patchpool_add_files+0x122>
mov    %rax,%r15
mov    0x8(%rsp),%rax
add    $0x8,%rax
mov    %rax,0x10(%rsp)
lea    0x20(%rsp),%r13
jmp    1234 <patchpool_add_files+0x84>
mov    %r15,%rdi
call   10d0 <strdup@plt>
mov    0x18(%rsp),%rdi
mov    %eax,%esi
call   1080 <patchpool_add@plt>
nop
mov    %rbx,%rdi
call   10c0 <readdir@plt>
mov    %rax,%r15
test   %rax,%rax
je     12d2 <patchpool_add_files+0x122>
mov    (%r15),%rax
cmpb   $0x2e,(%rax)
je     1220 <patchpool_add_files+0x70>
mov    %r13,%rdi
mov    %r12,%rsi
mov    $0x180,%edx
call   1040 <strncpy@plt>
mov    (%r15),%rsi
mov    %r13,%rdi
mov    $0x180,%edx
call   1090 <strncat@plt>
mov    %r13,%rdi
mov    %rsp,%rsi
call   10b0 <lstat@plt>
mov    (%rsp),%edi
call   1060 <S_ISDIR@plt>
test   %eax,%eax
jne    1220 <patchpool_add_files+0x70>
mov    (%r15),%r15
mov    %r15,%rdi
mov    $0x2e,%esi
call   1070 <strrchr@plt>
test   %rax,%rax
je     1220 <patchpool_add_files+0x70>
mov    %rax,%rbp
mov    0x8(%rsp),%rax
mov    (%rax),%rdi
test   %rdi,%rdi
je     1220 <patchpool_add_files+0x70>
add    $0x1,%rbp
mov    0x10(%rsp),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %rbp,%rsi
call   1030 <strcasecmp@plt>
test   %rax,%rax
je     120b <patchpool_add_files+0x5b>
mov    (%r14),%rdi
add    $0x8,%r14
test   %rdi,%rdi
jne    12b0 <patchpool_add_files+0x100>
jmp    1220 <patchpool_add_files+0x70>
mov    %rbx,%rdi
call   10a0 <closedir@plt>
add    $0x1a8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.patchpool = type { i32 }
%struct.dirent = type { i8* }
%struct.stat = type { i32 }
define void @patchpool_add_files(%struct.patchpool* %0, i8* %1, i8** %2) {
%4 = alloca %struct.patchpool*
%5 = alloca i8*
%6 = alloca i8**
%7 = alloca i32*
%8 = alloca %struct.dirent*
%9 = alloca %struct.stat
%10 = alloca [384 x i8]
%11 = alloca i8*
%12 = alloca i32
store %struct.patchpool* %0, %struct.patchpool** %4
store i8* %1, i8** %5
store i8** %2, i8*** %6
%13 = load i8*, i8** %5
%14 = call i32* @opendir(i8* %13)
store i32* %14, i32** %7
%15 = load i32*, i32** %7
%16 = icmp ne i32* %15, null
br i1 %16, label %18, label %17
17: ; preds = %3
br label %88
18: ; preds = %3
br label %19
19: ; preds = %84, %31, %18
%20 = load i32*, i32** %7
%21 = call %struct.dirent* @readdir(i32* %20)
store %struct.dirent* %21, %struct.dirent** %8
%22 = icmp ne %struct.dirent* %21, null
br i1 %22, label %23, label %85
23: ; preds = %19
%24 = load %struct.dirent*, %struct.dirent** %8
%25 = getelementptr inbounds %struct.dirent, %struct.dirent* %24, i32 0, i32 0
%26 = load i8*, i8** %25
%27 = getelementptr inbounds i8, i8* %26, i64 0
%28 = load i8, i8* %27
%29 = sext i8 %28 to i32
%30 = icmp eq i32 %29, 46
br i1 %30, label %31, label %32
31: ; preds = %23
br label %19
32: ; preds = %23
%33 = getelementptr inbounds [384 x i8], [384 x i8]* %10, i64 0, i64 0
%34 = load i8*, i8** %5
%35 = call i32 @strncpy(i8* %33, i8* %34, i32 384)
%36 = getelementptr inbounds [384 x i8], [384 x i8]* %10, i64 0, i64 0
%37 = load %struct.dirent*, %struct.dirent** %8
%38 = getelementptr inbounds %struct.dirent, %struct.dirent* %37, i32 0, i32 0
%39 = load i8*, i8** %38
%40 = call i32 @strncat(i8* %36, i8* %39, i32 384)
%41 = getelementptr inbounds [384 x i8], [384 x i8]* %10, i64 0, i64 0
%42 = call i32 @lstat(i8* %41, %struct.stat* %9)
%43 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%44 = load i32, i32* %43
%45 = call i32 @S_ISDIR(i32 %44)
%46 = icmp ne i32 %45, 0
br i1 %46, label %84, label %47
47: ; preds = %32
%48 = load %struct.dirent*, %struct.dirent** %8
%49 = getelementptr inbounds %struct.dirent, %struct.dirent* %48, i32 0, i32 0
%50 = load i8*, i8** %49
%51 = call i8* @strrchr(i8* %50, i8 signext 46)
store i8* %51, i8** %11
%52 = load i8*, i8** %11
%53 = icmp ne i8* %52, null
br i1 %53, label %54, label %83
54: ; preds = %47
store i32 0, i32* %12
br label %55
55: ; preds = %79, %54
%56 = load i8**, i8*** %6
%57 = load i32, i32* %12
%58 = sext i32 %57 to i64
%59 = getelementptr inbounds i8*, i8** %56, i64 %58
%60 = load i8*, i8** %59
%61 = icmp ne i8* %60, null
br i1 %61, label %62, label %82
62: ; preds = %55
%63 = load i8**, i8*** %6
%64 = load i32, i32* %12
%65 = sext i32 %64 to i64
%66 = getelementptr inbounds i8*, i8** %63, i64 %65
%67 = load i8*, i8** %66
%68 = load i8*, i8** %11
%69 = getelementptr inbounds i8, i8* %68, i64 1
%70 = call i64 @strcasecmp(i8* %67, i8* %69)
%71 = icmp eq i64 %70, 0
br i1 %71, label %72, label %79
72: ; preds = %62
%73 = load %struct.patchpool*, %struct.patchpool** %4
%74 = load %struct.dirent*, %struct.dirent** %8
%75 = getelementptr inbounds %struct.dirent, %struct.dirent* %74, i32 0, i32 0
%76 = load i8*, i8** %75
%77 = call i32 @strdup(i8* %76)
%78 = call i32 @patchpool_add(%struct.patchpool* %73, i32 %77)
br label %82
79: ; preds = %62
%80 = load i32, i32* %12
%81 = add nsw i32 %80, 1
store i32 %81, i32* %12
br label %55
82: ; preds = %72, %55
br label %83
83: ; preds = %82, %47
br label %84
84: ; preds = %83, %32
br label %19
85: ; preds = %19
%86 = load i32*, i32** %7
%87 = call i32 @closedir(i32* %86)
br label %88
88: ; preds = %85, %17
ret void
}

LLVM IR (-O2):
%struct.patchpool = type { i32 }
%struct.stat = type { i32 }
%struct.dirent = type { i8* }
define void @patchpool_add_files(%struct.patchpool* %0, i8* %1, i8** nocapture %2) {
%4 = alloca %struct.stat
%5 = alloca [384 x i8]
%6 = bitcast %struct.stat* %4 to i8*
%7 = getelementptr inbounds [384 x i8], [384 x i8]* %5, i64 0, i64 0
%8 = tail call i32* @opendir(i8* %1)
%9 = icmp eq i32* %8, null
br i1 %9, label %56, label %10
10: ; preds = %3
%11 = call %struct.dirent* @readdir(i32* %8)
%12 = icmp eq %struct.dirent* %11, null
br i1 %12, label %54, label %13
13: ; preds = %10
%14 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
br label %15
15: ; preds = %13, %51
%16 = phi %struct.dirent* [ %11, %13 ], [ %52, %51 ]
%17 = getelementptr inbounds %struct.dirent, %struct.dirent* %16, i64 0, i32 0
%18 = load i8*, i8** %17
%19 = load i8, i8* %18
%20 = icmp eq i8 %19, 46
br i1 %20, label %51, label %21
21: ; preds = %15
%22 = call i32 @strncpy(i8* %7, i8* %1, i32 384)
%23 = load i8*, i8** %17
%24 = call i32 @strncat(i8* %7, i8* %23, i32 384)
%25 = call i32 @lstat(i8* %7, %struct.stat* %4)
%26 = load i32, i32* %14
%27 = call i32 @S_ISDIR(i32 %26)
%28 = icmp eq i32 %27, 0
br i1 %28, label %29, label %51
29: ; preds = %21
%30 = load i8*, i8** %17
%31 = call i8* @strrchr(i8* dereferenceable(1) %30, i8 signext 46)
%32 = icmp eq i8* %31, null
br i1 %32, label %51, label %33
33: ; preds = %29
%34 = load i8*, i8** %2
%35 = icmp eq i8* %34, null
br i1 %35, label %51, label %36
36: ; preds = %33
%37 = getelementptr inbounds i8, i8* %31, i64 1
br label %42
38: ; preds = %42
%39 = getelementptr inbounds i8*, i8** %2, i64 %47
%40 = load i8*, i8** %39
%41 = icmp eq i8* %40, null
br i1 %41, label %51, label %42
42: ; preds = %36, %38
%43 = phi i64 [ 0, %36 ], [ %47, %38 ]
%44 = phi i8* [ %34, %36 ], [ %40, %38 ]
%45 = call i64 @strcasecmp(i8* %44, i8* %37)
%46 = icmp eq i64 %45, 0
%47 = add nuw i64 %43, 1
br i1 %46, label %48, label %38
48: ; preds = %42
%49 = call i32 @strdup(i8* %30)
%50 = call i32 @patchpool_add(%struct.patchpool* %0, i32 %49)
br label %51
51: ; preds = %38, %21, %48, %29, %33, %15
%52 = call %struct.dirent* @readdir(i32* %8)
%53 = icmp eq %struct.dirent* %52, null
br i1 %53, label %54, label %15
54: ; preds = %51, %10
%55 = call i32 @closedir(i32* %8)
br label %56
56: ; preds = %3, %54
ret void
}