#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 */
struct stat {
  int st_size;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
unsigned long appmemcrc(unsigned char *, int, unsigned long);
int /*<<< orphan*/ fileno(int /*<<< orphan*/ *);
int fread(unsigned char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(unsigned char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, unsigned long,
                         int /*<<< orphan*/);
int fstat(int /*<<< orphan*/, struct stat *);
unsigned long ftell(int /*<<< orphan*/ *);
scalar_t__ malloc(int);
unsigned long umodcrc(FILE *fdopen) {
  FILE *fd;
  int err, len;
  unsigned char *buff;
  unsigned long crc = 0x00000000, oldoff = 0;
  struct stat xstat;
  fd = fdopen;
  oldoff = ftell(fd);
  err = fstat(fileno(fd), &xstat);
  if (err < 0)
    return (-1);
  if (xstat.st_size >= 20)
    xstat.st_size -= 20;
  else
    return (-1);
  buff = (unsigned char *)malloc(0x4000);
  if (!buff)
    return (-1);
  len = 0x4000;
  do {
    if (len > xstat.st_size)
      len = xstat.st_size;
    err = fread(buff, len, 1, fd);
    if (err != 1)
      break;
    crc = appmemcrc(buff, len, crc);
    xstat.st_size -= len;
  } while (xstat.st_size);
  fseek(fd, oldoff, SEEK_SET);
  free(buff);
  return (crc);
}


x86 assembly (clang, -O0):
<umodcrc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <ftell@plt>
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
call   1070 <fileno@plt>
mov    %eax,%edi
lea    -0x40(%rbp),%rsi
call   10a0 <fstat@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11df <umodcrc+0x5f>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    12ba <umodcrc+0x13a>
cmpl   $0x14,-0x40(%rbp)
jl     11f7 <umodcrc+0x77>
mov    -0x40(%rbp),%eax
sub    $0x14,%eax
mov    %eax,-0x40(%rbp)
jmp    1204 <umodcrc+0x84>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    12ba <umodcrc+0x13a>
mov    $0x4000,%edi
call   1080 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    122a <umodcrc+0xaa>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    12ba <umodcrc+0x13a>
movl   $0x4000,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x40(%rbp),%eax
jle    1243 <umodcrc+0xc3>
mov    -0x40(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%esi
mov    -0x18(%rbp),%rcx
mov    $0x1,%edx
call   1040 <fread@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x1,-0x1c(%rbp)
je     126a <umodcrc+0xea>
jmp    1293 <umodcrc+0x113>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%esi
mov    -0x30(%rbp),%rdx
call   1050 <appmemcrc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%ecx
mov    -0x40(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x40(%rbp)
cmpl   $0x0,-0x40(%rbp)
jne    1231 <umodcrc+0xb1>
mov    -0x18(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    0x2d3e(%rip),%rax
mov    (%rax),%edx
call   1090 <fseek@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<umodcrc>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r15
call   1060 <ftell@plt>
mov    %rax,%r14
mov    %r15,%rdi
call   1070 <fileno@plt>
mov    %rsp,%rsi
mov    %eax,%edi
call   10a0 <fstat@plt>
mov    $0xffffffffffffffff,%r13
test   %eax,%eax
js     122c <umodcrc+0xac>
mov    (%rsp),%ebx
cmp    $0x14,%ebx
jl     122c <umodcrc+0xac>
mov    $0x4000,%ebp
mov    $0x4000,%edi
call   1080 <malloc@plt>
test   %rax,%rax
je     122c <umodcrc+0xac>
mov    %rax,%r12
add    $0xffffffec,%ebx
xor    %r13d,%r13d
nopl   0x0(%rax,%rax,1)
cmp    %ebx,%ebp
cmovg  %ebx,%ebp
mov    %r12,%rdi
mov    %ebp,%esi
mov    $0x1,%edx
mov    %r15,%rcx
call   1040 <fread@plt>
cmp    $0x1,%eax
jne    1210 <umodcrc+0x90>
mov    %r12,%rdi
mov    %ebp,%esi
mov    %r13,%rdx
call   1050 <appmemcrc@plt>
mov    %rax,%r13
sub    %ebp,%ebx
jne    11e0 <umodcrc+0x60>
mov    0x2dc9(%rip),%rax
mov    (%rax),%edx
mov    %r15,%rdi
mov    %r14,%rsi
call   1090 <fseek@plt>
mov    %r12,%rdi
call   1030 <free@plt>
mov    %r13,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@SEEK_SET = global i32 0
define i64 @umodcrc(i32* %0) {
%2 = alloca i64
%3 = alloca i32*
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i64
%10 = alloca %struct.stat
store i32* %0, i32** %3
store i64 0, i64* %8
store i64 0, i64* %9
%11 = load i32*, i32** %3
store i32* %11, i32** %4
%12 = load i32*, i32** %4
%13 = call i64 @ftell(i32* %12)
store i64 %13, i64* %9
%14 = load i32*, i32** %4
%15 = call i32 @fileno(i32* %14)
%16 = call i32 @fstat(i32 %15, %struct.stat* %10)
store i32 %16, i32* %5
%17 = load i32, i32* %5
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %1
store i64 -1, i64* %2
br label %73
20: ; preds = %1
%21 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%22 = load i32, i32* %21
%23 = icmp sge i32 %22, 20
br i1 %23, label %24, label %28
24: ; preds = %20
%25 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%26 = load i32, i32* %25
%27 = sub nsw i32 %26, 20
store i32 %27, i32* %25
br label %29
28: ; preds = %20
store i64 -1, i64* %2
br label %73
29: ; preds = %24
%30 = call i64 @malloc(i32 16384)
%31 = inttoptr i64 %30 to i8*
store i8* %31, i8** %7
%32 = load i8*, i8** %7
%33 = icmp ne i8* %32, null
br i1 %33, label %35, label %34
34: ; preds = %29
store i64 -1, i64* %2
br label %73
35: ; preds = %29
store i32 16384, i32* %6
br label %36
36: ; preds = %61, %35
%37 = load i32, i32* %6
%38 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%39 = load i32, i32* %38
%40 = icmp sgt i32 %37, %39
br i1 %40, label %41, label %44
41: ; preds = %36
%42 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%43 = load i32, i32* %42
store i32 %43, i32* %6
br label %44
44: ; preds = %41, %36
%45 = load i8*, i8** %7
%46 = load i32, i32* %6
%47 = load i32*, i32** %4
%48 = call i32 @fread(i8* %45, i32 %46, i32 1, i32* %47)
store i32 %48, i32* %5
%49 = load i32, i32* %5
%50 = icmp ne i32 %49, 1
br i1 %50, label %51, label %52
51: ; preds = %44
br label %65
52: ; preds = %44
%53 = load i8*, i8** %7
%54 = load i32, i32* %6
%55 = load i64, i64* %8
%56 = call i64 @appmemcrc(i8* %53, i32 %54, i64 %55)
store i64 %56, i64* %8
%57 = load i32, i32* %6
%58 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%59 = load i32, i32* %58
%60 = sub nsw i32 %59, %57
store i32 %60, i32* %58
br label %61
61: ; preds = %52
%62 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%63 = load i32, i32* %62
%64 = icmp ne i32 %63, 0
br i1 %64, label %36, label %65
65: ; preds = %61, %51
%66 = load i32*, i32** %4
%67 = load i64, i64* %9
%68 = load i32, i32* @SEEK_SET
%69 = call i32 @fseek(i32* %66, i64 %67, i32 %68)
%70 = load i8*, i8** %7
%71 = call i32 @free(i8* %70)
%72 = load i64, i64* %8
store i64 %72, i64* %2
br label %73
73: ; preds = %65, %34, %28, %19
%74 = load i64, i64* %2
ret i64 %74
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@SEEK_SET = global i32 0
define i64 @umodcrc(i32* nocapture %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = tail call i64 @ftell(i32* %0)
%5 = tail call i32 @fileno(i32* %0)
%6 = call i32 @fstat(i32 %5, %struct.stat* %2)
%7 = icmp slt i32 %6, 0
br i1 %7, label %35, label %8
8: ; preds = %1
%9 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%10 = load i32, i32* %9
%11 = icmp sgt i32 %10, 19
br i1 %11, label %12, label %35
12: ; preds = %8
%13 = tail call i64 @malloc(i32 16384)
%14 = inttoptr i64 %13 to i8*
%15 = icmp eq i64 %13, 0
br i1 %15, label %35, label %16
16: ; preds = %12
%17 = add nsw i32 %10, -20
br label %18
18: ; preds = %16, %26
%19 = phi i32 [ %28, %26 ], [ %17, %16 ]
%20 = phi i32 [ %23, %26 ], [ 16384, %16 ]
%21 = phi i64 [ %27, %26 ], [ 0, %16 ]
%22 = icmp sgt i32 %20, %19
%23 = select i1 %22, i32 %19, i32 %20
%24 = tail call i32 @fread(i8* %14, i32 %23, i32 1, i32* %0)
%25 = icmp eq i32 %24, 1
br i1 %25, label %26, label %30
26: ; preds = %18
%27 = tail call i64 @appmemcrc(i8* %14, i32 %23, i64 %21)
%28 = sub nsw i32 %19, %23
%29 = icmp eq i32 %28, 0
br i1 %29, label %30, label %18
30: ; preds = %18, %26
%31 = phi i64 [ %21, %18 ], [ %27, %26 ]
%32 = load i32, i32* @SEEK_SET
%33 = tail call i32 @fseek(i32* %0, i64 %4, i32 %32)
%34 = tail call i32 @free(i8* %14)
br label %35
35: ; preds = %12, %8, %1, %30
%36 = phi i64 [ %31, %30 ], [ -1, %1 ], [ -1, %8 ], [ -1, %12 ]
ret i64 %36
}