#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 *, unsigned long);
long psi_get_uint16_be(unsigned char *, int);
scalar_t__ psi_read_crc(int /*<<< orphan*/ *, unsigned char *, int,
                        unsigned long *);
scalar_t__ psi_skip_chunk(int /*<<< orphan*/ *, unsigned long, unsigned long);
int /*<<< orphan*/ stderr;
int psi_load_header(FILE *fp, unsigned long size, unsigned long crc,
                    unsigned *enc) {
  unsigned long vers;
  unsigned char buf[8];
  if (size < 4) {
    return (1);
  }
  if (psi_read_crc(fp, buf, 4, &crc)) {
    return (1);
  }
  vers = psi_get_uint16_be(buf, 0);
  if (vers != 0x0000) {
    fprintf(stderr, "psi: unknown version (%lu)\n", vers);
    return (1);
  }
  switch (psi_get_uint16_be(buf, 2)) {
  case 0x0100:
    *enc = 0x0001;
    break;
  case 0x0101:
    *enc = 0x8001;
    break;
  case 0x0200:
    *enc = 0x0002;
    break;
  case 0x0201:
    *enc = 0x8002;
    break;
  case 0x0202:
    *enc = 0x4002;
    break;
  case 0x0300:
    *enc = 0x0003;
    break;
  default:
    *enc = 0x0000;
    break;
  }
  if (psi_skip_chunk(fp, size - 4, crc)) {
    return (1);
  }
  return (0);
}


