#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;
int /*<<< orphan*/ __set_errno(int);
int errno;
scalar_t__ lseek(int, scalar_t__, int /*<<< orphan*/);
int read(int, void *, size_t);
int write(int, void *, size_t);
ssize_t __fake_pread_write(int fd, void *buf, size_t count, off_t offset,
                           int do_pwrite) {
  int save_errno;
  ssize_t result;
  off_t old_offset;
  if ((old_offset = lseek(fd, 0, SEEK_CUR)) == (off_t)-1)
    return -1;
  if (lseek(fd, offset, SEEK_SET) == (off_t)-1)
    return -1;
  if (do_pwrite == 1) {
    result = write(fd, buf, count);
  } else {
    result = read(fd, buf, count);
  }
  save_errno = errno;
  if (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):
<__fake_pread_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e83(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1050 <lseek@plt>
mov    %rax,-0x38(%rbp)
cmp    $0xffffffffffffffff,%rax
jne    118a <__fake_pread_write+0x4a>
movl   $0xffffffff,-0x4(%rbp)
jmp    1241 <__fake_pread_write+0x101>
mov    -0x8(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    0x2e38(%rip),%rax
mov    (%rax),%edx
call   1050 <lseek@plt>
cmp    $0xffffffffffffffff,%rax
jne    11b5 <__fake_pread_write+0x75>
movl   $0xffffffff,-0x4(%rbp)
jmp    1241 <__fake_pread_write+0x101>
cmpl   $0x1,-0x24(%rbp)
jne    11d7 <__fake_pread_write+0x97>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <write@plt>
mov    %eax,-0x2c(%rbp)
jmp    11ea <__fake_pread_write+0xaa>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1060 <read@plt>
mov    %eax,-0x2c(%rbp)
mov    0x2de7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x38(%rbp),%rsi
mov    0x2dcc(%rip),%rax
mov    (%rax),%edx
call   1050 <lseek@plt>
cmp    $0xffffffffffffffff,%rax
jne    1233 <__fake_pread_write+0xf3>
cmpl   $0xffffffff,-0x2c(%rbp)
jne    1227 <__fake_pread_write+0xe7>
mov    -0x28(%rbp),%edi
call   1030 <__set_errno@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1241 <__fake_pread_write+0x101>
mov    -0x28(%rbp),%edi
call   1030 <__set_errno@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__fake_pread_write>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8d,%r13d
mov    %rcx,%r15
mov    %rdx,(%rsp)
mov    %rsi,%r12
mov    %edi,%ebx
mov    0x2e87(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1050 <lseek@plt>
mov    $0xffffffff,%ebp
cmp    $0xffffffffffffffff,%rax
je     11f2 <__fake_pread_write+0xb2>
mov    %rax,%r14
mov    0x2e51(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
mov    %r15,%rsi
call   1050 <lseek@plt>
cmp    $0xffffffffffffffff,%rax
je     11f2 <__fake_pread_write+0xb2>
mov    %ebx,%edi
mov    %r12,%rsi
cmp    $0x1,%r13d
jne    11a7 <__fake_pread_write+0x67>
mov    (%rsp),%rdx
call   1040 <write@plt>
jmp    11b0 <__fake_pread_write+0x70>
mov    (%rsp),%rdx
call   1060 <read@plt>
mov    %eax,%r15d
mov    0x2e16(%rip),%rcx
mov    0x2e17(%rip),%rax
mov    (%rax),%r12d
mov    (%rcx),%edx
mov    %ebx,%edi
mov    %r14,%rsi
call   1050 <lseek@plt>
cmp    $0xffffffffffffffff,%rax
jne    11e7 <__fake_pread_write+0xa7>
mov    $0xffffffff,%ebp
cmp    $0xffffffff,%r15d
mov    $0xffffffff,%r15d
jne    11f2 <__fake_pread_write+0xb2>
mov    %r12d,%edi
call   1030 <__set_errno@plt>
mov    %r15d,%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 @__fake_pread_write(i32 %0, i8* %1, i64 %2, i64 %3, i32 %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i64
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i64
store i32 %0, i32* %7
store i8* %1, i8** %8
store i64 %2, i64* %9
store i64 %3, i64* %10
store i32 %4, i32* %11
%15 = load i32, i32* %7
%16 = load i32, i32* @SEEK_CUR
%17 = call i64 @lseek(i32 %15, i64 0, i32 %16)
store i64 %17, i64* %14
%18 = icmp eq i64 %17, -1
br i1 %18, label %19, label %20
19: ; preds = %5
store i32 -1, i32* %6
br label %58
20: ; preds = %5
%21 = load i32, i32* %7
%22 = load i64, i64* %10
%23 = load i32, i32* @SEEK_SET
%24 = call i64 @lseek(i32 %21, i64 %22, i32 %23)
%25 = icmp eq i64 %24, -1
br i1 %25, label %26, label %27
26: ; preds = %20
store i32 -1, i32* %6
br label %58
27: ; preds = %20
%28 = load i32, i32* %11
%29 = icmp eq i32 %28, 1
br i1 %29, label %30, label %35
30: ; preds = %27
%31 = load i32, i32* %7
%32 = load i8*, i8** %8
%33 = load i64, i64* %9
%34 = call i32 @write(i32 %31, i8* %32, i64 %33)
store i32 %34, i32* %13
br label %40
35: ; preds = %27
%36 = load i32, i32* %7
%37 = load i8*, i8** %8
%38 = load i64, i64* %9
%39 = call i32 @read(i32 %36, i8* %37, i64 %38)
store i32 %39, i32* %13
br label %40
40: ; preds = %35, %30
%41 = load i32, i32* @errno
store i32 %41, i32* %12
%42 = load i32, i32* %7
%43 = load i64, i64* %14
%44 = load i32, i32* @SEEK_SET
%45 = call i64 @lseek(i32 %42, i64 %43, i32 %44)
%46 = icmp eq i64 %45, -1
br i1 %46, label %47, label %54
47: ; preds = %40
%48 = load i32, i32* %13
%49 = icmp eq i32 %48, -1
br i1 %49, label %50, label %53
50: ; preds = %47
%51 = load i32, i32* %12
%52 = call i32 @__set_errno(i32 %51)
br label %53
53: ; preds = %50, %47
store i32 -1, i32* %6
br label %58
54: ; preds = %40
%55 = load i32, i32* %12
%56 = call i32 @__set_errno(i32 %55)
%57 = load i32, i32* %13
store i32 %57, i32* %6
br label %58
58: ; preds = %54, %53, %26, %19
%59 = load i32, i32* %6
ret i32 %59
}

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