int establish_comm_uart(void) {
  char buf[256];
  unsigned char c80 = 0x80;
  int n;
  input_flush_serial_port();
  write_serial_port(&c80, 1);
  usleep(5000);
  write_serial_port(&c80, 1);
  usleep(5000);
  write_serial_port("#", 1);
  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 {
    buf[n] = '\0';
    if (n > 0 && buf[n - 1] == '>')
      return 0;
  }
  return -2;
}


x86 assembly (clang, -O0):
<establish_comm_uart>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
movb   $0x80,-0x111(%rbp)
mov    $0x0,%al
call   1060 <input_flush_serial_port@plt>
lea    -0x111(%rbp),%rdi
mov    $0x1,%esi
mov    $0x0,%al
call   1030 <write_serial_port@plt>
mov    $0x1388,%edi
mov    $0x0,%al
call   1070 <usleep@plt>
lea    -0x111(%rbp),%rdi
mov    $0x1,%esi
mov    $0x0,%al
call   1030 <write_serial_port@plt>
mov    $0x1388,%edi
mov    $0x0,%al
call   1070 <usleep@plt>
lea    0xe5a(%rip),%rdi
mov    $0x1,%esi
mov    $0x0,%al
call   1030 <write_serial_port@plt>
lea    -0x110(%rbp),%rdi
mov    $0xff,%esi
lea    0xe3d(%rip),%rdx
movsd  0xe2b(%rip),%xmm0
mov    $0x1,%al
call   1050 <read_serial_port_timeout@plt>
mov    %eax,-0x118(%rbp)
cmpl   $0x0,-0x118(%rbp)
jge    120f <establish_comm_uart+0xbf>
mov    -0x118(%rbp),%esi
lea    0xe10(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    125f <establish_comm_uart+0x10f>
movslq -0x118(%rbp),%rax
movb   $0x0,-0x110(%rbp,%rax,1)
cmpl   $0x0,-0x118(%rbp)
jle    1253 <establish_comm_uart+0x103>
mov    -0x118(%rbp),%eax
sub    $0x1,%eax
cltq
movsbl -0x110(%rbp,%rax,1),%eax
ff
cmp    $0x3e,%eax
jne    1253 <establish_comm_uart+0x103>
movl   $0x0,-0x4(%rbp)
jmp    125f <establish_comm_uart+0x10f>
jmp    1258 <establish_comm_uart+0x108>
movl   $0xfffffffe,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<establish_comm_uart>:
push   %rbx
sub    $0x110,%rsp
movb   $0x80,0xf(%rsp)
xor    %eax,%eax
call   1060 <input_flush_serial_port@plt>
lea    0xf(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x1,%esi
xor    %eax,%eax
call   1030 <write_serial_port@plt>
mov    $0x1388,%edi
xor    %eax,%eax
call   1070 <usleep@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
xor    %eax,%eax
call   1030 <write_serial_port@plt>
mov    $0x1388,%edi
xor    %eax,%eax
call   1070 <usleep@plt>
lea    0xe62(%rip),%rdi
mov    $0x1,%esi
xor    %eax,%eax
call   1030 <write_serial_port@plt>
lea    0xe51(%rip),%rdx
lea    0x10(%rsp),%rdi
movsd  0xe3a(%rip),%xmm0
mov    $0xff,%esi
mov    $0x1,%al
call   1050 <read_serial_port_timeout@plt>
test   %eax,%eax
js     11f2 <establish_comm_uart+0xa2>
mov    %eax,%ecx
movb   $0x0,0x10(%rsp,%rcx,1)
je     11eb <establish_comm_uart+0x9b>
add    $0xffffffff,%eax
xor    %ecx,%ecx
cmpb   $0x3e,0x10(%rsp,%rax,1)
je     1207 <establish_comm_uart+0xb7>
mov    $0xfffffffe,%ecx
jmp    1207 <establish_comm_uart+0xb7>
lea    0xe13(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xffffffff,%ecx
mov    %ecx,%eax
add    $0x110,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [2 x i8] c"#\00"
@.str.1 = [2 x i8] c">\00"
@.str.2 = [21 x i8] c"error reading, n=%d\0A\00"
define i32 @establish_comm_uart() {
%1 = alloca i32
%2 = alloca [256 x i8]
%3 = alloca i8
%4 = alloca i32
store i8 -128, i8* %3
%5 = call i32 (...) @input_flush_serial_port()
%6 = call i32 (i8*, i32, ...) bitcast (i32 (...)* @write_serial_port to i32 (i8*, i32, ...)*)(i8* %3, i32 1)
%7 = call i32 (i32, ...) bitcast (i32 (...)* @usleep to i32 (i32, ...)*)(i32 5000)
%8 = call i32 (i8*, i32, ...) bitcast (i32 (...)* @write_serial_port to i32 (i8*, i32, ...)*)(i8* %3, i32 1)
%9 = call i32 (i32, ...) bitcast (i32 (...)* @usleep to i32 (i32, ...)*)(i32 5000)
%10 = call i32 (i8*, i32, ...) bitcast (i32 (...)* @write_serial_port to i32 (i8*, i32, ...)*)(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 1)
%11 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%12 = call i32 (i8*, i64, i8*, double, ...) bitcast (i32 (...)* @read_serial_port_timeout to i32 (i8*, i64, i8*, double, ...)*)(i8* %11, i64 255, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), double 2.000000e-01)
store i32 %12, i32* %4
%13 = load i32, i32* %4
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %0
%16 = load i32, i32* %4
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i32 %16)
store i32 -1, i32* %1
br label %35
18: ; preds = %0
%19 = load i32, i32* %4
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 %20
store i8 0, i8* %21
%22 = load i32, i32* %4
%23 = icmp sgt i32 %22, 0
br i1 %23, label %24, label %33
24: ; preds = %18
%25 = load i32, i32* %4
%26 = sub nsw i32 %25, 1
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 %27
%29 = load i8, i8* %28
%30 = sext i8 %29 to i32
%31 = icmp eq i32 %30, 62
br i1 %31, label %32, label %33
32: ; preds = %24
store i32 0, i32* %1
br label %35
33: ; preds = %24, %18
br label %34
34: ; preds = %33
store i32 -2, i32* %1
br label %35
35: ; preds = %34, %32, %15
%36 = load i32, i32* %1
ret i32 %36
}

LLVM IR (-O2):
@.str = [2 x i8] c"#\00"
@.str.1 = [2 x i8] c">\00"
@.str.2 = [21 x i8] c"error reading, n=%d\0A\00"
define i32 @establish_comm_uart() {
%1 = alloca [256 x i8]
%2 = alloca i8
%3 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 0
store i8 -128, i8* %2
%4 = tail call i32 (...) @input_flush_serial_port()
%5 = call i32 (i8*, i32, ...) bitcast (i32 (...)* @write_serial_port to i32 (i8*, i32, ...)*)(i8* %2, i32 1)
%6 = call i32 (i32, ...) bitcast (i32 (...)* @usleep to i32 (i32, ...)*)(i32 5000)
%7 = call i32 (i8*, i32, ...) bitcast (i32 (...)* @write_serial_port to i32 (i8*, i32, ...)*)(i8* %2, i32 1)
%8 = call i32 (i32, ...) bitcast (i32 (...)* @usleep to i32 (i32, ...)*)(i32 5000)
%9 = call i32 (i8*, i32, ...) bitcast (i32 (...)* @write_serial_port to i32 (i8*, i32, ...)*)(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 1)
%10 = call i32 (i8*, i64, i8*, double, ...) bitcast (i32 (...)* @read_serial_port_timeout to i32 (i8*, i64, i8*, double, ...)*)(i8* %3, i64 255, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), double 2.000000e-01)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %0
%13 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i32 %10)
br label %25
14: ; preds = %0
%15 = zext i32 %10 to i64
%16 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 %15
store i8 0, i8* %16
%17 = icmp eq i32 %10, 0
br i1 %17, label %24, label %18
18: ; preds = %14
%19 = add nsw i32 %10, -1
%20 = zext i32 %19 to i64
%21 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 %20
%22 = load i8, i8* %21
%23 = icmp eq i8 %22, 62
br i1 %23, label %25, label %24
24: ; preds = %14, %18
br label %25
25: ; preds = %18, %24, %12
%26 = phi i32 [ -1, %12 ], [ -2, %24 ], [ 0, %18 ]
ret i32 %26
}