#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 */
scalar_t__ check_ack();
int /*<<< orphan*/ error(char *);
int /*<<< orphan*/ printf(char *, int);
int read_one();
scalar_t__ stm_cmd(int);
int stm_ver2(int verbose) {
  int i;
  int val;
  int rv;
  if (stm_cmd(0x01) == 0)
    error("GET2 command rejected");
  for (i = 0; i < 3; i++) {
    val = read_one();
    if (i == 0)
      rv = val;
    if (verbose)
      if (i == 0)
        printf("Bootloader version = %02x\n", val);
      else
        printf("VER 2 = %02x\n", val);
  }
  if (check_ack() == 0)
    error("GET version 2 ended badly");
  return rv;
}


x86 assembly (clang, -O0):
<stm_ver2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    $0x1,%edi
call   1030 <stm_cmd@plt>
cmp    $0x0,%rax
jne    117b <stm_ver2+0x2b>
lea    0xe8a(%rip),%rdi
call   1070 <error@plt>
movl   $0x0,-0x8(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    11f5 <stm_ver2+0xa5>
mov    $0x0,%al
call   1050 <read_one@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0x8(%rbp)
jne    11a6 <stm_ver2+0x56>
mov    -0xc(%rbp),%eax
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x4(%rbp)
je     11e2 <stm_ver2+0x92>
cmpl   $0x0,-0x8(%rbp)
jne    11ce <stm_ver2+0x7e>
mov    -0xc(%rbp),%esi
lea    0xe52(%rip),%rdi
call   1060 <printf@plt>
jmp    11dd <stm_ver2+0x8d>
mov    -0xc(%rbp),%esi
lea    0xe59(%rip),%rdi
call   1060 <printf@plt>
jmp    11e2 <stm_ver2+0x92>
jmp    11e7 <stm_ver2+0x97>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1182 <stm_ver2+0x32>
mov    $0x0,%al
call   1040 <check_ack@plt>
cmp    $0x0,%rax
jne    1212 <stm_ver2+0xc2>
lea    0xe32(%rip),%rdi
call   1070 <error@plt>
mov    -0x10(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<stm_ver2>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebp
mov    $0x1,%edi
call   1030 <stm_cmd@plt>
test   %rax,%rax
jne    1170 <stm_ver2+0x20>
lea    0xe95(%rip),%rdi
call   1070 <error@plt>
xor    %eax,%eax
call   1050 <read_one@plt>
mov    %eax,%ebx
test   %ebp,%ebp
je     11b6 <stm_ver2+0x66>
lea    0xe92(%rip),%rdi
mov    %ebx,%esi
call   1060 <printf@plt>
xor    %eax,%eax
call   1050 <read_one@plt>
lea    0xe98(%rip),%rbp
mov    %rbp,%rdi
mov    %eax,%esi
call   1060 <printf@plt>
xor    %eax,%eax
call   1050 <read_one@plt>
mov    %rbp,%rdi
mov    %eax,%esi
call   1060 <printf@plt>
jmp    11c4 <stm_ver2+0x74>
xor    %eax,%eax
call   1050 <read_one@plt>
xor    %eax,%eax
call   1050 <read_one@plt>
xor    %eax,%eax
call   1040 <check_ack@plt>
test   %rax,%rax
jne    11dc <stm_ver2+0x8c>
lea    0xe68(%rip),%rdi
call   1070 <error@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [22 x i8] c"GET2 command rejected\00"
@.str.1 = [27 x i8] c"Bootloader version = %02x\0A\00"
@.str.2 = [14 x i8] c"VER 2 = %02x\0A\00"
@.str.3 = [26 x i8] c"GET version 2 ended badly\00"
define i32 @stm_ver2(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %2
%6 = call i64 @stm_cmd(i32 1)
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %1
%9 = call i32 @error(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %10
10: ; preds = %8, %1
store i32 0, i32* %3
br label %11
11: ; preds = %34, %10
%12 = load i32, i32* %3
%13 = icmp slt i32 %12, 3
br i1 %13, label %14, label %37
14: ; preds = %11
%15 = call i32 (...) @read_one()
store i32 %15, i32* %4
%16 = load i32, i32* %3
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %14
%19 = load i32, i32* %4
store i32 %19, i32* %5
br label %20
20: ; preds = %18, %14
%21 = load i32, i32* %2
%22 = icmp ne i32 %21, 0
br i1 %22, label %23, label %33
23: ; preds = %20
%24 = load i32, i32* %3
%25 = icmp eq i32 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %23
%27 = load i32, i32* %4
%28 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i32 %27)
br label %32
29: ; preds = %23
%30 = load i32, i32* %4
%31 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 %30)
br label %32
32: ; preds = %29, %26
br label %33
33: ; preds = %32, %20
br label %34
34: ; preds = %33
%35 = load i32, i32* %3
%36 = add nsw i32 %35, 1
store i32 %36, i32* %3
br label %11
37: ; preds = %11
%38 = call i64 (...) @check_ack()
%39 = icmp eq i64 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %37
%41 = call i32 @error(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
br label %42
42: ; preds = %40, %37
%43 = load i32, i32* %5
ret i32 %43
}

LLVM IR (-O2):
@.str = [22 x i8] c"GET2 command rejected\00"
@.str.1 = [27 x i8] c"Bootloader version = %02x\0A\00"
@.str.2 = [14 x i8] c"VER 2 = %02x\0A\00"
@.str.3 = [26 x i8] c"GET version 2 ended badly\00"
define i32 @stm_ver2(i32 %0) {
%2 = tail call i64 @stm_cmd(i32 1)
%3 = icmp eq i64 %2, 0
br i1 %3, label %4, label %6
4: ; preds = %1
%5 = tail call i32 @error(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %6
6: ; preds = %4, %1
%7 = icmp eq i32 %0, 0
%8 = tail call i32 (...) @read_one()
br i1 %7, label %9, label %12
9: ; preds = %6
%10 = tail call i32 (...) @read_one()
%11 = tail call i32 (...) @read_one()
br label %18
12: ; preds = %6
%13 = tail call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i32 %8)
%14 = tail call i32 (...) @read_one()
%15 = tail call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 %14)
%16 = tail call i32 (...) @read_one()
%17 = tail call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 %16)
br label %18
18: ; preds = %9, %12
%19 = tail call i64 (...) @check_ack()
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %18
%22 = tail call i32 @error(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
br label %23
23: ; preds = %21, %18
ret i32 %8
}