#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*/ decode_buffer(int /*<<< orphan*/ **, int *);
int /*<<< orphan*/ free(int /*<<< orphan*/ *);
int /*<<< orphan*/ heuristic_getver(int /*<<< orphan*/ *, int, char *, int);
int /*<<< orphan*/ load_to_buffer(char *, int /*<<< orphan*/ **, int *);
int /*<<< orphan*/ printf(char *);
bool do_getver(char *fn) {
  uint8_t *buf = NULL;
  int len = 0;
  bool result = false;
  char verinfo[64];
  printf("You have selected MCU firmware version search.\n\n");
  if (!load_to_buffer(fn, &buf, &len)) {
    result = 2;
    goto FINALLY;
  };
  switch (check_fw(buf, len)) {
  case 2: {
    printf("Looks like encoded FW image, decoding...\n");
    if (!decode_buffer(&buf, &len)) {
      printf("Error while decoding!");
      result = 2;
      goto FINALLY;
    };
    break;
  }
  case 1: {
    printf("Looks like raw FW image...\n");
    break;
  }
  case 0:
  default: {
    printf("Input file does not looks like FW image!\n");
    {
      result = 2;
      goto FINALLY;
    };
    break;
  }
  }
  printf("\n");
  if (!heuristic_getver(buf, len, verinfo, 64)) {
    printf("Can't get version info!");
    goto FINALLY;
  };
  printf("Version info:\n\n");
  printf(verinfo);
  printf("\n");
  result = true;
FINALLY:
  if (buf != NULL)
    free(buf);
  return result;
}


