#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 */
typedef int /*<<< orphan*/ spinner;

/* Variables and functions */
char _get_char(int);
int /*<<< orphan*/ _put_char(int, char);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ stdout;
scalar_t__ tcdrain(int);
int /*<<< orphan*/ write(int, char *, int);
void _connect_bootloader(int fd) {
  const char spinner[] = "|/-\\";
  unsigned tries = 0;
  while (_get_char(fd) != 'b') {
    printf("connecting [%c]", spinner[tries % (sizeof(spinner) - 1)]);
    _put_char(fd, '?');
    ++tries;
    if (tcdrain(fd)) {
      puts("\rSerial adapter disconnected");
      close(fd);
      exit(1);
    }
    printf("\r");
    fflush(stdout);
  }
  do {
    write(fd, "B\n", 2);
  } while (_get_char(fd) != '>');
  puts("\rconnected.");
}


x86 assembly (clang, -O0):
<_connect_bootloader>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    0xe5f(%rip),%eax
mov    %eax,-0x9(%rbp)
mov    0xe5a(%rip),%al
mov    %al,-0x5(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x4(%rbp),%edi
call   10a0 <_get_char@plt>
movsbl %al,%eax
cmp    $0x62,%eax
je     1249 <_connect_bootloader+0xb9>
mov    -0x10(%rbp),%eax
and    $0x3,%rax
movsbl -0x9(%rbp,%rax,1),%esi
lea    0xe2a(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x4(%rbp),%edi
mov    $0x3f,%esi
call   1030 <_put_char@plt>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
mov    -0x4(%rbp),%edi
call   1050 <tcdrain@plt>
cmp    $0x0,%rax
je     1228 <_connect_bootloader+0x98>
lea    0xe04(%rip),%rdi
call   1040 <puts@plt>
mov    -0x4(%rbp),%edi
call   1080 <close@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    0xe03(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    0x2da3(%rip),%rax
mov    (%rax),%edi
call   1090 <fflush@plt>
jmp    11b4 <_connect_bootloader+0x24>
jmp    124e <_connect_bootloader+0xbe>
mov    -0x4(%rbp),%edi
lea    0xddc(%rip),%rsi
mov    $0x2,%edx
call   1060 <write@plt>
mov    -0x4(%rbp),%edi
call   10a0 <_get_char@plt>
movsbl %al,%eax
cmp    $0x3e,%eax
jne    124e <_connect_bootloader+0xbe>
lea    0xdba(%rip),%rdi
call   1040 <puts@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_connect_bootloader>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edi,%ebp
call   10b0 <_get_char@plt>
cmp    $0x62,%al
je     121d <_connect_bootloader+0x7d>
lea    0xe46(%rip),%r15
lea    0xe44(%rip),%r14
mov    0x2e18(%rip),%r12
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
mov    %ebx,%eax
and    $0x3,%eax
movsbl (%rax,%r15,1),%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1080 <printf@plt>
mov    %ebp,%edi
mov    $0x3f,%esi
call   1040 <_put_char@plt>
mov    %ebp,%edi
call   1060 <tcdrain@plt>
test   %rax,%rax
jne    125e <_connect_bootloader+0xbe>
add    $0x1,%ebx
mov    $0xd,%edi
call   1030 <putchar@plt>
mov    (%r12),%edi
call   10a0 <fflush@plt>
mov    %ebp,%edi
call   10b0 <_get_char@plt>
cmp    $0x62,%al
jne    11d0 <_connect_bootloader+0x30>
lea    0xe0e(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x2,%edx
call   1070 <write@plt>
mov    %ebp,%edi
call   10b0 <_get_char@plt>
cmp    $0x3e,%al
jne    1230 <_connect_bootloader+0x90>
lea    0xde4(%rip),%rdi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <puts@plt>
lea    0xdb0(%rip),%rdi
call   1050 <puts@plt>
mov    %ebp,%edi
call   1090 <close@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@__const._connect_bootloader.spinner = [5 x i8] c"|/-\\\00"
@.str = [16 x i8] c"connecting [%c]\00"
@.str.1 = [29 x i8] c"\0DSerial adapter disconnected\00"
@.str.2 = [2 x i8] c"\0D\00"
@stdout = global i32 0
@.str.3 = [3 x i8] c"B\0A\00"
@.str.4 = [12 x i8] c"\0Dconnected.\00"
define void @_connect_bootloader(i32 %0) {
%2 = alloca i32
%3 = alloca [5 x i8]
%4 = alloca i32
store i32 %0, i32* %2
%5 = bitcast [5 x i8]* %3 to i8*
store i32 0, i32* %4
br label %6
6: ; preds = %31, %1
%7 = load i32, i32* %2
%8 = call signext i8 @_get_char(i32 %7)
%9 = sext i8 %8 to i32
%10 = icmp ne i32 %9, 98
br i1 %10, label %11, label %35
11: ; preds = %6
%12 = load i32, i32* %4
%13 = zext i32 %12 to i64
%14 = urem i64 %13, 4
%15 = getelementptr inbounds [5 x i8], [5 x i8]* %3, i64 0, i64 %14
%16 = load i8, i8* %15
%17 = sext i8 %16 to i32
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %17)
%19 = load i32, i32* %2
%20 = call i32 @_put_char(i32 %19, i8 signext 63)
%21 = load i32, i32* %4
%22 = add i32 %21, 1
store i32 %22, i32* %4
%23 = load i32, i32* %2
%24 = call i64 @tcdrain(i32 %23)
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %31
26: ; preds = %11
%27 = call i32 @puts(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
%28 = load i32, i32* %2
%29 = call i32 @close(i32 %28)
%30 = call i32 @exit(i32 1)
unreachable
31: ; preds = %11
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%33 = load i32, i32* @stdout
%34 = call i32 @fflush(i32 %33)
br label %6
35: ; preds = %6
br label %36
36: ; preds = %39, %35
%37 = load i32, i32* %2
%38 = call i32 @write(i32 %37, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 2)
br label %39
39: ; preds = %36
%40 = load i32, i32* %2
%41 = call signext i8 @_get_char(i32 %40)
%42 = sext i8 %41 to i32
%43 = icmp ne i32 %42, 62
br i1 %43, label %36, label %44
44: ; preds = %39
%45 = call i32 @puts(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
ret void
}

LLVM IR (-O2):
@__const._connect_bootloader.spinner = [5 x i8] c"|/-\\\00"
@.str = [16 x i8] c"connecting [%c]\00"
@.str.1 = [29 x i8] c"\0DSerial adapter disconnected\00"
@stdout = global i32 0
@.str.3 = [3 x i8] c"B\0A\00"
@.str.4 = [12 x i8] c"\0Dconnected.\00"
define void @_connect_bootloader(i32 %0) {
%2 = tail call signext i8 @_get_char(i32 %0)
%3 = icmp eq i8 %2, 98
br i1 %3, label %26, label %4
4: ; preds = %1, %19
%5 = phi i32 [ %20, %19 ], [ 0, %1 ]
%6 = and i32 %5, 3
%7 = zext i32 %6 to i64
%8 = getelementptr inbounds [5 x i8], [5 x i8]* @__const._connect_bootloader.spinner, i64 0, i64 %7
%9 = load i8, i8* %8
%10 = sext i8 %9 to i32
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = tail call i32 @_put_char(i32 %0, i8 signext 63)
%13 = tail call i64 @tcdrain(i32 %0)
%14 = icmp eq i64 %13, 0
br i1 %14, label %19, label %15
15: ; preds = %4
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
%17 = tail call i32 @close(i32 %0)
%18 = tail call i32 @exit(i32 1)
unreachable
19: ; preds = %4
%20 = add i32 %5, 1
%21 = tail call i32 @putchar(i32 13)
%22 = load i32, i32* @stdout
%23 = tail call i32 @fflush(i32 %22)
%24 = tail call signext i8 @_get_char(i32 %0)
%25 = icmp eq i8 %24, 98
br i1 %25, label %26, label %4
26: ; preds = %19, %1
br label %27
27: ; preds = %26, %27
%28 = tail call i32 @write(i32 %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 2)
%29 = tail call signext i8 @_get_char(i32 %0)
%30 = icmp eq i8 %29, 62
br i1 %30, label %31, label %27
31: ; preds = %27
%32 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
ret void
}