#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_debugmsg(int tty, void *msg) {
  int rc;
  kwboot_printv("Sending debug message. Please reboot the target...");
  rc = tcflush(tty, TCIOFLUSH);
  if (rc)
    return rc;
  do {
    char buf[16];
    rc = kwboot_tty_send(tty, msg, 8);
    if (rc) {
      usleep(1000 * 1000);
      continue;
    }
    rc = kwboot_tty_recv(tty, buf, 16, 1);
    kwboot_spinner();
  } while (rc);
  kwboot_printv("\n");
  return rc;
}


x86 assembly (clang, -O0):
<kwboot_debugmsg>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe8a(%rip),%rdi
call   1050 <kwboot_printv@plt>
mov    -0x8(%rbp),%edi
mov    0x2e5b(%rip),%rax
mov    (%rax),%esi
call   1040 <tcflush@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     11a4 <kwboot_debugmsg+0x44>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1212 <kwboot_debugmsg+0xb2>
jmp    11a9 <kwboot_debugmsg+0x49>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x8,%edx
call   1060 <kwboot_tty_send@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     11d6 <kwboot_debugmsg+0x76>
mov    $0xf4240,%edi
call   1080 <usleep@plt>
jmp    11f6 <kwboot_debugmsg+0x96>
mov    -0x8(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x10,%edx
mov    $0x1,%ecx
call   1030 <kwboot_tty_recv@plt>
mov    %eax,-0x14(%rbp)
mov    $0x0,%al
call   1070 <kwboot_spinner@plt>
cmpl   $0x0,-0x14(%rbp)
jne    11a9 <kwboot_debugmsg+0x49>
lea    0xe2c(%rip),%rdi
call   1050 <kwboot_printv@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<kwboot_debugmsg>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r15
mov    %edi,%ebp
lea    0xe8a(%rip),%rdi
call   1050 <kwboot_printv@plt>
mov    0x2e5e(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1040 <tcflush@plt>
test   %eax,%eax
jne    11f5 <kwboot_debugmsg+0x95>
mov    %rsp,%r14
jmp    11c1 <kwboot_debugmsg+0x61>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x10,%edx
mov    $0x1,%ecx
call   1030 <kwboot_tty_recv@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1070 <kwboot_spinner@plt>
test   %ebx,%ebx
je     11e7 <kwboot_debugmsg+0x87>
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x8,%edx
call   1060 <kwboot_tty_send@plt>
test   %eax,%eax
je     11a0 <kwboot_debugmsg+0x40>
mov    $0xf4240,%edi
call   1080 <usleep@plt>
mov    $0x1,%ebx
test   %ebx,%ebx
jne    11c1 <kwboot_debugmsg+0x61>
lea    0xe45(%rip),%rdi
call   1050 <kwboot_printv@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [51 x i8] c"Sending debug message. Please reboot the target...\00"
@TCIOFLUSH = global i32 0
@.str.1 = [2 x i8] c"\0A\00"
define i32 @kwboot_debugmsg(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca [16 x i8]
store i32 %0, i32* %4
store i8* %1, i8** %5
%8 = call i32 @kwboot_printv(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str, i64 0, i64 0))
%9 = load i32, i32* %4
%10 = load i32, i32* @TCIOFLUSH
%11 = call i32 @tcflush(i32 %9, i32 %10)
store i32 %11, i32* %6
%12 = load i32, i32* %6
%13 = icmp ne i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = load i32, i32* %6
store i32 %15, i32* %3
br label %36
16: ; preds = %2
br label %17
17: ; preds = %30, %16
%18 = load i32, i32* %4
%19 = load i8*, i8** %5
%20 = call i32 @kwboot_tty_send(i32 %18, i8* %19, i32 8)
store i32 %20, i32* %6
%21 = load i32, i32* %6
%22 = icmp ne i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %17
%24 = call i32 @usleep(i32 1000000)
br label %30
25: ; preds = %17
%26 = load i32, i32* %4
%27 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 0
%28 = call i32 @kwboot_tty_recv(i32 %26, i8* %27, i32 16, i32 1)
store i32 %28, i32* %6
%29 = call i32 (...) @kwboot_spinner()
br label %30
30: ; preds = %25, %23
%31 = load i32, i32* %6
%32 = icmp ne i32 %31, 0
br i1 %32, label %17, label %33
33: ; preds = %30
%34 = call i32 @kwboot_printv(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%35 = load i32, i32* %6
store i32 %35, i32* %3
br label %36
36: ; preds = %33, %14
%37 = load i32, i32* %3
ret i32 %37
}

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