#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 */

/* Variables and functions */
unsigned char check_sum(unsigned char *, int);
int TiCheckHeader(unsigned char *stream, int *img_width, int *img_height,
                  int *img_type, int stream_size) {
  if (stream_size < (22))
    return (8);
  if (check_sum(stream, (22) - 1) != stream[21])
    return (8);
  (*img_width = (int)((stream[2 + 0] << 8) | (stream[2 + 1] << 0)));
  (*img_height = (int)((stream[4 + 0] << 8) | (stream[4 + 1] << 0)));
  (*img_type = (int)(stream[7 + 0]));
  return (0);
}


x86 assembly (clang, -O0):
<TiCheckHeader>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8d,-0x2c(%rbp)
cmpl   $0x16,-0x2c(%rbp)
jge    1142 <TiCheckHeader+0x32>
movl   $0x8,-0x4(%rbp)
jmp    11c0 <TiCheckHeader+0xb0>
mov    -0x10(%rbp),%rdi
mov    $0x15,%esi
call   1030 <check_sum@plt>
movzbl %al,%eax
mov    -0x10(%rbp),%rcx
movzbl 0x15(%rcx),%ecx
cmp    %ecx,%eax
je     116f <TiCheckHeader+0x5f>
movl   $0x8,-0x4(%rbp)
jmp    11c0 <TiCheckHeader+0xb0>
mov    -0x10(%rbp),%rax
movzbl 0x2(%rax),%ecx
shl    $0x8,%ecx
mov    -0x10(%rbp),%rax
movzbl 0x3(%rax),%eax
shl    $0x0,%eax
or     %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
movzbl 0x4(%rax),%ecx
shl    $0x8,%ecx
mov    -0x10(%rbp),%rax
movzbl 0x5(%rax),%eax
shl    $0x0,%eax
or     %eax,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
movzbl 0x7(%rax),%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<TiCheckHeader>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    $0x8,%ebp
cmp    $0x16,%r8d
jl     1160 <TiCheckHeader+0x50>
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rsi,%r12
mov    %rdi,%rbx
mov    $0x15,%esi
call   1030 <check_sum@plt>
cmp    0x15(%rbx),%al
jne    1160 <TiCheckHeader+0x50>
movzwl 0x2(%rbx),%eax
shl    $0x10,%eax
bswap  %eax
mov    %eax,(%r12)
movzwl 0x4(%rbx),%eax
shl    $0x10,%eax
bswap  %eax
mov    %eax,(%r15)
movzbl 0x7(%rbx),%eax
mov    %eax,(%r14)
xor    %ebp,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @TiCheckHeader(i8* %0, i32* %1, i32* %2, i32* %3, i32 %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca i32*
%11 = alloca i32
store i8* %0, i8** %7
store i32* %1, i32** %8
store i32* %2, i32** %9
store i32* %3, i32** %10
store i32 %4, i32* %11
%12 = load i32, i32* %11
%13 = icmp slt i32 %12, 22
br i1 %13, label %14, label %15
14: ; preds = %5
store i32 8, i32* %6
br label %55
15: ; preds = %5
%16 = load i8*, i8** %7
%17 = call zeroext i8 @check_sum(i8* %16, i32 21)
%18 = zext i8 %17 to i32
%19 = load i8*, i8** %7
%20 = getelementptr inbounds i8, i8* %19, i64 21
%21 = load i8, i8* %20
%22 = zext i8 %21 to i32
%23 = icmp ne i32 %18, %22
br i1 %23, label %24, label %25
24: ; preds = %15
store i32 8, i32* %6
br label %55
25: ; preds = %15
%26 = load i8*, i8** %7
%27 = getelementptr inbounds i8, i8* %26, i64 2
%28 = load i8, i8* %27
%29 = zext i8 %28 to i32
%30 = shl i32 %29, 8
%31 = load i8*, i8** %7
%32 = getelementptr inbounds i8, i8* %31, i64 3
%33 = load i8, i8* %32
%34 = zext i8 %33 to i32
%35 = shl i32 %34, 0
%36 = or i32 %30, %35
%37 = load i32*, i32** %8
store i32 %36, i32* %37
%38 = load i8*, i8** %7
%39 = getelementptr inbounds i8, i8* %38, i64 4
%40 = load i8, i8* %39
%41 = zext i8 %40 to i32
%42 = shl i32 %41, 8
%43 = load i8*, i8** %7
%44 = getelementptr inbounds i8, i8* %43, i64 5
%45 = load i8, i8* %44
%46 = zext i8 %45 to i32
%47 = shl i32 %46, 0
%48 = or i32 %42, %47
%49 = load i32*, i32** %9
store i32 %48, i32* %49
%50 = load i8*, i8** %7
%51 = getelementptr inbounds i8, i8* %50, i64 7
%52 = load i8, i8* %51
%53 = zext i8 %52 to i32
%54 = load i32*, i32** %10
store i32 %53, i32* %54
store i32 0, i32* %6
br label %55
55: ; preds = %25, %24, %14
%56 = load i32, i32* %6
ret i32 %56
}

LLVM IR (-O2):
define i32 @TiCheckHeader(i8* %0, i32* nocapture writeonly %1, i32* nocapture writeonly %2, i32* nocapture writeonly %3, i32 %4) {
%6 = icmp slt i32 %4, 22
br i1 %6, label %32, label %7
7: ; preds = %5
%8 = tail call zeroext i8 @check_sum(i8* %0, i32 21)
%9 = getelementptr inbounds i8, i8* %0, i64 21
%10 = load i8, i8* %9
%11 = icmp eq i8 %8, %10
br i1 %11, label %12, label %32
12: ; preds = %7
%13 = getelementptr inbounds i8, i8* %0, i64 2
%14 = load i8, i8* %13
%15 = zext i8 %14 to i32
%16 = shl nuw nsw i32 %15, 8
%17 = getelementptr inbounds i8, i8* %0, i64 3
%18 = load i8, i8* %17
%19 = zext i8 %18 to i32
%20 = or i32 %16, %19
store i32 %20, i32* %1
%21 = getelementptr inbounds i8, i8* %0, i64 4
%22 = load i8, i8* %21
%23 = zext i8 %22 to i32
%24 = shl nuw nsw i32 %23, 8
%25 = getelementptr inbounds i8, i8* %0, i64 5
%26 = load i8, i8* %25
%27 = zext i8 %26 to i32
%28 = or i32 %24, %27
store i32 %28, i32* %2
%29 = getelementptr inbounds i8, i8* %0, i64 7
%30 = load i8, i8* %29
%31 = zext i8 %30 to i32
store i32 %31, i32* %3
br label %32
32: ; preds = %7, %5, %12
%33 = phi i32 [ 0, %12 ], [ 8, %5 ], [ 8, %7 ]
ret i32 %33
}