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

/* Variables and functions */
int /*<<< orphan*/ FS_CloseDir();
int /*<<< orphan*/ FS_OpenDir(char const *);
int FS_ReadDir(char *);
int /*<<< orphan*/ sprintf(char *, char *, char const *, char *);
int strlen(char *);
scalar_t__ strncasecmp(char *, char const *, int);
int get_idx_filename(char *result, const char *dir, const char *ext, int idx,
                     const char *prefix) {
  if (!FS_OpenDir(dir))
    return 0;
  char filename[13];
  int type;
  int count = 0;
  while ((type = FS_ReadDir(filename)) != 0) {
    if (type == 1 &&
        strncasecmp(filename + strlen(filename) - 4, ext, 4) == 0) {
      count++;
      if (idx + 1 == count) {
        sprintf(result, "%s%s", prefix, filename);
        FS_CloseDir();
        return 1;
      }
    }
  }
  FS_CloseDir();
  return 0;
}


x86 assembly (clang, -O0):
<get_idx_filename>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    %r8,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <FS_OpenDir@plt>
cmp    $0x0,%eax
jne    1199 <get_idx_filename+0x39>
movl   $0x0,-0x4(%rbp)
jmp    1250 <get_idx_filename+0xf0>
movl   $0x0,-0x48(%rbp)
lea    -0x3d(%rbp),%rdi
call   1070 <FS_ReadDir@plt>
mov    %eax,-0x44(%rbp)
cmp    $0x0,%eax
je     1242 <get_idx_filename+0xe2>
cmpl   $0x1,-0x44(%rbp)
jne    123d <get_idx_filename+0xdd>
lea    -0x3d(%rbp),%rax
mov    %rax,-0x50(%rbp)
lea    -0x3d(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x50(%rbp),%rdi
cltq
add    %rax,%rdi
add    $0xfffffffffffffffc,%rdi
mov    -0x20(%rbp),%rsi
mov    $0x4,%edx
call   1050 <strncasecmp@plt>
cmp    $0x0,%rax
jne    123d <get_idx_filename+0xdd>
mov    -0x48(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x48(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
cmp    -0x48(%rbp),%eax
jne    1238 <get_idx_filename+0xd8>
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rdx
lea    -0x3d(%rbp),%rcx
lea    0xde0(%rip),%rsi
call   1080 <sprintf@plt>
mov    $0x0,%al
call   1030 <FS_CloseDir@plt>
movl   $0x1,-0x4(%rbp)
jmp    1250 <get_idx_filename+0xf0>
jmp    123d <get_idx_filename+0xdd>
jmp    11a0 <get_idx_filename+0x40>
mov    $0x0,%al
call   1030 <FS_CloseDir@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_idx_filename>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r8,%rbx
mov    %ecx,%r12d
mov    %rdx,%r13
mov    %rdi,%r15
mov    %rsi,%rdi
call   1060 <FS_OpenDir@plt>
test   %eax,%eax
je     120b <get_idx_filename+0xab>
mov    %rbx,(%rsp)
xor    %r14d,%r14d
lea    0xb(%rsp),%rbp
jmp    11c4 <get_idx_filename+0x64>
nopl   0x0(%rax,%rax,1)
mov    %rbp,%rdi
call   1040 <strlen@plt>
cltq
lea    (%rsp,%rax,1),%rdi
add    $0x7,%rdi
mov    %r13,%rsi
mov    $0x4,%edx
call   1050 <strncasecmp@plt>
test   %rax,%rax
je     11d9 <get_idx_filename+0x79>
mov    %rbp,%rdi
call   1070 <FS_ReadDir@plt>
cmp    $0x1,%eax
je     11a0 <get_idx_filename+0x40>
mov    %eax,%ebx
test   %eax,%eax
jne    11c4 <get_idx_filename+0x64>
jmp    1202 <get_idx_filename+0xa2>
lea    0x1(%r14),%eax
cmp    %r12d,%r14d
mov    %eax,%r14d
jne    11c4 <get_idx_filename+0x64>
lea    0xe14(%rip),%rsi
lea    0xb(%rsp),%rcx
mov    %r15,%rdi
mov    (%rsp),%rdx
call   1080 <sprintf@plt>
mov    $0x1,%ebx
xor    %eax,%eax
call   1030 <FS_CloseDir@plt>
jmp    120d <get_idx_filename+0xad>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"%s%s\00"
define i32 @get_idx_filename(i8* %0, i8* %1, i8* %2, i32 %3, i8* %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i8*
%12 = alloca [13 x i8]
%13 = alloca i32
%14 = alloca i32
store i8* %0, i8** %7
store i8* %1, i8** %8
store i8* %2, i8** %9
store i32 %3, i32* %10
store i8* %4, i8** %11
%15 = load i8*, i8** %8
%16 = call i32 @FS_OpenDir(i8* %15)
%17 = icmp ne i32 %16, 0
br i1 %17, label %19, label %18
18: ; preds = %5
store i32 0, i32* %6
br label %54
19: ; preds = %5
store i32 0, i32* %14
br label %20
20: ; preds = %51, %19
%21 = getelementptr inbounds [13 x i8], [13 x i8]* %12, i64 0, i64 0
%22 = call i32 @FS_ReadDir(i8* %21)
store i32 %22, i32* %13
%23 = icmp ne i32 %22, 0
br i1 %23, label %24, label %52
24: ; preds = %20
%25 = load i32, i32* %13
%26 = icmp eq i32 %25, 1
br i1 %26, label %27, label %51
27: ; preds = %24
%28 = getelementptr inbounds [13 x i8], [13 x i8]* %12, i64 0, i64 0
%29 = getelementptr inbounds [13 x i8], [13 x i8]* %12, i64 0, i64 0
%30 = call i32 @strlen(i8* %29)
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %28, i64 %31
%33 = getelementptr inbounds i8, i8* %32, i64 -4
%34 = load i8*, i8** %9
%35 = call i64 @strncasecmp(i8* %33, i8* %34, i32 4)
%36 = icmp eq i64 %35, 0
br i1 %36, label %37, label %51
37: ; preds = %27
%38 = load i32, i32* %14
%39 = add nsw i32 %38, 1
store i32 %39, i32* %14
%40 = load i32, i32* %10
%41 = add nsw i32 %40, 1
%42 = load i32, i32* %14
%43 = icmp eq i32 %41, %42
br i1 %43, label %44, label %50
44: ; preds = %37
%45 = load i8*, i8** %7
%46 = load i8*, i8** %11
%47 = getelementptr inbounds [13 x i8], [13 x i8]* %12, i64 0, i64 0
%48 = call i32 @sprintf(i8* %45, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %46, i8* %47)
%49 = call i32 (...) @FS_CloseDir()
store i32 1, i32* %6
br label %54
50: ; preds = %37
br label %51
51: ; preds = %50, %27, %24
br label %20
52: ; preds = %20
%53 = call i32 (...) @FS_CloseDir()
store i32 0, i32* %6
br label %54
54: ; preds = %52, %44, %18
%55 = load i32, i32* %6
ret i32 %55
}

LLVM IR (-O2):
@.str = [5 x i8] c"%s%s\00"
define i32 @get_idx_filename(i8* nocapture writeonly %0, i8* %1, i8* nocapture %2, i32 %3, i8* %4) {
%6 = alloca [13 x i8]
%7 = tail call i32 @FS_OpenDir(i8* %1)
%8 = icmp eq i32 %7, 0
br i1 %8, label %31, label %9
9: ; preds = %5
%10 = getelementptr inbounds [13 x i8], [13 x i8]* %6, i64 0, i64 0
br label %11
11: ; preds = %23, %9
%12 = phi i32 [ %24, %23 ], [ 0, %9 ]
br label %13
13: ; preds = %15, %11
%14 = call i32 @FS_ReadDir(i8* %10)
switch i32 %14, label %15 [
i32 0, label %28
i32 1, label %16
]
15: ; preds = %13, %16
br label %13
16: ; preds = %13
%17 = call i32 @strlen(i8* %10)
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds [13 x i8], [13 x i8]* %6, i64 0, i64 %18
%20 = getelementptr inbounds i8, i8* %19, i64 -4
%21 = call i64 @strncasecmp(i8* %20, i8* %2, i32 4)
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %15
23: ; preds = %16
%24 = add nuw nsw i32 %12, 1
%25 = icmp eq i32 %12, %3
br i1 %25, label %26, label %11
26: ; preds = %23
%27 = call i32 @sprintf(i8* dereferenceable(1) %0, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %4, i8* %10)
br label %28
28: ; preds = %13, %26
%29 = phi i32 [ 1, %26 ], [ %14, %13 ]
%30 = call i32 (...) @FS_CloseDir()
br label %31
31: ; preds = %5, %28
%32 = phi i32 [ %29, %28 ], [ 0, %5 ]
ret i32 %32
}