#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*/ fprintf(int /*<<< orphan*/, char *, short);
void *fread(short *, int, short, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int fread_a_frame(FILE *file, short *frame) {
  short read_short = 0;
  short read_len = 0;
  int ret = 0;
  short *tmp = frame;
  ret = fread(&read_short, 2, 1, file);
  if (ret < 1)
    return -1;
  if (read_short == 0x6b21) {
    *tmp++ = read_short;
    ret = fread(&read_short, 2, 1, file);
    if (ret < 1)
      return -1;
    *tmp++ = read_short;
    if (read_short == 0) {
      return 0;
    } else {
      read_len = fread(tmp, 2, read_short, file);
      if (read_len != read_short)
        return -1;
      else
        return read_short;
    }
  } else {
    fprintf(stderr, "wrong header: 0x%hx\n", read_short);
    return -2;
  }
  return -1;
}


x86 assembly (clang, -O0):
<fread_a_frame>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movw   $0x0,-0x1a(%rbp)
movw   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rcx
lea    -0x1a(%rbp),%rdi
mov    $0x2,%esi
mov    $0x1,%edx
call   1030 <fread@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x1,-0x20(%rbp)
jge    117b <fread_a_frame+0x5b>
movl   $0xffffffff,-0x4(%rbp)
jmp    1264 <fread_a_frame+0x144>
movswl -0x1a(%rbp),%eax
cmp    $0x6b21,%eax
jne    1244 <fread_a_frame+0x124>
mov    -0x1a(%rbp),%cx
mov    -0x28(%rbp),%rax
mov    %rax,%rdx
add    $0x2,%rdx
mov    %rdx,-0x28(%rbp)
mov    %cx,(%rax)
mov    -0x10(%rbp),%rcx
lea    -0x1a(%rbp),%rdi
mov    $0x2,%esi
mov    $0x1,%edx
call   1030 <fread@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x1,-0x20(%rbp)
jge    11d0 <fread_a_frame+0xb0>
movl   $0xffffffff,-0x4(%rbp)
jmp    1264 <fread_a_frame+0x144>
mov    -0x1a(%rbp),%cx
mov    -0x28(%rbp),%rax
mov    %rax,%rdx
add    $0x2,%rdx
mov    %rdx,-0x28(%rbp)
mov    %cx,(%rax)
movswl -0x1a(%rbp),%eax
cmp    $0x0,%eax
jne    11ff <fread_a_frame+0xdf>
movl   $0x0,-0x4(%rbp)
jmp    1264 <fread_a_frame+0x144>
mov    -0x28(%rbp),%rdi
mov    -0x1a(%rbp),%ax
mov    -0x10(%rbp),%rcx
mov    $0x2,%esi
movswl %ax,%edx
call   1030 <fread@plt>
mov    %ax,-0x1c(%rbp)
movswl -0x1c(%rbp),%eax
movswl -0x1a(%rbp),%ecx
cmp    %ecx,%eax
je     1238 <fread_a_frame+0x118>
movl   $0xffffffff,-0x4(%rbp)
jmp    1264 <fread_a_frame+0x144>
movswl -0x1a(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1264 <fread_a_frame+0x144>
mov    0x2d95(%rip),%rax
mov    (%rax),%edi
lea    0xdac(%rip),%rsi
movswl -0x1a(%rbp),%edx
call   1040 <fprintf@plt>
movl   $0xfffffffe,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fread_a_frame>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
movw   $0x0,0x6(%rsp)
lea    0x6(%rsp),%rdi
mov    $0x2,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
mov    $0xffffffff,%ebp
test   %eax,%eax
jle    11dc <fread_a_frame+0xbc>
movzwl 0x6(%rsp),%eax
cmp    $0x6b21,%eax
jne    11bb <fread_a_frame+0x9b>
movw   $0x6b21,(%r14)
lea    0x6(%rsp),%rdi
mov    $0x2,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
test   %eax,%eax
jle    11dc <fread_a_frame+0xbc>
movswl 0x6(%rsp),%r15d
mov    %r15w,0x2(%r14)
test   %r15d,%r15d
je     11da <fread_a_frame+0xba>
add    $0x4,%r14
mov    %r14,%rdi
mov    $0x2,%esi
mov    %r15d,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
cwtl
cmp    %r15d,%eax
mov    $0xffffffff,%ebp
cmove  %r15d,%ebp
jmp    11dc <fread_a_frame+0xbc>
mov    0x2e1e(%rip),%rcx
mov    (%rcx),%edi
movswl %ax,%edx
lea    0xe32(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0xfffffffe,%ebp
jmp    11dc <fread_a_frame+0xbc>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [21 x i8] c"wrong header: 0x%hx\0A\00"
define i32 @fread_a_frame(i32* %0, i16* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i16*
%6 = alloca i16
%7 = alloca i16
%8 = alloca i32
%9 = alloca i16*
store i32* %0, i32** %4
store i16* %1, i16** %5
store i16 0, i16* %6
store i16 0, i16* %7
store i32 0, i32* %8
%10 = load i16*, i16** %5
store i16* %10, i16** %9
%11 = load i32*, i32** %4
%12 = call i8* @fread(i16* %6, i32 2, i16 signext 1, i32* %11)
%13 = ptrtoint i8* %12 to i32
store i32 %13, i32* %8
%14 = load i32, i32* %8
%15 = icmp slt i32 %14, 1
br i1 %15, label %16, label %17
16: ; preds = %2
store i32 -1, i32* %3
br label %58
17: ; preds = %2
%18 = load i16, i16* %6
%19 = sext i16 %18 to i32
%20 = icmp eq i32 %19, 27425
br i1 %20, label %21, label %54
21: ; preds = %17
%22 = load i16, i16* %6
%23 = load i16*, i16** %9
%24 = getelementptr inbounds i16, i16* %23, i32 1
store i16* %24, i16** %9
store i16 %22, i16* %23
%25 = load i32*, i32** %4
%26 = call i8* @fread(i16* %6, i32 2, i16 signext 1, i32* %25)
%27 = ptrtoint i8* %26 to i32
store i32 %27, i32* %8
%28 = load i32, i32* %8
%29 = icmp slt i32 %28, 1
br i1 %29, label %30, label %31
30: ; preds = %21
store i32 -1, i32* %3
br label %58
31: ; preds = %21
%32 = load i16, i16* %6
%33 = load i16*, i16** %9
%34 = getelementptr inbounds i16, i16* %33, i32 1
store i16* %34, i16** %9
store i16 %32, i16* %33
%35 = load i16, i16* %6
%36 = sext i16 %35 to i32
%37 = icmp eq i32 %36, 0
br i1 %37, label %38, label %39
38: ; preds = %31
store i32 0, i32* %3
br label %58
39: ; preds = %31
%40 = load i16*, i16** %9
%41 = load i16, i16* %6
%42 = load i32*, i32** %4
%43 = call i8* @fread(i16* %40, i32 2, i16 signext %41, i32* %42)
%44 = ptrtoint i8* %43 to i16
store i16 %44, i16* %7
%45 = load i16, i16* %7
%46 = sext i16 %45 to i32
%47 = load i16, i16* %6
%48 = sext i16 %47 to i32
%49 = icmp ne i32 %46, %48
br i1 %49, label %50, label %51
50: ; preds = %39
store i32 -1, i32* %3
br label %58
51: ; preds = %39
%52 = load i16, i16* %6
%53 = sext i16 %52 to i32
store i32 %53, i32* %3
br label %58
54: ; preds = %17
%55 = load i32, i32* @stderr
%56 = load i16, i16* %6
%57 = call i32 @fprintf(i32 %55, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i16 signext %56)
store i32 -2, i32* %3
br label %58
58: ; preds = %54, %51, %50, %38, %30, %16
%59 = load i32, i32* %3
ret i32 %59
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [21 x i8] c"wrong header: 0x%hx\0A\00"
define i32 @fread_a_frame(i32* nocapture %0, i16* nocapture %1) {
%3 = alloca i16
%4 = bitcast i16* %3 to i8*
store i16 0, i16* %3
%5 = call i8* @fread(i16* %3, i32 2, i16 signext 1, i32* %0)
%6 = ptrtoint i8* %5 to i64
%7 = trunc i64 %6 to i32
%8 = icmp slt i32 %7, 1
br i1 %8, label %34, label %9
9: ; preds = %2
%10 = load i16, i16* %3
%11 = icmp eq i16 %10, 27425
br i1 %11, label %12, label %31
12: ; preds = %9
store i16 27425, i16* %1
%13 = call i8* @fread(i16* %3, i32 2, i16 signext 1, i32* %0)
%14 = ptrtoint i8* %13 to i64
%15 = trunc i64 %14 to i32
%16 = icmp slt i32 %15, 1
br i1 %16, label %34, label %17
17: ; preds = %12
%18 = getelementptr inbounds i16, i16* %1, i64 1
%19 = load i16, i16* %3
store i16 %19, i16* %18
%20 = icmp eq i16 %19, 0
br i1 %20, label %34, label %21
21: ; preds = %17
%22 = getelementptr inbounds i16, i16* %1, i64 2
%23 = tail call i8* @fread(i16* %22, i32 2, i16 signext %19, i32* %0)
%24 = ptrtoint i8* %23 to i64
%25 = trunc i64 %24 to i32
%26 = shl i32 %25, 16
%27 = ashr exact i32 %26, 16
%28 = sext i16 %19 to i32
%29 = icmp eq i32 %27, %28
%30 = select i1 %29, i32 %28, i32 -1
br label %34
31: ; preds = %9
%32 = load i32, i32* @stderr
%33 = tail call i32 @fprintf(i32 %32, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i16 signext %10)
br label %34
34: ; preds = %21, %17, %12, %2, %31
%35 = phi i32 [ -2, %31 ], [ -1, %2 ], [ -1, %12 ], [ 0, %17 ], [ %30, %21 ]
ret i32 %35
}