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

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
int errno;
int lseek(int, int, int /*<<< orphan*/);
int write(int, void const *, size_t);
ssize_t pwrite(int fd, const void *data, size_t length, off_t offset) {
  ssize_t result;
  off_t save_offset;
  int save_errno;
  save_offset = lseek(fd, offset, SEEK_SET);
  if (save_offset == -1)
    return -1;
  result = write(fd, data, length);
  save_errno = errno;
  lseek(fd, save_offset, SEEK_SET);
  errno = save_errno;
  return result;
}


x86 assembly (clang, -O0):
<pwrite>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x1c(%rbp),%esi
mov    0x2e95(%rip),%rax
mov    (%rax),%edx
call   1040 <lseek@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    1163 <pwrite+0x43>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a8 <pwrite+0x88>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <write@plt>
mov    %eax,-0x20(%rbp)
mov    0x2e63(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x24(%rbp),%esi
mov    0x2e49(%rip),%rax
mov    (%rax),%edx
call   1040 <lseek@plt>
mov    -0x28(%rbp),%ecx
mov    0x2e40(%rip),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pwrite>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %edi,%ebx
mov    0x2e9e(%rip),%r12
mov    (%r12),%edx
mov    %ecx,%esi
call   1040 <lseek@plt>
cmp    $0xffffffff,%eax
je     117b <pwrite+0x5b>
mov    %eax,%ebp
mov    %ebx,%edi
mov    %r15,%rsi
mov    %r14,%rdx
call   1030 <write@plt>
mov    %eax,%r14d
mov    0x2e7d(%rip),%r15
mov    (%r15),%r13d
mov    (%r12),%edx
mov    %ebx,%edi
mov    %ebp,%esi
call   1040 <lseek@plt>
mov    %r13d,(%r15)
mov    %r14d,%eax
jmp    1180 <pwrite+0x60>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@SEEK_SET = global i32 0
@errno = global i32 0
define i32 @pwrite(i32 %0, i8* nocapture %1, i64 %2, i32 %3) {
%5 = load i32, i32* @SEEK_SET
%6 = tail call i32 @lseek(i32 %0, i32 %3, i32 %5)
%7 = icmp eq i32 %6, -1
br i1 %7, label %13, label %8
8: ; preds = %4
%9 = tail call i32 @write(i32 %0, i8* %1, i64 %2)
%10 = load i32, i32* @errno
%11 = load i32, i32* @SEEK_SET
%12 = tail call i32 @lseek(i32 %0, i32 %6, i32 %11)
store i32 %10, i32* @errno
br label %13
13: ; preds = %4, %8
%14 = phi i32 [ %9, %8 ], [ -1, %4 ]
ret i32 %14
}