#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 lseek(int, scalar_t__, int /*<<< orphan*/);
int read(int, void *, size_t);
ssize_t pread(int __fd, void *__buf, size_t __nbytes, off_t __offset) {
  ssize_t ret;
  off_t old = lseek(__fd, 0, SEEK_CUR);
  if (old == (off_t)-1)
    return -1;
  if (-1 == lseek(__fd, __offset, SEEK_SET))
    return -1;
  ret = read(__fd, __buf, __nbytes);
  lseek(__fd, old, SEEK_SET);
  return ret;
}


x86 assembly (clang, -O0):
<pread>:
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    0x2ea7(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1030 <lseek@plt>
cltq
mov    %rax,-0x30(%rbp)
cmpq   $0xffffffffffffffff,-0x30(%rbp)
jne    1169 <pread+0x49>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c7 <pread+0xa7>
mov    -0x8(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    0x2e61(%rip),%rax
mov    (%rax),%edx
call   1030 <lseek@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    1199 <pread+0x79>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c7 <pread+0xa7>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <read@plt>
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    0x2e1e(%rip),%rax
mov    (%rax),%edx
call   1030 <lseek@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pread>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r12
mov    %rdx,%r14
mov    %rsi,%r15
mov    %edi,%ebx
mov    0x2eab(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1030 <lseek@plt>
cmp    $0xffffffff,%eax
je     118b <pread+0x6b>
mov    %eax,%ebp
mov    0x2e84(%rip),%r13
mov    0x0(%r13),%edx
mov    %ebx,%edi
mov    %r12,%rsi
call   1030 <lseek@plt>
cmp    $0xffffffff,%eax
je     118b <pread+0x6b>
movslq %ebp,%r12
mov    %ebx,%edi
mov    %r15,%rsi
mov    %r14,%rdx
call   1040 <read@plt>
mov    %eax,%ebp
mov    0x0(%r13),%edx
mov    %ebx,%edi
mov    %r12,%rsi
call   1030 <lseek@plt>
mov    %ebp,%eax
jmp    1190 <pread+0x70>
mov    $0xffffffff,%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
define i32 @pread(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 i64
store i32 %0, i32* %6
store i8* %1, i8** %7
store i64 %2, i64* %8
store i64 %3, i64* %9
%12 = load i32, i32* %6
%13 = load i32, i32* @SEEK_CUR
%14 = call i32 @lseek(i32 %12, i64 0, i32 %13)
%15 = sext i32 %14 to i64
store i64 %15, i64* %11
%16 = load i64, i64* %11
%17 = icmp eq i64 %16, -1
br i1 %17, label %18, label %19
18: ; preds = %4
store i32 -1, i32* %5
br label %36
19: ; preds = %4
%20 = load i32, i32* %6
%21 = load i64, i64* %9
%22 = load i32, i32* @SEEK_SET
%23 = call i32 @lseek(i32 %20, i64 %21, i32 %22)
%24 = icmp eq i32 -1, %23
br i1 %24, label %25, label %26
25: ; preds = %19
store i32 -1, i32* %5
br label %36
26: ; preds = %19
%27 = load i32, i32* %6
%28 = load i8*, i8** %7
%29 = load i64, i64* %8
%30 = call i32 @read(i32 %27, i8* %28, i64 %29)
store i32 %30, i32* %10
%31 = load i32, i32* %6
%32 = load i64, i64* %11
%33 = load i32, i32* @SEEK_SET
%34 = call i32 @lseek(i32 %31, i64 %32, i32 %33)
%35 = load i32, i32* %10
store i32 %35, i32* %5
br label %36
36: ; preds = %26, %25, %18
%37 = load i32, i32* %5
ret i32 %37
}

LLVM IR (-O2):
@SEEK_CUR = global i32 0
@SEEK_SET = global i32 0
define i32 @pread(i32 %0, i8* nocapture %1, i64 %2, i64 %3) {
%5 = load i32, i32* @SEEK_CUR
%6 = tail call i32 @lseek(i32 %0, i64 0, i32 %5)
%7 = sext i32 %6 to i64
%8 = icmp eq i32 %6, -1
br i1 %8, label %17, label %9
9: ; preds = %4
%10 = load i32, i32* @SEEK_SET
%11 = tail call i32 @lseek(i32 %0, i64 %3, i32 %10)
%12 = icmp eq i32 %11, -1
br i1 %12, label %17, label %13
13: ; preds = %9
%14 = tail call i32 @read(i32 %0, i8* %1, i64 %2)
%15 = load i32, i32* @SEEK_SET
%16 = tail call i32 @lseek(i32 %0, i64 %7, i32 %15)
br label %17
17: ; preds = %9, %4, %13
%18 = phi i32 [ %14, %13 ], [ -1, %4 ], [ -1, %9 ]
ret i32 %18
}