#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*/ TCIOFLUSH;
int /*<<< orphan*/ kwboot_printv(char *);
int /*<<< orphan*/ kwboot_spinner();
int kwboot_tty_recv(int, char *, int, int);
int kwboot_tty_send(int, void *, int);
int tcflush(int, int /*<<< orphan*/);
int /*<<< orphan*/ usleep(int);
int kwboot_bootmsg(int tty, void *msg) {
  int rc;
  char c;
  if (msg == NULL)
    kwboot_printv("Please reboot the target into UART boot mode...");
  else
    kwboot_printv("Sending boot message. Please reboot the target...");
  do {
    rc = tcflush(tty, TCIOFLUSH);
    if (rc)
      break;
    rc = kwboot_tty_send(tty, msg, 8);
    if (rc) {
      usleep(1000 * 1000);
      continue;
    }
    rc = kwboot_tty_recv(tty, &c, 1, 1000);
    kwboot_spinner();
  } while (rc || c != 21);
  kwboot_printv("\n");
  return rc;
}


x86 assembly (clang, -O0):
<kwboot_bootmsg>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    118b <kwboot_bootmsg+0x2b>
lea    0xe7f(%rip),%rdi
call   1050 <kwboot_printv@plt>
jmp    1197 <kwboot_bootmsg+0x37>
lea    0xe9e(%rip),%rdi
call   1050 <kwboot_printv@plt>
jmp    119c <kwboot_bootmsg+0x3c>
mov    -0x4(%rbp),%edi
mov    0x2e3a(%rip),%rax
mov    (%rax),%esi
call   1040 <tcflush@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     11bf <kwboot_bootmsg+0x5f>
jmp    1233 <kwboot_bootmsg+0xd3>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x8,%edx
call   1060 <kwboot_tty_send@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     11ec <kwboot_bootmsg+0x8c>
mov    $0xf4240,%edi
call   1080 <usleep@plt>
jmp    120c <kwboot_bootmsg+0xac>
mov    -0x4(%rbp),%edi
lea    -0x15(%rbp),%rsi
mov    $0x1,%edx
mov    $0x3e8,%ecx
call   1030 <kwboot_tty_recv@plt>
mov    %eax,-0x14(%rbp)
mov    $0x0,%al
call   1070 <kwboot_spinner@plt>
mov    $0x1,%al
cmpl   $0x0,-0x14(%rbp)
mov    %al,-0x16(%rbp)
jne    1228 <kwboot_bootmsg+0xc8>
movsbl -0x15(%rbp),%eax
cmp    $0x15,%eax
setne  %al
mov    %al,-0x16(%rbp)
mov    -0x16(%rbp),%al
test   $0x1,%al
jne    119c <kwboot_bootmsg+0x3c>
lea    0xe28(%rip),%rdi
call   1050 <kwboot_printv@plt>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<kwboot_bootmsg>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %edi,%r12d
test   %rsi,%rsi
lea    0xe85(%rip),%rax
lea    0xeae(%rip),%rdi
cmove  %rax,%rdi
call   1050 <kwboot_printv@plt>
mov    0x2e4e(%rip),%r13
lea    0x7(%rsp),%r14
nopw   0x0(%rax,%rax,1)
mov    0x0(%r13),%esi
mov    %r12d,%edi
call   1040 <tcflush@plt>
mov    %eax,%ebx
test   %eax,%eax
jne    1209 <kwboot_bootmsg+0xa9>
mov    %r12d,%edi
mov    %r15,%rsi
mov    $0x8,%edx
call   1060 <kwboot_tty_send@plt>
test   %eax,%eax
je     11e0 <kwboot_bootmsg+0x80>
mov    $0xf4240,%edi
call   1080 <usleep@plt>
mov    $0x1,%ebp
test   %ebp,%ebp
je     1202 <kwboot_bootmsg+0xa2>
jmp    11a0 <kwboot_bootmsg+0x40>
nopl   0x0(%rax,%rax,1)
mov    %r12d,%edi
mov    %r14,%rsi
mov    $0x1,%edx
mov    $0x3e8,%ecx
call   1030 <kwboot_tty_recv@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1070 <kwboot_spinner@plt>
test   %ebp,%ebp
jne    11a0 <kwboot_bootmsg+0x40>
cmpb   $0x15,0x7(%rsp)
jne    11a0 <kwboot_bootmsg+0x40>
lea    0xe52(%rip),%rdi
call   1050 <kwboot_printv@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [48 x i8] c"Please reboot the target into UART boot mode...\00"
@.str.1 = [50 x i8] c"Sending boot message. Please reboot the target...\00"
@TCIOFLUSH = global i32 0
@.str.2 = [2 x i8] c"\0A\00"
define i32 @kwboot_bootmsg(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8
store i32 %0, i32* %3
store i8* %1, i8** %4
%7 = load i8*, i8** %4
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = call i32 @kwboot_printv(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str, i64 0, i64 0))
br label %13
11: ; preds = %2
%12 = call i32 @kwboot_printv(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.1, i64 0, i64 0))
br label %13
13: ; preds = %11, %9
br label %14
14: ; preds = %40, %13
%15 = load i32, i32* %3
%16 = load i32, i32* @TCIOFLUSH
%17 = call i32 @tcflush(i32 %15, i32 %16)
store i32 %17, i32* %5
%18 = load i32, i32* %5
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %14
br label %42
21: ; preds = %14
%22 = load i32, i32* %3
%23 = load i8*, i8** %4
%24 = call i32 @kwboot_tty_send(i32 %22, i8* %23, i32 8)
store i32 %24, i32* %5
%25 = load i32, i32* %5
%26 = icmp ne i32 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %21
%28 = call i32 @usleep(i32 1000000)
br label %33
29: ; preds = %21
%30 = load i32, i32* %3
%31 = call i32 @kwboot_tty_recv(i32 %30, i8* %6, i32 1, i32 1000)
store i32 %31, i32* %5
%32 = call i32 (...) @kwboot_spinner()
br label %33
33: ; preds = %29, %27
%34 = load i32, i32* %5
%35 = icmp ne i32 %34, 0
br i1 %35, label %40, label %36
36: ; preds = %33
%37 = load i8, i8* %6
%38 = sext i8 %37 to i32
%39 = icmp ne i32 %38, 21
br label %40
40: ; preds = %36, %33
%41 = phi i1 [ true, %33 ], [ %39, %36 ]
br i1 %41, label %14, label %42
42: ; preds = %40, %20
%43 = call i32 @kwboot_printv(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%44 = load i32, i32* %5
ret i32 %44
}

