#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 int /*<<< orphan*/ uint8_t;

/* Variables and functions */
int check_fw(int /*<<< orphan*/ *, int);
int /*<<< orphan*/ memmove(int /*<<< orphan*/ *, int /*<<< orphan*/ *, int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ *realloc(int /*<<< orphan*/ *, int);
int /*<<< orphan*/ xor_cnt(int /*<<< orphan*/ *, int, int);
bool decode_buffer(uint8_t **buf, int *len) {
  if (check_fw(*buf, *len) != 2) {
    printf("Your MCU FW image does not looks like encoded MTC FW!\n");
    goto EXCEPT;
  };
  xor_cnt(&(*buf)[4], *len - 4, 4);
  memmove(*buf, &(*buf)[4], *len - 4);
  *buf = realloc(*buf, *len - 4);
  if (*buf == NULL) {
    printf("Error reallocating buffer!\n");
    goto EXCEPT;
  };
  *len = *len - 4;
  return true;
EXCEPT:
  return false;
}


x86 assembly (clang, -O0):
<decode_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   1050 <check_fw@plt>
cmp    $0x2,%eax
je     118c <decode_buffer+0x3c>
lea    0xe7e(%rip),%rdi
call   1040 <printf@plt>
jmp    1223 <decode_buffer+0xd3>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
add    $0x10,%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%esi
sub    $0x4,%esi
mov    $0x4,%edx
call   1030 <xor_cnt@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
add    $0x10,%rsi
mov    -0x18(%rbp),%rax
mov    (%rax),%edx
sub    $0x4,%edx
call   1070 <memmove@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%esi
sub    $0x4,%esi
call   1060 <realloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
jne    1208 <decode_buffer+0xb8>
lea    0xe39(%rip),%rdi
call   1040 <printf@plt>
jmp    1223 <decode_buffer+0xd3>
mov    -0x18(%rbp),%rax
mov    (%rax),%ecx
sub    $0x4,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x1,-0x4(%rbp)
jmp    122a <decode_buffer+0xda>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<decode_buffer>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
mov    (%rdi),%rdi
mov    (%rsi),%esi
call   1050 <check_fw@plt>
cmp    $0x2,%eax
jne    11b3 <decode_buffer+0x63>
mov    (%rbx),%rdi
add    $0x10,%rdi
mov    (%r14),%esi
add    $0xfffffffc,%esi
mov    $0x4,%edx
call   1040 <xor_cnt@plt>
mov    (%rbx),%rdi
lea    0x10(%rdi),%rsi
mov    (%r14),%edx
add    $0xfffffffc,%edx
call   1070 <memmove@plt>
mov    (%rbx),%rdi
mov    (%r14),%esi
add    $0xfffffffc,%esi
call   1060 <realloc@plt>
mov    %rax,(%rbx)
test   %rax,%rax
je     11bc <decode_buffer+0x6c>
addl   $0xfffffffc,(%r14)
mov    $0x1,%eax
jmp    11ca <decode_buffer+0x7a>
lea    0xe61(%rip),%rdi
jmp    11c3 <decode_buffer+0x73>
lea    0xe3d(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [55 x i8] c"Your MCU FW image does not looks like encoded MTC FW!\0A\00"
@.str.1 = [28 x i8] c"Error reallocating buffer!\0A\00"
define i32 @decode_buffer(i32** %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32**
%5 = alloca i32*
store i32** %0, i32*** %4
store i32* %1, i32** %5
%6 = load i32**, i32*** %4
%7 = load i32*, i32** %6
%8 = load i32*, i32** %5
%9 = load i32, i32* %8
%10 = call i32 @check_fw(i32* %7, i32 %9)
%11 = icmp ne i32 %10, 2
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = call i32 @printf(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str, i64 0, i64 0))
br label %48
14: ; preds = %2
%15 = load i32**, i32*** %4
%16 = load i32*, i32** %15
%17 = getelementptr inbounds i32, i32* %16, i64 4
%18 = load i32*, i32** %5
%19 = load i32, i32* %18
%20 = sub nsw i32 %19, 4
%21 = call i32 @xor_cnt(i32* %17, i32 %20, i32 4)
%22 = load i32**, i32*** %4
%23 = load i32*, i32** %22
%24 = load i32**, i32*** %4
%25 = load i32*, i32** %24
%26 = getelementptr inbounds i32, i32* %25, i64 4
%27 = load i32*, i32** %5
%28 = load i32, i32* %27
%29 = sub nsw i32 %28, 4
%30 = call i32 @memmove(i32* %23, i32* %26, i32 %29)
%31 = load i32**, i32*** %4
%32 = load i32*, i32** %31
%33 = load i32*, i32** %5
%34 = load i32, i32* %33
%35 = sub nsw i32 %34, 4
%36 = call i32* @realloc(i32* %32, i32 %35)
%37 = load i32**, i32*** %4
store i32* %36, i32** %37
%38 = load i32**, i32*** %4
%39 = load i32*, i32** %38
%40 = icmp eq i32* %39, null
br i1 %40, label %41, label %43
41: ; preds = %14
%42 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
br label %48
43: ; preds = %14
%44 = load i32*, i32** %5
%45 = load i32, i32* %44
%46 = sub nsw i32 %45, 4
%47 = load i32*, i32** %5
store i32 %46, i32* %47
store i32 1, i32* %3
br label %49
48: ; preds = %41, %12
store i32 0, i32* %3
br label %49
49: ; preds = %48, %43
%50 = load i32, i32* %3
ret i32 %50
}

LLVM IR (-O2):
@str = [27 x i8] c"Error reallocating buffer!\00"
@str.2 = [54 x i8] c"Your MCU FW image does not looks like encoded MTC FW!\00"
define i32 @decode_buffer(i32** nocapture %0, i32* nocapture %1) {
%3 = load i32*, i32** %0
%4 = load i32, i32* %1
%5 = tail call i32 @check_fw(i32* %3, i32 %4)
%6 = icmp eq i32 %5, 2
br i1 %6, label %9, label %7
7: ; preds = %2
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([54 x i8], [54 x i8]* @str.2, i64 0, i64 0))
br label %30
9: ; preds = %2
%10 = load i32*, i32** %0
%11 = getelementptr inbounds i32, i32* %10, i64 4
%12 = load i32, i32* %1
%13 = add nsw i32 %12, -4
%14 = tail call i32 @xor_cnt(i32* %11, i32 %13, i32 4)
%15 = load i32*, i32** %0
%16 = getelementptr inbounds i32, i32* %15, i64 4
%17 = load i32, i32* %1
%18 = add nsw i32 %17, -4
%19 = tail call i32 @memmove(i32* %15, i32* %16, i32 %18)
%20 = load i32*, i32** %0
%21 = load i32, i32* %1
%22 = add nsw i32 %21, -4
%23 = tail call i32* @realloc(i32* %20, i32 %22)
store i32* %23, i32** %0
%24 = icmp eq i32* %23, null
br i1 %24, label %25, label %27
25: ; preds = %9
%26 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str, i64 0, i64 0))
br label %30
27: ; preds = %9
%28 = load i32, i32* %1
%29 = add nsw i32 %28, -4
store i32 %29, i32* %1
br label %30
30: ; preds = %7, %25, %27
%31 = phi i32 [ 1, %27 ], [ 0, %25 ], [ 0, %7 ]
ret i32 %31
}