#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*/ buf;

/* Variables and functions */
int /*<<< orphan*/ input_flush_serial_port();
int /*<<< orphan*/ printf(char *, int);
int read_serial_port_timeout(unsigned char *, int, char *, double);
int /*<<< orphan*/ write_serial_port(char *, int);
int bootloader_version(char *str, int str_size) {
  char buf[256];
  int n, i, j;
  *str = '\0';
  input_flush_serial_port();
  write_serial_port("V#", 2);
  n = read_serial_port_timeout((unsigned char *)buf, sizeof(buf) - 1, ">", 0.2);
  if (n < 0) {
    printf("error reading, n=%d\n", n);
    return -1;
  } else {
    if (n > 0 && buf[n - 1] == '>') {
      n--;
      if (n > str_size - 1)
        n = str_size - 1;
      for (i = j = 0; i < n; i++) {
        if (buf[i] == '\r' || buf[i] == '\n')
          continue;
        str[j++] = buf[i];
      }
      str[j] = '\0';
      return 0;
    }
  }
  return -2;
}


x86 assembly (clang, -O0):
<bootloader_version>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
mov    $0x0,%al
call   1060 <input_flush_serial_port@plt>
lea    0xea1(%rip),%rdi
mov    $0x2,%esi
call   1030 <write_serial_port@plt>
lea    -0x120(%rbp),%rdi
mov    $0xff,%esi
lea    0xe87(%rip),%rdx
movsd  0xe74(%rip),%xmm0
call   1050 <read_serial_port_timeout@plt>
mov    %eax,-0x124(%rbp)
cmpl   $0x0,-0x124(%rbp)
jge    11c2 <bootloader_version+0x82>
mov    -0x124(%rbp),%esi
lea    0xe5c(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d9 <bootloader_version+0x199>
cmpl   $0x0,-0x124(%rbp)
jle    12cd <bootloader_version+0x18d>
mov    -0x124(%rbp),%eax
sub    $0x1,%eax
cltq
movsbl -0x120(%rbp,%rax,1),%eax
ff
cmp    $0x3e,%eax
jne    12cd <bootloader_version+0x18d>
mov    -0x124(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x124(%rbp)
mov    -0x124(%rbp),%eax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jle    121a <bootloader_version+0xda>
mov    -0x14(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x124(%rbp)
movl   $0x0,-0x12c(%rbp)
movl   $0x0,-0x128(%rbp)
mov    -0x128(%rbp),%eax
cmp    -0x124(%rbp),%eax
jge    12b2 <bootloader_version+0x172>
movslq -0x128(%rbp),%rax
movsbl -0x120(%rbp,%rax,1),%eax
ff
cmp    $0xd,%eax
je     1270 <bootloader_version+0x130>
movslq -0x128(%rbp),%rax
movsbl -0x120(%rbp,%rax,1),%eax
ff
cmp    $0xa,%eax
jne    1275 <bootloader_version+0x135>
jmp    129e <bootloader_version+0x15e>
movslq -0x128(%rbp),%rax
mov    -0x120(%rbp,%rax,1),%dl
mov    -0x10(%rbp),%rax
mov    -0x12c(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x12c(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x128(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x128(%rbp)
jmp    122e <bootloader_version+0xee>
mov    -0x10(%rbp),%rax
movslq -0x12c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
movl   $0x0,-0x4(%rbp)
jmp    12d9 <bootloader_version+0x199>
jmp    12d2 <bootloader_version+0x192>
movl   $0xfffffffe,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bootloader_version>:
push   %rbp
push   %r14
push   %rbx
sub    $0x100,%rsp
mov    %esi,%ebp
mov    %rdi,%r14
movb   $0x0,(%rdi)
xor    %eax,%eax
call   1060 <input_flush_serial_port@plt>
lea    0xea7(%rip),%rdi
mov    $0x2,%esi
call   1030 <write_serial_port@plt>
lea    0xe99(%rip),%rdx
mov    %rsp,%rdi
movsd  0xe83(%rip),%xmm0
mov    $0xff,%esi
call   1050 <read_serial_port_timeout@plt>
mov    %eax,%esi
test   %eax,%eax
js     11e1 <bootloader_version+0xa1>
mov    $0xfffffffe,%eax
je     11f2 <bootloader_version+0xb2>
lea    -0x1(%rsi),%ecx
cmpb   $0x3e,(%rsp,%rcx,1)
jne    11f2 <bootloader_version+0xb2>
cmp    %ebp,%esi
cmovg  %ebp,%esi
xor    %eax,%eax
mov    $0x0,%ecx
cmp    $0x2,%esi
jl     11d7 <bootloader_version+0x97>
add    $0xffffffff,%esi
cmp    $0x1,%esi
jne    11fe <bootloader_version+0xbe>
xor    %edx,%edx
xor    %ecx,%ecx
test   $0x1,%sil
je     11d7 <bootloader_version+0x97>
mov    (%rsp,%rdx,1),%dl
cmp    $0xa,%dl
je     11d7 <bootloader_version+0x97>
cmp    $0xd,%dl
je     11d7 <bootloader_version+0x97>
movslq %ecx,%rsi
add    $0x1,%ecx
mov    %dl,(%r14,%rsi,1)
movslq %ecx,%rcx
movb   $0x0,(%r14,%rcx,1)
jmp    11f2 <bootloader_version+0xb2>
lea    0xe25(%rip),%rdi
call   1040 <printf@plt>
mov    $0xffffffff,%eax
add    $0x100,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    %esi,%edi
and    $0xfffffffe,%edi
xor    %edx,%edx
xor    %ecx,%ecx
jmp    1219 <bootloader_version+0xd9>
nopl   0x0(%rax)
add    $0x2,%rdx
cmp    %rdx,%rdi
je     11ba <bootloader_version+0x7a>
movzbl (%rsp,%rdx,1),%ebx
cmp    $0xa,%bl
je     1231 <bootloader_version+0xf1>
cmp    $0xd,%bl
je     1231 <bootloader_version+0xf1>
movslq %ecx,%rbp
add    $0x1,%ecx
mov    %bl,(%r14,%rbp,1)
movzbl 0x1(%rsp,%rdx,1),%ebx
cmp    $0xa,%bl
je     1210 <bootloader_version+0xd0>
cmp    $0xd,%bl
je     1210 <bootloader_version+0xd0>
movslq %ecx,%rbp
add    $0x1,%ecx
mov    %bl,(%r14,%rbp,1)
jmp    1210 <bootloader_version+0xd0>

LLVM IR (-O0):
@.str = [3 x i8] c"V#\00"
@.str.1 = [2 x i8] c">\00"
@.str.2 = [21 x i8] c"error reading, n=%d\0A\00"
define i32 @bootloader_version(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca [256 x i8]
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%10 = load i8*, i8** %4
store i8 0, i8* %10
%11 = call i32 (...) @input_flush_serial_port()
%12 = call i32 @write_serial_port(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 2)
%13 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%14 = call i32 @read_serial_port_timeout(i8* %13, i32 255, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), double 2.000000e-01)
store i32 %14, i32* %7
%15 = load i32, i32* %7
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %2
%18 = load i32, i32* %7
%19 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i32 %18)
store i32 -1, i32* %3
br label %81
20: ; preds = %2
%21 = load i32, i32* %7
%22 = icmp sgt i32 %21, 0
br i1 %22, label %23, label %79
23: ; preds = %20
%24 = load i32, i32* %7
%25 = sub nsw i32 %24, 1
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 %26
%28 = load i8, i8* %27
%29 = sext i8 %28 to i32
%30 = icmp eq i32 %29, 62
br i1 %30, label %31, label %79
31: ; preds = %23
%32 = load i32, i32* %7
%33 = add nsw i32 %32, -1
store i32 %33, i32* %7
%34 = load i32, i32* %7
%35 = load i32, i32* %5
%36 = sub nsw i32 %35, 1
%37 = icmp sgt i32 %34, %36
br i1 %37, label %38, label %41
38: ; preds = %31
%39 = load i32, i32* %5
%40 = sub nsw i32 %39, 1
store i32 %40, i32* %7
br label %41
41: ; preds = %38, %31
store i32 0, i32* %9
store i32 0, i32* %8
br label %42
42: ; preds = %71, %41
%43 = load i32, i32* %8
%44 = load i32, i32* %7
%45 = icmp slt i32 %43, %44
br i1 %45, label %46, label %74
46: ; preds = %42
%47 = load i32, i32* %8
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 %48
%50 = load i8, i8* %49
%51 = sext i8 %50 to i32
%52 = icmp eq i32 %51, 13
br i1 %52, label %60, label %53
53: ; preds = %46
%54 = load i32, i32* %8
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 %55
%57 = load i8, i8* %56
%58 = sext i8 %57 to i32
%59 = icmp eq i32 %58, 10
br i1 %59, label %60, label %61
60: ; preds = %53, %46
br label %71
61: ; preds = %53
%62 = load i32, i32* %8
%63 = sext i32 %62 to i64
%64 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 %63
%65 = load i8, i8* %64
%66 = load i8*, i8** %4
%67 = load i32, i32* %9
%68 = add nsw i32 %67, 1
store i32 %68, i32* %9
%69 = sext i32 %67 to i64
%70 = getelementptr inbounds i8, i8* %66, i64 %69
store i8 %65, i8* %70
br label %71
71: ; preds = %61, %60
%72 = load i32, i32* %8
%73 = add nsw i32 %72, 1
store i32 %73, i32* %8
br label %42
74: ; preds = %42
%75 = load i8*, i8** %4
%76 = load i32, i32* %9
%77 = sext i32 %76 to i64
%78 = getelementptr inbounds i8, i8* %75, i64 %77
store i8 0, i8* %78
store i32 0, i32* %3
br label %81
79: ; preds = %23, %20
br label %80
80: ; preds = %79
store i32 -2, i32* %3
br label %81
81: ; preds = %80, %74, %17
%82 = load i32, i32* %3
ret i32 %82
}

LLVM IR (-O2):
@.str = [3 x i8] c"V#\00"
@.str.1 = [2 x i8] c">\00"
@.str.2 = [21 x i8] c"error reading, n=%d\0A\00"
define i32 @bootloader_version(i8* nocapture writeonly %0, i32 %1) {
%3 = alloca [256 x i8]
%4 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
store i8 0, i8* %0
%5 = tail call i32 (...) @input_flush_serial_port()
%6 = tail call i32 @write_serial_port(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 2)
%7 = call i32 @read_serial_port_timeout(i8* %4, i32 255, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), double 2.000000e-01)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i32 %7)
br label %70
11: ; preds = %2
%12 = icmp eq i32 %7, 0
br i1 %12, label %70, label %13
13: ; preds = %11
%14 = add nsw i32 %7, -1
%15 = zext i32 %14 to i64
%16 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 %15
%17 = load i8, i8* %16
%18 = icmp eq i8 %17, 62
br i1 %18, label %19, label %70
19: ; preds = %13
%20 = icmp sgt i32 %7, %1
%21 = select i1 %20, i32 %1, i32 %7
%22 = icmp sgt i32 %21, 1
br i1 %22, label %23, label %66
23: ; preds = %19
%24 = add nsw i32 %21, -1
%25 = zext i32 %24 to i64
%26 = and i64 %25, 1
%27 = icmp eq i32 %24, 1
br i1 %27, label %54, label %28
28: ; preds = %23
%29 = and i64 %25, 4294967294
br label %30
30: ; preds = %49, %28
%31 = phi i64 [ 0, %28 ], [ %51, %49 ]
%32 = phi i32 [ 0, %28 ], [ %50, %49 ]
%33 = phi i64 [ 0, %28 ], [ %52, %49 ]
%34 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 %31
%35 = load i8, i8* %34
switch i8 %35, label %36 [
i8 13, label %40
i8 10, label %40
]
36: ; preds = %30
%37 = add nsw i32 %32, 1
%38 = sext i32 %32 to i64
%39 = getelementptr inbounds i8, i8* %0, i64 %38
store i8 %35, i8* %39
br label %40
40: ; preds = %30, %30, %36
%41 = phi i32 [ %32, %30 ], [ %37, %36 ], [ %32, %30 ]
%42 = or i64 %31, 1
%43 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 %42
%44 = load i8, i8* %43
switch i8 %44, label %45 [
i8 13, label %49
i8 10, label %49
]
45: ; preds = %40
%46 = add nsw i32 %41, 1
%47 = sext i32 %41 to i64
%48 = getelementptr inbounds i8, i8* %0, i64 %47
store i8 %44, i8* %48
br label %49
49: ; preds = %45, %40, %40
%50 = phi i32 [ %41, %40 ], [ %46, %45 ], [ %41, %40 ]
%51 = add nuw nsw i64 %31, 2
%52 = add i64 %33, 2
%53 = icmp eq i64 %52, %29
br i1 %53, label %54, label %30
54: ; preds = %49, %23
%55 = phi i32 [ undef, %23 ], [ %50, %49 ]
%56 = phi i64 [ 0, %23 ], [ %51, %49 ]
%57 = phi i32 [ 0, %23 ], [ %50, %49 ]
%58 = icmp eq i64 %26, 0
br i1 %58, label %66, label %59
59: ; preds = %54
%60 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 %56
%61 = load i8, i8* %60
switch i8 %61, label %62 [
i8 13, label %66
i8 10, label %66
]
62: ; preds = %59
%63 = add nsw i32 %57, 1
%64 = sext i32 %57 to i64
%65 = getelementptr inbounds i8, i8* %0, i64 %64
store i8 %61, i8* %65
br label %66
66: ; preds = %54, %62, %59, %59, %19
%67 = phi i32 [ 0, %19 ], [ %55, %54 ], [ %57, %59 ], [ %63, %62 ], [ %57, %59 ]
%68 = sext i32 %67 to i64
%69 = getelementptr inbounds i8, i8* %0, i64 %68
store i8 0, i8* %69
br label %70
70: ; preds = %13, %11, %66, %9
%71 = phi i32 [ -1, %9 ], [ 0, %66 ], [ -2, %11 ], [ -2, %13 ]
ret i32 %71
}