#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 /*<<< orphan*/ fpos_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
scalar_t__ fgetpos(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
size_t fread(void *, int, size_t, int /*<<< orphan*/ *);
scalar_t__ fseek(int /*<<< orphan*/ *, long, int);
scalar_t__ fsetpos(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
size_t optk_fread_at(FILE *stream, long offset, int whence, void *block,
                     size_t blocksize) {
  fpos_t pos;
  size_t result;
  if (fgetpos(stream, &pos) != 0)
    return 0;
  if (fseek(stream, offset, whence) == 0)
    result = fread(block, 1, blocksize, stream);
  else
    result = 0;
  if (fsetpos(stream, &pos) != 0)
    result = 0;
  return result;
}


x86 assembly (clang, -O0):
<optk_fread_at>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x34(%rbp),%rsi
call   1040 <fgetpos@plt>
cmp    $0x0,%rax
je     117f <optk_fread_at+0x3f>
movq   $0x0,-0x8(%rbp)
jmp    11e7 <optk_fread_at+0xa7>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1050 <fseek@plt>
cmp    $0x0,%rax
jne    11b8 <optk_fread_at+0x78>
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rdx
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %rax,-0x40(%rbp)
jmp    11c0 <optk_fread_at+0x80>
movq   $0x0,-0x40(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x34(%rbp),%rsi
call   1060 <fsetpos@plt>
cmp    $0x0,%rax
je     11df <optk_fread_at+0x9f>
movq   $0x0,-0x40(%rbp)
mov    -0x40(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<optk_fread_at>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %r8,%r14
mov    %rcx,%r15
mov    %edx,%r12d
mov    %rsi,%rbp
mov    %rdi,%rbx
lea    0xc(%rsp),%rsi
call   1040 <fgetpos@plt>
test   %rax,%rax
je     116e <optk_fread_at+0x2e>
xor    %eax,%eax
jmp    11b5 <optk_fread_at+0x75>
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    %r12d,%edx
call   1050 <fseek@plt>
test   %rax,%rax
je     1186 <optk_fread_at+0x46>
xor    %r14d,%r14d
jmp    119c <optk_fread_at+0x5c>
mov    %r15,%rdi
mov    $0x1,%esi
mov    %r14,%rdx
mov    %rbx,%rcx
call   1030 <fread@plt>
mov    %rax,%r14
lea    0xc(%rsp),%rsi
mov    %rbx,%rdi
call   1060 <fsetpos@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
cmove  %r14,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i64 @optk_fread_at(i32* %0, i64 %1, i32 %2, i8* %3, i64 %4) {
%6 = alloca i64
%7 = alloca i32*
%8 = alloca i64
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i64
%12 = alloca i32
%13 = alloca i64
store i32* %0, i32** %7
store i64 %1, i64* %8
store i32 %2, i32* %9
store i8* %3, i8** %10
store i64 %4, i64* %11
%14 = load i32*, i32** %7
%15 = call i64 @fgetpos(i32* %14, i32* %12)
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %5
store i64 0, i64* %6
br label %37
18: ; preds = %5
%19 = load i32*, i32** %7
%20 = load i64, i64* %8
%21 = load i32, i32* %9
%22 = call i64 @fseek(i32* %19, i64 %20, i32 %21)
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %29
24: ; preds = %18
%25 = load i8*, i8** %10
%26 = load i64, i64* %11
%27 = load i32*, i32** %7
%28 = call i64 @fread(i8* %25, i32 1, i64 %26, i32* %27)
store i64 %28, i64* %13
br label %30
29: ; preds = %18
store i64 0, i64* %13
br label %30
30: ; preds = %29, %24
%31 = load i32*, i32** %7
%32 = call i64 @fsetpos(i32* %31, i32* %12)
%33 = icmp ne i64 %32, 0
br i1 %33, label %34, label %35
34: ; preds = %30
store i64 0, i64* %13
br label %35
35: ; preds = %34, %30
%36 = load i64, i64* %13
store i64 %36, i64* %6
br label %37
37: ; preds = %35, %17
%38 = load i64, i64* %6
ret i64 %38
}

LLVM IR (-O2):
define i64 @optk_fread_at(i32* nocapture %0, i64 %1, i32 %2, i8* nocapture %3, i64 %4) {
%6 = alloca i32
%7 = bitcast i32* %6 to i8*
%8 = call i64 @fgetpos(i32* %0, i32* %6)
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %20
10: ; preds = %5
%11 = tail call i64 @fseek(i32* %0, i64 %1, i32 %2)
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %10
%14 = tail call i64 @fread(i8* %3, i32 1, i64 %4, i32* %0)
br label %15
15: ; preds = %10, %13
%16 = phi i64 [ %14, %13 ], [ 0, %10 ]
%17 = call i64 @fsetpos(i32* %0, i32* %6)
%18 = icmp eq i64 %17, 0
%19 = select i1 %18, i64 %16, i64 0
br label %20
20: ; preds = %5, %15
%21 = phi i64 [ %19, %15 ], [ 0, %5 ]
ret i64 %21
}