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

/* Variables and functions */
int /*<<< orphan*/ SEEK_CUR;
int fread(char *, int, int, int /*<<< orphan*/ *);
scalar_t__ fseeko(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
int64_t read_backwards(FILE *fp, char *buf, int64_t size) {
  int64_t ret;
  if ((ret = fseeko(fp, -size, SEEK_CUR)) != 0)
    return -16;
  if ((ret = fread(buf, 1, size, fp)) != size)
    return -16;
  if (fseeko(fp, -((int64_t)ret), SEEK_CUR) != 0)
    return -16;
  return ret;
}


x86 assembly (clang, -O0):
<read_backwards>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
xor    %esi,%esi
sub    -0x1c(%rbp),%esi
mov    0x2ea5(%rip),%rax
mov    (%rax),%edx
call   1040 <fseeko@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
je     1162 <read_backwards+0x42>
movl   $0xfffffff0,-0x4(%rbp)
jmp    11c2 <read_backwards+0xa2>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%edx
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %eax,-0x20(%rbp)
cmp    -0x1c(%rbp),%eax
je     118f <read_backwards+0x6f>
movl   $0xfffffff0,-0x4(%rbp)
jmp    11c2 <read_backwards+0xa2>
mov    -0x10(%rbp),%rdi
xor    %esi,%esi
sub    -0x20(%rbp),%esi
mov    0x2e49(%rip),%rax
mov    (%rax),%edx
call   1040 <fseeko@plt>
cmp    $0x0,%rax
je     11bc <read_backwards+0x9c>
movl   $0xfffffff0,-0x4(%rbp)
jmp    11c2 <read_backwards+0xa2>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_backwards>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %rsi,%r12
mov    %rdi,%r15
mov    %edx,%ebp
neg    %ebp
mov    0x2ea9(%rip),%r13
mov    0x0(%r13),%edx
mov    %ebp,%esi
call   1040 <fseeko@plt>
mov    $0xfffffff0,%ebx
test   %eax,%eax
jne    1185 <read_backwards+0x65>
mov    %r12,%rdi
mov    $0x1,%esi
mov    %r14d,%edx
mov    %r15,%rcx
call   1030 <fread@plt>
cmp    %r14d,%eax
jne    1185 <read_backwards+0x65>
mov    0x0(%r13),%edx
mov    %r15,%rdi
mov    %ebp,%esi
call   1040 <fseeko@plt>
test   %rax,%rax
mov    $0xfffffff0,%ebx
cmove  %r14d,%ebx
mov    %ebx,%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
define i32 @read_backwards(i32* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
store i32* %0, i32** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%9 = load i32*, i32** %5
%10 = load i32, i32* %7
%11 = sub nsw i32 0, %10
%12 = load i32, i32* @SEEK_CUR
%13 = call i64 @fseeko(i32* %9, i32 %11, i32 %12)
%14 = trunc i64 %13 to i32
store i32 %14, i32* %8
%15 = icmp ne i32 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %3
store i32 -16, i32* %4
br label %35
17: ; preds = %3
%18 = load i8*, i8** %6
%19 = load i32, i32* %7
%20 = load i32*, i32** %5
%21 = call i32 @fread(i8* %18, i32 1, i32 %19, i32* %20)
store i32 %21, i32* %8
%22 = load i32, i32* %7
%23 = icmp ne i32 %21, %22
br i1 %23, label %24, label %25
24: ; preds = %17
store i32 -16, i32* %4
br label %35
25: ; preds = %17
%26 = load i32*, i32** %5
%27 = load i32, i32* %8
%28 = sub nsw i32 0, %27
%29 = load i32, i32* @SEEK_CUR
%30 = call i64 @fseeko(i32* %26, i32 %28, i32 %29)
%31 = icmp ne i64 %30, 0
br i1 %31, label %32, label %33
32: ; preds = %25
store i32 -16, i32* %4
br label %35
33: ; preds = %25
%34 = load i32, i32* %8
store i32 %34, i32* %4
br label %35
35: ; preds = %33, %32, %24, %16
%36 = load i32, i32* %4
ret i32 %36
}

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