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

/* Variables and functions */
scalar_t__ strncasecmp(char *, char const *, int);
char *strrchr(char *, char);
int get_file_format(char *filename) {
  int i;
  static const char *extension[] = {"pgx", "bin", "j3d", "jp3d", "j2k", "img"};
  static const int format[] = {1, 0, 1, 1, 0, 2};
  char *ext = strrchr(filename, '.');
  if (ext) {
    ext++;
    for (i = 0; i < sizeof(format) / sizeof(format[0]); i++) {
      if (strncasecmp(ext, extension[i], 3) == 0) {
        return format[i];
      }
    }
  }
  return -1;
}


x86 assembly (clang, -O0):
<get_file_format>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x2e,%esi
call   1030 <strrchr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11bf <get_file_format+0x9f>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x14(%rbp)
movslq -0x14(%rbp),%rax
cmp    $0x6,%rax
jae    11ba <get_file_format+0x9a>
mov    -0x20(%rbp),%rdi
movslq -0x14(%rbp),%rcx
lea    0x2ec7(%rip),%rax
mov    (%rax,%rcx,8),%rsi
mov    $0x3,%edx
call   1040 <strncasecmp@plt>
cmp    $0x0,%rax
jne    11a7 <get_file_format+0x87>
movslq -0x14(%rbp),%rcx
lea    0xe84(%rip),%rax
mov    (%rax,%rcx,4),%eax
mov    %eax,-0x4(%rbp)
jmp    11c6 <get_file_format+0xa6>
jmp    11ac <get_file_format+0x8c>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    115c <get_file_format+0x3c>
jmp    11bf <get_file_format+0x9f>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_file_format>:
push   %rbp
push   %rbx
push   %rax
mov    $0x2e,%esi
call   1030 <strrchr@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
je     120b <get_file_format+0xeb>
mov    %rax,%rbx
add    $0x1,%rbx
lea    0xeb7(%rip),%rsi
mov    %rbx,%rdi
mov    $0x3,%edx
call   1040 <strncasecmp@plt>
test   %rax,%rax
je     11e3 <get_file_format+0xc3>
lea    0xe9e(%rip),%rsi
mov    %rbx,%rdi
mov    $0x3,%edx
call   1040 <strncasecmp@plt>
test   %rax,%rax
je     11e7 <get_file_format+0xc7>
lea    0xe89(%rip),%rsi
mov    %rbx,%rdi
mov    $0x3,%edx
call   1040 <strncasecmp@plt>
test   %rax,%rax
je     11ee <get_file_format+0xce>
lea    0xe74(%rip),%rsi
mov    %rbx,%rdi
mov    $0x3,%edx
call   1040 <strncasecmp@plt>
test   %rax,%rax
je     11f5 <get_file_format+0xd5>
lea    0xe60(%rip),%rsi
mov    %rbx,%rdi
mov    $0x3,%edx
call   1040 <strncasecmp@plt>
test   %rax,%rax
je     11fc <get_file_format+0xdc>
lea    0xe4b(%rip),%rsi
mov    %rbx,%rdi
mov    $0x3,%edx
call   1040 <strncasecmp@plt>
test   %rax,%rax
jne    120b <get_file_format+0xeb>
mov    $0x5,%eax
jmp    1201 <get_file_format+0xe1>
xor    %eax,%eax
jmp    1201 <get_file_format+0xe1>
mov    $0x1,%eax
jmp    1201 <get_file_format+0xe1>
mov    $0x2,%eax
jmp    1201 <get_file_format+0xe1>
mov    $0x3,%eax
jmp    1201 <get_file_format+0xe1>
mov    $0x4,%eax
lea    0xe18(%rip),%rcx
mov    (%rcx,%rax,4),%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@get_file_format.extension = internal global [6 x i8*] [i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i32 0, i32 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i32 0, i32 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i32 0, i32 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i32 0, i32 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i32 0, i32 0)]
@.str = [4 x i8] c"pgx\00"
@.str.1 = [4 x i8] c"bin\00"
@.str.2 = [4 x i8] c"j3d\00"
@.str.3 = [5 x i8] c"jp3d\00"
@.str.4 = [4 x i8] c"j2k\00"
@.str.5 = [4 x i8] c"img\00"
@get_file_format.format = internal constant [6 x i32] [i32 1, i32 0, i32 1, i32 1, i32 0, i32 2]
define i32 @get_file_format(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = call i8* @strrchr(i8* %6, i8 signext 46)
store i8* %7, i8** %5
%8 = load i8*, i8** %5
%9 = icmp ne i8* %8, null
br i1 %9, label %10, label %35
10: ; preds = %1
%11 = load i8*, i8** %5
%12 = getelementptr inbounds i8, i8* %11, i32 1
store i8* %12, i8** %5
store i32 0, i32* %4
br label %13
13: ; preds = %31, %10
%14 = load i32, i32* %4
%15 = sext i32 %14 to i64
%16 = icmp ult i64 %15, 6
br i1 %16, label %17, label %34
17: ; preds = %13
%18 = load i8*, i8** %5
%19 = load i32, i32* %4
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds [6 x i8*], [6 x i8*]* @get_file_format.extension, i64 0, i64 %20
%22 = load i8*, i8** %21
%23 = call i64 @strncasecmp(i8* %18, i8* %22, i32 3)
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %30
25: ; preds = %17
%26 = load i32, i32* %4
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds [6 x i32], [6 x i32]* @get_file_format.format, i64 0, i64 %27
%29 = load i32, i32* %28
store i32 %29, i32* %2
br label %36
30: ; preds = %17
br label %31
31: ; preds = %30
%32 = load i32, i32* %4
%33 = add nsw i32 %32, 1
store i32 %33, i32* %4
br label %13
34: ; preds = %13
br label %35
35: ; preds = %34, %1
store i32 -1, i32* %2
br label %36
36: ; preds = %35, %25
%37 = load i32, i32* %2
ret i32 %37
}

