#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 */
struct sampler {
  int dummy;
};

/* Variables and functions */
int EIO;
int bmp_open(struct sampler **, char const *);
scalar_t__ memcmp(char const *, char *, int);
size_t strlen(char const *);
int wav_open(struct sampler **, char const *);
int open_sampler_by_extension(struct sampler **sampler, const char *filename) {
  size_t len;
  len = strlen(filename);
  if (len < 5)
    return -EIO;
  if (memcmp(&filename[len - 4], ".bmp", 4) == 0)
    return bmp_open(sampler, filename);
  if (memcmp(&filename[len - 4], ".wav", 4) == 0)
    return wav_open(sampler, filename);
  return -EIO;
}


x86 assembly (clang, -O0):
<open_sampler_by_extension>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x5,-0x20(%rbp)
jae    117b <open_sampler_by_extension+0x3b>
xor    %eax,%eax
mov    0x2e6f(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1207 <open_sampler_by_extension+0xc7>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rax
sub    $0x4,%rax
add    %rax,%rdi
lea    0xe6f(%rip),%rsi
mov    $0x4,%edx
call   1050 <memcmp@plt>
cmp    $0x0,%rax
jne    11ba <open_sampler_by_extension+0x7a>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1030 <bmp_open@plt>
mov    %eax,-0x4(%rbp)
jmp    1207 <open_sampler_by_extension+0xc7>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rax
sub    $0x4,%rax
add    %rax,%rdi
lea    0xe35(%rip),%rsi
mov    $0x4,%edx
call   1050 <memcmp@plt>
cmp    $0x0,%rax
jne    11f9 <open_sampler_by_extension+0xb9>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <wav_open@plt>
mov    %eax,-0x4(%rbp)
jmp    1207 <open_sampler_by_extension+0xc7>
xor    %eax,%eax
mov    0x2dde(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_sampler_by_extension>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%rbx
mov    %rdi,%r14
mov    %rsi,%rdi
call   1040 <strlen@plt>
cmp    $0x4,%rax
jbe    1193 <open_sampler_by_extension+0x53>
lea    (%rax,%rbx,1),%r15
add    $0xfffffffffffffffc,%r15
lea    0xe98(%rip),%rsi
mov    %r15,%rdi
mov    $0x4,%edx
call   1050 <memcmp@plt>
test   %rax,%rax
je     11a4 <open_sampler_by_extension+0x64>
lea    0xe84(%rip),%rsi
mov    %r15,%rdi
mov    $0x4,%edx
call   1050 <memcmp@plt>
test   %rax,%rax
je     11b4 <open_sampler_by_extension+0x74>
mov    0x2e46(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
pop    %rbx
pop    %r14
pop    %r15
ret
mov    %r14,%rdi
mov    %rbx,%rsi
pop    %rbx
pop    %r14
pop    %r15
jmp    1030 <bmp_open@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
pop    %rbx
pop    %r14
pop    %r15
jmp    1060 <wav_open@plt>

LLVM IR (-O0):
%struct.sampler = type { i32 }
@EIO = global i32 0
@.str = [5 x i8] c".bmp\00"
@.str.1 = [5 x i8] c".wav\00"
define i32 @open_sampler_by_extension(%struct.sampler** %0, i8* %1) {
%3 = alloca i32
%4 = alloca %struct.sampler**
%5 = alloca i8*
%6 = alloca i64
store %struct.sampler** %0, %struct.sampler*** %4
store i8* %1, i8** %5
%7 = load i8*, i8** %5
%8 = call i64 @strlen(i8* %7)
store i64 %8, i64* %6
%9 = load i64, i64* %6
%10 = icmp ult i64 %9, 5
br i1 %10, label %11, label %14
11: ; preds = %2
%12 = load i32, i32* @EIO
%13 = sub nsw i32 0, %12
store i32 %13, i32* %3
br label %39
14: ; preds = %2
%15 = load i8*, i8** %5
%16 = load i64, i64* %6
%17 = sub i64 %16, 4
%18 = getelementptr inbounds i8, i8* %15, i64 %17
%19 = call i64 @memcmp(i8* %18, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 4)
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %25
21: ; preds = %14
%22 = load %struct.sampler**, %struct.sampler*** %4
%23 = load i8*, i8** %5
%24 = call i32 @bmp_open(%struct.sampler** %22, i8* %23)
store i32 %24, i32* %3
br label %39
25: ; preds = %14
%26 = load i8*, i8** %5
%27 = load i64, i64* %6
%28 = sub i64 %27, 4
%29 = getelementptr inbounds i8, i8* %26, i64 %28
%30 = call i64 @memcmp(i8* %29, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 4)
%31 = icmp eq i64 %30, 0
br i1 %31, label %32, label %36
32: ; preds = %25
%33 = load %struct.sampler**, %struct.sampler*** %4
%34 = load i8*, i8** %5
%35 = call i32 @wav_open(%struct.sampler** %33, i8* %34)
store i32 %35, i32* %3
br label %39
36: ; preds = %25
%37 = load i32, i32* @EIO
%38 = sub nsw i32 0, %37
store i32 %38, i32* %3
br label %39
39: ; preds = %36, %32, %21, %11
%40 = load i32, i32* %3
ret i32 %40
}

LLVM IR (-O2):
%struct.sampler = type { i32 }
@EIO = global i32 0
@.str = [5 x i8] c".bmp\00"
@.str.1 = [5 x i8] c".wav\00"
define i32 @open_sampler_by_extension(%struct.sampler** %0, i8* %1) {
%3 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%4 = icmp ult i64 %3, 5
br i1 %4, label %5, label %8
5: ; preds = %2
%6 = load i32, i32* @EIO
%7 = sub nsw i32 0, %6
br label %23
8: ; preds = %2
%9 = add i64 %3, -4
%10 = getelementptr inbounds i8, i8* %1, i64 %9
%11 = tail call i64 @memcmp(i8* %10, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 4)
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %8
%14 = tail call i32 @bmp_open(%struct.sampler** %0, i8* %1)
br label %23
15: ; preds = %8
%16 = tail call i64 @memcmp(i8* %10, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 4)
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %15
%19 = tail call i32 @wav_open(%struct.sampler** %0, i8* %1)
br label %23
20: ; preds = %15
%21 = load i32, i32* @EIO
%22 = sub nsw i32 0, %21
br label %23
23: ; preds = %20, %18, %13, %5
%24 = phi i32 [ %7, %5 ], [ %14, %13 ], [ %19, %18 ], [ %22, %20 ]
ret i32 %24
}