#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();
scalar_t__ isspace(char);
int /*<<< orphan*/ printf(char *, int);
int read_serial_port_timeout(unsigned char *, int, char *, double);
int /*<<< orphan*/ snprintf(char *, int, char *, unsigned int);
int sscanf(char *, char *, unsigned int *);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ write_serial_port(char *, int /*<<< orphan*/);
int read_word(unsigned int addr, unsigned int *word) {
  char buf[256], *p;
  int n, r;
  input_flush_serial_port();
  snprintf(buf, sizeof(buf), "w%X,#", addr);
  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] == '>') {
      p = buf;
      while (isspace(*p))
        p++;
      r = sscanf(p, "0x%x", word);
      if (r == 1) {
        return 0;
      } else {
        return -3;
      }
    }
  }
  return -2;
}


x86 assembly (clang, -O0):
<read_word>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   10a0 <input_flush_serial_port@plt>
lea    -0x110(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x100,%esi
lea    0xe59(%rip),%rdx
call   1060 <snprintf@plt>
lea    -0x110(%rbp),%rax
mov    %rax,-0x128(%rbp)
lea    -0x110(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x128(%rbp),%rdi
mov    %eax,%esi
call   1030 <write_serial_port@plt>
lea    -0x110(%rbp),%rdi
mov    $0xff,%esi
lea    0xe1f(%rip),%rdx
movsd  0xe09(%rip),%xmm0
call   1080 <read_serial_port_timeout@plt>
mov    %eax,-0x11c(%rbp)
cmpl   $0x0,-0x11c(%rbp)
jge    122d <read_word+0xad>
mov    -0x11c(%rbp),%esi
lea    0xdf4(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f1 <read_word+0x171>
movslq -0x11c(%rbp),%rax
movb   $0x0,-0x110(%rbp,%rax,1)
cmpl   $0x0,-0x11c(%rbp)
jle    12e5 <read_word+0x165>
mov    -0x11c(%rbp),%eax
sub    $0x1,%eax
cltq
movsbl -0x110(%rbp,%rax,1),%eax
ff
cmp    $0x3e,%eax
jne    12e5 <read_word+0x165>
lea    -0x110(%rbp),%rax
mov    %rax,-0x118(%rbp)
mov    -0x118(%rbp),%rax
movsbl (%rax),%edi
call   1070 <isspace@plt>
cmp    $0x0,%rax
je     12a3 <read_word+0x123>
mov    -0x118(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x118(%rbp)
jmp    1273 <read_word+0xf3>
mov    -0x118(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xd70(%rip),%rsi
call   1090 <sscanf@plt>
mov    %eax,-0x120(%rbp)
cmpl   $0x1,-0x120(%rbp)
jne    12d9 <read_word+0x159>
movl   $0x0,-0x4(%rbp)
jmp    12f1 <read_word+0x171>
movl   $0xfffffffd,-0x4(%rbp)
jmp    12f1 <read_word+0x171>
jmp    12ea <read_word+0x16a>
movl   $0xfffffffe,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_word>:
push   %rbp
push   %r14
push   %rbx
sub    $0x100,%rsp
mov    %rsi,%r14
mov    %edi,%ebp
xor    %eax,%eax
call   10a0 <input_flush_serial_port@plt>
lea    0xe6a(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x100,%esi
mov    %ebp,%ecx
call   1060 <snprintf@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %rbx,%rdi
mov    %eax,%esi
call   1030 <write_serial_port@plt>
lea    0xe45(%rip),%rdx
movsd  0xe2f(%rip),%xmm0
mov    %rbx,%rdi
mov    $0xff,%esi
call   1080 <read_serial_port_timeout@plt>
mov    %eax,%esi
test   %eax,%eax
js     1234 <read_word+0xb4>
mov    %esi,%eax
movb   $0x0,(%rsp,%rax,1)
mov    $0xfffffffe,%eax
je     1245 <read_word+0xc5>
add    $0xffffffff,%esi
cmpb   $0x3e,(%rsp,%rsi,1)
jne    1245 <read_word+0xc5>
lea    -0x1(%rsp),%rbx
nop
movsbl 0x1(%rbx),%edi
add    $0x1,%rbx
call   1070 <isspace@plt>
test   %rax,%rax
jne    1200 <read_word+0x80>
lea    0xe0c(%rip),%rsi
mov    %rbx,%rdi
mov    %r14,%rdx
call   1090 <sscanf@plt>
xor    %ecx,%ecx
cmp    $0x1,%eax
sete   %cl
lea    (%rcx,%rcx,2),%eax
add    $0xfffffffd,%eax
jmp    1245 <read_word+0xc5>
lea    0xdd5(%rip),%rdi
call   1050 <printf@plt>
mov    $0xffffffff,%eax
add    $0x100,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [6 x i8] c"w%X,#\00"
@.str.1 = [2 x i8] c">\00"
@.str.2 = [21 x i8] c"error reading, n=%d\0A\00"
@.str.3 = [5 x i8] c"0x%x\00"
define i32 @read_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 ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %0)
%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 %33
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 %33, 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 %23, label %33
23: ; preds = %17, %23
%24 = phi i8* [ %28, %23 ], [ %4, %17 ]
%25 = load i8, i8* %24
%26 = call i64 @isspace(i8 signext %25)
%27 = icmp eq i64 %26, 0
%28 = getelementptr inbounds i8, i8* %24, i64 1
br i1 %27, label %29, label %23
29: ; preds = %23
%30 = call i32 @sscanf(i8* %24, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32* %1)
%31 = icmp eq i32 %30, 1
%32 = select i1 %31, i32 0, i32 -3
br label %33
33: ; preds = %17, %13, %29, %11
%34 = phi i32 [ -1, %11 ], [ %32, %29 ], [ -2, %13 ], [ -2, %17 ]
ret i32 %34
}