#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*/ SEEK_END;
int /*<<< orphan*/ fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
scalar_t__ malloc(int);
scalar_t__ memcmp(char *, char *, int);
int search_EOCD(FILE *p, int file_size, int EOCD_SIZE) {
  char *ptr;
  char *iobuf = (char *)malloc(EOCD_SIZE * sizeof(char));
  fseek(p, -(EOCD_SIZE), SEEK_END);
  fread(iobuf, 1, EOCD_SIZE, p);
  char *magic = "PK\5\6";
  int EOCD_offset = 0;
  for (ptr = (iobuf + (EOCD_SIZE - 22)); ptr >= iobuf; ptr -= 4) {
    if (memcmp(magic, ptr, 4) == 0) {
      EOCD_offset = file_size - (EOCD_SIZE - (ptr - iobuf));
      break;
    }
  }
  free(iobuf);
  return EOCD_offset;
}


x86 assembly (clang, -O0):
<search_EOCD>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
movslq -0x10(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
xor    %esi,%esi
sub    -0x10(%rbp),%esi
mov    0x2e63(%rip),%rax
mov    (%rax),%edx
call   1070 <fseek@plt>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%edx
mov    -0x8(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
lea    0xe58(%rip),%rax
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x20(%rbp),%rax
mov    -0x10(%rbp),%ecx
sub    $0x16,%ecx
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
cmp    -0x20(%rbp),%rax
jb     1228 <search_EOCD+0xd8>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    $0x4,%edx
call   1050 <memcmp@plt>
cmp    $0x0,%rax
jne    1212 <search_EOCD+0xc2>
movslq -0xc(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rsi
sub    %rsi,%rdx
sub    %rdx,%rcx
sub    %rcx,%rax
mov    %eax,-0x2c(%rbp)
jmp    1228 <search_EOCD+0xd8>
jmp    1217 <search_EOCD+0xc7>
mov    -0x18(%rbp),%rax
add    $0xfffffffffffffffc,%rax
mov    %rax,-0x18(%rbp)
jmp    11c7 <search_EOCD+0x77>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x2c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<search_EOCD>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%ebx
mov    %esi,%ebp
mov    %rdi,%r15
mov    %edx,%r12d
neg    %r12d
mov    %edx,%edi
call   1060 <malloc@plt>
mov    %rax,%r14
mov    0x2e72(%rip),%rax
mov    (%rax),%edx
mov    %r15,%rdi
mov    %r12d,%esi
call   1070 <fseek@plt>
mov    %r14,%rdi
mov    $0x1,%esi
mov    %ebx,%edx
mov    %r15,%rcx
call   1040 <fread@plt>
xor    %r15d,%r15d
cmp    $0x16,%ebx
jl     11d6 <search_EOCD+0x86>
add    $0xffffffea,%ebx
add    %r14,%rbx
add    $0xffffffea,%ebp
lea    0xe53(%rip),%r12
nopl   (%rax)
mov    %r12,%rdi
mov    %rbx,%rsi
mov    $0x4,%edx
call   1050 <memcmp@plt>
test   %rax,%rax
je     11d3 <search_EOCD+0x83>
add    $0xfffffffffffffffc,%rbx
add    $0xfffffffc,%ebp
cmp    %r14,%rbx
jae    11b0 <search_EOCD+0x60>
jmp    11d6 <search_EOCD+0x86>
mov    %ebp,%r15d
mov    %r14,%rdi
call   1030 <free@plt>
mov    %r15d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@SEEK_END = global i32 0
@.str = [5 x i8] c"PK\05\06\00"
define i32 @search_EOCD(i32* %0, i32 %1, i32 %2) {
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32
store i32* %0, i32** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%11 = load i32, i32* %6
%12 = sext i32 %11 to i64
%13 = mul i64 %12, 1
%14 = trunc i64 %13 to i32
%15 = call i64 @malloc(i32 %14)
%16 = inttoptr i64 %15 to i8*
store i8* %16, i8** %8
%17 = load i32*, i32** %4
%18 = load i32, i32* %6
%19 = sub nsw i32 0, %18
%20 = load i32, i32* @SEEK_END
%21 = call i32 @fseek(i32* %17, i32 %19, i32 %20)
%22 = load i8*, i8** %8
%23 = load i32, i32* %6
%24 = load i32*, i32** %4
%25 = call i32 @fread(i8* %22, i32 1, i32 %23, i32* %24)
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8** %9
store i32 0, i32* %10
%26 = load i8*, i8** %8
%27 = load i32, i32* %6
%28 = sub nsw i32 %27, 22
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %26, i64 %29
store i8* %30, i8** %7
br label %31
31: ; preds = %54, %3
%32 = load i8*, i8** %7
%33 = load i8*, i8** %8
%34 = icmp uge i8* %32, %33
br i1 %34, label %35, label %57
35: ; preds = %31
%36 = load i8*, i8** %9
%37 = load i8*, i8** %7
%38 = call i64 @memcmp(i8* %36, i8* %37, i32 4)
%39 = icmp eq i64 %38, 0
br i1 %39, label %40, label %53
40: ; preds = %35
%41 = load i32, i32* %5
%42 = sext i32 %41 to i64
%43 = load i32, i32* %6
%44 = sext i32 %43 to i64
%45 = load i8*, i8** %7
%46 = load i8*, i8** %8
%47 = ptrtoint i8* %45 to i64
%48 = ptrtoint i8* %46 to i64
%49 = sub i64 %47, %48
%50 = sub nsw i64 %44, %49
%51 = sub nsw i64 %42, %50
%52 = trunc i64 %51 to i32
store i32 %52, i32* %10
br label %57
53: ; preds = %35
br label %54
54: ; preds = %53
%55 = load i8*, i8** %7
%56 = getelementptr inbounds i8, i8* %55, i64 -4
store i8* %56, i8** %7
br label %31
57: ; preds = %40, %31
%58 = load i8*, i8** %8
%59 = call i32 @free(i8* %58)
%60 = load i32, i32* %10
ret i32 %60
}

LLVM IR (-O2):
@SEEK_END = global i32 0
@.str = [5 x i8] c"PK\05\06\00"
define i32 @search_EOCD(i32* nocapture %0, i32 %1, i32 %2) {
%4 = sub i32 0, %2
%5 = tail call i64 @malloc(i32 %2)
%6 = inttoptr i64 %5 to i8*
%7 = load i32, i32* @SEEK_END
%8 = tail call i32 @fseek(i32* %0, i32 %4, i32 %7)
%9 = tail call i32 @fread(i8* %6, i32 1, i32 %2, i32* %0)
%10 = icmp slt i32 %2, 22
br i1 %10, label %28, label %11
11: ; preds = %3
%12 = add nsw i32 %2, -22
%13 = zext i32 %12 to i64
%14 = getelementptr inbounds i8, i8* %6, i64 %13
br label %15
15: ; preds = %11, %25
%16 = phi i8* [ %26, %25 ], [ %14, %11 ]
%17 = tail call i64 @memcmp(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %16, i32 4)
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %25
19: ; preds = %15
%20 = ptrtoint i8* %16 to i64
%21 = sub i64 %20, %5
%22 = trunc i64 %21 to i32
%23 = sub i32 %1, %2
%24 = add i32 %23, %22
br label %28
25: ; preds = %15
%26 = getelementptr inbounds i8, i8* %16, i64 -4
%27 = icmp ult i8* %26, %6
br i1 %27, label %28, label %15
28: ; preds = %25, %3, %19
%29 = phi i32 [ %24, %19 ], [ 0, %3 ], [ 0, %25 ]
%30 = tail call i32 @free(i8* %6)
ret i32 %29
}