#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*/ *);
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);
  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 + 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>
cltq
cmp    $0x20,%rax
je     1218 <is_ape+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    12e2 <is_ape+0x172>
lea    -0x30(%rbp),%rdi
lea    0xddd(%rip),%rsi
mov    $0x8,%edx
call   1060 <memcmp@plt>
cmp    $0x0,%rax
je     1259 <is_ape+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    12e2 <is_ape+0x172>
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
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    12c8 <is_ape+0x158>
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>
cmp    $0x20,%eax
jne    1252 <is_ape+0xe2>
lea    0xe17(%rip),%rsi
mov    %rsp,%rdi
mov    $0x8,%edx
call   1060 <memcmp@plt>
mov    %rax,%rbp
mov    0x2dd8(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1080 <fseek@plt>
test   %rbp,%rbp
jne    1266 <is_ape+0xf6>
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    124d <is_ape+0xdd>
lea    0x14(%rsp),%rdi
call   1040 <is_tag_ape2long@plt>
mov    %eax,%r12d
shr    $0x19,%r12d
not    %r12d
and    $0x20,%r12d
add    %ebx,%r12d
jmp    1266 <is_ape+0xf6>
mov    0x2d7f(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1080 <fseek@plt>
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
@SEEK_SET = global i32 0
@.str = [9 x i8] c"APETAGEX\00"
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 = 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 %65
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 %65
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 = getelementptr inbounds i8, i8* %44, i64 4
%46 = call i32 @is_tag_ape2long(i8* %45)
%47 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 0
%48 = getelementptr inbounds i8, i8* %47, i64 8
%49 = call i32 @is_tag_ape2long(i8* %48)
%50 = icmp eq i32 %49, 2000
br i1 %50, label %51, label %60
51: ; preds = %38
%52 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 0
%53 = getelementptr inbounds i8, i8* %52, i64 8
%54 = getelementptr inbounds i8, i8* %53, i64 4
%55 = getelementptr inbounds i8, i8* %54, i64 8
%56 = call i32 @is_tag_ape2long(i8* %55)
%57 = and i32 %56, 1073741824
%58 = icmp ne i32 %57, 0
%59 = xor i1 %58, true
br label %60
60: ; preds = %51, %38
%61 = phi i1 [ false, %38 ], [ %59, %51 ]
%62 = zext i1 %61 to i64
%63 = select i1 %61, i32 32, i32 0
%64 = add nsw i32 %46, %63
store i32 %64, i32* %2
br label %65
65: ; preds = %60, %33, %24
%66 = load i32, i32* %2
ret i32 %66
}

LLVM IR (-O2):
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str = [9 x i8] c"APETAGEX\00"
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 = 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 %37
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 %37
22: ; preds = %17
%23 = getelementptr inbounds [32 x i8], [32 x i8]* %2, i64 0, i64 8
%24 = getelementptr inbounds [32 x i8], [32 x i8]* %2, i64 0, i64 12
%25 = call i32 @is_tag_ape2long(i8* %24)
%26 = call i32 @is_tag_ape2long(i8* %23)
%27 = icmp eq i32 %26, 2000
br i1 %27, label %28, label %34
28: ; preds = %22
%29 = getelementptr inbounds [32 x i8], [32 x i8]* %2, i64 0, i64 20
%30 = call i32 @is_tag_ape2long(i8* %29)
%31 = lshr i32 %30, 25
%32 = and i32 %31, 32
%33 = xor i32 %32, 32
br label %34
34: ; preds = %28, %22
%35 = phi i32 [ 0, %22 ], [ %33, %28 ]
%36 = add nsw i32 %35, %25
br label %37
37: ; preds = %17, %34, %14
%38 = phi i32 [ 0, %14 ], [ %36, %34 ], [ 0, %17 ]
ret i32 %38
}