#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 /*<<< orphan*/ decode_buffer(int /*<<< orphan*/ **, int *);
int /*<<< orphan*/ free(int /*<<< orphan*/ *);
int /*<<< orphan*/ load_to_buffer(char *, int /*<<< orphan*/ **, int *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ save_from_buffer(int /*<<< orphan*/ *, int, char *);
bool do_decode(char *ifn, char *ofn) {
  uint8_t *buf = NULL;
  int len = 0;
  bool result = false;
  printf("You have selected MCU firmware decode. Good choice! ;)\n\n");
  if (!load_to_buffer(ifn, &buf, &len)) {
    result = 2;
    goto FINALLY;
  };
  if (!decode_buffer(&buf, &len)) {
    result = 2;
    goto FINALLY;
  };
  if (!save_from_buffer(buf, len, ofn)) {
    result = 2;
    goto FINALLY;
  };
  printf("All is done!\n");
  result = true;
FINALLY:
  if (buf != NULL)
    free(buf);
  return result;
}


x86 assembly (clang, -O0):
<do_decode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
lea    0xe83(%rip),%rdi
call   1050 <printf@plt>
mov    -0x8(%rbp),%rdi
lea    -0x18(%rbp),%rsi
lea    -0x1c(%rbp),%rdx
call   1070 <load_to_buffer@plt>
cmp    $0x0,%eax
jne    11a8 <do_decode+0x58>
movl   $0x2,-0x20(%rbp)
jmp    1202 <do_decode+0xb2>
lea    -0x18(%rbp),%rdi
lea    -0x1c(%rbp),%rsi
call   1060 <decode_buffer@plt>
cmp    $0x0,%eax
jne    11ca <do_decode+0x7a>
movl   $0x2,-0x20(%rbp)
jmp    1202 <do_decode+0xb2>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    -0x10(%rbp),%rdx
call   1040 <save_from_buffer@plt>
cmp    $0x0,%eax
jne    11ef <do_decode+0x9f>
movl   $0x2,-0x20(%rbp)
jmp    1202 <do_decode+0xb2>
lea    0xe43(%rip),%rdi
call   1050 <printf@plt>
movl   $0x1,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1216 <do_decode+0xc6>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_decode>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
movq   $0x0,0x10(%rsp)
movl   $0x0,0xc(%rsp)
lea    0xe8b(%rip),%rdi
call   1040 <puts@plt>
lea    0x10(%rsp),%rsi
lea    0xc(%rsp),%rdx
mov    %rbx,%rdi
call   1070 <load_to_buffer@plt>
mov    $0x2,%ebx
test   %eax,%eax
je     11ce <do_decode+0x7e>
lea    0x10(%rsp),%rdi
lea    0xc(%rsp),%rsi
call   1060 <decode_buffer@plt>
test   %eax,%eax
je     11ce <do_decode+0x7e>
mov    0x10(%rsp),%rdi
mov    0xc(%rsp),%esi
mov    %r14,%rdx
call   1050 <save_from_buffer@plt>
test   %eax,%eax
je     11ce <do_decode+0x7e>
lea    0xe74(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%ebx
mov    0x10(%rsp),%rdi
test   %rdi,%rdi
je     11dd <do_decode+0x8d>
call   1030 <free@plt>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [57 x i8] c"You have selected MCU firmware decode. Good choice! ;)\0A\0A\00"
@.str.1 = [14 x i8] c"All is done!\0A\00"
define i32 @do_decode(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
store i32* null, i32** %5
store i32 0, i32* %6
store i32 0, i32* %7
%8 = call i32 @printf(i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str, i64 0, i64 0))
%9 = load i8*, i8** %3
%10 = call i32 @load_to_buffer(i8* %9, i32** %5, i32* %6)
%11 = icmp ne i32 %10, 0
br i1 %11, label %13, label %12
12: ; preds = %2
store i32 2, i32* %7
br label %26
13: ; preds = %2
%14 = call i32 @decode_buffer(i32** %5, i32* %6)
%15 = icmp ne i32 %14, 0
br i1 %15, label %17, label %16
16: ; preds = %13
store i32 2, i32* %7
br label %26
17: ; preds = %13
%18 = load i32*, i32** %5
%19 = load i32, i32* %6
%20 = load i8*, i8** %4
%21 = call i32 @save_from_buffer(i32* %18, i32 %19, i8* %20)
%22 = icmp ne i32 %21, 0
br i1 %22, label %24, label %23
23: ; preds = %17
store i32 2, i32* %7
br label %26
24: ; preds = %17
%25 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %7
br label %26
26: ; preds = %24, %23, %16, %12
%27 = load i32*, i32** %5
%28 = icmp ne i32* %27, null
br i1 %28, label %29, label %32
29: ; preds = %26
%30 = load i32*, i32** %5
%31 = call i32 @free(i32* %30)
br label %32
32: ; preds = %29, %26
%33 = load i32, i32* %7
ret i32 %33
}

LLVM IR (-O2):
@str = [56 x i8] c"You have selected MCU firmware decode. Good choice! ;)\0A\00"
@str.2 = [13 x i8] c"All is done!\00"
define i32 @do_decode(i8* %0, i8* %1) {
%3 = alloca i32*
%4 = alloca i32
%5 = bitcast i32** %3 to i8*
store i32* null, i32** %3
%6 = bitcast i32* %4 to i8*
store i32 0, i32* %4
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([56 x i8], [56 x i8]* @str, i64 0, i64 0))
%8 = call i32 @load_to_buffer(i8* %0, i32** %3, i32* %4)
%9 = icmp eq i32 %8, 0
br i1 %9, label %20, label %10
10: ; preds = %2
%11 = call i32 @decode_buffer(i32** %3, i32* %4)
%12 = icmp eq i32 %11, 0
br i1 %12, label %20, label %13
13: ; preds = %10
%14 = load i32*, i32** %3
%15 = load i32, i32* %4
%16 = call i32 @save_from_buffer(i32* %14, i32 %15, i8* %1)
%17 = icmp eq i32 %16, 0
br i1 %17, label %20, label %18
18: ; preds = %13
%19 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.2, i64 0, i64 0))
br label %20
20: ; preds = %13, %10, %2, %18
%21 = phi i32 [ 1, %18 ], [ 2, %2 ], [ 2, %10 ], [ 2, %13 ]
%22 = load i32*, i32** %3
%23 = icmp eq i32* %22, null
br i1 %23, label %26, label %24
24: ; preds = %20
%25 = call i32 @free(i32* %22)
br label %26
26: ; preds = %24, %20
ret i32 %21
}