#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*/ FILE;

/* Variables and functions */
scalar_t__ EBADF;
scalar_t__ EINVAL;
scalar_t__ EOF;
scalar_t__ ESPIPE;
#define SEEK_CUR 130
#define SEEK_END 129
#define SEEK_SET 128
scalar_t__ errno;
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ fread(long *, int, int, int /*<<< orphan*/ *);
int fseek(int /*<<< orphan*/ *, long, int);
long ftell(int /*<<< orphan*/ *);
scalar_t__ getc(int /*<<< orphan*/ *);
int xfseek(FILE *stream, long offset, int whence) {
  int r;
  long pos;
  r = fseek(stream, offset, whence);
  if ((r == -1) && ((errno == ESPIPE) || (errno == EBADF))) {
    switch (whence) {
    case SEEK_CUR:
      if (whence < 0) {
        errno = EINVAL;
        return -1;
      }
      for (; offset > 0; offset--) {
        if (EOF == getc(stream)) {
          errno = EOF;
          return -1;
        }
      }
      errno = 0;
      return 0;
    case SEEK_SET:
      pos = ftell(stream);
      if ((offset - pos) < 0) {
        errno = EINVAL;
        return -1;
      }
      return xfseek(stream, offset - pos, SEEK_CUR);
    case SEEK_END:
      if (offset != 0) {
        errno = EINVAL;
        return -1;
      }
      while (!feof(stream))
        fread(&pos, 1, 1, stream);
      return 0;
    default:
      errno = EINVAL;
      return -1;
    }
  } else
    return r;
}


