#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 */
scalar_t__ EOF;
scalar_t__ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int ygo_replay_fread(struct ygo_replay *, int /*<<< orphan*/ *);
int ygo_replay_read(struct ygo_replay *replay, const char *filename) {
  FILE *stream = fopen(filename, "rb");
  if (!stream)
    return 1;
  int err = ygo_replay_fread(replay, stream);
  if (err != 0) {
    fclose(stream);
    return err;
  }
  if (fclose(stream) == EOF)
    return 2;
  return 0;
}


x86 assembly (clang, -O0):
<ygo_replay_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xeb5(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    116b <ygo_replay_read+0x3b>
movl   $0x1,-0x4(%rbp)
jmp    11c5 <ygo_replay_read+0x95>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1030 <ygo_replay_fread@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     1199 <ygo_replay_read+0x69>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11c5 <ygo_replay_read+0x95>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    0x2e3f(%rip),%rcx
cmp    (%rcx),%rax
jne    11be <ygo_replay_read+0x8e>
movl   $0x2,-0x4(%rbp)
jmp    11c5 <ygo_replay_read+0x95>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ygo_replay_read>:
push   %rbp
push   %rbx
push   %rax
mov    %rsi,%rax
mov    %rdi,%rbp
lea    0xec0(%rip),%rsi
mov    %rax,%rdi
call   1050 <fopen@plt>
test   %rax,%rax
je     117d <ygo_replay_read+0x4d>
mov    %rax,%rbx
mov    %rbp,%rdi
mov    %rax,%rsi
call   1030 <ygo_replay_fread@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1040 <fclose@plt>
test   %ebp,%ebp
jne    1182 <ygo_replay_read+0x52>
mov    0x2e78(%rip),%rcx
xor    %ebp,%ebp
cmp    (%rcx),%rax
sete   %bpl
add    %ebp,%ebp
jmp    1182 <ygo_replay_read+0x52>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.ygo_replay = type { i32 }
@.str = [3 x i8] c"rb\00"
@EOF = global i64 0
define i32 @ygo_replay_read(%struct.ygo_replay* %0, i8* %1) {
%3 = alloca i32
%4 = alloca %struct.ygo_replay*
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i32
store %struct.ygo_replay* %0, %struct.ygo_replay** %4
store i8* %1, i8** %5
%8 = load i8*, i8** %5
%9 = call i32* @fopen(i8* %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %9, i32** %6
%10 = load i32*, i32** %6
%11 = icmp ne i32* %10, null
br i1 %11, label %13, label %12
12: ; preds = %2
store i32 1, i32* %3
br label %30
13: ; preds = %2
%14 = load %struct.ygo_replay*, %struct.ygo_replay** %4
%15 = load i32*, i32** %6
%16 = call i32 @ygo_replay_fread(%struct.ygo_replay* %14, i32* %15)
store i32 %16, i32* %7
%17 = load i32, i32* %7
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %23
19: ; preds = %13
%20 = load i32*, i32** %6
%21 = call i64 @fclose(i32* %20)
%22 = load i32, i32* %7
store i32 %22, i32* %3
br label %30
23: ; preds = %13
%24 = load i32*, i32** %6
%25 = call i64 @fclose(i32* %24)
%26 = load i64, i64* @EOF
%27 = icmp eq i64 %25, %26
br i1 %27, label %28, label %29
28: ; preds = %23
store i32 2, i32* %3
br label %30
29: ; preds = %23
store i32 0, i32* %3
br label %30
30: ; preds = %29, %28, %19, %12
%31 = load i32, i32* %3
ret i32 %31
}

LLVM IR (-O2):
%struct.ygo_replay = type { i32 }
@.str = [3 x i8] c"rb\00"
@EOF = global i64 0
define i32 @ygo_replay_read(%struct.ygo_replay* %0, i8* nocapture %1) {
%3 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %13, label %5
5: ; preds = %2
%6 = tail call i32 @ygo_replay_fread(%struct.ygo_replay* %0, i32* %3)
%7 = icmp eq i32 %6, 0
%8 = tail call i64 @fclose(i32* %3)
br i1 %7, label %9, label %13
9: ; preds = %5
%10 = load i64, i64* @EOF
%11 = icmp eq i64 %8, %10
%12 = select i1 %11, i32 2, i32 0
br label %13
13: ; preds = %5, %9, %2
%14 = phi i32 [ 1, %2 ], [ %12, %9 ], [ %6, %5 ]
ret i32 %14
}