#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 const *);
scalar_t__ strncmp(char const *, char *, int);
int get_filetype(const char *filename) {
  const char *ext = filename + (strlen(filename) - 4);
  if (ext < filename)
    return 0;
  if (strncmp(ext, ".bin", 4) == 0)
    return 1;
  if (strncmp(ext, ".hex", 4) == 0)
    return 2;
  return 0;
}


x86 assembly (clang, -O0):
<get_filetype>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
sub    $0x4,%ecx
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    116a <get_filetype+0x4a>
movl   $0x0,-0x4(%rbp)
jmp    11c7 <get_filetype+0xa7>
mov    -0x18(%rbp),%rdi
lea    0xe8b(%rip),%rsi
mov    $0x4,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    1195 <get_filetype+0x75>
movl   $0x1,-0x4(%rbp)
jmp    11c7 <get_filetype+0xa7>
mov    -0x18(%rbp),%rdi
lea    0xe65(%rip),%rsi
mov    $0x4,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    11c0 <get_filetype+0xa0>
movl   $0x2,-0x4(%rbp)
jmp    11c7 <get_filetype+0xa7>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_filetype>:
push   %rbx
mov    %rdi,%rbx
call   1040 <strlen@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0x4,%ecx
jl     1176 <get_filetype+0x56>
movslq %ecx,%rax
add    %rax,%rbx
add    $0xfffffffffffffffc,%rbx
lea    0xebd(%rip),%rsi
mov    %rbx,%rdi
mov    $0x4,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
je     1178 <get_filetype+0x58>
lea    0xea9(%rip),%rsi
mov    %rbx,%rdi
mov    $0x4,%edx
call   1030 <strncmp@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
sete   %al
add    %eax,%eax
pop    %rbx
ret
mov    $0x1,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [5 x i8] c".bin\00"
@.str.1 = [5 x i8] c".hex\00"
define i32 @get_filetype(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8*
store i8* %0, i8** %3
%5 = load i8*, i8** %3
%6 = load i8*, i8** %3
%7 = call i32 @strlen(i8* %6)
%8 = sub nsw i32 %7, 4
%9 = sext i32 %8 to i64
%10 = getelementptr inbounds i8, i8* %5, i64 %9
store i8* %10, i8** %4
%11 = load i8*, i8** %4
%12 = load i8*, i8** %3
%13 = icmp ult i8* %11, %12
br i1 %13, label %14, label %15
14: ; preds = %1
store i32 0, i32* %2
br label %26
15: ; preds = %1
%16 = load i8*, i8** %4
%17 = call i64 @strncmp(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 = %15
store i32 1, i32* %2
br label %26
20: ; preds = %15
%21 = load i8*, i8** %4
%22 = call i64 @strncmp(i8* %21, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 4)
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %20
store i32 2, i32* %2
br label %26
25: ; preds = %20
store i32 0, i32* %2
br label %26
26: ; preds = %25, %24, %19, %14
%27 = load i32, i32* %2
ret i32 %27
}

LLVM IR (-O2):
@.str = [5 x i8] c".bin\00"
@.str.1 = [5 x i8] c".hex\00"
define i32 @get_filetype(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = add nsw i32 %2, -4
%4 = sext i32 %3 to i64
%5 = getelementptr inbounds i8, i8* %0, i64 %4
%6 = icmp slt i32 %2, 4
br i1 %6, label %14, label %7
7: ; preds = %1
%8 = tail call i64 @strncmp(i8* %5, 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 %14, label %10
10: ; preds = %7
%11 = tail call i64 @strncmp(i8* %5, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 4)
%12 = icmp eq i64 %11, 0
%13 = select i1 %12, i32 2, i32 0
br label %14
14: ; preds = %10, %7, %1
%15 = phi i32 [ 0, %1 ], [ 1, %7 ], [ %13, %10 ]
ret i32 %15
}