#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 */
int /*<<< orphan*/ init_error(char *);
int read_f(char *);
int /*<<< orphan*/ sprintf(char *, char *, char);
int /*<<< orphan*/ write_one(int);
void stm_init(void) {
  char buf[80];
  char val;
  int n;
  write_one(0x7F);
  n = read_f(&val);
  if (n == 0) {
    write_one(0x7F);
    n = read_f(&val);
  }
  if (n == 0)
    init_error("Timeout initializing communication with bootloader");
  if (val == 0x79 || val == 0x1F)
    return;
  if (val != 0x79) {
    sprintf(buf, "Unexpected response (%02x) from bootloader", val);
    init_error(buf);
  }
}


x86 assembly (clang, -O0):
<stm_init>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    $0x7f,%edi
call   1050 <write_one@plt>
lea    -0x51(%rbp),%rdi
call   1040 <read_f@plt>
mov    %eax,-0x58(%rbp)
cmpl   $0x0,-0x58(%rbp)
jne    117e <stm_init+0x3e>
mov    $0x7f,%edi
call   1050 <write_one@plt>
lea    -0x51(%rbp),%rdi
call   1040 <read_f@plt>
mov    %eax,-0x58(%rbp)
cmpl   $0x0,-0x58(%rbp)
jne    1194 <stm_init+0x54>
lea    0xe71(%rip),%rdi
call   1030 <init_error@plt>
movsbl -0x51(%rbp),%eax
cmp    $0x79,%eax
je     11ae <stm_init+0x6e>
movsbl -0x51(%rbp),%eax
cmp    $0x1f,%eax
jne    11b3 <stm_init+0x73>
jmp    11dd <stm_init+0x9d>
movsbl -0x51(%rbp),%eax
cmp    $0x79,%eax
je     11dd <stm_init+0x9d>
lea    -0x50(%rbp),%rdi
lea    0xe68(%rip),%rsi
movsbl -0x51(%rbp),%edx
call   1060 <sprintf@plt>
lea    -0x50(%rbp),%rdi
call   1030 <init_error@plt>
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<stm_init>:
push   %rbx
sub    $0x60,%rsp
mov    $0x7f,%edi
call   1050 <write_one@plt>
lea    0xf(%rsp),%rdi
call   1040 <read_f@plt>
test   %eax,%eax
jne    1181 <stm_init+0x41>
mov    $0x7f,%edi
call   1050 <write_one@plt>
lea    0xf(%rsp),%rdi
call   1040 <read_f@plt>
test   %eax,%eax
jne    1181 <stm_init+0x41>
lea    0xe84(%rip),%rdi
call   1030 <init_error@plt>
mov    0xf(%rsp),%al
cmp    $0x1f,%al
je     11ac <stm_init+0x6c>
cmp    $0x79,%al
je     11ac <stm_init+0x6c>
movsbl %al,%edx
lea    0xe9c(%rip),%rsi
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
call   1060 <sprintf@plt>
mov    %rbx,%rdi
call   1030 <init_error@plt>
add    $0x60,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [51 x i8] c"Timeout initializing communication with bootloader\00"
@.str.1 = [43 x i8] c"Unexpected response (%02x) from bootloader\00"
define void @stm_init() {
%1 = alloca [80 x i8]
%2 = alloca i8
%3 = alloca i32
%4 = call i32 @write_one(i32 127)
%5 = call i32 @read_f(i8* %2)
store i32 %5, i32* %3
%6 = load i32, i32* %3
%7 = icmp eq i32 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %0
%9 = call i32 @write_one(i32 127)
%10 = call i32 @read_f(i8* %2)
store i32 %10, i32* %3
br label %11
11: ; preds = %8, %0
%12 = load i32, i32* %3
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %11
%15 = call i32 @init_error(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str, i64 0, i64 0))
br label %16
16: ; preds = %14, %11
%17 = load i8, i8* %2
%18 = sext i8 %17 to i32
%19 = icmp eq i32 %18, 121
br i1 %19, label %24, label %20
20: ; preds = %16
%21 = load i8, i8* %2
%22 = sext i8 %21 to i32
%23 = icmp eq i32 %22, 31
br i1 %23, label %24, label %25
24: ; preds = %20, %16
br label %35
25: ; preds = %20
%26 = load i8, i8* %2
%27 = sext i8 %26 to i32
%28 = icmp ne i32 %27, 121
br i1 %28, label %29, label %35
29: ; preds = %25
%30 = getelementptr inbounds [80 x i8], [80 x i8]* %1, i64 0, i64 0
%31 = load i8, i8* %2
%32 = call i32 @sprintf(i8* %30, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.1, i64 0, i64 0), i8 signext %31)
%33 = getelementptr inbounds [80 x i8], [80 x i8]* %1, i64 0, i64 0
%34 = call i32 @init_error(i8* %33)
br label %35
35: ; preds = %24, %29, %25
ret void
}

LLVM IR (-O2):
@.str = [51 x i8] c"Timeout initializing communication with bootloader\00"
@.str.1 = [43 x i8] c"Unexpected response (%02x) from bootloader\00"
define void @stm_init() {
%1 = alloca [80 x i8]
%2 = alloca i8
%3 = getelementptr inbounds [80 x i8], [80 x i8]* %1, i64 0, i64 0
%4 = tail call i32 @write_one(i32 127)
%5 = call i32 @read_f(i8* %2)
%6 = icmp eq i32 %5, 0
br i1 %6, label %7, label %13
7: ; preds = %0
%8 = call i32 @write_one(i32 127)
%9 = call i32 @read_f(i8* %2)
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %7
%12 = call i32 @init_error(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str, i64 0, i64 0))
br label %13
13: ; preds = %0, %11, %7
%14 = load i8, i8* %2
switch i8 %14, label %15 [
i8 121, label %18
i8 31, label %18
]
15: ; preds = %13
%16 = call i32 @sprintf(i8* %3, i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @.str.1, i64 0, i64 0), i8 signext %14)
%17 = call i32 @init_error(i8* %3)
br label %18
18: ; preds = %13, %13, %15
ret void
}