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

/* Variables and functions */
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
scalar_t__ strcmp(char *, char *);
int strlen(char *);
int open_wav_r(char *filename, FILE **fp) {
  int filenamelen;
  char *fileext;
  filenamelen = strlen(filename);
  fileext = &filename[filenamelen - 4];
  if (strcmp(fileext, ".wav") != 0) {
    printf("Error: Input file must be a .wav file.\n");
    return -1;
  }
  *fp = fopen(filename, "rb");
  if (*fp == NULL) {
    perror(filename);
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<open_wav_r>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x1c(%rbp),%ecx
sub    $0x4,%ecx
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
lea    0xe75(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
je     11b2 <open_wav_r+0x62>
lea    0xe64(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f6 <open_wav_r+0xa6>
mov    -0x10(%rbp),%rdi
lea    0xe70(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
cmpq   $0x0,(%rax)
jne    11ef <open_wav_r+0x9f>
mov    -0x10(%rbp),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f6 <open_wav_r+0xa6>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_wav_r>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
call   1040 <strlen@plt>
cltq
lea    (%rax,%rbx,1),%rdi
add    $0xfffffffffffffffc,%rdi
lea    0xe90(%rip),%rsi
call   1050 <strcmp@plt>
test   %rax,%rax
je     118d <open_wav_r+0x3d>
lea    0xe87(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%eax
jmp    11a9 <open_wav_r+0x59>
lea    0xe71(%rip),%rsi
mov    %rbx,%rdi
call   1060 <fopen@plt>
mov    %rax,%rcx
mov    %rax,(%r14)
xor    %eax,%eax
test   %rcx,%rcx
je     11b1 <open_wav_r+0x61>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    %rbx,%rdi
call   1070 <perror@plt>
jmp    1186 <open_wav_r+0x36>

LLVM IR (-O0):
@.str = [5 x i8] c".wav\00"
@.str.1 = [40 x i8] c"Error: Input file must be a .wav file.\0A\00"
@.str.2 = [3 x i8] c"rb\00"
define i32 @open_wav_r(i8* %0, i32** %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32**
%6 = alloca i32
%7 = alloca i8*
store i8* %0, i8** %4
store i32** %1, i32*** %5
%8 = load i8*, i8** %4
%9 = call i32 @strlen(i8* %8)
store i32 %9, i32* %6
%10 = load i8*, i8** %4
%11 = load i32, i32* %6
%12 = sub nsw i32 %11, 4
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds i8, i8* %10, i64 %13
store i8* %14, i8** %7
%15 = load i8*, i8** %7
%16 = call i64 @strcmp(i8* %15, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %2
%19 = call i32 @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %31
20: ; preds = %2
%21 = load i8*, i8** %4
%22 = call i32* @fopen(i8* %21, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%23 = load i32**, i32*** %5
store i32* %22, i32** %23
%24 = load i32**, i32*** %5
%25 = load i32*, i32** %24
%26 = icmp eq i32* %25, null
br i1 %26, label %27, label %30
27: ; preds = %20
%28 = load i8*, i8** %4
%29 = call i32 @perror(i8* %28)
store i32 -1, i32* %3
br label %31
30: ; preds = %20
store i32 0, i32* %3
br label %31
31: ; preds = %30, %27, %18
%32 = load i32, i32* %3
ret i32 %32
}

LLVM IR (-O2):
@.str = [5 x i8] c".wav\00"
@.str.2 = [3 x i8] c"rb\00"
@str = [39 x i8] c"Error: Input file must be a .wav file.\00"
define i32 @open_wav_r(i8* %0, i32** nocapture writeonly %1) {
%3 = tail call i32 @strlen(i8* %0)
%4 = add nsw i32 %3, -4
%5 = sext i32 %4 to i64
%6 = getelementptr inbounds i8, i8* %0, i64 %5
%7 = tail call i64 @strcmp(i8* %6, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%8 = icmp eq i64 %7, 0
br i1 %8, label %11, label %9
9: ; preds = %2
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @str, i64 0, i64 0))
br label %16
11: ; preds = %2
%12 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32* %12, i32** %1
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %16
14: ; preds = %11
%15 = tail call i32 @perror(i8* %0)
br label %16
16: ; preds = %11, %14, %9
%17 = phi i32 [ -1, %9 ], [ -1, %14 ], [ 0, %11 ]
ret i32 %17
}