LLVM IR (-O2):
@.str = [48 x i8] c"Please reboot the target into UART boot mode...\00"
@.str.1 = [50 x i8] c"Sending boot message. Please reboot the target...\00"
@TCIOFLUSH = global i32 0
@.str.2 = [2 x i8] c"\0A\00"
define i32 @kwboot_bootmsg(i32 %0, i8* %1) {
%3 = alloca i8
%4 = icmp eq i8* %1, null
%5 = select i1 %4, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.1, i64 0, i64 0)
%6 = tail call i32 @kwboot_printv(i8* %5)
br label %7
7: ; preds = %19, %2
%8 = load i32, i32* @TCIOFLUSH
%9 = call i32 @tcflush(i32 %0, i32 %8)
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %25
11: ; preds = %7
%12 = call i32 @kwboot_tty_send(i32 %0, i8* %1, i32 8)
%13 = icmp eq i32 %12, 0
br i1 %13, label %16, label %14
14: ; preds = %11
%15 = call i32 @usleep(i32 1000000)
br label %19
16: ; preds = %11
%17 = call i32 @kwboot_tty_recv(i32 %0, i8* %3, i32 1, i32 1000)
%18 = call i32 (...) @kwboot_spinner()
br label %19
19: ; preds = %16, %14
%20 = phi i32 [ 1, %14 ], [ %17, %16 ]
%21 = icmp ne i32 %20, 0
%22 = load i8, i8* %3
%23 = icmp ne i8 %22, 21
%24 = select i1 %21, i1 true, i1 %23
br i1 %24, label %7, label %25
25: ; preds = %7, %19
%26 = call i32 @kwboot_printv(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
ret i32 %9
}