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

/* Variables and functions */
int /*<<< orphan*/ SYS_pwrite64;
int errno;
int syscall(int /*<<< orphan*/, int /*<<< orphan*/, char const *, size_t,
            int /*<<< orphan*/);
int usfs_write(uint64_t fh, const char *buf, size_t size, off_t offset,
               size_t *bytes_written) {
  int ret = syscall(SYS_pwrite64, fh, buf, size, offset);
  if (-1 == ret) {
    ret = errno;
    *bytes_written = 0;
  } else {
    *bytes_written = ret;
    ret = 0;
  }
  return ret;
}


x86 assembly (clang, -O0):
<usfs_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8,-0x28(%rbp)
mov    0x2ea7(%rip),%rax
mov    (%rax),%edi
mov    -0x4(%rbp),%esi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    -0x1c(%rbp),%r8d
call   1030 <syscall@plt>
mov    %eax,-0x2c(%rbp)
mov    $0xffffffff,%eax
cmp    -0x2c(%rbp),%eax
jne    1174 <usfs_write+0x64>
mov    0x2e81(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%rax
movq   $0x0,(%rax)
jmp    1186 <usfs_write+0x76>
movslq -0x2c(%rbp),%rcx
mov    -0x28(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<usfs_write>:
push   %rbx
mov    %r8,%rbx
mov    %ecx,%r8d
mov    %rdx,%rcx
mov    %rsi,%rdx
mov    %edi,%esi
mov    0x2eb2(%rip),%rax
mov    (%rax),%edi
call   1030 <syscall@plt>
xor    %ecx,%ecx
cmp    $0xffffffff,%eax
mov    0x2ea7(%rip),%rdx
cmove  %ecx,%eax
cltq
cmove  (%rdx),%ecx
mov    %rax,(%rbx)
mov    %ecx,%eax
pop    %rbx
ret

LLVM IR (-O0):
@SYS_pwrite64 = global i32 0
@errno = global i32 0
define i32 @usfs_write(i32 %0, i8* %1, i64 %2, i32 %3, i64* %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32
%10 = alloca i64*
%11 = alloca i32
store i32 %0, i32* %6
store i8* %1, i8** %7
store i64 %2, i64* %8
store i32 %3, i32* %9
store i64* %4, i64** %10
%12 = load i32, i32* @SYS_pwrite64
%13 = load i32, i32* %6
%14 = load i8*, i8** %7
%15 = load i64, i64* %8
%16 = load i32, i32* %9
%17 = call i32 @syscall(i32 %12, i32 %13, i8* %14, i64 %15, i32 %16)
store i32 %17, i32* %11
%18 = load i32, i32* %11
%19 = icmp eq i32 -1, %18
br i1 %19, label %20, label %23
20: ; preds = %5
%21 = load i32, i32* @errno
store i32 %21, i32* %11
%22 = load i64*, i64** %10
store i64 0, i64* %22
br label %27
23: ; preds = %5
%24 = load i32, i32* %11
%25 = sext i32 %24 to i64
%26 = load i64*, i64** %10
store i64 %25, i64* %26
store i32 0, i32* %11
br label %27
27: ; preds = %23, %20
%28 = load i32, i32* %11
ret i32 %28
}

LLVM IR (-O2):
@SYS_pwrite64 = global i32 0
@errno = global i32 0
define i32 @usfs_write(i32 %0, i8* %1, i64 %2, i32 %3, i64* nocapture writeonly %4) {
%6 = load i32, i32* @SYS_pwrite64
%7 = tail call i32 @syscall(i32 %6, i32 %0, i8* %1, i64 %2, i32 %3)
%8 = icmp eq i32 %7, -1
%9 = load i32, i32* @errno
%10 = select i1 %8, i32 0, i32 %7
%11 = sext i32 %10 to i64
%12 = select i1 %8, i32 %9, i32 0
store i64 %11, i64* %4
ret i32 %12
}