#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 unsigned char uint8_t;
typedef int ssize_t;

/* Variables and functions */
scalar_t__ hazer_checksum_buffer(void const *, int, unsigned char *,
                                 unsigned char *);
int hazer_length(void const *, size_t);
ssize_t hazer_validate(const void *buffer, size_t size) {
  ssize_t result = -1;
  ssize_t length = 0;
  const uint8_t *bp = (uint8_t *)0;
  uint8_t msn = 0;
  uint8_t lsn = 0;
  if ((length = hazer_length(buffer, size)) <= 0) {
  } else if ((bp = (uint8_t *)hazer_checksum_buffer(
                  buffer, length, &msn, &lsn)) == (unsigned char *)0) {
  } else if ((msn != bp[1]) || (lsn != bp[2])) {
  } else {
    result = length;
  }
  return result;
}


x86 assembly (clang, -O0):
<hazer_validate>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xffffffff,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movb   $0x0,-0x21(%rbp)
movb   $0x0,-0x22(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <hazer_length@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jg     116c <hazer_validate+0x4c>
jmp    11d0 <hazer_validate+0xb0>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%esi
lea    -0x21(%rbp),%rdx
lea    -0x22(%rbp),%rcx
call   1040 <hazer_checksum_buffer@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1193 <hazer_validate+0x73>
jmp    11cb <hazer_validate+0xab>
movzbl -0x21(%rbp),%eax
mov    -0x20(%rbp),%rcx
movzbl 0x1(%rcx),%ecx
cmp    %ecx,%eax
jne    11bb <hazer_validate+0x9b>
movzbl -0x22(%rbp),%eax
mov    -0x20(%rbp),%rcx
movzbl 0x2(%rcx),%ecx
cmp    %ecx,%eax
je     11c0 <hazer_validate+0xa0>
jmp    11c6 <hazer_validate+0xa6>
mov    -0x18(%rbp),%eax
mov    %eax,-0x14(%rbp)
jmp    11cb <hazer_validate+0xab>
jmp    11d0 <hazer_validate+0xb0>
mov    -0x14(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hazer_validate>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
movb   $0x0,0xf(%rsp)
movb   $0x0,0xe(%rsp)
call   1030 <hazer_length@plt>
mov    $0xffffffff,%r14d
test   %eax,%eax
jle    1179 <hazer_validate+0x59>
mov    %eax,%ebp
lea    0xf(%rsp),%rdx
lea    0xe(%rsp),%rcx
mov    %rbx,%rdi
mov    %eax,%esi
call   1040 <hazer_checksum_buffer@plt>
test   %rax,%rax
je     1179 <hazer_validate+0x59>
mov    0xf(%rsp),%cl
cmp    0x1(%rax),%cl
jne    1179 <hazer_validate+0x59>
mov    0xe(%rsp),%cl
cmp    0x2(%rax),%cl
mov    $0xffffffff,%r14d
cmove  %ebp,%r14d
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @hazer_validate(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8
%9 = alloca i8
store i8* %0, i8** %3
store i64 %1, i64* %4
store i32 -1, i32* %5
store i32 0, i32* %6
store i8* null, i8** %7
store i8 0, i8* %8
store i8 0, i8* %9
%10 = load i8*, i8** %3
%11 = load i64, i64* %4
%12 = call i32 @hazer_length(i8* %10, i64 %11)
store i32 %12, i32* %6
%13 = icmp sle i32 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %2
br label %43
15: ; preds = %2
%16 = load i8*, i8** %3
%17 = load i32, i32* %6
%18 = call i64 @hazer_checksum_buffer(i8* %16, i32 %17, i8* %8, i8* %9)
%19 = inttoptr i64 %18 to i8*
store i8* %19, i8** %7
%20 = icmp eq i8* %19, null
br i1 %20, label %21, label %22
21: ; preds = %15
br label %42
22: ; preds = %15
%23 = load i8, i8* %8
%24 = zext i8 %23 to i32
%25 = load i8*, i8** %7
%26 = getelementptr inbounds i8, i8* %25, i64 1
%27 = load i8, i8* %26
%28 = zext i8 %27 to i32
%29 = icmp ne i32 %24, %28
br i1 %29, label %38, label %30
30: ; preds = %22
%31 = load i8, i8* %9
%32 = zext i8 %31 to i32
%33 = load i8*, i8** %7
%34 = getelementptr inbounds i8, i8* %33, i64 2
%35 = load i8, i8* %34
%36 = zext i8 %35 to i32
%37 = icmp ne i32 %32, %36
br i1 %37, label %38, label %39
38: ; preds = %30, %22
br label %41
39: ; preds = %30
%40 = load i32, i32* %6
store i32 %40, i32* %5
br label %41
41: ; preds = %39, %38
br label %42
42: ; preds = %41, %21
br label %43
43: ; preds = %42, %14
%44 = load i32, i32* %5
ret i32 %44
}

LLVM IR (-O2):
define i32 @hazer_validate(i8* %0, i64 %1) {
%3 = alloca i8
%4 = alloca i8
store i8 0, i8* %3
store i8 0, i8* %4
%5 = tail call i32 @hazer_length(i8* %0, i64 %1)
%6 = icmp slt i32 %5, 1
br i1 %6, label %22, label %7
7: ; preds = %2
%8 = call i64 @hazer_checksum_buffer(i8* %0, i32 %5, i8* %3, i8* %4)
%9 = inttoptr i64 %8 to i8*
%10 = icmp eq i64 %8, 0
br i1 %10, label %22, label %11
11: ; preds = %7
%12 = load i8, i8* %3
%13 = getelementptr inbounds i8, i8* %9, i64 1
%14 = load i8, i8* %13
%15 = icmp eq i8 %12, %14
br i1 %15, label %16, label %22
16: ; preds = %11
%17 = load i8, i8* %4
%18 = getelementptr inbounds i8, i8* %9, i64 2
%19 = load i8, i8* %18
%20 = icmp eq i8 %17, %19
%21 = select i1 %20, i32 %5, i32 -1
br label %22
22: ; preds = %16, %7, %11, %2
%23 = phi i32 [ -1, %2 ], [ -1, %7 ], [ -1, %11 ], [ %21, %16 ]
ret i32 %23
}