LLVM IR (-O2):
@.str = [4 x i8] c"pgx\00"
@.str.1 = [4 x i8] c"bin\00"
@.str.2 = [4 x i8] c"j3d\00"
@.str.3 = [5 x i8] c"jp3d\00"
@.str.4 = [4 x i8] c"j2k\00"
@.str.5 = [4 x i8] c"img\00"
@get_file_format.format = internal unnamed_addr constant [6 x i32] [i32 1, i32 0, i32 1, i32 1, i32 0, i32 2]
define i32 @get_file_format(i8* %0) {
%2 = tail call i8* @strrchr(i8* dereferenceable(1) %0, i8 signext 46)
%3 = icmp eq i8* %2, null
br i1 %3, label %27, label %4
4: ; preds = %1
%5 = getelementptr inbounds i8, i8* %2, i64 1
%6 = tail call i64 @strncasecmp(i8* %5, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 3)
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %12
8: ; preds = %24, %21, %18, %15, %12, %4
%9 = phi i64 [ 0, %4 ], [ 1, %12 ], [ 2, %15 ], [ 3, %18 ], [ 4, %21 ], [ 5, %24 ]
%10 = getelementptr inbounds [6 x i32], [6 x i32]* @get_file_format.format, i64 0, i64 %9
%11 = load i32, i32* %10
br label %27
12: ; preds = %4
%13 = tail call i64 @strncasecmp(i8* %5, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 3)
%14 = icmp eq i64 %13, 0
br i1 %14, label %8, label %15
15: ; preds = %12
%16 = tail call i64 @strncasecmp(i8* %5, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 3)
%17 = icmp eq i64 %16, 0
br i1 %17, label %8, label %18
18: ; preds = %15
%19 = tail call i64 @strncasecmp(i8* %5, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 3)
%20 = icmp eq i64 %19, 0
br i1 %20, label %8, label %21
21: ; preds = %18
%22 = tail call i64 @strncasecmp(i8* %5, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 3)
%23 = icmp eq i64 %22, 0
br i1 %23, label %8, label %24
24: ; preds = %21
%25 = tail call i64 @strncasecmp(i8* %5, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i32 3)
%26 = icmp eq i64 %25, 0
br i1 %26, label %8, label %27
27: ; preds = %24, %1, %8
%28 = phi i32 [ %11, %8 ], [ -1, %1 ], [ -1, %24 ]
ret i32 %28
}