#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*/ snprintf(char *, int, char *, unsigned int, unsigned int);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ write_serial_port(char *, int /*<<< orphan*/);
int write_word(unsigned int addr, unsigned int word) {
  char buf[256];
  int n;
  input_flush_serial_port();
  snprintf(buf, sizeof(buf), "W%X,%X#", addr, word);
  write_serial_port(buf, strlen(buf));
  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):
<write_word>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    $0x0,%al
call   1080 <input_flush_serial_port@plt>
lea    -0x110(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    -0xc(%rbp),%r8d
mov    $0x100,%esi
lea    0xe76(%rip),%rdx
call   1060 <snprintf@plt>
lea    -0x110(%rbp),%rax
mov    %rax,-0x120(%rbp)
lea    -0x110(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x120(%rbp),%rdi
mov    %eax,%esi
call   1030 <write_serial_port@plt>
lea    -0x110(%rbp),%rdi
mov    $0xff,%esi
lea    0xe3e(%rip),%rdx
movsd  0xe26(%rip),%xmm0
call   1070 <read_serial_port_timeout@plt>
mov    %eax,-0x114(%rbp)
cmpl   $0x0,-0x114(%rbp)
jge    1210 <write_word+0xb0>
mov    -0x114(%rbp),%esi
lea    0xe13(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1260 <write_word+0x100>
movslq -0x114(%rbp),%rax
movb   $0x0,-0x110(%rbp,%rax,1)
cmpl   $0x0,-0x114(%rbp)
jle    1254 <write_word+0xf4>
mov    -0x114(%rbp),%eax
sub    $0x1,%eax
cltq
movsbl -0x110(%rbp,%rax,1),%eax
ff
cmp    $0x3e,%eax
jne    1254 <write_word+0xf4>
movl   $0x0,-0x4(%rbp)
jmp    1260 <write_word+0x100>
jmp    1259 <write_word+0xf9>
movl   $0xfffffffe,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_word>:
push   %rbp
push   %r14
push   %rbx
sub    $0x100,%rsp
mov    %esi,%r14d
mov    %edi,%ebp
xor    %eax,%eax
call   1080 <input_flush_serial_port@plt>
lea    0xe8a(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x100,%esi
mov    %ebp,%ecx
mov    %r14d,%r8d
call   1060 <snprintf@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %rbx,%rdi
mov    %eax,%esi
call   1030 <write_serial_port@plt>
lea    0xe64(%rip),%rdx
movsd  0xe4c(%rip),%xmm0
mov    %rbx,%rdi
mov    $0xff,%esi
call   1070 <read_serial_port_timeout@plt>
test   %eax,%eax
js     11df <write_word+0x7f>
mov    %eax,%ecx
movb   $0x0,(%rsp,%rcx,1)
je     11d8 <write_word+0x78>
add    $0xffffffff,%eax
xor    %ecx,%ecx
cmpb   $0x3e,(%rsp,%rax,1)
je     11f2 <write_word+0x92>
mov    $0xfffffffe,%ecx
jmp    11f2 <write_word+0x92>
lea    0xe2c(%rip),%rdi
mov    %eax,%esi
call   1050 <printf@plt>
mov    $0xffffffff,%ecx
mov    %ecx,%eax
add    $0x100,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [8 x i8] c"W%X,%X#\00"
@.str.1 = [2 x i8] c">\00"
@.str.2 = [21 x i8] c"error reading, n=%d\0A\00"
define i32 @write_word(i32 %0, i32 %1) {
%3 = alloca [256 x i8]
%4 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%5 = tail call i32 (...) @input_flush_serial_port()
%6 = call i32 @snprintf(i8* %4, i32 256, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %0, i32 %1)
%7 = call i32 @strlen(i8* %4)
%8 = call i32 @write_serial_port(i8* %4, i32 %7)
%9 = 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)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i32 %9)
br label %24
13: ; preds = %2
%14 = zext i32 %9 to i64
%15 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 %14
store i8 0, i8* %15
%16 = icmp eq i32 %9, 0
br i1 %16, label %23, label %17
17: ; preds = %13
%18 = add nsw i32 %9, -1
%19 = zext i32 %18 to i64
%20 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 %19
%21 = load i8, i8* %20
%22 = icmp eq i8 %21, 62
br i1 %22, label %24, label %23
23: ; preds = %13, %17
br label %24
24: ; preds = %17, %23, %11
%25 = phi i32 [ -1, %11 ], [ -2, %23 ], [ 0, %17 ]
ret i32 %25
}