#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*/);
size_t write(int, void const *, size_t);
ssize_t pwrite(int fd, const void *p, size_t n, off_t off) {
  off_t ooff;
  int oerrno;
  if ((ooff = lseek(fd, off, SEEK_SET)) == -1)
    return -1;
  n = write(fd, p, n);
  oerrno = errno;
  lseek(fd, ooff, SEEK_SET);
  errno = oerrno;
  return n;
}


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,-0x20(%rbp)
cmp    $0xffffffff,%eax
jne    1162 <pwrite+0x42>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a9 <pwrite+0x89>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x18(%rbp)
mov    0x2e63(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x20(%rbp),%esi
mov    0x2e49(%rip),%rax
mov    (%rax),%edx
call   1040 <lseek@plt>
mov    -0x24(%rbp),%ecx
mov    0x2e40(%rip),%rax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%rax
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     1178 <pwrite+0x58>
mov    %eax,%ebp
mov    %ebx,%edi
mov    %r15,%rsi
mov    %r14,%rdx
call   1030 <write@plt>
mov    %rax,%r14
mov    0x2e7d(%rip),%r15
mov    (%r15),%r13d
mov    (%r12),%edx
mov    %ebx,%edi
mov    %ebp,%esi
call   1040 <lseek@plt>
mov    %r13d,(%r15)
jmp    117e <pwrite+0x5e>
mov    $0xffffffff,%r14d
mov    %r14d,%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
store i32 %0, i32* %6
store i8* %1, i8** %7
store i64 %2, i64* %8
store i32 %3, i32* %9
%12 = load i32, i32* %6
%13 = load i32, i32* %9
%14 = load i32, i32* @SEEK_SET
%15 = call i32 @lseek(i32 %12, i32 %13, i32 %14)
store i32 %15, i32* %10
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %18
17: ; preds = %4
store i32 -1, i32* %5
br label %31
18: ; preds = %4
%19 = load i32, i32* %6
%20 = load i8*, i8** %7
%21 = load i64, i64* %8
%22 = call i64 @write(i32 %19, i8* %20, i64 %21)
store i64 %22, i64* %8
%23 = load i32, i32* @errno
store i32 %23, i32* %11
%24 = load i32, i32* %6
%25 = load i32, i32* %10
%26 = load i32, i32* @SEEK_SET
%27 = call i32 @lseek(i32 %24, i32 %25, i32 %26)
%28 = load i32, i32* %11
store i32 %28, i32* @errno
%29 = load i64, i64* %8
%30 = trunc i64 %29 to i32
store i32 %30, i32* %5
br label %31
31: ; preds = %18, %17
%32 = load i32, i32* %5
ret i32 %32
}

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 %14, label %8
8: ; preds = %4
%9 = tail call i64 @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
%13 = trunc i64 %9 to i32
br label %14
14: ; preds = %4, %8
%15 = phi i32 [ %13, %8 ], [ -1, %4 ]
ret i32 %15
}