#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 /*<<< orphan*/ 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*/ *);
int is_tag_ape2long(unsigned char *);
scalar_t__ memcmp(unsigned char *, char *, int);
int /*<<< orphan*/ memset(unsigned char *, int /*<<< orphan*/, int);
int is_ape(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);
  fread(&buf, 1, sizeof(buf), fp);
  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 + 4) +
               (((is_tag_ape2long(buf + 8) == 2000) &&
                 !(is_tag_ape2long(buf + 8 + 4 + 8) & 0x40000000))
                    ? 32
                    : 0));
}


x86 assembly (clang, -O0):
<is_ape>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%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>
lea    -0x30(%rbp),%rdi
lea    0xe0b(%rip),%rsi
mov    $0x8,%edx
call   1060 <memcmp@plt>
cmp    $0x0,%rax
je     122b <is_ape+0xbb>
mov    -0x10(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    0x2dc0(%rip),%rax
mov    (%rax),%edx
call   1080 <fseek@plt>
movl   $0x0,-0x4(%rbp)
jmp    12b4 <is_ape+0x144>
mov    -0x10(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    0x2d9e(%rip),%rax
mov    (%rax),%edx
call   1080 <fseek@plt>
lea    -0x30(%rbp),%rdi
add    $0x8,%rdi
add    $0x4,%rdi
call   1040 <is_tag_ape2long@plt>
mov    %eax,-0x48(%rbp)
lea    -0x30(%rbp),%rdi
add    $0x8,%rdi
call   1040 <is_tag_ape2long@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0x7d0,%ecx
mov    %al,-0x41(%rbp)
jne    129a <is_ape+0x12a>
lea    -0x30(%rbp),%rdi
add    $0x8,%rdi
add    $0x4,%rdi
add    $0x8,%rdi
call   1040 <is_tag_ape2long@plt>
and    $0x40000000,%eax
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
mov    %al,-0x41(%rbp)
mov    -0x48(%rbp),%eax
mov    -0x41(%rbp),%sil
xor    %ecx,%ecx
mov    $0x20,%edx
test   $0x1,%sil
cmovne %edx,%ecx
add    %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<is_ape>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %rdi,%rbx
call   1070 <ftell@plt>
mov    %rax,%r14
mov    %rsp,%r15
xor    %r12d,%r12d
mov    %r15,%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    0x2e2a(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
call   1080 <fseek@plt>
mov    %r15,%rdi
mov    $0x1,%esi
mov    $0x20,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
lea    0xe1c(%rip),%rsi
mov    %r15,%rdi
mov    $0x8,%edx
call   1060 <memcmp@plt>
mov    %rax,%rbp
mov    0x2ddd(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1080 <fseek@plt>
test   %rbp,%rbp
jne    124b <is_ape+0xdb>
lea    0x8(%rsp),%rbp
lea    0xc(%rsp),%rdi
call   1040 <is_tag_ape2long@plt>
mov    %eax,%ebx
mov    %rbp,%rdi
call   1040 <is_tag_ape2long@plt>
xor    %r12d,%r12d
cmp    $0x7d0,%eax
jne    1248 <is_ape+0xd8>
lea    0x14(%rsp),%rdi
call   1040 <is_tag_ape2long@plt>
mov    %eax,%r12d
shr    $0x19,%r12d
not    %r12d
and    $0x20,%r12d
add    %ebx,%r12d
mov    %r12d,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@SEEK_END = global i32 0
@.str = [9 x i8] c"APETAGEX\00"
@SEEK_SET = global i32 0
define i32 @is_ape(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 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 0
%23 = call i64 @memcmp(i8* %22, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 8)
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %30
25: ; preds = %1
%26 = load i32*, i32** %3
%27 = load i64, i64* %5
%28 = load i32, i32* @SEEK_SET
%29 = call i32 @fseek(i32* %26, i64 %27, i32 %28)
store i32 0, i32* %2
br label %57
30: ; preds = %1
%31 = load i32*, i32** %3
%32 = load i64, i64* %5
%33 = load i32, i32* @SEEK_SET
%34 = call i32 @fseek(i32* %31, i64 %32, i32 %33)
%35 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 0
%36 = getelementptr inbounds i8, i8* %35, i64 8
%37 = getelementptr inbounds i8, i8* %36, i64 4
%38 = call i32 @is_tag_ape2long(i8* %37)
%39 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 0
%40 = getelementptr inbounds i8, i8* %39, i64 8
%41 = call i32 @is_tag_ape2long(i8* %40)
%42 = icmp eq i32 %41, 2000
br i1 %42, label %43, label %52
43: ; preds = %30
%44 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 0
%45 = getelementptr inbounds i8, i8* %44, i64 8
%46 = getelementptr inbounds i8, i8* %45, i64 4
%47 = getelementptr inbounds i8, i8* %46, i64 8
%48 = call i32 @is_tag_ape2long(i8* %47)
%49 = and i32 %48, 1073741824
%50 = icmp ne i32 %49, 0
%51 = xor i1 %50, true
br label %52
52: ; preds = %43, %30
%53 = phi i1 [ false, %30 ], [ %51, %43 ]
%54 = zext i1 %53 to i64
%55 = select i1 %53, i32 32, i32 0
%56 = add nsw i32 %38, %55
store i32 %56, i32* %2
br label %57
57: ; preds = %52, %25
%58 = load i32, i32* %2
ret i32 %58
}

LLVM IR (-O2):
@SEEK_END = global i32 0
@.str = [9 x i8] c"APETAGEX\00"
@SEEK_SET = global i32 0
define i32 @is_ape(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 = call i64 @memcmp(i8* %3, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 8)
%14 = icmp eq i64 %13, 0
%15 = load i32, i32* @SEEK_SET
%16 = call i32 @fseek(i32* %0, i64 %4, i32 %15)
br i1 %14, label %17, label %32
17: ; preds = %1
%18 = getelementptr inbounds [32 x i8], [32 x i8]* %2, i64 0, i64 8
%19 = getelementptr inbounds [32 x i8], [32 x i8]* %2, i64 0, i64 12
%20 = call i32 @is_tag_ape2long(i8* %19)
%21 = call i32 @is_tag_ape2long(i8* %18)
%22 = icmp eq i32 %21, 2000
br i1 %22, label %23, label %29
23: ; preds = %17
%24 = getelementptr inbounds [32 x i8], [32 x i8]* %2, i64 0, i64 20
%25 = call i32 @is_tag_ape2long(i8* %24)
%26 = lshr i32 %25, 25
%27 = and i32 %26, 32
%28 = xor i32 %27, 32
br label %29
29: ; preds = %23, %17
%30 = phi i32 [ 0, %17 ], [ %28, %23 ]
%31 = add nsw i32 %30, %20
br label %32
32: ; preds = %1, %29
%33 = phi i32 [ %31, %29 ], [ 0, %1 ]
ret i32 %33
}