#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 ssize_t;
typedef int /*<<< orphan*/ mode_t;

/* Variables and functions */
int O_CREAT;
int O_WRONLY;
int /*<<< orphan*/ _fsop_close_safe(int);
int _fsop_fxchg(int, int, size_t);
int /*<<< orphan*/ fsop_unlink(char const *);
int open(char const *, int, int /*<<< orphan*/);
ssize_t fsop_frecv(int sfd, const char *file, mode_t mode, size_t block) {
  int dfd = 0;
  ssize_t count = 0;
  fsop_unlink(file);
  if ((dfd = open(file, O_WRONLY | O_CREAT, mode)) < 0)
    return -1;
  if ((count = _fsop_fxchg(sfd, dfd, block)) < 0)
    return -1;
  _fsop_close_safe(dfd);
  return count;
}


x86 assembly (clang, -O0):
<fsop_frecv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <fsop_unlink@plt>
mov    -0x10(%rbp),%rdi
mov    0x2e58(%rip),%rax
mov    (%rax),%esi
mov    0x2e77(%rip),%rax
or     (%rax),%esi
mov    -0x14(%rbp),%edx
call   1060 <open@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    11a3 <fsop_frecv+0x63>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d8 <fsop_frecv+0x98>
mov    -0x8(%rbp),%edi
mov    -0x24(%rbp),%esi
mov    -0x20(%rbp),%rdx
call   1040 <_fsop_fxchg@plt>
mov    %eax,-0x28(%rbp)
cmp    $0x0,%eax
jge    11ca <fsop_frecv+0x8a>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d8 <fsop_frecv+0x98>
mov    -0x24(%rbp),%edi
call   1050 <_fsop_close_safe@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fsop_frecv>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %edx,%ebx
mov    %rsi,%rbp
mov    %edi,%r15d
mov    %rsi,%rdi
call   1030 <fsop_unlink@plt>
mov    0x2e6f(%rip),%rax
mov    0x2e90(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbp,%rdi
mov    %ebx,%edx
call   1060 <open@plt>
mov    $0xffffffff,%ebx
test   %eax,%eax
js     119f <fsop_frecv+0x5f>
mov    %eax,%ebp
mov    %r15d,%edi
mov    %eax,%esi
mov    %r14,%rdx
call   1040 <_fsop_fxchg@plt>
test   %eax,%eax
js     119f <fsop_frecv+0x5f>
mov    %eax,%r14d
mov    %ebp,%edi
call   1050 <_fsop_close_safe@plt>
mov    %r14d,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
define i32 @fsop_frecv(i32 %0, i8* %1, i32 %2, i64 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i64
%10 = alloca i32
%11 = alloca i32
store i32 %0, i32* %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i64 %3, i64* %9
store i32 0, i32* %10
store i32 0, i32* %11
%12 = load i8*, i8** %7
%13 = call i32 @fsop_unlink(i8* %12)
%14 = load i8*, i8** %7
%15 = load i32, i32* @O_WRONLY
%16 = load i32, i32* @O_CREAT
%17 = or i32 %15, %16
%18 = load i32, i32* %8
%19 = call i32 @open(i8* %14, i32 %17, i32 %18)
store i32 %19, i32* %10
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %4
store i32 -1, i32* %5
br label %33
22: ; preds = %4
%23 = load i32, i32* %6
%24 = load i32, i32* %10
%25 = load i64, i64* %9
%26 = call i32 @_fsop_fxchg(i32 %23, i32 %24, i64 %25)
store i32 %26, i32* %11
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %29
28: ; preds = %22
store i32 -1, i32* %5
br label %33
29: ; preds = %22
%30 = load i32, i32* %10
%31 = call i32 @_fsop_close_safe(i32 %30)
%32 = load i32, i32* %11
store i32 %32, i32* %5
br label %33
33: ; preds = %29, %28, %21
%34 = load i32, i32* %5
ret i32 %34
}

LLVM IR (-O2):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
define i32 @fsop_frecv(i32 %0, i8* %1, i32 %2, i64 %3) {
%5 = tail call i32 @fsop_unlink(i8* %1)
%6 = load i32, i32* @O_WRONLY
%7 = load i32, i32* @O_CREAT
%8 = or i32 %7, %6
%9 = tail call i32 @open(i8* %1, i32 %8, i32 %2)
%10 = icmp slt i32 %9, 0
br i1 %10, label %16, label %11
11: ; preds = %4
%12 = tail call i32 @_fsop_fxchg(i32 %0, i32 %9, i64 %3)
%13 = icmp slt i32 %12, 0
br i1 %13, label %16, label %14
14: ; preds = %11
%15 = tail call i32 @_fsop_close_safe(i32 %9)
br label %16
16: ; preds = %11, %4, %14
%17 = phi i32 [ %12, %14 ], [ -1, %4 ], [ -1, %11 ]
ret i32 %17
}