#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_SET;
scalar_t__ calloc(int, int);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(char *, char *, int);
char *strstr(char *, char *);
scalar_t__ strtol(char *, char **, int);
int findXrefOffsetInMalformedFile(FILE *file) {
  fseek(file, (450 * 1000), SEEK_SET);
  char *readBuffer = (char *)calloc(1000, 1);
  int xrefOffset = 0;
  do {
    fread(readBuffer, 1000, 1, file);
    char *xrbuf = strstr(readBuffer, "startxref");
    if (xrbuf != NULL) {
      char *number = (char *)calloc(7, 1);
      strncpy(number, xrbuf + 9 + 1, 6);
      xrefOffset = (int)strtol(number, (char **)NULL, 10);
      free(number);
      if (xrefOffset < 400000 || xrefOffset > 525000)
        xrefOffset = 0;
    }
  } while (xrefOffset == 0 && !feof(file));
  free(readBuffer);
  return xrefOffset;
}


x86 assembly (clang, -O0):
<findXrefOffsetInMalformedFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e49(%rip),%rax
mov    (%rax),%edx
mov    $0x6ddd0,%esi
call   1090 <fseek@plt>
mov    $0x3e8,%edi
mov    $0x1,%esi
call   1060 <calloc@plt>
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    $0x3e8,%esi
mov    $0x1,%edx
call   1050 <fread@plt>
mov    -0x10(%rbp),%rdi
lea    0xe21(%rip),%rsi
call   10a0 <strstr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1264 <findXrefOffsetInMalformedFile+0xe4>
mov    $0x7,%edi
mov    $0x1,%esi
call   1060 <calloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
add    $0x9,%rsi
add    $0x1,%rsi
mov    $0x6,%edx
call   1040 <strncpy@plt>
mov    -0x28(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   1080 <strtol@plt>
mov    %eax,-0x14(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
cmpl   $0x61a80,-0x14(%rbp)
jl     1258 <findXrefOffsetInMalformedFile+0xd8>
cmpl   $0x802c8,-0x14(%rbp)
jle    125f <findXrefOffsetInMalformedFile+0xdf>
movl   $0x0,-0x14(%rbp)
jmp    1264 <findXrefOffsetInMalformedFile+0xe4>
jmp    1269 <findXrefOffsetInMalformedFile+0xe9>
xor    %eax,%eax
cmpl   $0x0,-0x14(%rbp)
mov    %al,-0x29(%rbp)
jne    128c <findXrefOffsetInMalformedFile+0x10c>
mov    -0x8(%rbp),%rdi
call   1070 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
test   $0x1,%al
jne    11bd <findXrefOffsetInMalformedFile+0x3d>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
mov    -0x14(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<findXrefOffsetInMalformedFile>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdi,%r14
mov    0x2e4d(%rip),%rax
mov    (%rax),%edx
mov    $0x6ddd0,%esi
call   1090 <fseek@plt>
mov    $0x3e8,%edi
mov    $0x1,%esi
call   1060 <calloc@plt>
mov    %rax,%r12
lea    0xe48(%rip),%r15
jmp    11cc <findXrefOffsetInMalformedFile+0x4c>
nopw   0x0(%rax,%rax,1)
mov    %r14,%rdi
call   1070 <feof@plt>
test   %eax,%eax
jne    1247 <findXrefOffsetInMalformedFile+0xc7>
mov    %r12,%rdi
mov    $0x3e8,%esi
mov    $0x1,%edx
mov    %r14,%rcx
call   1050 <fread@plt>
mov    %r12,%rdi
mov    %r15,%rsi
call   10a0 <strstr@plt>
test   %rax,%rax
je     11c0 <findXrefOffsetInMalformedFile+0x40>
mov    %rax,%rbx
mov    $0x7,%edi
mov    $0x1,%esi
call   1060 <calloc@plt>
mov    %rax,%r13
add    $0xa,%rbx
mov    %rax,%rdi
mov    %rbx,%rsi
mov    $0x6,%edx
call   1040 <strncpy@plt>
mov    %r13,%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1080 <strtol@plt>
mov    %rax,%rbx
mov    %r13,%rdi
call   1030 <free@plt>
lea    -0x802c9(%rbx),%eax
cmp    $0xfffe17b6,%eax
jbe    11c0 <findXrefOffsetInMalformedFile+0x40>
jmp    1249 <findXrefOffsetInMalformedFile+0xc9>
xor    %ebx,%ebx
mov    %r12,%rdi
call   1030 <free@plt>
mov    %ebx,%eax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@SEEK_SET = global i32 0
@.str = [10 x i8] c"startxref\00"
define i32 @findXrefOffsetInMalformedFile(i32* %0) {
%2 = alloca i32*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
store i32* %0, i32** %2
%7 = load i32*, i32** %2
%8 = load i32, i32* @SEEK_SET
%9 = call i32 @fseek(i32* %7, i32 450000, i32 %8)
%10 = call i64 @calloc(i32 1000, i32 1)
%11 = inttoptr i64 %10 to i8*
store i8* %11, i8** %3
store i32 0, i32* %4
br label %12
12: ; preds = %49, %1
%13 = load i8*, i8** %3
%14 = load i32*, i32** %2
%15 = call i32 @fread(i8* %13, i32 1000, i32 1, i32* %14)
%16 = load i8*, i8** %3
%17 = call i8* @strstr(i8* %16, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
store i8* %17, i8** %5
%18 = load i8*, i8** %5
%19 = icmp ne i8* %18, null
br i1 %19, label %20, label %40
20: ; preds = %12
%21 = call i64 @calloc(i32 7, i32 1)
%22 = inttoptr i64 %21 to i8*
store i8* %22, i8** %6
%23 = load i8*, i8** %6
%24 = load i8*, i8** %5
%25 = getelementptr inbounds i8, i8* %24, i64 9
%26 = getelementptr inbounds i8, i8* %25, i64 1
%27 = call i32 @strncpy(i8* %23, i8* %26, i32 6)
%28 = load i8*, i8** %6
%29 = call i64 @strtol(i8* %28, i8** null, i32 10)
%30 = trunc i64 %29 to i32
store i32 %30, i32* %4
%31 = load i8*, i8** %6
%32 = call i32 @free(i8* %31)
%33 = load i32, i32* %4
%34 = icmp slt i32 %33, 400000
br i1 %34, label %38, label %35
35: ; preds = %20
%36 = load i32, i32* %4
%37 = icmp sgt i32 %36, 525000
br i1 %37, label %38, label %39
38: ; preds = %35, %20
store i32 0, i32* %4
br label %39
39: ; preds = %38, %35
br label %40
40: ; preds = %39, %12
br label %41
41: ; preds = %40
%42 = load i32, i32* %4
%43 = icmp eq i32 %42, 0
br i1 %43, label %44, label %49
44: ; preds = %41
%45 = load i32*, i32** %2
%46 = call i32 @feof(i32* %45)
%47 = icmp ne i32 %46, 0
%48 = xor i1 %47, true
br label %49
49: ; preds = %44, %41
%50 = phi i1 [ false, %41 ], [ %48, %44 ]
br i1 %50, label %12, label %51
51: ; preds = %49
%52 = load i8*, i8** %3
%53 = call i32 @free(i8* %52)
%54 = load i32, i32* %4
ret i32 %54
}

LLVM IR (-O2):
@SEEK_SET = global i32 0
@.str = [10 x i8] c"startxref\00"
define i32 @findXrefOffsetInMalformedFile(i32* nocapture %0) {
%2 = load i32, i32* @SEEK_SET
%3 = tail call i32 @fseek(i32* %0, i32 450000, i32 %2)
%4 = tail call i64 @calloc(i32 1000, i32 1)
%5 = inttoptr i64 %4 to i8*
br label %6
6: ; preds = %20, %1
%7 = tail call i32 @fread(i8* %5, i32 1000, i32 1, i32* %0)
%8 = tail call i8* @strstr(i8* dereferenceable(1) %5, i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%9 = icmp eq i8* %8, null
br i1 %9, label %20, label %10
10: ; preds = %6
%11 = tail call i64 @calloc(i32 7, i32 1)
%12 = inttoptr i64 %11 to i8*
%13 = getelementptr inbounds i8, i8* %8, i64 10
%14 = tail call i32 @strncpy(i8* %12, i8* %13, i32 6)
%15 = tail call i64 @strtol(i8* nocapture %12, i8** null, i32 10)
%16 = trunc i64 %15 to i32
%17 = tail call i32 @free(i8* %12)
%18 = add i32 %16, -525001
%19 = icmp ult i32 %18, -125001
br i1 %19, label %20, label %23
20: ; preds = %10, %6
%21 = tail call i32 @feof(i32* %0)
%22 = icmp eq i32 %21, 0
br i1 %22, label %6, label %25
23: ; preds = %10
%24 = trunc i64 %15 to i32
br label %25
25: ; preds = %20, %23
%26 = phi i32 [ %24, %23 ], [ 0, %20 ]
%27 = tail call i32 @free(i8* %5)
ret i32 %26
}