#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 short create_checksum(unsigned char *, int);
int frame_stuffing(unsigned char *, unsigned char *, int, int);
int /*<<< orphan*/ free(unsigned char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ memcpy(unsigned char *, void *, int);
int add_checksum_and_stuff(unsigned char *frame, int size, int max_size) {
  unsigned short int checksum;
  unsigned char *ptrtochar;
  checksum = create_checksum(frame, size);
  frame[size] = (unsigned char)(checksum & 0x0FF);
  frame[size + 1] = (unsigned char)(checksum >> 8);
  size += (sizeof(unsigned short));
  ptrtochar = (unsigned char *)malloc(max_size);
  if (!ptrtochar) {
    return (-3);
  }
  memcpy(ptrtochar, (void *)frame, size);
  size = frame_stuffing(ptrtochar, frame, size, max_size);
  free(ptrtochar);
  return (size);
}


x86 assembly (clang, -O0):
<add_checksum_and_stuff>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1040 <create_checksum@plt>
mov    %ax,-0x1a(%rbp)
movzwl -0x1a(%rbp),%eax
and    $0xff,%eax
mov    %al,%dl
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
movzwl -0x1a(%rbp),%eax
sar    $0x8,%eax
mov    %al,%dl
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
movslq -0x14(%rbp),%rax
add    $0x2,%rax
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%edi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11cf <add_checksum_and_stuff+0x7f>
movl   $0xfffffffd,-0x4(%rbp)
jmp    1204 <add_checksum_and_stuff+0xb4>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1050 <memcpy@plt>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
mov    -0x18(%rbp),%ecx
call   1060 <frame_stuffing@plt>
mov    %eax,-0x14(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<add_checksum_and_stuff>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %esi,%ebp
mov    %rdi,%r15
call   1040 <create_checksum@plt>
movslq %ebp,%rcx
mov    %ax,(%r15,%rcx,1)
mov    %r14d,%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     11aa <add_checksum_and_stuff+0x5a>
mov    %rax,%rbx
add    $0x2,%ebp
mov    %rax,%rdi
mov    %r15,%rsi
mov    %ebp,%edx
call   1050 <memcpy@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %ebp,%edx
mov    %r14d,%ecx
call   1060 <frame_stuffing@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %ebp,%eax
jmp    11af <add_checksum_and_stuff+0x5f>
mov    $0xfffffffd,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @add_checksum_and_stuff(i8* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i16
%9 = alloca i8*
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%10 = load i8*, i8** %5
%11 = load i32, i32* %6
%12 = call zeroext i16 @create_checksum(i8* %10, i32 %11)
store i16 %12, i16* %8
%13 = load i16, i16* %8
%14 = zext i16 %13 to i32
%15 = and i32 %14, 255
%16 = trunc i32 %15 to i8
%17 = load i8*, i8** %5
%18 = load i32, i32* %6
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %17, i64 %19
store i8 %16, i8* %20
%21 = load i16, i16* %8
%22 = zext i16 %21 to i32
%23 = ashr i32 %22, 8
%24 = trunc i32 %23 to i8
%25 = load i8*, i8** %5
%26 = load i32, i32* %6
%27 = add nsw i32 %26, 1
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %25, i64 %28
store i8 %24, i8* %29
%30 = load i32, i32* %6
%31 = sext i32 %30 to i64
%32 = add i64 %31, 2
%33 = trunc i64 %32 to i32
store i32 %33, i32* %6
%34 = load i32, i32* %7
%35 = call i64 @malloc(i32 %34)
%36 = inttoptr i64 %35 to i8*
store i8* %36, i8** %9
%37 = load i8*, i8** %9
%38 = icmp ne i8* %37, null
br i1 %38, label %40, label %39
39: ; preds = %3
store i32 -3, i32* %4
br label %53
40: ; preds = %3
%41 = load i8*, i8** %9
%42 = load i8*, i8** %5
%43 = load i32, i32* %6
%44 = call i32 @memcpy(i8* %41, i8* %42, i32 %43)
%45 = load i8*, i8** %9
%46 = load i8*, i8** %5
%47 = load i32, i32* %6
%48 = load i32, i32* %7
%49 = call i32 @frame_stuffing(i8* %45, i8* %46, i32 %47, i32 %48)
store i32 %49, i32* %6
%50 = load i8*, i8** %9
%51 = call i32 @free(i8* %50)
%52 = load i32, i32* %6
store i32 %52, i32* %4
br label %53
53: ; preds = %40, %39
%54 = load i32, i32* %4
ret i32 %54
}

LLVM IR (-O2):
define i32 @add_checksum_and_stuff(i8* %0, i32 %1, i32 %2) {
%4 = tail call zeroext i16 @create_checksum(i8* %0, i32 %1)
%5 = trunc i16 %4 to i8
%6 = sext i32 %1 to i64
%7 = getelementptr inbounds i8, i8* %0, i64 %6
store i8 %5, i8* %7
%8 = lshr i16 %4, 8
%9 = trunc i16 %8 to i8
%10 = add nsw i32 %1, 1
%11 = sext i32 %10 to i64
%12 = getelementptr inbounds i8, i8* %0, i64 %11
store i8 %9, i8* %12
%13 = add i32 %1, 2
%14 = tail call i64 @malloc(i32 %2)
%15 = inttoptr i64 %14 to i8*
%16 = icmp eq i64 %14, 0
br i1 %16, label %21, label %17
17: ; preds = %3
%18 = tail call i32 @memcpy(i8* %15, i8* %0, i32 %13)
%19 = tail call i32 @frame_stuffing(i8* %15, i8* %0, i32 %13, i32 %2)
%20 = tail call i32 @free(i8* %15)
br label %21
21: ; preds = %3, %17
%22 = phi i32 [ %19, %17 ], [ -3, %3 ]
ret i32 %22
}