#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 */
struct ygo_replay {
  int dummy;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ SEEK_END;
scalar_t__ ferror(int /*<<< orphan*/ *);
size_t fread(char *, int, long, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
scalar_t__ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/, int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
char *malloc(long);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int ygo_replay_sread(struct ygo_replay *, char *, size_t);
int ygo_replay_fread(struct ygo_replay *replay, FILE *stream) {
  int err = 0;
  if (fseek(stream, 0, SEEK_END) != 0) {
    err = 3;
    goto err_fseek;
  }
  long size = ftell(stream);
  rewind(stream);
  char *buf = malloc((size + 1) * sizeof(char));
  if (!buf) {
    err = 5;
    goto err_malloc;
  }
  size_t nread = fread(buf, sizeof(char), size, stream);
  if (nread != size && ferror(stream) != 0) {
    err = 4;
    goto err_fread;
  }
  err = ygo_replay_sread(replay, buf, nread);
err_fread:
  free(buf);
err_malloc:
err_fseek:
  return err;
}


x86 assembly (clang, -O0):
<ygo_replay_fread>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e46(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10a0 <fseek@plt>
cmp    $0x0,%rax
je     11c1 <ygo_replay_fread+0x41>
movl   $0x3,-0x14(%rbp)
jmp    126c <ygo_replay_fread+0xec>
mov    -0x10(%rbp),%rdi
call   1080 <ftell@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1070 <rewind@plt>
mov    -0x20(%rbp),%rdi
add    $0x1,%rdi
shl    $0x0,%rdi
call   1090 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1203 <ygo_replay_fread+0x83>
movl   $0x5,-0x14(%rbp)
jmp    1267 <ygo_replay_fread+0xe7>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
call   1050 <fread@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x20(%rbp),%rax
je     124a <ygo_replay_fread+0xca>
mov    -0x10(%rbp),%rdi
call   1040 <ferror@plt>
cmp    $0x0,%rax
je     124a <ygo_replay_fread+0xca>
movl   $0x4,-0x14(%rbp)
jmp    125e <ygo_replay_fread+0xde>
mov    -0x8(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1060 <ygo_replay_sread@plt>
mov    %eax,-0x14(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
jmp    126c <ygo_replay_fread+0xec>
mov    -0x14(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ygo_replay_fread>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r14
mov    0x2e50(%rip),%rax
mov    (%rax),%edx
mov    %rsi,%rdi
xor    %esi,%esi
call   10a0 <fseek@plt>
mov    $0x3,%ebp
test   %rax,%rax
jne    121f <ygo_replay_fread+0x9f>
mov    %rbx,%rdi
call   1080 <ftell@plt>
mov    %rax,%r12
mov    %rbx,%rdi
call   1070 <rewind@plt>
lea    0x1(%r12),%rdi
call   1090 <malloc@plt>
test   %rax,%rax
je     121a <ygo_replay_fread+0x9a>
mov    %rax,%r15
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r12,%rdx
mov    %rbx,%rcx
call   1050 <fread@plt>
mov    %rax,%r13
cmp    %r12,%rax
je     1200 <ygo_replay_fread+0x80>
mov    %rbx,%rdi
call   1040 <ferror@plt>
mov    $0x4,%ebp
test   %rax,%rax
jne    1210 <ygo_replay_fread+0x90>
mov    %r14,%rdi
mov    %r15,%rsi
mov    %r13,%rdx
call   1060 <ygo_replay_sread@plt>
mov    %eax,%ebp
mov    %r15,%rdi
call   1030 <free@plt>
jmp    121f <ygo_replay_fread+0x9f>
mov    $0x5,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.ygo_replay = type { i32 }
@SEEK_END = global i32 0
define i32 @ygo_replay_fread(%struct.ygo_replay* %0, i32* %1) {
%3 = alloca %struct.ygo_replay*
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i64
store %struct.ygo_replay* %0, %struct.ygo_replay** %3
store i32* %1, i32** %4
store i32 0, i32* %5
%9 = load i32*, i32** %4
%10 = load i32, i32* @SEEK_END
%11 = call i64 @fseek(i32* %9, i32 0, i32 %10)
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %2
store i32 3, i32* %5
br label %48
14: ; preds = %2
%15 = load i32*, i32** %4
%16 = call i64 @ftell(i32* %15)
store i64 %16, i64* %6
%17 = load i32*, i32** %4
%18 = call i32 @rewind(i32* %17)
%19 = load i64, i64* %6
%20 = add nsw i64 %19, 1
%21 = mul i64 %20, 1
%22 = call i8* @malloc(i64 %21)
store i8* %22, i8** %7
%23 = load i8*, i8** %7
%24 = icmp ne i8* %23, null
br i1 %24, label %26, label %25
25: ; preds = %14
store i32 5, i32* %5
br label %47
26: ; preds = %14
%27 = load i8*, i8** %7
%28 = load i64, i64* %6
%29 = load i32*, i32** %4
%30 = call i64 @fread(i8* %27, i32 1, i64 %28, i32* %29)
store i64 %30, i64* %8
%31 = load i64, i64* %8
%32 = load i64, i64* %6
%33 = icmp ne i64 %31, %32
br i1 %33, label %34, label %39
34: ; preds = %26
%35 = load i32*, i32** %4
%36 = call i64 @ferror(i32* %35)
%37 = icmp ne i64 %36, 0
br i1 %37, label %38, label %39
38: ; preds = %34
store i32 4, i32* %5
br label %44
39: ; preds = %34, %26
%40 = load %struct.ygo_replay*, %struct.ygo_replay** %3
%41 = load i8*, i8** %7
%42 = load i64, i64* %8
%43 = call i32 @ygo_replay_sread(%struct.ygo_replay* %40, i8* %41, i64 %42)
store i32 %43, i32* %5
br label %44
44: ; preds = %39, %38
%45 = load i8*, i8** %7
%46 = call i32 @free(i8* %45)
br label %47
47: ; preds = %44, %25
br label %48
48: ; preds = %47, %13
%49 = load i32, i32* %5
ret i32 %49
}

LLVM IR (-O2):
%struct.ygo_replay = type { i32 }
@SEEK_END = global i32 0
define i32 @ygo_replay_fread(%struct.ygo_replay* %0, i32* nocapture %1) {
%3 = load i32, i32* @SEEK_END
%4 = tail call i64 @fseek(i32* %1, i32 0, i32 %3)
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %23
6: ; preds = %2
%7 = tail call i64 @ftell(i32* %1)
%8 = tail call i32 @rewind(i32* %1)
%9 = add nsw i64 %7, 1
%10 = tail call i8* @malloc(i64 %9)
%11 = icmp eq i8* %10, null
br i1 %11, label %23, label %12
12: ; preds = %6
%13 = tail call i64 @fread(i8* %10, i32 1, i64 %7, i32* %1)
%14 = icmp eq i64 %13, %7
br i1 %14, label %18, label %15
15: ; preds = %12
%16 = tail call i64 @ferror(i32* %1)
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %15, %12
%19 = tail call i32 @ygo_replay_sread(%struct.ygo_replay* %0, i8* %10, i64 %13)
br label %20
20: ; preds = %15, %18
%21 = phi i32 [ %19, %18 ], [ 4, %15 ]
%22 = tail call i32 @free(i8* %10)
br label %23
23: ; preds = %6, %2, %20
%24 = phi i32 [ %21, %20 ], [ 3, %2 ], [ 5, %6 ]
ret i32 %24
}