#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*/ buf;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int fread(unsigned char **, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, size_t, int /*<<< orphan*/);
size_t ftell(int /*<<< orphan*/ *);
scalar_t__ is_id3v1(int /*<<< orphan*/ *);
scalar_t__ is_tag_ape2long(unsigned char *);
scalar_t__ memcmp(unsigned char *, char *, int);
int /*<<< orphan*/ memset(unsigned char *, int /*<<< orphan*/, int);
int is_ape_ver(FILE *fp) {
  char unsigned buf[32];
  size_t savedFilePosition;
  savedFilePosition = ftell(fp);
  memset(buf, 0, sizeof(buf));
  fseek(fp, (is_id3v1(fp) ? -32 - 128 : -32), SEEK_END);
  if (fread(&buf, 1, sizeof(buf), fp) != sizeof(buf)) {
    fseek(fp, savedFilePosition, SEEK_SET);
    return 0;
  }
  if (memcmp(buf, "APETAGEX", 8) != 0) {
    fseek(fp, savedFilePosition, SEEK_SET);
    return 0;
  }
  fseek(fp, savedFilePosition, SEEK_SET);
  return (int)is_tag_ape2long(buf + 8);
}


x86 assembly (clang, -O0):
<is_ape_ver>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1070 <ftell@plt>
mov    %rax,-0x38(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1050 <memset@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rdi
call   1090 <is_id3v1@plt>
mov    -0x40(%rbp),%rdi
mov    %rax,%rdx
mov    $0xffffffe0,%eax
mov    $0xffffff60,%ecx
cmp    $0x0,%rdx
cmovne %ecx,%eax
movslq %eax,%rsi
mov    0x2e1c(%rip),%rax
mov    (%rax),%edx
call   1080 <fseek@plt>
lea    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
mov    $0x20,%edx
call   1030 <fread@plt>
cltq
cmp    $0x20,%rax
je     1218 <is_ape_ver+0xa8>
mov    -0x10(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    0x2dd3(%rip),%rax
mov    (%rax),%edx
call   1080 <fseek@plt>
movl   $0x0,-0x4(%rbp)
jmp    127f <is_ape_ver+0x10f>
lea    -0x30(%rbp),%rdi
lea    0xddd(%rip),%rsi
mov    $0x8,%edx
call   1060 <memcmp@plt>
cmp    $0x0,%rax
je     1259 <is_ape_ver+0xe9>
mov    -0x10(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    0x2d92(%rip),%rax
mov    (%rax),%edx
call   1080 <fseek@plt>
movl   $0x0,-0x4(%rbp)
jmp    127f <is_ape_ver+0x10f>
mov    -0x10(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    0x2d70(%rip),%rax
mov    (%rax),%edx
call   1080 <fseek@plt>
lea    -0x30(%rbp),%rdi
add    $0x8,%rdi
call   1040 <is_tag_ape2long@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<is_ape_ver>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rdi,%rbx
call   1070 <ftell@plt>
mov    %rax,%r15
mov    %rsp,%r12
xor    %r14d,%r14d
mov    %r12,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1050 <memset@plt>
mov    %rbx,%rdi
call   1090 <is_id3v1@plt>
xor    %esi,%esi
test   %rax,%rax
sete   %sil
shl    $0x7,%esi
or     $0xffffffffffffff60,%rsi
mov    0x2e2b(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
call   1080 <fseek@plt>
mov    %r12,%rdi
mov    $0x1,%esi
mov    $0x20,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
cmp    $0x20,%eax
jne    1220 <is_ape_ver+0xb0>
lea    0xe18(%rip),%rsi
mov    %rsp,%rdi
mov    $0x8,%edx
call   1060 <memcmp@plt>
mov    %rax,%r12
mov    0x2dd9(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    %r15,%rsi
call   1080 <fseek@plt>
test   %r12,%r12
jne    1234 <is_ape_ver+0xc4>
lea    0x8(%rsp),%rdi
call   1040 <is_tag_ape2long@plt>
mov    %rax,%r14
jmp    1234 <is_ape_ver+0xc4>
mov    0x2db1(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    %r15,%rsi
call   1080 <fseek@plt>
mov    %r14d,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str = [9 x i8] c"APETAGEX\00"
define i32 @is_ape_ver(i32* %0) {
%2 = alloca i32
%3 = alloca i32*
%4 = alloca [32 x i8]
%5 = alloca i64
store i32* %0, i32** %3
%6 = load i32*, i32** %3
%7 = call i64 @ftell(i32* %6)
store i64 %7, i64* %5
%8 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 0
%9 = call i32 @memset(i8* %8, i32 0, i32 32)
%10 = load i32*, i32** %3
%11 = load i32*, i32** %3
%12 = call i64 @is_id3v1(i32* %11)
%13 = icmp ne i64 %12, 0
%14 = zext i1 %13 to i64
%15 = select i1 %13, i32 -160, i32 -32
%16 = sext i32 %15 to i64
%17 = load i32, i32* @SEEK_END
%18 = call i32 @fseek(i32* %10, i64 %16, i32 %17)
%19 = bitcast [32 x i8]* %4 to i8**
%20 = load i32*, i32** %3
%21 = call i32 @fread(i8** %19, i32 1, i32 32, i32* %20)
%22 = sext i32 %21 to i64
%23 = icmp ne i64 %22, 32
br i1 %23, label %24, label %29
24: ; preds = %1
%25 = load i32*, i32** %3
%26 = load i64, i64* %5
%27 = load i32, i32* @SEEK_SET
%28 = call i32 @fseek(i32* %25, i64 %26, i32 %27)
store i32 0, i32* %2
br label %47
29: ; preds = %1
%30 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 0
%31 = call i64 @memcmp(i8* %30, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 8)
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %38
33: ; preds = %29
%34 = load i32*, i32** %3
%35 = load i64, i64* %5
%36 = load i32, i32* @SEEK_SET
%37 = call i32 @fseek(i32* %34, i64 %35, i32 %36)
store i32 0, i32* %2
br label %47
38: ; preds = %29
%39 = load i32*, i32** %3
%40 = load i64, i64* %5
%41 = load i32, i32* @SEEK_SET
%42 = call i32 @fseek(i32* %39, i64 %40, i32 %41)
%43 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 0
%44 = getelementptr inbounds i8, i8* %43, i64 8
%45 = call i64 @is_tag_ape2long(i8* %44)
%46 = trunc i64 %45 to i32
store i32 %46, i32* %2
br label %47
47: ; preds = %38, %33, %24
%48 = load i32, i32* %2
ret i32 %48
}

LLVM IR (-O2):
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str = [9 x i8] c"APETAGEX\00"
define i32 @is_ape_ver(i32* %0) {
%2 = alloca [32 x i8]
%3 = getelementptr inbounds [32 x i8], [32 x i8]* %2, i64 0, i64 0
%4 = tail call i64 @ftell(i32* %0)
%5 = call i32 @memset(i8* %3, i32 0, i32 32)
%6 = call i64 @is_id3v1(i32* %0)
%7 = icmp eq i64 %6, 0
%8 = select i1 %7, i64 -32, i64 -160
%9 = load i32, i32* @SEEK_END
%10 = call i32 @fseek(i32* %0, i64 %8, i32 %9)
%11 = bitcast [32 x i8]* %2 to i8**
%12 = call i32 @fread(i8** %11, i32 1, i32 32, i32* %0)
%13 = icmp eq i32 %12, 32
br i1 %13, label %17, label %14
14: ; preds = %1
%15 = load i32, i32* @SEEK_SET
%16 = call i32 @fseek(i32* %0, i64 %4, i32 %15)
br label %26
17: ; preds = %1
%18 = call i64 @memcmp(i8* %3, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 8)
%19 = icmp eq i64 %18, 0
%20 = load i32, i32* @SEEK_SET
%21 = call i32 @fseek(i32* %0, i64 %4, i32 %20)
br i1 %19, label %22, label %26
22: ; preds = %17
%23 = getelementptr inbounds [32 x i8], [32 x i8]* %2, i64 0, i64 8
%24 = call i64 @is_tag_ape2long(i8* %23)
%25 = trunc i64 %24 to i32
br label %26
26: ; preds = %17, %22, %14
%27 = phi i32 [ 0, %14 ], [ %25, %22 ], [ 0, %17 ]
ret i32 %27
}