#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(char **, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, size_t, int /*<<< orphan*/);
size_t ftell(int /*<<< orphan*/ *);
scalar_t__ memcmp(char *, char *, int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int is_id3v1(FILE *fp) {
  int n = 0;
  char buf[16];
  size_t savedFilePosition;
  savedFilePosition = ftell(fp);
  fseek(fp, 0, SEEK_END);
  do {
    n++;
    memset(buf, 0, sizeof(buf));
    fseek(fp, ((-128) * n) - 3, SEEK_END);
    if (fread(&buf, 1, sizeof(buf), fp) != sizeof(buf)) {
      fseek(fp, savedFilePosition, SEEK_SET);
      return 0;
    }
    if (memcmp(buf, "APETAGEX", 8) == 0)
      break;
  } while (memcmp(buf + 3, "TAG", 3) == 0);
  fseek(fp, savedFilePosition, SEEK_SET);
  return (n - 1) * 128;
}


x86 assembly (clang, -O0):
<is_id3v1>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <ftell@plt>
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e6d(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1070 <fseek@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    -0x10(%rbp),%rdi
imul   $0xffffff80,-0x14(%rbp),%eax
sub    $0x3,%eax
movslq %eax,%rsi
mov    0x2e31(%rip),%rax
mov    (%rax),%edx
call   1070 <fseek@plt>
lea    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
mov    $0x10,%edx
call   1030 <fread@plt>
cltq
cmp    $0x10,%rax
je     1203 <is_id3v1+0xb3>
mov    -0x10(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    0x2de8(%rip),%rax
mov    (%rax),%edx
call   1070 <fseek@plt>
movl   $0x0,-0x4(%rbp)
jmp    1271 <is_id3v1+0x121>
lea    -0x30(%rbp),%rdi
lea    0xdf2(%rip),%rsi
mov    $0x8,%edx
call   1050 <memcmp@plt>
cmp    $0x0,%rax
jne    1227 <is_id3v1+0xd7>
jmp    124f <is_id3v1+0xff>
jmp    122c <is_id3v1+0xdc>
lea    -0x30(%rbp),%rdi
add    $0x3,%rdi
lea    0xdce(%rip),%rsi
mov    $0x3,%edx
call   1050 <memcmp@plt>
cmp    $0x0,%rax
je     1186 <is_id3v1+0x36>
mov    -0x10(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    0x2d7a(%rip),%rax
mov    (%rax),%edx
call   1070 <fseek@plt>
mov    -0x14(%rbp),%eax
sub    $0x1,%eax
shl    $0x7,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<is_id3v1>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rdi,%r15
call   1060 <ftell@plt>
mov    %rax,0x8(%rsp)
mov    0x2e76(%rip),%rax
mov    (%rax),%edx
xor    %ebx,%ebx
mov    %r15,%rdi
xor    %esi,%esi
call   1070 <fseek@plt>
lea    0x10(%rsp),%r13
lea    0xe74(%rip),%rbp
lea    0xe76(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rbx,%r12
mov    %r13,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
lea    -0x83(%rbx),%rsi
mov    0x2e28(%rip),%rax
mov    (%rax),%edx
mov    %r15,%rdi
call   1070 <fseek@plt>
mov    %r13,%rdi
mov    $0x1,%esi
mov    $0x10,%edx
mov    %r15,%rcx
call   1030 <fread@plt>
cmp    $0x10,%eax
jne    1230 <is_id3v1+0xe0>
mov    %r13,%rdi
mov    %rbp,%rsi
mov    $0x8,%edx
call   1050 <memcmp@plt>
test   %rax,%rax
je     1215 <is_id3v1+0xc5>
lea    0x13(%rsp),%rdi
mov    %r14,%rsi
mov    $0x3,%edx
call   1050 <memcmp@plt>
lea    -0x80(%r12),%rbx
test   %rax,%rax
je     11a0 <is_id3v1+0x50>
mov    0x2dbc(%rip),%rax
mov    (%rax),%edx
mov    %r15,%rdi
mov    0x8(%rsp),%rsi
call   1070 <fseek@plt>
neg    %r12d
jmp    1249 <is_id3v1+0xf9>
mov    0x2da1(%rip),%rax
mov    (%rax),%edx
mov    %r15,%rdi
mov    0x8(%rsp),%rsi
call   1070 <fseek@plt>
xor    %r12d,%r12d
mov    %r12d,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
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"
@.str.1 = [4 x i8] c"TAG\00"
define i32 @is_id3v1(i32* %0) {
%2 = alloca i32
%3 = alloca i32*
%4 = alloca i32
%5 = alloca [16 x i8]
%6 = alloca i64
store i32* %0, i32** %3
store i32 0, i32* %4
%7 = load i32*, i32** %3
%8 = call i64 @ftell(i32* %7)
store i64 %8, i64* %6
%9 = load i32*, i32** %3
%10 = load i32, i32* @SEEK_END
%11 = call i32 @fseek(i32* %9, i64 0, i32 %10)
br label %12
12: ; preds = %40, %1
%13 = load i32, i32* %4
%14 = add nsw i32 %13, 1
store i32 %14, i32* %4
%15 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 0
%16 = call i32 @memset(i8* %15, i32 0, i32 16)
%17 = load i32*, i32** %3
%18 = load i32, i32* %4
%19 = mul nsw i32 -128, %18
%20 = sub nsw i32 %19, 3
%21 = sext i32 %20 to i64
%22 = load i32, i32* @SEEK_END
%23 = call i32 @fseek(i32* %17, i64 %21, i32 %22)
%24 = bitcast [16 x i8]* %5 to i8**
%25 = load i32*, i32** %3
%26 = call i32 @fread(i8** %24, i32 1, i32 16, i32* %25)
%27 = sext i32 %26 to i64
%28 = icmp ne i64 %27, 16
br i1 %28, label %29, label %34
29: ; preds = %12
%30 = load i32*, i32** %3
%31 = load i64, i64* %6
%32 = load i32, i32* @SEEK_SET
%33 = call i32 @fseek(i32* %30, i64 %31, i32 %32)
store i32 0, i32* %2
br label %53
34: ; preds = %12
%35 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 0
%36 = call i64 @memcmp(i8* %35, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 8)
%37 = icmp eq i64 %36, 0
br i1 %37, label %38, label %39
38: ; preds = %34
br label %45
39: ; preds = %34
br label %40
40: ; preds = %39
%41 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 0
%42 = getelementptr inbounds i8, i8* %41, i64 3
%43 = call i64 @memcmp(i8* %42, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 3)
%44 = icmp eq i64 %43, 0
br i1 %44, label %12, label %45
45: ; preds = %40, %38
%46 = load i32*, i32** %3
%47 = load i64, i64* %6
%48 = load i32, i32* @SEEK_SET
%49 = call i32 @fseek(i32* %46, i64 %47, i32 %48)
%50 = load i32, i32* %4
%51 = sub nsw i32 %50, 1
%52 = mul nsw i32 %51, 128
store i32 %52, i32* %2
br label %53
53: ; preds = %45, %29
%54 = load i32, i32* %2
ret i32 %54
}

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