#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...");
  do {
    char buf[16];
    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, buf, 16, 1000);
    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,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe8a(%rip),%rdi
call   1050 <kwboot_printv@plt>
mov    -0x4(%rbp),%edi
mov    0x2e5b(%rip),%rax
mov    (%rax),%esi
call   1040 <tcflush@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     119e <kwboot_debugmsg+0x3e>
jmp    11f5 <kwboot_debugmsg+0x95>
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     11cb <kwboot_debugmsg+0x6b>
mov    $0xf4240,%edi
call   1080 <usleep@plt>
jmp    11eb <kwboot_debugmsg+0x8b>
mov    -0x4(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x10,%edx
mov    $0x3e8,%ecx
call   1030 <kwboot_tty_recv@plt>
mov    %eax,-0x14(%rbp)
mov    $0x0,%al
call   1070 <kwboot_spinner@plt>
cmpl   $0x0,-0x14(%rbp)
jne    117b <kwboot_debugmsg+0x1b>
lea    0xe37(%rip),%rdi
call   1050 <kwboot_printv@plt>
mov    -0x14(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<kwboot_debugmsg>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r15
mov    %edi,%r12d
lea    0xe85(%rip),%rdi
call   1050 <kwboot_printv@plt>
mov    0x2e59(%rip),%r13
mov    %rsp,%r14
jmp    11b2 <kwboot_debugmsg+0x52>
nopl   0x0(%rax)
mov    %r12d,%edi
mov    %r14,%rsi
mov    $0x10,%edx
mov    $0x3e8,%ecx
call   1030 <kwboot_tty_recv@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1070 <kwboot_spinner@plt>
test   %ebp,%ebp
je     11eb <kwboot_debugmsg+0x8b>
mov    0x0(%r13),%esi
mov    %r12d,%edi
call   1040 <tcflush@plt>
mov    %eax,%ebx
test   %eax,%eax
jne    11eb <kwboot_debugmsg+0x8b>
mov    %r12d,%edi
mov    %r15,%rsi
mov    $0x8,%edx
call   1060 <kwboot_tty_send@plt>
test   %eax,%eax
je     1190 <kwboot_debugmsg+0x30>
mov    $0xf4240,%edi
call   1080 <usleep@plt>
mov    $0x1,%ebp
test   %ebp,%ebp
jne    11b2 <kwboot_debugmsg+0x52>
lea    0xe41(%rip),%rdi
call   1050 <kwboot_printv@plt>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
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 i8*
%5 = alloca i32
%6 = alloca [16 x i8]
store i32 %0, i32* %3
store i8* %1, i8** %4
%7 = call i32 @kwboot_printv(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str, i64 0, i64 0))
br label %8
8: ; preds = %28, %2
%9 = load i32, i32* %3
%10 = load i32, i32* @TCIOFLUSH
%11 = call i32 @tcflush(i32 %9, i32 %10)
store i32 %11, i32* %5
%12 = load i32, i32* %5
%13 = icmp ne i32 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %8
br label %31
15: ; preds = %8
%16 = load i32, i32* %3
%17 = load i8*, i8** %4
%18 = call i32 @kwboot_tty_send(i32 %16, i8* %17, i32 8)
store i32 %18, i32* %5
%19 = load i32, i32* %5
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %15
%22 = call i32 @usleep(i32 1000000)
br label %28
23: ; preds = %15
%24 = load i32, i32* %3
%25 = getelementptr inbounds [16 x i8], [16 x i8]* %6, i64 0, i64 0
%26 = call i32 @kwboot_tty_recv(i32 %24, i8* %25, i32 16, i32 1000)
store i32 %26, i32* %5
%27 = call i32 (...) @kwboot_spinner()
br label %28
28: ; preds = %23, %21
%29 = load i32, i32* %5
%30 = icmp ne i32 %29, 0
br i1 %30, label %8, label %31
31: ; preds = %28, %14
%32 = call i32 @kwboot_printv(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%33 = load i32, i32* %5
ret i32 %33
}

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 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 0
br label %6
6: ; preds = %19, %2
%7 = load i32, i32* @TCIOFLUSH
%8 = call i32 @tcflush(i32 %0, i32 %7)
%9 = icmp eq i32 %8, 0
br i1 %9, label %11, label %10
10: ; preds = %6
br label %22
11: ; preds = %6
%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* %5, i32 16, i32 1000)
%18 = call i32 (...) @kwboot_spinner()
br label %19
19: ; preds = %16, %14
%20 = phi i32 [ 1, %14 ], [ %17, %16 ]
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %6
22: ; preds = %19, %10
%23 = call i32 @kwboot_printv(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
ret i32 %8
}