x86 assembly (clang, -O0):
<psi_load_header>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
cmpq   $0x4,-0x18(%rbp)
jae    116f <psi_load_header+0x2f>
movl   $0x1,-0x4(%rbp)
jmp    12ff <psi_load_header+0x1bf>
mov    -0x10(%rbp),%rdi
lea    -0x38(%rbp),%rsi
mov    $0x4,%edx
lea    -0x20(%rbp),%rcx
call   1050 <psi_read_crc@plt>
cmp    $0x0,%rax
je     119b <psi_load_header+0x5b>
movl   $0x1,-0x4(%rbp)
jmp    12ff <psi_load_header+0x1bf>
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
call   1060 <psi_get_uint16_be@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     11da <psi_load_header+0x9a>
mov    0x2e24(%rip),%rax
mov    (%rax),%edi
mov    -0x30(%rbp),%rdx
lea    0xe37(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12ff <psi_load_header+0x1bf>
lea    -0x38(%rbp),%rdi
mov    $0x2,%esi
call   1060 <psi_get_uint16_be@plt>
mov    %rax,%rcx
mov    %rcx,-0x40(%rbp)
sub    $0x100,%rax
je     1269 <psi_load_header+0x129>
jmp    1200 <psi_load_header+0xc0>
mov    -0x40(%rbp),%rax
sub    $0x101,%rax
je     1278 <psi_load_header+0x138>
jmp    1215 <psi_load_header+0xd5>
mov    -0x40(%rbp),%rax
sub    $0x200,%rax
je     1287 <psi_load_header+0x147>
jmp    122a <psi_load_header+0xea>
mov    -0x40(%rbp),%rax
sub    $0x201,%rax
je     1296 <psi_load_header+0x156>
jmp    123f <psi_load_header+0xff>
mov    -0x40(%rbp),%rax
sub    $0x202,%rax
je     12a5 <psi_load_header+0x165>
jmp    1254 <psi_load_header+0x114>
mov    -0x40(%rbp),%rax
sub    $0x300,%rax
je     12b4 <psi_load_header+0x174>
jmp    12c3 <psi_load_header+0x183>
mov    -0x28(%rbp),%rax
movl   $0x1,(%rax)
jmp    12cd <psi_load_header+0x18d>
mov    -0x28(%rbp),%rax
movl   $0x8001,(%rax)
jmp    12cd <psi_load_header+0x18d>
mov    -0x28(%rbp),%rax
movl   $0x2,(%rax)
jmp    12cd <psi_load_header+0x18d>
mov    -0x28(%rbp),%rax
movl   $0x8002,(%rax)
jmp    12cd <psi_load_header+0x18d>
mov    -0x28(%rbp),%rax
movl   $0x4002,(%rax)
jmp    12cd <psi_load_header+0x18d>
mov    -0x28(%rbp),%rax
movl   $0x3,(%rax)
jmp    12cd <psi_load_header+0x18d>
mov    -0x28(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
sub    $0x4,%rsi
mov    -0x20(%rbp),%rdx
call   1030 <psi_skip_chunk@plt>
cmp    $0x0,%rax
je     12f8 <psi_load_header+0x1b8>
movl   $0x1,-0x4(%rbp)
jmp    12ff <psi_load_header+0x1bf>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<psi_load_header>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdx,0x8(%rsp)
mov    $0x1,%ebp
cmp    $0x4,%rsi
jb     1245 <psi_load_header+0x105>
mov    %rcx,%r15
mov    %rsi,%rbx
mov    %rdi,%r14
lea    0x10(%rsp),%rsi
lea    0x8(%rsp),%rcx
mov    $0x4,%edx
call   1050 <psi_read_crc@plt>
test   %rax,%rax
jne    1245 <psi_load_header+0x105>
lea    0x10(%rsp),%rdi
xor    %esi,%esi
call   1060 <psi_get_uint16_be@plt>
test   %rax,%rax
je     11b2 <psi_load_header+0x72>
mov    0x2e44(%rip),%rcx
mov    (%rcx),%edi
lea    0xe5b(%rip),%rsi
mov    %rax,%rdx
call   1040 <fprintf@plt>
jmp    1245 <psi_load_header+0x105>
lea    0x10(%rsp),%rdi
mov    $0x2,%esi
call   1060 <psi_get_uint16_be@plt>
cmp    $0x200,%rax
jg     11e8 <psi_load_header+0xa8>
cmp    $0x100,%rax
je     1207 <psi_load_header+0xc7>
cmp    $0x101,%rax
je     120e <psi_load_header+0xce>
cmp    $0x200,%rax
jne    1223 <psi_load_header+0xe3>
mov    $0x2,%eax
jmp    1225 <psi_load_header+0xe5>
cmp    $0x201,%rax
je     1215 <psi_load_header+0xd5>
cmp    $0x202,%rax
je     121c <psi_load_header+0xdc>
cmp    $0x300,%rax
jne    1223 <psi_load_header+0xe3>
mov    $0x3,%eax
jmp    1225 <psi_load_header+0xe5>
mov    $0x1,%eax
jmp    1225 <psi_load_header+0xe5>
mov    $0x8001,%eax
jmp    1225 <psi_load_header+0xe5>
mov    $0x8002,%eax
jmp    1225 <psi_load_header+0xe5>
mov    $0x4002,%eax
jmp    1225 <psi_load_header+0xe5>
xor    %eax,%eax
mov    %eax,(%r15)
add    $0xfffffffffffffffc,%rbx
mov    0x8(%rsp),%rdx
mov    %r14,%rdi
mov    %rbx,%rsi
call   1030 <psi_skip_chunk@plt>
xor    %ebp,%ebp
test   %rax,%rax
setne  %bpl
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [28 x i8] c"psi: unknown version (%lu)\0A\00"
define i32 @psi_load_header(i32* %0, i64 %1, i64 %2, i32* %3) {
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i32*
%10 = alloca i64
%11 = alloca [8 x i8]
store i32* %0, i32** %6
store i64 %1, i64* %7
store i64 %2, i64* %8
store i32* %3, i32** %9
%12 = load i64, i64* %7
%13 = icmp ult i64 %12, 4
br i1 %13, label %14, label %15
14: ; preds = %4
store i32 1, i32* %5
br label %56
15: ; preds = %4
%16 = load i32*, i32** %6
%17 = getelementptr inbounds [8 x i8], [8 x i8]* %11, i64 0, i64 0
%18 = call i64 @psi_read_crc(i32* %16, i8* %17, i32 4, i64* %8)
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %15
store i32 1, i32* %5
br label %56
21: ; preds = %15
%22 = getelementptr inbounds [8 x i8], [8 x i8]* %11, i64 0, i64 0
%23 = call i64 @psi_get_uint16_be(i8* %22, i32 0)
store i64 %23, i64* %10
%24 = load i64, i64* %10
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %30
26: ; preds = %21
%27 = load i32, i32* @stderr
%28 = load i64, i64* %10
%29 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i64 %28)
store i32 1, i32* %5
br label %56
30: ; preds = %21
%31 = getelementptr inbounds [8 x i8], [8 x i8]* %11, i64 0, i64 0
%32 = call i64 @psi_get_uint16_be(i8* %31, i32 2)
switch i64 %32, label %45 [
i64 256, label %33
i64 257, label %35
i64 512, label %37
i64 513, label %39
i64 514, label %41
i64 768, label %43
]
33: ; preds = %30
%34 = load i32*, i32** %9
store i32 1, i32* %34
br label %47
35: ; preds = %30
%36 = load i32*, i32** %9
store i32 32769, i32* %36
br label %47
37: ; preds = %30
%38 = load i32*, i32** %9
store i32 2, i32* %38
br label %47
39: ; preds = %30
%40 = load i32*, i32** %9
store i32 32770, i32* %40
br label %47
41: ; preds = %30
%42 = load i32*, i32** %9
store i32 16386, i32* %42
br label %47
43: ; preds = %30
%44 = load i32*, i32** %9
store i32 3, i32* %44
br label %47
45: ; preds = %30
%46 = load i32*, i32** %9
store i32 0, i32* %46
br label %47
47: ; preds = %45, %43, %41, %39, %37, %35, %33
%48 = load i32*, i32** %6
%49 = load i64, i64* %7
%50 = sub i64 %49, 4
%51 = load i64, i64* %8
%52 = call i64 @psi_skip_chunk(i32* %48, i64 %50, i64 %51)
%53 = icmp ne i64 %52, 0
br i1 %53, label %54, label %55
54: ; preds = %47
store i32 1, i32* %5
br label %56
55: ; preds = %47
store i32 0, i32* %5
br label %56
56: ; preds = %55, %54, %26, %20, %14
%57 = load i32, i32* %5
ret i32 %57
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [28 x i8] c"psi: unknown version (%lu)\0A\00"
define i32 @psi_load_header(i32* %0, i64 %1, i64 %2, i32* nocapture writeonly %3) {
%5 = alloca i64
%6 = alloca [8 x i8]
store i64 %2, i64* %5
%7 = getelementptr inbounds [8 x i8], [8 x i8]* %6, i64 0, i64 0
%8 = icmp ult i64 %1, 4
br i1 %8, label %33, label %9
9: ; preds = %4
%10 = call i64 @psi_read_crc(i32* %0, i8* %7, i32 4, i64* %5)
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %33
12: ; preds = %9
%13 = call i64 @psi_get_uint16_be(i8* %7, i32 0)
%14 = icmp eq i64 %13, 0
br i1 %14, label %18, label %15
15: ; preds = %12
%16 = load i32, i32* @stderr
%17 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i64 %13)
br label %33
18: ; preds = %12
%19 = call i64 @psi_get_uint16_be(i8* %7, i32 2)
switch i64 %19, label %25 [
i64 256, label %26
i64 257, label %20
i64 512, label %21
i64 513, label %22
i64 514, label %23
i64 768, label %24
]
20: ; preds = %18
br label %26
21: ; preds = %18
br label %26
22: ; preds = %18
br label %26
23: ; preds = %18
br label %26
24: ; preds = %18
br label %26
25: ; preds = %18
br label %26
26: ; preds = %18, %25, %24, %23, %22, %21, %20
%27 = phi i32 [ 0, %25 ], [ 3, %24 ], [ 16386, %23 ], [ 32770, %22 ], [ 2, %21 ], [ 32769, %20 ], [ 1, %18 ]
store i32 %27, i32* %3
%28 = add i64 %1, -4
%29 = load i64, i64* %5
%30 = call i64 @psi_skip_chunk(i32* %0, i64 %28, i64 %29)
%31 = icmp ne i64 %30, 0
%32 = zext i1 %31 to i32
br label %33
33: ; preds = %26, %9, %4, %15
%34 = phi i32 [ 1, %15 ], [ 1, %4 ], [ 1, %9 ], [ %32, %26 ]
ret i32 %34
}