#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 char uint8_t;

/* Variables and functions */
int calc_sum(char *, int);
int check_fw(char *, int);
int /*<<< orphan*/ memmove(char *, char *, int);
int /*<<< orphan*/ printf(char *);
char *realloc(char *, int);
int /*<<< orphan*/ xor_cnt(char *, int, int);
bool encode_buffer(uint8_t **buf, int *len) {
  if (check_fw(*buf, *len) != 1) {
    printf("Your MCU FW image does not looks like raw FW!\n");
    goto EXCEPT;
  };
  *buf = realloc(*buf, *len + 4);
  if (*buf == NULL) {
    printf("Error reallocating buffer!\n");
    goto EXCEPT;
  };
  memmove(&(*buf)[4], *buf, *len);
  xor_cnt(&(*buf)[4], *len, 4);
  (*buf)[0] = 'm';
  (*buf)[1] = 't';
  (*buf)[2] = 'c';
  (*buf)[3] = 0x00;
  (*buf)[3] = 0xFF - calc_sum(*buf, *len + 4);
  *len = *len + 4;
  return true;
EXCEPT:
  return false;
}


x86 assembly (clang, -O0):
<encode_buffer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%esi
call   1060 <check_fw@plt>
cmp    $0x1,%eax
je     119c <encode_buffer+0x3c>
lea    0xe6e(%rip),%rdi
call   1040 <printf@plt>
jmp    1282 <encode_buffer+0x122>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%esi
add    $0x4,%esi
call   1070 <realloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
jne    11da <encode_buffer+0x7a>
lea    0xe5f(%rip),%rdi
call   1040 <printf@plt>
jmp    1282 <encode_buffer+0x122>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
add    $0x4,%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
mov    -0x18(%rbp),%rax
mov    (%rax),%edx
call   1080 <memmove@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
add    $0x4,%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%esi
mov    $0x4,%edx
call   1030 <xor_cnt@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
movb   $0x6d,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
movb   $0x74,0x1(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
movb   $0x63,0x2(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
movb   $0x0,0x3(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%esi
add    $0x4,%esi
call   1050 <calc_sum@plt>
mov    %eax,%ecx
mov    $0xff,%eax
sub    %ecx,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %cl,0x3(%rax)
mov    -0x18(%rbp),%rax
mov    (%rax),%ecx
add    $0x4,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x1,-0x4(%rbp)
jmp    1289 <encode_buffer+0x129>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<encode_buffer>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
mov    (%rdi),%rdi
mov    (%rsi),%esi
call   1060 <check_fw@plt>
cmp    $0x1,%eax
jne    11f1 <encode_buffer+0x91>
mov    (%rbx),%rdi
mov    (%r14),%esi
add    $0x4,%esi
call   1070 <realloc@plt>
mov    %rax,(%rbx)
test   %rax,%rax
je     11fa <encode_buffer+0x9a>
mov    %rax,%rdi
add    $0x4,%rdi
mov    (%r14),%edx
mov    %rax,%rsi
call   1080 <memmove@plt>
mov    (%rbx),%rdi
add    $0x4,%rdi
mov    (%r14),%esi
mov    $0x4,%edx
call   1040 <xor_cnt@plt>
mov    (%rbx),%rax
movb   $0x6d,(%rax)
mov    (%rbx),%rax
movb   $0x74,0x1(%rax)
mov    (%rbx),%rax
movb   $0x63,0x2(%rax)
mov    (%rbx),%rax
movb   $0x0,0x3(%rax)
mov    (%rbx),%rdi
mov    (%r14),%esi
add    $0x4,%esi
call   1050 <calc_sum@plt>
not    %al
mov    (%rbx),%rcx
mov    %al,0x3(%rcx)
addl   $0x4,(%r14)
mov    $0x1,%eax
jmp    1208 <encode_buffer+0xa8>
lea    0xe23(%rip),%rdi
jmp    1201 <encode_buffer+0xa1>
lea    0xdff(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [47 x i8] c"Your MCU FW image does not looks like raw FW!\0A\00"
@.str.1 = [28 x i8] c"Error reallocating buffer!\0A\00"
define i32 @encode_buffer(i8** %0, i32* %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i32*
store i8** %0, i8*** %4
store i32* %1, i32** %5
%6 = load i8**, i8*** %4
%7 = load i8*, i8** %6
%8 = load i32*, i32** %5
%9 = load i32, i32* %8
%10 = call i32 @check_fw(i8* %7, i32 %9)
%11 = icmp ne i32 %10, 1
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = call i32 @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0))
br label %69
14: ; preds = %2
%15 = load i8**, i8*** %4
%16 = load i8*, i8** %15
%17 = load i32*, i32** %5
%18 = load i32, i32* %17
%19 = add nsw i32 %18, 4
%20 = call i8* @realloc(i8* %16, i32 %19)
%21 = load i8**, i8*** %4
store i8* %20, i8** %21
%22 = load i8**, i8*** %4
%23 = load i8*, i8** %22
%24 = icmp eq i8* %23, null
br i1 %24, label %25, label %27
25: ; preds = %14
%26 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
br label %69
27: ; preds = %14
%28 = load i8**, i8*** %4
%29 = load i8*, i8** %28
%30 = getelementptr inbounds i8, i8* %29, i64 4
%31 = load i8**, i8*** %4
%32 = load i8*, i8** %31
%33 = load i32*, i32** %5
%34 = load i32, i32* %33
%35 = call i32 @memmove(i8* %30, i8* %32, i32 %34)
%36 = load i8**, i8*** %4
%37 = load i8*, i8** %36
%38 = getelementptr inbounds i8, i8* %37, i64 4
%39 = load i32*, i32** %5
%40 = load i32, i32* %39
%41 = call i32 @xor_cnt(i8* %38, i32 %40, i32 4)
%42 = load i8**, i8*** %4
%43 = load i8*, i8** %42
%44 = getelementptr inbounds i8, i8* %43, i64 0
store i8 109, i8* %44
%45 = load i8**, i8*** %4
%46 = load i8*, i8** %45
%47 = getelementptr inbounds i8, i8* %46, i64 1
store i8 116, i8* %47
%48 = load i8**, i8*** %4
%49 = load i8*, i8** %48
%50 = getelementptr inbounds i8, i8* %49, i64 2
store i8 99, i8* %50
%51 = load i8**, i8*** %4
%52 = load i8*, i8** %51
%53 = getelementptr inbounds i8, i8* %52, i64 3
store i8 0, i8* %53
%54 = load i8**, i8*** %4
%55 = load i8*, i8** %54
%56 = load i32*, i32** %5
%57 = load i32, i32* %56
%58 = add nsw i32 %57, 4
%59 = call i32 @calc_sum(i8* %55, i32 %58)
%60 = sub nsw i32 255, %59
%61 = trunc i32 %60 to i8
%62 = load i8**, i8*** %4
%63 = load i8*, i8** %62
%64 = getelementptr inbounds i8, i8* %63, i64 3
store i8 %61, i8* %64
%65 = load i32*, i32** %5
%66 = load i32, i32* %65
%67 = add nsw i32 %66, 4
%68 = load i32*, i32** %5
store i32 %67, i32* %68
store i32 1, i32* %3
br label %70
69: ; preds = %25, %12
store i32 0, i32* %3
br label %70
70: ; preds = %69, %27
%71 = load i32, i32* %3
ret i32 %71
}

LLVM IR (-O2):
@str = [27 x i8] c"Error reallocating buffer!\00"
@str.2 = [46 x i8] c"Your MCU FW image does not looks like raw FW!\00"
define i32 @encode_buffer(i8** nocapture %0, i32* nocapture %1) {
%3 = load i8*, i8** %0
%4 = load i32, i32* %1
%5 = tail call i32 @check_fw(i8* %3, i32 %4)
%6 = icmp eq i32 %5, 1
br i1 %6, label %9, label %7
7: ; preds = %2
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([46 x i8], [46 x i8]* @str.2, i64 0, i64 0))
br label %42
9: ; preds = %2
%10 = load i8*, i8** %0
%11 = load i32, i32* %1
%12 = add nsw i32 %11, 4
%13 = tail call i8* @realloc(i8* %10, i32 %12)
store i8* %13, i8** %0
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %17
15: ; preds = %9
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str, i64 0, i64 0))
br label %42
17: ; preds = %9
%18 = getelementptr inbounds i8, i8* %13, i64 4
%19 = load i32, i32* %1
%20 = tail call i32 @memmove(i8* %18, i8* %13, i32 %19)
%21 = load i8*, i8** %0
%22 = getelementptr inbounds i8, i8* %21, i64 4
%23 = load i32, i32* %1
%24 = tail call i32 @xor_cnt(i8* %22, i32 %23, i32 4)
%25 = load i8*, i8** %0
store i8 109, i8* %25
%26 = load i8*, i8** %0
%27 = getelementptr inbounds i8, i8* %26, i64 1
store i8 116, i8* %27
%28 = load i8*, i8** %0
%29 = getelementptr inbounds i8, i8* %28, i64 2
store i8 99, i8* %29
%30 = load i8*, i8** %0
%31 = getelementptr inbounds i8, i8* %30, i64 3
store i8 0, i8* %31
%32 = load i8*, i8** %0
%33 = load i32, i32* %1
%34 = add nsw i32 %33, 4
%35 = tail call i32 @calc_sum(i8* %32, i32 %34)
%36 = trunc i32 %35 to i8
%37 = xor i8 %36, -1
%38 = load i8*, i8** %0
%39 = getelementptr inbounds i8, i8* %38, i64 3
store i8 %37, i8* %39
%40 = load i32, i32* %1
%41 = add nsw i32 %40, 4
store i32 %41, i32* %1
br label %42
42: ; preds = %7, %15, %17
%43 = phi i32 [ 1, %17 ], [ 0, %15 ], [ 0, %7 ]
ret i32 %43
}