#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 scalar_t__ off_t;

/* Variables and functions */
int /*<<< orphan*/ SEEK_CUR;
int /*<<< orphan*/ SEEK_SET;
scalar_t__ __libc_lseek(int, scalar_t__, int /*<<< orphan*/);
int __libc_write(int, void const *, size_t);
int /*<<< orphan*/ __set_errno(int);
int errno;
ssize_t __libc_pwrite(int fd, const void *buf, size_t nbyte, off_t offset) {
  int save_errno;
  ssize_t result;
  off_t old_offset = __libc_lseek(fd, 0, SEEK_CUR);
  if (old_offset == (off_t)-1)
    return -1;
  if (__libc_lseek(fd, offset, SEEK_SET) == (off_t)-1)
    return -1;
  result = __libc_write(fd, buf, nbyte);
  save_errno = errno;
  if (__libc_lseek(fd, old_offset, SEEK_SET) == (off_t)-1) {
    if (result == -1)
      __set_errno(save_errno);
    return -1;
  }
  __set_errno(save_errno);
  return result;
}


x86 assembly (clang, -O0):
<__libc_pwrite>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e97(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1040 <__libc_lseek@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0xffffffffffffffff,-0x30(%rbp)
jne    1177 <__libc_pwrite+0x47>
movl   $0xffffffff,-0x4(%rbp)
jmp    120c <__libc_pwrite+0xdc>
mov    -0x8(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    0x2e4b(%rip),%rax
mov    (%rax),%edx
call   1040 <__libc_lseek@plt>
cmp    $0xffffffffffffffff,%rax
jne    11a2 <__libc_pwrite+0x72>
movl   $0xffffffff,-0x4(%rbp)
jmp    120c <__libc_pwrite+0xdc>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1050 <__libc_write@plt>
mov    %eax,-0x28(%rbp)
mov    0x2e1c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    0x2e01(%rip),%rax
mov    (%rax),%edx
call   1040 <__libc_lseek@plt>
cmp    $0xffffffffffffffff,%rax
jne    11fe <__libc_pwrite+0xce>
cmpl   $0xffffffff,-0x28(%rbp)
jne    11f2 <__libc_pwrite+0xc2>
mov    -0x24(%rbp),%edi
call   1030 <__set_errno@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    120c <__libc_pwrite+0xdc>
mov    -0x24(%rbp),%edi
call   1030 <__set_errno@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__libc_pwrite>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r13
mov    %rdx,%r14
mov    %rsi,%r15
mov    %edi,%ebx
mov    0x2e9b(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1040 <__libc_lseek@plt>
mov    $0xffffffff,%ebp
cmp    $0xffffffffffffffff,%rax
je     11cc <__libc_pwrite+0x9c>
mov    %rax,%r12
mov    0x2e65(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
mov    %r13,%rsi
call   1040 <__libc_lseek@plt>
cmp    $0xffffffffffffffff,%rax
je     11cc <__libc_pwrite+0x9c>
mov    %ebx,%edi
mov    %r15,%rsi
mov    %r14,%rdx
call   1050 <__libc_write@plt>
mov    %eax,%r14d
mov    0x2e44(%rip),%rax
mov    (%rax),%r15d
mov    0x2e32(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
mov    %r12,%rsi
call   1040 <__libc_lseek@plt>
cmp    $0xffffffffffffffff,%rax
jne    11c1 <__libc_pwrite+0x91>
mov    $0xffffffff,%ebp
cmp    $0xffffffff,%r14d
mov    $0xffffffff,%r14d
jne    11cc <__libc_pwrite+0x9c>
mov    %r15d,%edi
call   1030 <__set_errno@plt>
mov    %r14d,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@SEEK_CUR = global i32 0
@SEEK_SET = global i32 0
@errno = global i32 0
define i32 @__libc_pwrite(i32 %0, i8* %1, i64 %2, i64 %3) {
%5 = load i32, i32* @SEEK_CUR
%6 = tail call i64 @__libc_lseek(i32 %0, i64 0, i32 %5)
%7 = icmp eq i64 %6, -1
br i1 %7, label %23, label %8
8: ; preds = %4
%9 = load i32, i32* @SEEK_SET
%10 = tail call i64 @__libc_lseek(i32 %0, i64 %3, i32 %9)
%11 = icmp eq i64 %10, -1
br i1 %11, label %23, label %12
12: ; preds = %8
%13 = tail call i32 @__libc_write(i32 %0, i8* %1, i64 %2)
%14 = load i32, i32* @errno
%15 = load i32, i32* @SEEK_SET
%16 = tail call i64 @__libc_lseek(i32 %0, i64 %6, i32 %15)
%17 = icmp eq i64 %16, -1
br i1 %17, label %18, label %20
18: ; preds = %12
%19 = icmp eq i32 %13, -1
br i1 %19, label %20, label %23
20: ; preds = %12, %18
%21 = phi i32 [ -1, %18 ], [ %13, %12 ]
%22 = tail call i32 @__set_errno(i32 %14)
br label %23
23: ; preds = %20, %18, %8, %4
%24 = phi i32 [ -1, %4 ], [ -1, %8 ], [ -1, %18 ], [ %21, %20 ]
ret i32 %24
}