x86 assembly (clang, -O0):
<do_getver>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
lea    0xe77(%rip),%rdi
call   1040 <printf@plt>
mov    -0x8(%rbp),%rdi
lea    -0x10(%rbp),%rsi
lea    -0x14(%rbp),%rdx
call   1080 <load_to_buffer@plt>
cmp    $0x0,%eax
jne    11b4 <do_getver+0x54>
movl   $0x2,-0x18(%rbp)
jmp    12c4 <do_getver+0x164>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1060 <check_fw@plt>
mov    %eax,%ecx
mov    %ecx,-0x64(%rbp)
test   %eax,%eax
je     1244 <do_getver+0xe4>
jmp    11d2 <do_getver+0x72>
mov    -0x64(%rbp),%eax
sub    $0x1,%eax
je     1233 <do_getver+0xd3>
jmp    11e3 <do_getver+0x83>
mov    -0x64(%rbp),%eax
sub    $0x2,%eax
jne    1249 <do_getver+0xe9>
jmp    11f4 <do_getver+0x94>
lea    0xe36(%rip),%rdi
call   1040 <printf@plt>
lea    -0x10(%rbp),%rdi
lea    -0x14(%rbp),%rsi
call   1050 <decode_buffer@plt>
cmp    $0x0,%eax
jne    122e <do_getver+0xce>
lea    0xe3e(%rip),%rdi
call   1040 <printf@plt>
movl   $0x2,-0x18(%rbp)
jmp    12c4 <do_getver+0x164>
jmp    1261 <do_getver+0x101>
lea    0xe37(%rip),%rdi
call   1040 <printf@plt>
jmp    1261 <do_getver+0x101>
jmp    1249 <do_getver+0xe9>
lea    0xe3d(%rip),%rdi
call   1040 <printf@plt>
movl   $0x2,-0x18(%rbp)
jmp    12c4 <do_getver+0x164>
lea    0xdc7(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
lea    -0x60(%rbp),%rdx
mov    $0x40,%ecx
call   1070 <heuristic_getver@plt>
cmp    $0x0,%eax
jne    129c <do_getver+0x13c>
lea    0xe25(%rip),%rdi
call   1040 <printf@plt>
jmp    12c4 <do_getver+0x164>
lea    0xe2c(%rip),%rdi
call   1040 <printf@plt>
lea    -0x60(%rbp),%rdi
call   1040 <printf@plt>
lea    0xd77(%rip),%rdi
call   1040 <printf@plt>
movl   $0x1,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     12d8 <do_getver+0x178>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_getver>:
push   %rbx
sub    $0x50,%rsp
mov    %rdi,%rbx
movq   $0x0,0x8(%rsp)
movl   $0x0,0x4(%rsp)
lea    0xe8e(%rip),%rdi
call   1050 <puts@plt>
lea    0x8(%rsp),%rsi
lea    0x4(%rsp),%rdx
mov    %rbx,%rdi
call   10a0 <load_to_buffer@plt>
mov    $0x2,%ebx
test   %eax,%eax
je     1282 <do_getver+0x102>
mov    0x8(%rsp),%rdi
mov    0x4(%rsp),%esi
call   1080 <check_fw@plt>
cmp    $0x1,%eax
je     120d <do_getver+0x8d>
cmp    $0x2,%eax
jne    1266 <do_getver+0xe6>
lea    0xea1(%rip),%rdi
call   1050 <puts@plt>
lea    0x8(%rsp),%rdi
lea    0x4(%rsp),%rsi
call   1070 <decode_buffer@plt>
test   %eax,%eax
jne    1219 <do_getver+0x99>
lea    0xdfa(%rip),%rdi
call   1060 <printf@plt>
jmp    1282 <do_getver+0x102>
lea    0xe4a(%rip),%rdi
call   1050 <puts@plt>
mov    $0xa,%edi
call   1040 <putchar@plt>
mov    0x8(%rsp),%rdi
mov    0x4(%rsp),%esi
lea    0x10(%rsp),%rdx
mov    $0x40,%ecx
call   1090 <heuristic_getver@plt>
test   %eax,%eax
je     1274 <do_getver+0xf4>
lea    0xe33(%rip),%rdi
call   1050 <puts@plt>
lea    0x10(%rsp),%rdi
call   1060 <printf@plt>
mov    $0xa,%edi
call   1040 <putchar@plt>
mov    $0x1,%ebx
jmp    1282 <do_getver+0x102>
lea    0xe44(%rip),%rdi
call   1050 <puts@plt>
jmp    1282 <do_getver+0x102>
lea    0xd9b(%rip),%rdi
call   1060 <printf@plt>
xor    %ebx,%ebx
mov    0x8(%rsp),%rdi
test   %rdi,%rdi
je     1291 <do_getver+0x111>
call   1030 <free@plt>
mov    %ebx,%eax
add    $0x50,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [49 x i8] c"You have selected MCU firmware version search.\0A\0A\00"
@.str.1 = [42 x i8] c"Looks like encoded FW image, decoding...\0A\00"
@.str.2 = [22 x i8] c"Error while decoding!\00"
@.str.3 = [28 x i8] c"Looks like raw FW image...\0A\00"
@.str.4 = [42 x i8] c"Input file does not looks like FW image!\0A\00"
@.str.5 = [2 x i8] c"\0A\00"
@.str.6 = [24 x i8] c"Can't get version info!\00"
@.str.7 = [16 x i8] c"Version info:\0A\0A\00"
define i32 @do_getver(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32
%6 = alloca [64 x i8]
store i8* %0, i8** %2
store i32* null, i32** %3
store i32 0, i32* %4
store i32 0, i32* %5
%7 = call i32 @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0))
%8 = load i8*, i8** %2
%9 = call i32 @load_to_buffer(i8* %8, i32** %3, i32* %4)
%10 = icmp ne i32 %9, 0
br i1 %10, label %12, label %11
11: ; preds = %1
store i32 2, i32* %5
br label %42
12: ; preds = %1
%13 = load i32*, i32** %3
%14 = load i32, i32* %4
%15 = call i32 @check_fw(i32* %13, i32 %14)
switch i32 %15, label %26 [
i32 2, label %16
i32 1, label %23
i32 0, label %25
]
16: ; preds = %12
%17 = call i32 @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0))
%18 = call i32 @decode_buffer(i32** %3, i32* %4)
%19 = icmp ne i32 %18, 0
br i1 %19, label %22, label %20
20: ; preds = %16
%21 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
store i32 2, i32* %5
br label %42
22: ; preds = %16
br label %28
23: ; preds = %12
%24 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0))
br label %28
25: ; preds = %12
br label %26
26: ; preds = %12, %25
%27 = call i32 @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.4, i64 0, i64 0))
store i32 2, i32* %5
br label %42
28: ; preds = %23, %22
%29 = call i32 @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%30 = load i32*, i32** %3
%31 = load i32, i32* %4
%32 = getelementptr inbounds [64 x i8], [64 x i8]* %6, i64 0, i64 0
%33 = call i32 @heuristic_getver(i32* %30, i32 %31, i8* %32, i32 64)
%34 = icmp ne i32 %33, 0
br i1 %34, label %37, label %35
35: ; preds = %28
%36 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i64 0, i64 0))
br label %42
37: ; preds = %28
%38 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.7, i64 0, i64 0))
%39 = getelementptr inbounds [64 x i8], [64 x i8]* %6, i64 0, i64 0
%40 = call i32 @printf(i8* %39)
%41 = call i32 @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
store i32 1, i32* %5
br label %42
42: ; preds = %37, %35, %26, %20, %11
%43 = load i32*, i32** %3
%44 = icmp ne i32* %43, null
br i1 %44, label %45, label %48
45: ; preds = %42
%46 = load i32*, i32** %3
%47 = call i32 @free(i32* %46)
br label %48
48: ; preds = %45, %42
%49 = load i32, i32* %5
ret i32 %49
}

