#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*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char const *, int);
char *strrchr(char *, char);
int filesystem_detect_extension(const char *path, const char **extensions,
                                int length) {
  assert(path != NULL);
  assert(extensions != NULL);
  assert(length > 0);
  FILE *test = NULL;
  int index = 0;
  char final_path[255];
  strncpy(final_path, path, 255);
  int strlength = strlen(final_path);
  char *pointer = strrchr(final_path, '\0');
  while (index < length) {
    strncpy(pointer, extensions[index], 255 - strlength);
    test = fopen(final_path, "r");
    if (test != NULL) {
      fclose(test);
      return index;
    }
    index++;
  }
  return -1;
}


x86 assembly (clang, -O0):
<filesystem_detect_extension>:
push   %rbp
mov    %rsp,%rbp
sub    $0x140,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
cmpq   $0x0,-0x10(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
cmpq   $0x0,-0x18(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
cmpl   $0x0,-0x1c(%rbp)
setg   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
movq   $0x0,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
lea    -0x130(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0xff,%edx
call   1030 <strncpy@plt>
lea    -0x130(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x134(%rbp)
lea    -0x130(%rbp),%rdi
xor    %esi,%esi
call   1060 <strrchr@plt>
mov    %rax,-0x140(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    1269 <filesystem_detect_extension+0x109>
mov    -0x140(%rbp),%rdi
mov    -0x18(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
mov    $0xff,%edx
sub    -0x134(%rbp),%edx
call   1030 <strncpy@plt>
lea    -0x130(%rbp),%rdi
lea    0xdcd(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     125b <filesystem_detect_extension+0xfb>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1270 <filesystem_detect_extension+0x110>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    11f6 <filesystem_detect_extension+0x96>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x140,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<filesystem_detect_extension>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x108,%rsp
mov    %edx,%r14d
mov    %rsi,%r12
mov    %rdi,%rbx
xor    %edi,%edi
test   %rbx,%rbx
setne  %dil
call   1060 <assert@plt>
xor    %edi,%edi
test   %r12,%r12
setne  %dil
call   1060 <assert@plt>
xor    %edi,%edi
test   %r14d,%r14d
setg   %dil
call   1060 <assert@plt>
mov    %rsp,%r15
mov    $0xff,%ebp
mov    %r15,%rdi
mov    %rbx,%rsi
mov    $0xff,%edx
call   1030 <strncpy@plt>
mov    %r15,%rdi
call   1050 <strlen@plt>
mov    %eax,%r13d
mov    %r15,%rdi
call   1050 <strlen@plt>
mov    $0xffffffff,%ecx
test   %r14d,%r14d
jle    121d <filesystem_detect_extension+0xcd>
lea    (%rsp,%rax,1),%rbx
sub    %r13d,%ebp
mov    %r14d,%r14d
xor    %r15d,%r15d
mov    %rsp,%r13
nopl   0x0(%rax)
mov    (%r12,%r15,8),%rsi
mov    %rbx,%rdi
mov    %ebp,%edx
call   1030 <strncpy@plt>
mov    %r13,%rdi
lea    0xe08(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
jne    1212 <filesystem_detect_extension+0xc2>
add    $0x1,%r15
cmp    %r15,%r14
jne    11e0 <filesystem_detect_extension+0x90>
mov    $0xffffffff,%ecx
jmp    121d <filesystem_detect_extension+0xcd>
mov    %rax,%rdi
call   1040 <fclose@plt>
mov    %r15d,%ecx
mov    %ecx,%eax
add    $0x108,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
define i32 @filesystem_detect_extension(i8* %0, i8** %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8**
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i32
%10 = alloca [255 x i8]
%11 = alloca i32
%12 = alloca i8*
store i8* %0, i8** %5
store i8** %1, i8*** %6
store i32 %2, i32* %7
%13 = load i8*, i8** %5
%14 = icmp ne i8* %13, null
%15 = zext i1 %14 to i32
%16 = call i32 @assert(i32 %15)
%17 = load i8**, i8*** %6
%18 = icmp ne i8** %17, null
%19 = zext i1 %18 to i32
%20 = call i32 @assert(i32 %19)
%21 = load i32, i32* %7
%22 = icmp sgt i32 %21, 0
%23 = zext i1 %22 to i32
%24 = call i32 @assert(i32 %23)
store i32* null, i32** %8
store i32 0, i32* %9
%25 = getelementptr inbounds [255 x i8], [255 x i8]* %10, i64 0, i64 0
%26 = load i8*, i8** %5
%27 = call i32 @strncpy(i8* %25, i8* %26, i32 255)
%28 = getelementptr inbounds [255 x i8], [255 x i8]* %10, i64 0, i64 0
%29 = call i32 @strlen(i8* %28)
store i32 %29, i32* %11
%30 = getelementptr inbounds [255 x i8], [255 x i8]* %10, i64 0, i64 0
%31 = call i8* @strrchr(i8* %30, i8 signext 0)
store i8* %31, i8** %12
br label %32
32: ; preds = %54, %3
%33 = load i32, i32* %9
%34 = load i32, i32* %7
%35 = icmp slt i32 %33, %34
br i1 %35, label %36, label %57
36: ; preds = %32
%37 = load i8*, i8** %12
%38 = load i8**, i8*** %6
%39 = load i32, i32* %9
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i8*, i8** %38, i64 %40
%42 = load i8*, i8** %41
%43 = load i32, i32* %11
%44 = sub nsw i32 255, %43
%45 = call i32 @strncpy(i8* %37, i8* %42, i32 %44)
%46 = getelementptr inbounds [255 x i8], [255 x i8]* %10, i64 0, i64 0
%47 = call i32* @fopen(i8* %46, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %47, i32** %8
%48 = load i32*, i32** %8
%49 = icmp ne i32* %48, null
br i1 %49, label %50, label %54
50: ; preds = %36
%51 = load i32*, i32** %8
%52 = call i32 @fclose(i32* %51)
%53 = load i32, i32* %9
store i32 %53, i32* %4
br label %58
54: ; preds = %36
%55 = load i32, i32* %9
%56 = add nsw i32 %55, 1
store i32 %56, i32* %9
br label %32
57: ; preds = %32
store i32 -1, i32* %4
br label %58
58: ; preds = %57, %50
%59 = load i32, i32* %4
ret i32 %59
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
define i32 @filesystem_detect_extension(i8* %0, i8** %1, i32 %2) {
%4 = alloca [255 x i8]
%5 = icmp ne i8* %0, null
%6 = zext i1 %5 to i32
%7 = tail call i32 @assert(i32 %6)
%8 = icmp ne i8** %1, null
%9 = zext i1 %8 to i32
%10 = tail call i32 @assert(i32 %9)
%11 = icmp sgt i32 %2, 0
%12 = zext i1 %11 to i32
%13 = tail call i32 @assert(i32 %12)
%14 = getelementptr inbounds [255 x i8], [255 x i8]* %4, i64 0, i64 0
%15 = call i32 @strncpy(i8* %14, i8* %0, i32 255)
%16 = call i32 @strlen(i8* %14)
%17 = call i64 bitcast (i32 (i8*)* @strlen to i64 (i8*)*)(i8* %14)
%18 = getelementptr [255 x i8], [255 x i8]* %4, i64 0, i64 %17
br i1 %11, label %19, label %35
19: ; preds = %3
%20 = sub nsw i32 255, %16
%21 = zext i32 %2 to i64
br label %22
22: ; preds = %19, %32
%23 = phi i64 [ 0, %19 ], [ %33, %32 ]
%24 = getelementptr inbounds i8*, i8** %1, i64 %23
%25 = load i8*, i8** %24
%26 = call i32 @strncpy(i8* %18, i8* %25, i32 %20)
%27 = call i32* @fopen(i8* %14, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%28 = icmp eq i32* %27, null
br i1 %28, label %32, label %29
29: ; preds = %22
%30 = trunc i64 %23 to i32
%31 = call i32 @fclose(i32* %27)
br label %35
32: ; preds = %22
%33 = add nuw nsw i64 %23, 1
%34 = icmp eq i64 %33, %21
br i1 %34, label %35, label %22
35: ; preds = %32, %3, %29
%36 = phi i32 [ %30, %29 ], [ -1, %3 ], [ -1, %32 ]
ret i32 %36
}