x86 assembly (clang, -O0):
<xfseek>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1070 <fseek@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    13a1 <xfseek+0x241>
mov    0x2e39(%rip),%rax
mov    (%rax),%rax
mov    0x2e3f(%rip),%rcx
cmp    (%rcx),%rax
je     11c4 <xfseek+0x64>
mov    0x2e1f(%rip),%rax
mov    (%rax),%rax
mov    0x2e05(%rip),%rcx
cmp    (%rcx),%rax
jne    13a1 <xfseek+0x241>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x2c(%rbp)
sub    $0x80,%eax
je     12b1 <xfseek+0x151>
jmp    11da <xfseek+0x7a>
mov    -0x2c(%rbp),%eax
sub    $0x81,%eax
je     130e <xfseek+0x1ae>
jmp    11ed <xfseek+0x8d>
mov    -0x2c(%rbp),%eax
sub    $0x82,%eax
jne    1381 <xfseek+0x221>
jmp    1200 <xfseek+0xa0>
cmpl   $0x0,-0x1c(%rbp)
jge    122a <xfseek+0xca>
mov    0x2db7(%rip),%rax
mov    (%rax),%rcx
mov    0x2db5(%rip),%rax
mov    %rcx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    13a7 <xfseek+0x247>
jmp    122f <xfseek+0xcf>
cmpq   $0x0,-0x18(%rbp)
jle    1297 <xfseek+0x137>
mov    0x2da7(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
call   1080 <getc@plt>
mov    %rax,%rcx
mov    -0x38(%rbp),%rax
cmp    %rcx,%rax
jne    1281 <xfseek+0x121>
mov    0x2d80(%rip),%rax
mov    (%rax),%rcx
mov    0x2d5e(%rip),%rax
mov    %rcx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    13a7 <xfseek+0x247>
jmp    1286 <xfseek+0x126>
mov    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
jmp    122f <xfseek+0xcf>
mov    0x2d32(%rip),%rax
movq   $0x0,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    13a7 <xfseek+0x247>
mov    -0x10(%rbp),%rdi
call   1050 <ftell@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
sub    -0x28(%rbp),%rax
cmp    $0x0,%rax
jge    12f0 <xfseek+0x190>
mov    0x2cf1(%rip),%rax
mov    (%rax),%rcx
mov    0x2cef(%rip),%rax
mov    %rcx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    13a7 <xfseek+0x247>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
sub    -0x28(%rbp),%rsi
mov    $0x82,%edx
call   1040 <xfseek@plt>
mov    %eax,-0x4(%rbp)
jmp    13a7 <xfseek+0x247>
cmpq   $0x0,-0x18(%rbp)
je     1339 <xfseek+0x1d9>
mov    0x2ca8(%rip),%rax
mov    (%rax),%rcx
mov    0x2ca6(%rip),%rax
mov    %rcx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    13a7 <xfseek+0x247>
jmp    133e <xfseek+0x1de>
mov    -0x10(%rbp),%rdi
call   1060 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    135c <xfseek+0x1fc>
jmp    1375 <xfseek+0x215>
mov    -0x10(%rbp),%rcx
lea    -0x28(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1030 <fread@plt>
jmp    133e <xfseek+0x1de>
movl   $0x0,-0x4(%rbp)
jmp    13a7 <xfseek+0x247>
mov    0x2c40(%rip),%rax
mov    (%rax),%rcx
mov    0x2c3e(%rip),%rax
mov    %rcx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    13a7 <xfseek+0x247>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xfseek>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %rsi,%rbx
mov    %rdi,%r14
call   1060 <fseek@plt>
mov    %eax,%r15d
cmp    $0xffffffff,%eax
jne    1322 <xfseek+0x1d2>
mov    0x2e55(%rip),%rcx
mov    (%rcx),%rax
mov    0x2e5b(%rip),%r12
mov    0x2e34(%rip),%r13
mov    $0xffffffff,%r15d
cmp    (%r12),%rax
je     11a2 <xfseek+0x52>
cmp    0x0(%r13),%rax
jne    1322 <xfseek+0x1d2>
cmp    $0x80,%ebp
je     1208 <xfseek+0xb8>
cmp    $0x81,%ebp
je     129c <xfseek+0x14c>
cmp    $0x82,%ebp
jne    12a1 <xfseek+0x151>
test   %rbx,%rbx
jle    12b3 <xfseek+0x163>
mov    %rcx,%r12
add    $0x1,%rbx
mov    0x2e0f(%rip),%r15
nopl   0x0(%rax)
mov    (%r15),%rbp
mov    %r14,%rdi
call   1070 <getc@plt>
cmp    %rax,%rbp
je     1313 <xfseek+0x1c3>
add    $0xffffffffffffffff,%rbx
cmp    $0x2,%rbx
jge    11e0 <xfseek+0x90>
xor    %eax,%eax
xor    %r15d,%r15d
jmp    131c <xfseek+0x1cc>
mov    %r14,%rdi
call   1040 <ftell@plt>
sub    %rax,%rbx
js     12ba <xfseek+0x16a>
mov    %r14,%rdi
mov    %rbx,%rsi
mov    $0x82,%edx
call   1060 <fseek@plt>
mov    %eax,%r15d
cmp    $0xffffffff,%eax
jne    1322 <xfseek+0x1d2>
mov    0x2d94(%rip),%rax
mov    (%rax),%rax
cmp    (%r12),%rax
je     1255 <xfseek+0x105>
mov    $0xffffffff,%r15d
cmp    0x0(%r13),%rax
jne    1322 <xfseek+0x1d2>
test   %rbx,%rbx
je     1334 <xfseek+0x1e4>
add    $0x1,%rbx
mov    0x2d7f(%rip),%r12
mov    $0x0,%r15d
mov    (%r12),%rbp
mov    %r14,%rdi
call   1070 <getc@plt>
cmp    %rax,%rbp
je     1342 <xfseek+0x1f2>
add    $0xffffffffffffffff,%rbx
xor    %eax,%eax
cmp    $0x2,%rbx
mov    0x2d3b(%rip),%rcx
jge    126f <xfseek+0x11f>
jmp    131f <xfseek+0x1cf>
test   %rbx,%rbx
je     12d3 <xfseek+0x183>
mov    0x2d20(%rip),%rax
mov    (%rax),%rax
mov    $0xffffffff,%r15d
jmp    131f <xfseek+0x1cf>
xor    %eax,%eax
xor    %r15d,%r15d
jmp    131f <xfseek+0x1cf>
mov    0x2d07(%rip),%rax
mov    (%rax),%rax
mov    $0xffffffff,%r15d
mov    0x2cff(%rip),%rcx
jmp    131f <xfseek+0x1cf>
mov    %r14,%rdi
call   1050 <feof@plt>
xor    %r15d,%r15d
test   %eax,%eax
jne    1322 <xfseek+0x1d2>
mov    %rsp,%rbx
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x1,%edx
mov    %r14,%rcx
call   1030 <fread@plt>
mov    %r14,%rdi
call   1050 <feof@plt>
test   %eax,%eax
je     12f0 <xfseek+0x1a0>
jmp    1322 <xfseek+0x1d2>
mov    (%r15),%rax
mov    $0xffffffff,%r15d
mov    %r12,%rcx
mov    %rax,(%rcx)
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %eax,%eax
xor    %r15d,%r15d
mov    0x2c90(%rip),%rcx
jmp    131f <xfseek+0x1cf>
mov    (%r12),%rax
jmp    12c4 <xfseek+0x174>

LLVM IR (-O0):
@errno = global i64 0
@ESPIPE = global i64 0
@EBADF = global i64 0
@EINVAL = global i64 0
@EOF = global i64 0
define i32 @xfseek(i32* %0, i64 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
%9 = alloca i64
store i32* %0, i32** %5
store i64 %1, i64* %6
store i32 %2, i32* %7
%10 = load i32*, i32** %5
%11 = load i64, i64* %6
%12 = load i32, i32* %7
%13 = call i32 @fseek(i32* %10, i64 %11, i32 %12)
store i32 %13, i32* %8
%14 = load i32, i32* %8
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %79
16: ; preds = %3
%17 = load i64, i64* @errno
%18 = load i64, i64* @ESPIPE
%19 = icmp eq i64 %17, %18
br i1 %19, label %24, label %20
20: ; preds = %16
%21 = load i64, i64* @errno
%22 = load i64, i64* @EBADF
%23 = icmp eq i64 %21, %22
br i1 %23, label %24, label %79
24: ; preds = %20, %16
%25 = load i32, i32* %7
switch i32 %25, label %77 [
i32 130, label %26
i32 128, label %47
i32 129, label %62
]
26: ; preds = %24
%27 = load i32, i32* %7
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %26
%30 = load i64, i64* @EINVAL
store i64 %30, i64* @errno
store i32 -1, i32* %4
br label %81
31: ; preds = %26
br label %32
32: ; preds = %43, %31
%33 = load i64, i64* %6
%34 = icmp sgt i64 %33, 0
br i1 %34, label %35, label %46
35: ; preds = %32
%36 = load i64, i64* @EOF
%37 = load i32*, i32** %5
%38 = call i64 @getc(i32* %37)
%39 = icmp eq i64 %36, %38
br i1 %39, label %40, label %42
40: ; preds = %35
%41 = load i64, i64* @EOF
store i64 %41, i64* @errno
store i32 -1, i32* %4
br label %81
42: ; preds = %35
br label %43
43: ; preds = %42
%44 = load i64, i64* %6
%45 = add nsw i64 %44, -1
store i64 %45, i64* %6
br label %32
46: ; preds = %32
store i64 0, i64* @errno
store i32 0, i32* %4
br label %81
47: ; preds = %24
%48 = load i32*, i32** %5
%49 = call i64 @ftell(i32* %48)
store i64 %49, i64* %9
%50 = load i64, i64* %6
%51 = load i64, i64* %9
%52 = sub nsw i64 %50, %51
%53 = icmp slt i64 %52, 0
br i1 %53, label %54, label %56
54: ; preds = %47
%55 = load i64, i64* @EINVAL
store i64 %55, i64* @errno
store i32 -1, i32* %4
br label %81
56: ; preds = %47
%57 = load i32*, i32** %5
%58 = load i64, i64* %6
%59 = load i64, i64* %9
%60 = sub nsw i64 %58, %59
%61 = call i32 @xfseek(i32* %57, i64 %60, i32 130)
store i32 %61, i32* %4
br label %81
62: ; preds = %24
%63 = load i64, i64* %6
%64 = icmp ne i64 %63, 0
br i1 %64, label %65, label %67
65: ; preds = %62
%66 = load i64, i64* @EINVAL
store i64 %66, i64* @errno
store i32 -1, i32* %4
br label %81
67: ; preds = %62
br label %68
68: ; preds = %73, %67
%69 = load i32*, i32** %5
%70 = call i32 @feof(i32* %69)
%71 = icmp ne i32 %70, 0
%72 = xor i1 %71, true
br i1 %72, label %73, label %76
73: ; preds = %68
%74 = load i32*, i32** %5
%75 = call i32 @fread(i64* %9, i32 1, i32 1, i32* %74)
br label %68
76: ; preds = %68
store i32 0, i32* %4
br label %81
77: ; preds = %24
%78 = load i64, i64* @EINVAL
store i64 %78, i64* @errno
store i32 -1, i32* %4
br label %81
79: ; preds = %20, %3
%80 = load i32, i32* %8
store i32 %80, i32* %4
br label %81
81: ; preds = %79, %77, %76, %65, %56, %54, %46, %40, %29
%82 = load i32, i32* %4
ret i32 %82
}

LLVM IR (-O2):
@errno = global i64 0
@ESPIPE = global i64 0
@EBADF = global i64 0
@EINVAL = global i64 0
@EOF = global i64 0
define i32 @xfseek(i32* nocapture %0, i64 %1, i32 %2) {
%4 = alloca i64
%5 = bitcast i64* %4 to i8*
%6 = tail call i32 @fseek(i32* %0, i64 %1, i32 %2)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %72
8: ; preds = %3
%9 = load i64, i64* @errno
%10 = load i64, i64* @ESPIPE
%11 = icmp eq i64 %9, %10
%12 = load i64, i64* @EBADF
%13 = icmp eq i64 %9, %12
%14 = select i1 %11, i1 true, i1 %13
br i1 %14, label %15, label %72
15: ; preds = %8
switch i32 %2, label %67 [
i32 130, label %16
i32 128, label %28
i32 129, label %56
]
16: ; preds = %15
%17 = icmp sgt i64 %1, 0
br i1 %17, label %21, label %69
18: ; preds = %21
%19 = add nsw i64 %22, -1
%20 = icmp sgt i64 %22, 1
br i1 %20, label %21, label %69
21: ; preds = %16, %18
%22 = phi i64 [ %19, %18 ], [ %1, %16 ]
%23 = load i64, i64* @EOF
%24 = tail call i64 @getc(i32* %0)
%25 = icmp eq i64 %23, %24
br i1 %25, label %26, label %18
26: ; preds = %21
%27 = load i64, i64* @EOF
br label %69
28: ; preds = %15
%29 = tail call i64 @ftell(i32* %0)
%30 = sub nsw i64 %1, %29
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %28
%33 = load i64, i64* @EINVAL
br label %69
34: ; preds = %28
%35 = tail call i32 @fseek(i32* %0, i64 %30, i32 130)
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %72
37: ; preds = %34
%38 = load i64, i64* @errno
%39 = load i64, i64* @ESPIPE
%40 = icmp eq i64 %38, %39
%41 = load i64, i64* @EBADF
%42 = icmp eq i64 %38, %41
%43 = select i1 %40, i1 true, i1 %42
br i1 %43, label %44, label %72
44: ; preds = %37
%45 = icmp eq i64 %30, 0
br i1 %45, label %69, label %49
46: ; preds = %49
%47 = add nsw i64 %50, -1
%48 = icmp sgt i64 %50, 1
br i1 %48, label %49, label %69
49: ; preds = %44, %46
%50 = phi i64 [ %47, %46 ], [ %30, %44 ]
%51 = load i64, i64* @EOF
%52 = tail call i64 @getc(i32* %0)
%53 = icmp eq i64 %51, %52
br i1 %53, label %54, label %46
54: ; preds = %49
%55 = load i64, i64* @EOF
br label %69
56: ; preds = %15
%57 = icmp eq i64 %1, 0
br i1 %57, label %58, label %61
58: ; preds = %56
%59 = tail call i32 @feof(i32* %0)
%60 = icmp eq i32 %59, 0
br i1 %60, label %63, label %72
61: ; preds = %56
%62 = load i64, i64* @EINVAL
br label %69
63: ; preds = %58, %63
%64 = call i32 @fread(i64* %4, i32 1, i32 1, i32* %0)
%65 = tail call i32 @feof(i32* %0)
%66 = icmp eq i32 %65, 0
br i1 %66, label %63, label %72
67: ; preds = %15
%68 = load i64, i64* @EINVAL
br label %69
69: ; preds = %46, %18, %44, %16, %26, %32, %61, %67, %54
%70 = phi i64 [ %55, %54 ], [ %68, %67 ], [ %62, %61 ], [ %33, %32 ], [ %27, %26 ], [ 0, %16 ], [ 0, %44 ], [ 0, %18 ], [ 0, %46 ]
%71 = phi i32 [ -1, %54 ], [ -1, %67 ], [ -1, %61 ], [ -1, %32 ], [ -1, %26 ], [ 0, %16 ], [ 0, %44 ], [ 0, %18 ], [ 0, %46 ]
store i64 %70, i64* @errno
br label %72
72: ; preds = %63, %69, %58, %37, %34, %3, %8
%73 = phi i32 [ -1, %8 ], [ %6, %3 ], [ -1, %37 ], [ %35, %34 ], [ 0, %58 ], [ %71, %69 ], [ 0, %63 ]
ret i32 %73
}