LLVM IR (-O2):
@.str.2 = [22 x i8] c"Error while decoding!\00"
@.str.6 = [24 x i8] c"Can't get version info!\00"
@str = [48 x i8] c"You have selected MCU firmware version search.\0A\00"
@str.8 = [27 x i8] c"Looks like raw FW image...\00"
@str.9 = [15 x i8] c"Version info:\0A\00"
@str.10 = [41 x i8] c"Looks like encoded FW image, decoding...\00"
@str.11 = [41 x i8] c"Input file does not looks like FW image!\00"
define i32 @do_getver(i8* %0) {
%2 = alloca i32*
%3 = alloca i32
%4 = alloca [64 x i8]
%5 = bitcast i32** %2 to i8*
store i32* null, i32** %2
%6 = bitcast i32* %3 to i8*
store i32 0, i32* %3
%7 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([48 x i8], [48 x i8]* @str, i64 0, i64 0))
%9 = call i32 @load_to_buffer(i8* %0, i32** %2, i32* %3)
%10 = icmp eq i32 %9, 0
br i1 %10, label %37, label %11
11: ; preds = %1
%12 = load i32*, i32** %2
%13 = load i32, i32* %3
%14 = call i32 @check_fw(i32* %12, i32 %13)
switch i32 %14, label %23 [
i32 2, label %15
i32 1, label %21
]
15: ; preds = %11
%16 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @str.10, i64 0, i64 0))
%17 = call i32 @decode_buffer(i32** %2, i32* %3)
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %25
19: ; preds = %15
%20 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
br label %37
21: ; preds = %11
%22 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str.8, i64 0, i64 0))
br label %25
23: ; preds = %11
%24 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @str.11, i64 0, i64 0))
br label %37
25: ; preds = %15, %21
%26 = call i32 @putchar(i32 10)
%27 = load i32*, i32** %2
%28 = load i32, i32* %3
%29 = call i32 @heuristic_getver(i32* %27, i32 %28, i8* %7, i32 64)
%30 = icmp eq i32 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %25
%32 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i64 0, i64 0))
br label %37
33: ; preds = %25
%34 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.9, i64 0, i64 0))
%35 = call i32 @printf(i8* %7)
%36 = call i32 @putchar(i32 10)
br label %37
37: ; preds = %1, %33, %31, %23, %19
%38 = phi i32 [ 2, %23 ], [ 1, %33 ], [ 0, %31 ], [ 2, %19 ], [ 2, %1 ]
%39 = load i32*, i32** %2
%40 = icmp eq i32* %39, null
br i1 %40, label %43, label %41
41: ; preds = %37
%42 = call i32 @free(i32* %39)
br label %43
43: ; preds = %41, %37
ret i32 %38
}