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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ SEEK_SET;
scalar_t__ close(int);
int /*<<< orphan*/ le32toh(int /*<<< orphan*/);
int /*<<< orphan*/ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
scalar_t__ read(int, void *, int);
int mr500_read_crc(char *filename, off_t offset, uint32_t *crc_value) {
  int fdi;
  int ret = 0;
  fdi = open(filename, O_RDONLY);
  if (fdi < 0) {
    ret = -1;
  }
  if (lseek(fdi, offset, SEEK_SET) != offset) {
    ret = -1;
  }
  if (read(fdi, (void *)crc_value, sizeof(uint32_t)) < 0) {
    ret = -1;
  }
  *crc_value = le32toh(*crc_value);
  if (close(fdi) < 0) {
    ret = -1;
  }
  return ret;
}


x86 assembly (clang, -O0):
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e73(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1190 <mr500_read_crc+0x40>
movl   $0xffffffff,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0xc(%rbp),%esi
mov    0x2e3b(%rip),%rax
mov    (%rax),%edx
call   1040 <lseek@plt>
cmp    -0xc(%rbp),%eax
je     11b4 <mr500_read_crc+0x64>
movl   $0xffffffff,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    $0x4,%edx
call   1060 <read@plt>
cmp    $0x0,%rax
jge    11d6 <mr500_read_crc+0x86>
movl   $0xffffffff,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
call   1030 <le32toh@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
cmp    $0x0,%rax
jge    1202 <mr500_read_crc+0xb2>
movl   $0xffffffff,-0x20(%rbp)
mov    -0x20(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %esi,%r14d
mov    0x2e80(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,%ebx
mov    %eax,%ebp
sar    $0x1f,%ebp
mov    0x2e5b(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
mov    %r14d,%esi
call   1040 <lseek@plt>
mov    %eax,%r12d
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x4,%edx
call   1060 <read@plt>
mov    %rax,%r13
mov    (%r15),%edi
call   1030 <le32toh@plt>
mov    %eax,(%r15)
mov    %ebx,%edi
call   1050 <close@plt>
cmp    %r14d,%r12d
mov    $0xffffffff,%ecx
cmovne %ecx,%ebp
or     %r13,%rax
cmovs  %ecx,%ebp
mov    %ebp,%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_read_crc(i8* %0, i32 %1, i32* %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32* %2, i32** %6
store i32 0, i32* %8
%9 = load i8*, i8** %4
%10 = load i32, i32* @O_RDONLY
%11 = call i32 @open(i8* %9, i32 %10)
store i32 %11, i32* %7
%12 = load i32, i32* %7
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %3
store i32 -1, i32* %8
br label %15
15: ; preds = %14, %3
%16 = load i32, i32* %7
%17 = load i32, i32* %5
%18 = load i32, i32* @SEEK_SET
%19 = call i32 @lseek(i32 %16, i32 %17, i32 %18)
%20 = load i32, i32* %5
%21 = icmp ne i32 %19, %20
br i1 %21, label %22, label %23
22: ; preds = %15
store i32 -1, i32* %8
br label %23
23: ; preds = %22, %15
%24 = load i32, i32* %7
%25 = load i32*, i32** %6
%26 = bitcast i32* %25 to i8*
%27 = call i64 @read(i32 %24, i8* %26, i32 4)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %23
store i32 -1, i32* %8
br label %30
30: ; preds = %29, %23
%31 = load i32*, i32** %6
%32 = load i32, i32* %31
%33 = call i32 @le32toh(i32 %32)
%34 = load i32*, i32** %6
store i32 %33, i32* %34
%35 = load i32, i32* %7
%36 = call i64 @close(i32 %35)
%37 = icmp slt i64 %36, 0
br i1 %37, label %38, label %39
38: ; preds = %30
store i32 -1, i32* %8
br label %39
39: ; preds = %38, %30
%40 = load i32, i32* %8
ret i32 %40
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@SEEK_SET = global i32 0
define i32 @mr500_read_crc(i8* nocapture %0, i32 %1, i32* nocapture %2) {
%4 = load i32, i32* @O_RDONLY
%5 = tail call i32 @open(i8* %0, i32 %4)
%6 = ashr i32 %5, 31
%7 = load i32, i32* @SEEK_SET
%8 = tail call i32 @lseek(i32 %5, i32 %1, i32 %7)
%9 = icmp eq i32 %8, %1
%10 = bitcast i32* %2 to i8*
%11 = tail call i64 @read(i32 %5, i8* %10, i32 4)
%12 = load i32, i32* %2
%13 = tail call i32 @le32toh(i32 %12)
store i32 %13, i32* %2
%14 = tail call i64 @close(i32 %5)
%15 = or i64 %14, %11
%16 = icmp sgt i64 %15, -1
%17 = select i1 %16, i1 %9, i1 false
%18 = select i1 %17, i32 %6, i32 -1
ret i32 %18
}