#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_WRONLY;
int /*<<< orphan*/ SEEK_SET;
scalar_t__ close(int);
int /*<<< orphan*/ htole32(int /*<<< orphan*/);
int /*<<< orphan*/ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
scalar_t__ write(int, void *, int);
int mr500_save_crc(char *filename, off_t offset, uint32_t *crc_value) {
  int fdo;
  int ret = 0;
  uint32_t save_crc;
  fdo = open(filename, O_WRONLY);
  if (fdo < 0) {
    ret = -1;
  }
  if (lseek(fdo, offset, SEEK_SET) != offset) {
    ret = -1;
  }
  save_crc = htole32(*crc_value);
  if (write(fdo, (void *)&save_crc, sizeof(unsigned int)) < 0) {
    ret = -1;
  }
  if (close(fdo) < 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    %rdx,-0x18(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e5b(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1190 <mr500_save_crc+0x40>
movl   $0xffffffff,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0xc(%rbp),%esi
mov    0x2e43(%rip),%rax
mov    (%rax),%edx
call   1050 <lseek@plt>
cmp    -0xc(%rbp),%eax
je     11b4 <mr500_save_crc+0x64>
movl   $0xffffffff,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
call   1030 <htole32@plt>
mov    %eax,-0x24(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x24(%rbp),%rsi
mov    $0x4,%edx
call   1040 <write@plt>
cmp    $0x0,%rax
jge    11e4 <mr500_save_crc+0x94>
movl   $0xffffffff,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
cmp    $0x0,%rax
jge    11fd <mr500_save_crc+0xad>
movl   $0xffffffff,-0x20(%rbp)
mov    -0x20(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r14
mov    %esi,%r15d
mov    0x2e67(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,%ebx
mov    %eax,%ebp
sar    $0x1f,%ebp
mov    0x2e62(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
mov    %r15d,%esi
call   1050 <lseek@plt>
mov    %eax,%r12d
mov    (%r14),%edi
call   1030 <htole32@plt>
mov    %eax,0xc(%rsp)
lea    0xc(%rsp),%rsi
mov    %ebx,%edi
mov    $0x4,%edx
call   1040 <write@plt>
mov    %rax,%r14
mov    %ebx,%edi
call   1060 <close@plt>
cmp    %r15d,%r12d
mov    $0xffffffff,%ecx
cmovne %ecx,%ebp
or     %r14,%rax
cmovs  %ecx,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_WRONLY = global i32 0
@SEEK_SET = global i32 0
define i32 @mr500_save_crc(i8* %0, i32 %1, i32* %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32* %2, i32** %6
store i32 0, i32* %8
%10 = load i8*, i8** %4
%11 = load i32, i32* @O_WRONLY
%12 = call i32 @open(i8* %10, i32 %11)
store i32 %12, i32* %7
%13 = load i32, i32* %7
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %3
store i32 -1, i32* %8
br label %16
16: ; preds = %15, %3
%17 = load i32, i32* %7
%18 = load i32, i32* %5
%19 = load i32, i32* @SEEK_SET
%20 = call i32 @lseek(i32 %17, i32 %18, i32 %19)
%21 = load i32, i32* %5
%22 = icmp ne i32 %20, %21
br i1 %22, label %23, label %24
23: ; preds = %16
store i32 -1, i32* %8
br label %24
24: ; preds = %23, %16
%25 = load i32*, i32** %6
%26 = load i32, i32* %25
%27 = call i32 @htole32(i32 %26)
store i32 %27, i32* %9
%28 = load i32, i32* %7
%29 = bitcast i32* %9 to i8*
%30 = call i64 @write(i32 %28, i8* %29, i32 4)
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %33
32: ; preds = %24
store i32 -1, i32* %8
br label %33
33: ; preds = %32, %24
%34 = load i32, i32* %7
%35 = call i64 @close(i32 %34)
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %38
37: ; preds = %33
store i32 -1, i32* %8
br label %38
38: ; preds = %37, %33
%39 = load i32, i32* %8
ret i32 %39
}

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