#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 */
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
size_t fread(char *, int, size_t const, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
scalar_t__ fseek(int /*<<< orphan*/ *, long, int /*<<< orphan*/);
int /*<<< orphan*/ fwrite(char *, size_t, int, int /*<<< orphan*/ *);
char *xmalloc(size_t const);
int /*<<< orphan*/ *xopen(char const *, char const *);
int /*<<< orphan*/ *xtmpfile();
FILE *xopen_seekable(const char *name, const char *mode) {
  FILE *f = xopen(name, mode);
  if (fseek(f, 0L, SEEK_SET) != 0) {
    const size_t buflen = 64 * 1024;
    char *buffer = xmalloc(buflen);
    FILE *tmp = xtmpfile();
    while (!feof(f)) {
      size_t count = fread(buffer, 1, buflen, f);
      if (count < 1)
        break;
      fwrite(buffer, count, 1, tmp);
    }
    free(buffer);
    fclose(f);
    f = tmp;
    fseek(f, 0L, SEEK_SET);
  }
  return f;
}


x86 assembly (clang, -O0):
<xopen_seekable>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1070 <xopen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2e24(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   10a0 <fseek@plt>
cmp    $0x0,%rax
je     1286 <xopen_seekable+0xf6>
movq   $0x10000,-0x20(%rbp)
mov    $0x10000,%edi
call   1080 <xmalloc@plt>
mov    %rax,-0x28(%rbp)
mov    $0x0,%al
call   1060 <xtmpfile@plt>
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
call   1090 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1210 <xopen_seekable+0x80>
jmp    1256 <xopen_seekable+0xc6>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
mov    $0x10000,%edx
call   1040 <fread@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x1,-0x38(%rbp)
jae    123b <xopen_seekable+0xab>
jmp    1256 <xopen_seekable+0xc6>
mov    -0x28(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x30(%rbp),%rcx
mov    $0x1,%edx
call   10b0 <fwrite@plt>
jmp    11f2 <xopen_seekable+0x62>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2d65(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   10a0 <fseek@plt>
mov    -0x18(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xopen_seekable>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
call   1070 <xopen@plt>
mov    %rax,%r15
mov    0x2e39(%rip),%r12
mov    (%r12),%edx
mov    %rax,%rdi
xor    %esi,%esi
call   10a0 <fseek@plt>
test   %rax,%rax
je     123b <xopen_seekable+0xab>
mov    $0x10000,%edi
call   1080 <xmalloc@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1060 <xtmpfile@plt>
mov    %rax,%r14
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r15,%rdi
call   1090 <feof@plt>
test   %eax,%eax
jne    121b <xopen_seekable+0x8b>
mov    $0x10000,%edx
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %r15,%rcx
call   1040 <fread@plt>
test   %rax,%rax
je     121b <xopen_seekable+0x8b>
mov    %rbx,%rdi
mov    %rax,%rsi
mov    $0x1,%edx
mov    %r14,%rcx
call   10b0 <fwrite@plt>
jmp    11e0 <xopen_seekable+0x50>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r15,%rdi
call   1050 <fclose@plt>
mov    (%r12),%edx
mov    %r14,%rdi
xor    %esi,%esi
call   10a0 <fseek@plt>
jmp    123e <xopen_seekable+0xae>
mov    %r15,%r14
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@SEEK_SET = global i32 0
define i32* @xopen_seekable(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i32*
%9 = alloca i64
store i8* %0, i8** %3
store i8* %1, i8** %4
%10 = load i8*, i8** %3
%11 = load i8*, i8** %4
%12 = call i32* @xopen(i8* %10, i8* %11)
store i32* %12, i32** %5
%13 = load i32*, i32** %5
%14 = load i32, i32* @SEEK_SET
%15 = call i64 @fseek(i32* %13, i64 0, i32 %14)
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %46
17: ; preds = %2
store i64 65536, i64* %6
%18 = call i8* @xmalloc(i64 65536)
store i8* %18, i8** %7
%19 = call i32* (...) @xtmpfile()
store i32* %19, i32** %8
br label %20
20: ; preds = %32, %17
%21 = load i32*, i32** %5
%22 = call i32 @feof(i32* %21)
%23 = icmp ne i32 %22, 0
%24 = xor i1 %23, true
br i1 %24, label %25, label %37
25: ; preds = %20
%26 = load i8*, i8** %7
%27 = load i32*, i32** %5
%28 = call i64 @fread(i8* %26, i32 1, i64 65536, i32* %27)
store i64 %28, i64* %9
%29 = load i64, i64* %9
%30 = icmp ult i64 %29, 1
br i1 %30, label %31, label %32
31: ; preds = %25
br label %37
32: ; preds = %25
%33 = load i8*, i8** %7
%34 = load i64, i64* %9
%35 = load i32*, i32** %8
%36 = call i32 @fwrite(i8* %33, i64 %34, i32 1, i32* %35)
br label %20
37: ; preds = %31, %20
%38 = load i8*, i8** %7
%39 = call i32 @free(i8* %38)
%40 = load i32*, i32** %5
%41 = call i32 @fclose(i32* %40)
%42 = load i32*, i32** %8
store i32* %42, i32** %5
%43 = load i32*, i32** %5
%44 = load i32, i32* @SEEK_SET
%45 = call i64 @fseek(i32* %43, i64 0, i32 %44)
br label %46
46: ; preds = %37, %2
%47 = load i32*, i32** %5
ret i32* %47
}

LLVM IR (-O2):
@SEEK_SET = global i32 0
define i32* @xopen_seekable(i8* %0, i8* %1) {
%3 = tail call i32* @xopen(i8* %0, i8* %1)
%4 = load i32, i32* @SEEK_SET
%5 = tail call i64 @fseek(i32* %3, i64 0, i32 %4)
%6 = icmp eq i64 %5, 0
br i1 %6, label %24, label %7
7: ; preds = %2
%8 = tail call i8* @xmalloc(i64 65536)
%9 = tail call i32* (...) @xtmpfile()
%10 = tail call i32 @feof(i32* %3)
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %19
12: ; preds = %7, %15
%13 = tail call i64 @fread(i8* %8, i32 1, i64 65536, i32* %3)
%14 = icmp eq i64 %13, 0
br i1 %14, label %19, label %15
15: ; preds = %12
%16 = tail call i32 @fwrite(i8* %8, i64 %13, i32 1, i32* %9)
%17 = tail call i32 @feof(i32* %3)
%18 = icmp eq i32 %17, 0
br i1 %18, label %12, label %19
19: ; preds = %15, %12, %7
%20 = tail call i32 @free(i8* %8)
%21 = tail call i32 @fclose(i32* %3)
%22 = load i32, i32* @SEEK_SET
%23 = tail call i64 @fseek(i32* %9, i64 0, i32 %22)
br label %24
24: ; preds = %19, %2
%25 = phi i32* [ %9, %19 ], [ %3, %2 ]
ret i32* %25
}