#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 int create_checksum(unsigned char *, int);
int frame_DeStuffing(unsigned char *, int);
int destuff_and_check_checksum(unsigned char *frame, int max_size) {
  unsigned int checksum, checksum_calc;
  int size;
  size = frame_DeStuffing(frame, max_size);
  checksum_calc = create_checksum(frame, size - (sizeof(unsigned short)));
  checksum =
      (((unsigned short)frame[size - ((sizeof(unsigned short)) - 1)]) << 8) |
      ((unsigned short)frame[size - (sizeof(unsigned short))]);
  if (checksum != checksum_calc)
    return (-1);
  else
    return (size);
}


x86 assembly (clang, -O0):
<destuff_and_check_checksum>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1040 <frame_DeStuffing@plt>
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
movslq -0x20(%rbp),%rax
sub    $0x2,%rax
mov    %eax,%esi
call   1030 <create_checksum@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x20(%rbp),%rcx
sub    $0x1,%rcx
movzbl (%rax,%rcx,1),%eax
movzwl %ax,%eax
shl    $0x8,%eax
mov    -0x10(%rbp),%rcx
movslq -0x20(%rbp),%rdx
sub    $0x2,%rdx
movzbl (%rcx,%rdx,1),%ecx
movzwl %cx,%ecx
or     %ecx,%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x1c(%rbp),%eax
je     119a <destuff_and_check_checksum+0x7a>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a0 <destuff_and_check_checksum+0x80>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<destuff_and_check_checksum>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
call   1040 <frame_DeStuffing@plt>
movslq %eax,%rbx
lea    -0x2(%rbx),%rsi
mov    %r14,%rdi
call   1030 <create_checksum@plt>
movzwl -0x2(%r14,%rbx,1),%ecx
cmp    %eax,%ecx
mov    $0xffffffff,%eax
cmovne %eax,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
define i32 @destuff_and_check_checksum(i8* %0, i32 %1) {
%3 = tail call i32 @frame_DeStuffing(i8* %0, i32 %1)
%4 = sext i32 %3 to i64
%5 = add nsw i64 %4, -2
%6 = trunc i64 %5 to i32
%7 = tail call i32 @create_checksum(i8* %0, i32 %6)
%8 = add nsw i64 %4, -1
%9 = getelementptr inbounds i8, i8* %0, i64 %8
%10 = load i8, i8* %9
%11 = zext i8 %10 to i32
%12 = shl nuw nsw i32 %11, 8
%13 = getelementptr inbounds i8, i8* %0, i64 %5
%14 = load i8, i8* %13
%15 = zext i8 %14 to i32
%16 = or i32 %12, %15
%17 = icmp eq i32 %16, %7
%18 = select i1 %17, i32 %3, i32 -1
ret i32 %18
}