#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 scalar_t__ uint32_t;
typedef int /*<<< orphan*/ off_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ SEEK_SET;
scalar_t__ close(int);
int /*<<< orphan*/ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
scalar_t__ read(int, scalar_t__ *, int);
int mr500_calculate_crc(char *filename, off_t offset, unsigned int length,
                        uint32_t *crc_value) {
  uint32_t temp;
  int fdi;
  int ret = 0;
  fdi = open(filename, O_RDONLY);
  if (fdi < 0) {
    ret = -1;
  }
  if (lseek(fdi, offset, SEEK_SET) != offset) {
    ret = -1;
  }
  *crc_value = 0;
  do {
    if (read(fdi, &temp, sizeof(uint32_t)) < 0) {
      ret = -1;
      break;
    }
    *crc_value += temp;
  } while (length -= 4);
  if (close(fdi) < 0) {
    ret = -1;
  }
  return ret;
}


x86 assembly (clang, -O0):
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e80(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1183 <mr500_calculate_crc+0x43>
movl   $0xffffffff,-0x28(%rbp)
mov    -0x24(%rbp),%edi
mov    -0xc(%rbp),%esi
mov    0x2e48(%rip),%rax
mov    (%rax),%edx
call   1030 <lseek@plt>
cmp    -0xc(%rbp),%eax
je     11a7 <mr500_calculate_crc+0x67>
movl   $0xffffffff,-0x28(%rbp)
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x24(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x8,%edx
call   1050 <read@plt>
cmp    $0x0,%rax
jge    11d9 <mr500_calculate_crc+0x99>
movl   $0xffffffff,-0x28(%rbp)
jmp    11f9 <mr500_calculate_crc+0xb9>
mov    -0x20(%rbp),%rcx
mov    -0x18(%rbp),%rax
add    (%rax),%rcx
mov    %rcx,(%rax)
mov    -0x10(%rbp),%eax
sub    $0x4,%eax
mov    %eax,-0x10(%rbp)
cmp    $0x0,%eax
jne    11b2 <mr500_calculate_crc+0x72>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
cmp    $0x0,%rax
jge    1212 <mr500_calculate_crc+0xd2>
movl   $0xffffffff,-0x28(%rbp)
mov    -0x28(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %edx,%ebp
mov    %esi,%r15d
mov    0x2e8e(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,%ebx
mov    %eax,%r13d
sar    $0x1f,%r13d
mov    0x2e67(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
mov    %r15d,%esi
call   1030 <lseek@plt>
cmp    %r15d,%eax
mov    $0xffffffff,%r12d
cmovne %r12d,%r13d
movq   $0x0,(%r14)
mov    %rsp,%r15
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x8,%edx
call   1050 <read@plt>
test   %rax,%rax
js     11c3 <mr500_calculate_crc+0x83>
mov    (%rsp),%rax
add    %rax,(%r14)
add    $0xfffffffc,%ebp
jne    11a0 <mr500_calculate_crc+0x60>
mov    %r13d,%r12d
mov    %ebx,%edi
call   1040 <close@plt>
test   %rax,%rax
mov    $0xffffffff,%eax
cmovns %r12d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@SEEK_SET = global i32 0
define i32 @mr500_calculate_crc(i8* %0, i32 %1, i32 %2, i64* %3) {
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i64*
%9 = alloca i64
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i64* %3, i64** %8
store i32 0, i32* %11
%12 = load i8*, i8** %5
%13 = load i32, i32* @O_RDONLY
%14 = call i32 @open(i8* %12, i32 %13)
store i32 %14, i32* %10
%15 = load i32, i32* %10
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %4
store i32 -1, i32* %11
br label %18
18: ; preds = %17, %4
%19 = load i32, i32* %10
%20 = load i32, i32* %6
%21 = load i32, i32* @SEEK_SET
%22 = call i32 @lseek(i32 %19, i32 %20, i32 %21)
%23 = load i32, i32* %6
%24 = icmp ne i32 %22, %23
br i1 %24, label %25, label %26
25: ; preds = %18
store i32 -1, i32* %11
br label %26
26: ; preds = %25, %18
%27 = load i64*, i64** %8
store i64 0, i64* %27
br label %28
28: ; preds = %38, %26
%29 = load i32, i32* %10
%30 = call i64 @read(i32 %29, i64* %9, i32 8)
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %33
32: ; preds = %28
store i32 -1, i32* %11
br label %42
33: ; preds = %28
%34 = load i64, i64* %9
%35 = load i64*, i64** %8
%36 = load i64, i64* %35
%37 = add nsw i64 %36, %34
store i64 %37, i64* %35
br label %38
38: ; preds = %33
%39 = load i32, i32* %7
%40 = sub i32 %39, 4
store i32 %40, i32* %7
%41 = icmp ne i32 %40, 0
br i1 %41, label %28, label %42
42: ; preds = %38, %32
%43 = load i32, i32* %10
%44 = call i64 @close(i32 %43)
%45 = icmp slt i64 %44, 0
br i1 %45, label %46, label %47
46: ; preds = %42
store i32 -1, i32* %11
br label %47
47: ; preds = %46, %42
%48 = load i32, i32* %11
ret i32 %48
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@SEEK_SET = global i32 0
define i32 @mr500_calculate_crc(i8* nocapture %0, i32 %1, i32 %2, i64* nocapture %3) {
%5 = alloca i64
%6 = bitcast i64* %5 to i8*
%7 = load i32, i32* @O_RDONLY
%8 = tail call i32 @open(i8* %0, i32 %7)
%9 = ashr i32 %8, 31
%10 = load i32, i32* @SEEK_SET
%11 = tail call i32 @lseek(i32 %8, i32 %1, i32 %10)
%12 = icmp eq i32 %11, %1
%13 = select i1 %12, i32 %9, i32 -1
store i64 0, i64* %3
br label %14
14: ; preds = %18, %4
%15 = phi i32 [ %2, %4 ], [ %22, %18 ]
%16 = call i64 @read(i32 %8, i64* %5, i32 8)
%17 = icmp slt i64 %16, 0
br i1 %17, label %24, label %18
18: ; preds = %14
%19 = load i64, i64* %5
%20 = load i64, i64* %3
%21 = add nsw i64 %20, %19
store i64 %21, i64* %3
%22 = add i32 %15, -4
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %14
24: ; preds = %14, %18
%25 = phi i32 [ %13, %18 ], [ -1, %14 ]
%26 = tail call i64 @close(i32 %8)
%27 = icmp slt i64 %26, 0
%28 = select i1 %27, i32 -1, i32 %25
ret i32 %28
}