#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;
scalar_t__ isprint(char);
int /*<<< orphan*/ kwboot_printv(char *, ...);
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, unsigned num_nacks) {
  int rc;
  char c;
  unsigned saw_nacks = 0;
  if (msg == NULL)
    kwboot_printv("Please reboot the target into UART boot mode...");
  else
    kwboot_printv("Sending boot message. Please reboot the target...");
  rc = tcflush(tty, TCIOFLUSH);
  if (rc)
    return rc;
  do {
    rc = kwboot_tty_send(tty, msg, 8);
    if (rc) {
      usleep(1000 * 1000);
      continue;
    }
    rc = kwboot_tty_recv(tty, &c, 1, 1);
    while (!rc && c != 21) {
      if (c == '\\')
        kwboot_printv("\\\\", c);
      else if (isprint(c) || c == '\r' || c == '\n')
        kwboot_printv("%c", c);
      else
        kwboot_printv("\\x%02hhx", c);
      rc = kwboot_tty_recv(tty, &c, 1, 1);
      saw_nacks = 0;
    }
  } while (rc || c != 21 || (++saw_nacks < num_nacks));
  kwboot_printv("\nGot expected NAKs\n");
  return rc;
}


x86 assembly (clang, -O0):
<kwboot_bootmsg>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1197 <kwboot_bootmsg+0x37>
lea    0xe75(%rip),%rdi
mov    $0x0,%al
call   1050 <kwboot_printv@plt>
jmp    11a5 <kwboot_bootmsg+0x45>
lea    0xe92(%rip),%rdi
mov    $0x0,%al
call   1050 <kwboot_printv@plt>
mov    -0x8(%rbp),%edi
mov    0x2e31(%rip),%rax
mov    (%rax),%esi
call   1040 <tcflush@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     11ce <kwboot_bootmsg+0x6e>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    133a <kwboot_bootmsg+0x1da>
jmp    11d3 <kwboot_bootmsg+0x73>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x8,%edx
call   1060 <kwboot_tty_send@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     1200 <kwboot_bootmsg+0xa0>
mov    $0xf4240,%edi
call   1080 <usleep@plt>
jmp    12e8 <kwboot_bootmsg+0x188>
mov    -0x8(%rbp),%edi
lea    -0x19(%rbp),%rsi
mov    $0x1,%ecx
mov    %ecx,%edx
call   1030 <kwboot_tty_recv@plt>
mov    %eax,-0x18(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x18(%rbp)
mov    %al,-0x21(%rbp)
jne    1232 <kwboot_bootmsg+0xd2>
movsbl -0x19(%rbp),%eax
cmp    $0x15,%eax
setne  %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    1242 <kwboot_bootmsg+0xe2>
jmp    12e3 <kwboot_bootmsg+0x183>
movsbl -0x19(%rbp),%eax
cmp    $0x5c,%eax
jne    1266 <kwboot_bootmsg+0x106>
movsbl -0x19(%rbp),%esi
lea    0xe08(%rip),%rdi
mov    $0x0,%al
call   1050 <kwboot_printv@plt>
jmp    12c1 <kwboot_bootmsg+0x161>
movsbl -0x19(%rbp),%edi
call   1070 <isprint@plt>
cmp    $0x0,%rax
jne    1293 <kwboot_bootmsg+0x133>
movsbl -0x19(%rbp),%eax
cmp    $0xd,%eax
je     1293 <kwboot_bootmsg+0x133>
movsbl -0x19(%rbp),%eax
cmp    $0xa,%eax
jne    12aa <kwboot_bootmsg+0x14a>
movsbl -0x19(%rbp),%esi
lea    0xdc7(%rip),%rdi
mov    $0x0,%al
call   1050 <kwboot_printv@plt>
jmp    12bc <kwboot_bootmsg+0x15c>
movsbl -0x19(%rbp),%esi
lea    0xdb3(%rip),%rdi
mov    $0x0,%al
call   1050 <kwboot_printv@plt>
jmp    12c1 <kwboot_bootmsg+0x161>
mov    -0x8(%rbp),%edi
lea    -0x19(%rbp),%rsi
mov    $0x1,%ecx
mov    %ecx,%edx
call   1030 <kwboot_tty_recv@plt>
mov    %eax,-0x18(%rbp)
movl   $0x0,-0x20(%rbp)
jmp    1216 <kwboot_bootmsg+0xb6>
jmp    12e8 <kwboot_bootmsg+0x188>
mov    $0x1,%al
cmpl   $0x0,-0x18(%rbp)
mov    %al,-0x22(%rbp)
jne    131b <kwboot_bootmsg+0x1bb>
movsbl -0x19(%rbp),%ecx
mov    $0x1,%al
cmp    $0x15,%ecx
mov    %al,-0x22(%rbp)
jne    131b <kwboot_bootmsg+0x1bb>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
cmp    -0x14(%rbp),%eax
setb   %al
mov    %al,-0x22(%rbp)
mov    -0x22(%rbp),%al
test   $0x1,%al
jne    11d3 <kwboot_bootmsg+0x73>
lea    0xd44(%rip),%rdi
mov    $0x0,%al
call   1050 <kwboot_printv@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%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    %edx,%r15d
mov    %rsi,%rbx
mov    %edi,%ebp
test   %rsi,%rsi
lea    0xe83(%rip),%rax
lea    0xeac(%rip),%rdi
cmove  %rax,%rdi
xor    %eax,%eax
call   1050 <kwboot_printv@plt>
mov    0x2e4a(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1040 <tcflush@plt>
mov    %eax,%r14d
test   %eax,%eax
jne    12a6 <kwboot_bootmsg+0x146>
mov    %r15d,0x4(%rsp)
lea    0x3(%rsp),%r15
lea    0xea7(%rip),%r12
lea    0xea3(%rip),%r14
xor    %r13d,%r13d
jmp    11da <kwboot_bootmsg+0x7a>
nopw   0x0(%rax,%rax,1)
mov    $0xf4240,%edi
call   1080 <usleep@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x8,%edx
call   1060 <kwboot_tty_send@plt>
test   %eax,%eax
jne    11d0 <kwboot_bootmsg+0x70>
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x1,%edx
mov    $0x1,%ecx
call   1030 <kwboot_tty_recv@plt>
jmp    1236 <kwboot_bootmsg+0xd6>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r12,%rdi
mov    $0x5c,%esi
xor    %eax,%eax
call   1050 <kwboot_printv@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x1,%edx
mov    $0x1,%ecx
call   1030 <kwboot_tty_recv@plt>
xor    %r13d,%r13d
movzbl 0x3(%rsp),%ecx
test   %eax,%eax
jne    1275 <kwboot_bootmsg+0x115>
cmp    $0x15,%cl
je     1275 <kwboot_bootmsg+0x115>
cmp    $0x5c,%cl
je     1210 <kwboot_bootmsg+0xb0>
movsbl %cl,%edi
call   1070 <isprint@plt>
movsbl 0x3(%rsp),%esi
test   %rax,%rax
je     1260 <kwboot_bootmsg+0x100>
mov    %r14,%rdi
jmp    1218 <kwboot_bootmsg+0xb8>
cmp    $0xd,%sil
je     125b <kwboot_bootmsg+0xfb>
cmp    $0xa,%sil
je     125b <kwboot_bootmsg+0xfb>
lea    0xdf5(%rip),%rdi
jmp    1218 <kwboot_bootmsg+0xb8>
test   %eax,%eax
jne    11da <kwboot_bootmsg+0x7a>
cmp    $0x15,%cl
jne    11da <kwboot_bootmsg+0x7a>
add    $0x1,%r13d
cmp    0x4(%rsp),%r13d
jb     11da <kwboot_bootmsg+0x7a>
lea    0xdd5(%rip),%rdi
xor    %r14d,%r14d
xor    %eax,%eax
call   1050 <kwboot_printv@plt>
mov    %r14d,%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 = [3 x i8] c"\\\\\00"
@.str.3 = [3 x i8] c"%c\00"
@.str.4 = [9 x i8] c"\\x%02hhx\00"
@.str.5 = [20 x i8] c"\0AGot expected NAKs\0A\00"
define i32 @kwboot_bootmsg(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8
%10 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 0, i32* %10
%11 = load i8*, i8** %6
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %15
13: ; preds = %3
%14 = call i32 (i8*, ...) @kwboot_printv(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str, i64 0, i64 0))
br label %17
15: ; preds = %3
%16 = call i32 (i8*, ...) @kwboot_printv(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.1, i64 0, i64 0))
br label %17
17: ; preds = %15, %13
%18 = load i32, i32* %5
%19 = load i32, i32* @TCIOFLUSH
%20 = call i32 @tcflush(i32 %18, i32 %19)
store i32 %20, i32* %8
%21 = load i32, i32* %8
%22 = icmp ne i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %17
%24 = load i32, i32* %8
store i32 %24, i32* %4
br label %96
25: ; preds = %17
br label %26
26: ; preds = %91, %25
%27 = load i32, i32* %5
%28 = load i8*, i8** %6
%29 = call i32 @kwboot_tty_send(i32 %27, i8* %28, i32 8)
store i32 %29, i32* %8
%30 = load i32, i32* %8
%31 = icmp ne i32 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %26
%33 = call i32 @usleep(i32 1000000)
br label %79
34: ; preds = %26
%35 = load i32, i32* %5
%36 = call i32 @kwboot_tty_recv(i32 %35, i8* %9, i32 1, i32 1)
store i32 %36, i32* %8
br label %37
37: ; preds = %75, %34
%38 = load i32, i32* %8
%39 = icmp ne i32 %38, 0
br i1 %39, label %44, label %40
40: ; preds = %37
%41 = load i8, i8* %9
%42 = sext i8 %41 to i32
%43 = icmp ne i32 %42, 21
br label %44
44: ; preds = %40, %37
%45 = phi i1 [ false, %37 ], [ %43, %40 ]
br i1 %45, label %46, label %78
46: ; preds = %44
%47 = load i8, i8* %9
%48 = sext i8 %47 to i32
%49 = icmp eq i32 %48, 92
br i1 %49, label %50, label %54
50: ; preds = %46
%51 = load i8, i8* %9
%52 = sext i8 %51 to i32
%53 = call i32 (i8*, ...) @kwboot_printv(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %52)
br label %75
54: ; preds = %46
%55 = load i8, i8* %9
%56 = call i64 @isprint(i8 signext %55)
%57 = icmp ne i64 %56, 0
br i1 %57, label %66, label %58
58: ; preds = %54
%59 = load i8, i8* %9
%60 = sext i8 %59 to i32
%61 = icmp eq i32 %60, 13
br i1 %61, label %66, label %62
62: ; preds = %58
%63 = load i8, i8* %9
%64 = sext i8 %63 to i32
%65 = icmp eq i32 %64, 10
br i1 %65, label %66, label %70
66: ; preds = %62, %58, %54
%67 = load i8, i8* %9
%68 = sext i8 %67 to i32
%69 = call i32 (i8*, ...) @kwboot_printv(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 %68)
br label %74
70: ; preds = %62
%71 = load i8, i8* %9
%72 = sext i8 %71 to i32
%73 = call i32 (i8*, ...) @kwboot_printv(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i32 %72)
br label %74
74: ; preds = %70, %66
br label %75
75: ; preds = %74, %50
%76 = load i32, i32* %5
%77 = call i32 @kwboot_tty_recv(i32 %76, i8* %9, i32 1, i32 1)
store i32 %77, i32* %8
store i32 0, i32* %10
br label %37
78: ; preds = %44
br label %79
79: ; preds = %78, %32
%80 = load i32, i32* %8
%81 = icmp ne i32 %80, 0
br i1 %81, label %91, label %82
82: ; preds = %79
%83 = load i8, i8* %9
%84 = sext i8 %83 to i32
%85 = icmp ne i32 %84, 21
br i1 %85, label %91, label %86
86: ; preds = %82
%87 = load i32, i32* %10
%88 = add i32 %87, 1
store i32 %88, i32* %10
%89 = load i32, i32* %7
%90 = icmp ult i32 %88, %89
br label %91
91: ; preds = %86, %82, %79
%92 = phi i1 [ true, %82 ], [ true, %79 ], [ %90, %86 ]
br i1 %92, label %26, label %93
93: ; preds = %91
%94 = call i32 (i8*, ...) @kwboot_printv(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0))
%95 = load i32, i32* %8
store i32 %95, i32* %4
br label %96
96: ; preds = %93, %23
%97 = load i32, i32* %4
ret i32 %97
}

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 = [3 x i8] c"\\\\\00"
@.str.3 = [3 x i8] c"%c\00"
@.str.4 = [9 x i8] c"\\x%02hhx\00"
@.str.5 = [20 x i8] c"\0AGot expected NAKs\0A\00"
define i32 @kwboot_bootmsg(i32 %0, i8* %1, i32 %2) {
%4 = alloca i8
%5 = icmp eq i8* %1, null
%6 = select i1 %5, 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)
%7 = tail call i32 (i8*, ...) @kwboot_printv(i8* %6)
%8 = load i32, i32* @TCIOFLUSH
%9 = tail call i32 @tcflush(i32 %0, i32 %8)
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %60
11: ; preds = %3, %53
%12 = phi i32 [ %54, %53 ], [ 0, %3 ]
br label %13
13: ; preds = %11, %16
%14 = call i32 @kwboot_tty_send(i32 %0, i8* %1, i32 8)
%15 = icmp eq i32 %14, 0
br i1 %15, label %18, label %16
16: ; preds = %13
%17 = call i32 @usleep(i32 1000000)
br label %13
18: ; preds = %13
%19 = call i32 @kwboot_tty_recv(i32 %0, i8* %4, i32 1, i32 1)
%20 = icmp eq i32 %19, 0
%21 = load i8, i8* %4
%22 = icmp ne i8 %21, 21
%23 = select i1 %20, i1 %22, i1 false
br i1 %23, label %24, label %46
24: ; preds = %18, %40
%25 = phi i8 [ %43, %40 ], [ %21, %18 ]
%26 = icmp eq i8 %25, 92
br i1 %26, label %27, label %29
27: ; preds = %24
%28 = call i32 (i8*, ...) @kwboot_printv(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 92)
br label %40
29: ; preds = %24
%30 = call i64 @isprint(i8 signext %25)
%31 = freeze i64 %30
%32 = icmp eq i64 %31, 0
%33 = load i8, i8* %4
%34 = sext i8 %33 to i32
br i1 %32, label %35, label %36
35: ; preds = %29
switch i8 %33, label %38 [
i8 13, label %36
i8 10, label %36
]
36: ; preds = %35, %35, %29
%37 = call i32 (i8*, ...) @kwboot_printv(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 %34)
br label %40
38: ; preds = %35
%39 = call i32 (i8*, ...) @kwboot_printv(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i32 %34)
br label %40
40: ; preds = %36, %38, %27
%41 = call i32 @kwboot_tty_recv(i32 %0, i8* %4, i32 1, i32 1)
%42 = icmp eq i32 %41, 0
%43 = load i8, i8* %4
%44 = icmp ne i8 %43, 21
%45 = select i1 %42, i1 %44, i1 false
br i1 %45, label %24, label %46
46: ; preds = %40, %18
%47 = phi i8 [ %21, %18 ], [ %43, %40 ]
%48 = phi i32 [ %19, %18 ], [ %41, %40 ]
%49 = phi i32 [ %12, %18 ], [ 0, %40 ]
%50 = icmp ne i32 %48, 0
%51 = icmp ne i8 %47, 21
%52 = select i1 %50, i1 true, i1 %51
br i1 %52, label %53, label %55
53: ; preds = %46, %55
%54 = phi i32 [ %56, %55 ], [ %49, %46 ]
br label %11
55: ; preds = %46
%56 = add i32 %49, 1
%57 = icmp ult i32 %56, %2
br i1 %57, label %53, label %58
58: ; preds = %55
%59 = call i32 (i8*, ...) @kwboot_printv(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0))
br label %60
60: ; preds = %3, %58
%61 = phi i32 [ 0, %58 ], [ %9, %3 ]
ret i32 %61
}