#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 strlen(char *);
scalar_t__ strncasecmp(char *, char *, int);
int check_format(char *template) {
  int n;
  int format;
  n = strlen(template);
  if (n < 4) {
    return (-1);
  }
  if (strncasecmp(&(template[n - 4]), ".bmp", 4) == 0) {
    format = 0;
  } else if (strncasecmp(&(template[n - 4]), ".png", 4) == 0) {
    format = 1;
  } else if (strncasecmp(&(template[n - 4]), ".ipx", 4) == 0) {
    format = 2;
  } else {
    format = -1;
  }
  return (format);
}


x86 assembly (clang, -O0):
<check_format>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x4,-0x14(%rbp)
jge    114e <check_format+0x2e>
movl   $0xffffffff,-0x4(%rbp)
jmp    1207 <check_format+0xe7>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%eax
sub    $0x4,%eax
cltq
add    %rax,%rdi
lea    0xe9c(%rip),%rsi
mov    $0x4,%edx
call   1040 <strncasecmp@plt>
cmp    $0x0,%rax
jne    1184 <check_format+0x64>
movl   $0x0,-0x18(%rbp)
jmp    1201 <check_format+0xe1>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%eax
sub    $0x4,%eax
cltq
add    %rax,%rdi
lea    0xe6b(%rip),%rsi
mov    $0x4,%edx
call   1040 <strncasecmp@plt>
cmp    $0x0,%rax
jne    11ba <check_format+0x9a>
movl   $0x1,-0x18(%rbp)
jmp    11fc <check_format+0xdc>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%eax
sub    $0x4,%eax
cltq
add    %rax,%rdi
lea    0xe3a(%rip),%rsi
mov    $0x4,%edx
call   1040 <strncasecmp@plt>
cmp    $0x0,%rax
jne    11f0 <check_format+0xd0>
movl   $0x2,-0x18(%rbp)
jmp    11f7 <check_format+0xd7>
movl   $0xffffffff,-0x18(%rbp)
jmp    11fc <check_format+0xdc>
jmp    1201 <check_format+0xe1>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_format>:
push   %rbx
mov    %rdi,%rbx
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    $0x4,%ecx
jl     1189 <check_format+0x69>
add    $0xfffffffc,%ecx
add    %rcx,%rbx
lea    0xebe(%rip),%rsi
mov    %rbx,%rdi
mov    $0x4,%edx
call   1040 <strncasecmp@plt>
test   %rax,%rax
je     118b <check_format+0x6b>
lea    0xeaa(%rip),%rsi
mov    %rbx,%rdi
mov    $0x4,%edx
call   1040 <strncasecmp@plt>
test   %rax,%rax
je     118f <check_format+0x6f>
lea    0xe96(%rip),%rsi
mov    %rbx,%rdi
mov    $0x4,%edx
call   1040 <strncasecmp@plt>
neg    %rax
sbb    %eax,%eax
or     $0x2,%eax
pop    %rbx
ret
xor    %eax,%eax
pop    %rbx
ret
mov    $0x1,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [5 x i8] c".bmp\00"
@.str.1 = [5 x i8] c".png\00"
@.str.2 = [5 x i8] c".ipx\00"
define i32 @check_format(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = call i32 @strlen(i8* %6)
store i32 %7, i32* %4
%8 = load i32, i32* %4
%9 = icmp slt i32 %8, 4
br i1 %9, label %10, label %11
10: ; preds = %1
store i32 -1, i32* %2
br label %43
11: ; preds = %1
%12 = load i8*, i8** %3
%13 = load i32, i32* %4
%14 = sub nsw i32 %13, 4
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds i8, i8* %12, i64 %15
%17 = call i64 @strncasecmp(i8* %16, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 4)
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %11
store i32 0, i32* %5
br label %41
20: ; preds = %11
%21 = load i8*, i8** %3
%22 = load i32, i32* %4
%23 = sub nsw i32 %22, 4
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i8, i8* %21, i64 %24
%26 = call i64 @strncasecmp(i8* %25, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 4)
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %29
28: ; preds = %20
store i32 1, i32* %5
br label %40
29: ; preds = %20
%30 = load i8*, i8** %3
%31 = load i32, i32* %4
%32 = sub nsw i32 %31, 4
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %30, i64 %33
%35 = call i64 @strncasecmp(i8* %34, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 4)
%36 = icmp eq i64 %35, 0
br i1 %36, label %37, label %38
37: ; preds = %29
store i32 2, i32* %5
br label %39
38: ; preds = %29
store i32 -1, i32* %5
br label %39
39: ; preds = %38, %37
br label %40
40: ; preds = %39, %28
br label %41
41: ; preds = %40, %19
%42 = load i32, i32* %5
store i32 %42, i32* %2
br label %43
43: ; preds = %41, %10
%44 = load i32, i32* %2
ret i32 %44
}

LLVM IR (-O2):
@.str = [5 x i8] c".bmp\00"
@.str.1 = [5 x i8] c".png\00"
@.str.2 = [5 x i8] c".ipx\00"
define i32 @check_format(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = icmp slt i32 %2, 4
br i1 %3, label %17, label %4
4: ; preds = %1
%5 = add nsw i32 %2, -4
%6 = zext i32 %5 to i64
%7 = getelementptr inbounds i8, i8* %0, i64 %6
%8 = tail call i64 @strncasecmp(i8* %7, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 4)
%9 = icmp eq i64 %8, 0
br i1 %9, label %17, label %10
10: ; preds = %4
%11 = tail call i64 @strncasecmp(i8* %7, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 4)
%12 = icmp eq i64 %11, 0
br i1 %12, label %17, label %13
13: ; preds = %10
%14 = tail call i64 @strncasecmp(i8* %7, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 4)
%15 = icmp eq i64 %14, 0
%16 = select i1 %15, i32 2, i32 -1
br label %17
17: ; preds = %4, %10, %13, %1
%18 = phi i32 [ -1, %1 ], [ 0, %4 ], [ 1, %10 ], [ %16, %13 ]
ret i32 %18
}