#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 read_serial_port_timeout(unsigned char *, int, char *, double);
int /*<<< orphan*/ snprintf(char *, int, char *, unsigned int);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ write_serial_port(char *, int /*<<< orphan*/);
int run_code(unsigned int addr) {
  char buf[256];
  int n;
  int i;
  input_flush_serial_port();
  snprintf(buf, sizeof(buf), "G%X#", addr);
  printf("sending command \"%s\"\n", buf);
  write_serial_port(buf, strlen(buf));
  n = read_serial_port_timeout((unsigned char *)buf, sizeof(buf) - 1, ">", 1.5);
  if (n < 0) {
    printf("error reading, n=%d\n", n);
    return -1;
  } else {
    buf[n] = '\0';
    printf("received %d chars: %s\n", n, buf);
    for (i = 0; i < n; i++)
      printf("buf[%d] = %02X\n", i, (unsigned char)buf[i]);
    if (n > 0 && buf[n - 1] == '>')
      return 0;
    if (addr == 0 && (buf[0] == 13 || buf[0] == 10))
      return 0;
  }
  return -2;
}


x86 assembly (clang, -O0):
<run_code>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
mov    %edi,-0x8(%rbp)
mov    $0x0,%al
call   1080 <input_flush_serial_port@plt>
lea    -0x110(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x100,%esi
lea    0xe7d(%rip),%rdx
call   1060 <snprintf@plt>
lea    -0x110(%rbp),%rsi
lea    0xe6f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@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    0xe43(%rip),%rdx
movsd  0xe18(%rip),%xmm0
call   1070 <read_serial_port_timeout@plt>
mov    %eax,-0x114(%rbp)
cmpl   $0x0,-0x114(%rbp)
jge    1220 <run_code+0xc0>
mov    -0x114(%rbp),%esi
lea    0xe18(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1314 <run_code+0x1b4>
movslq -0x114(%rbp),%rax
movb   $0x0,-0x110(%rbp,%rax,1)
mov    -0x114(%rbp),%esi
lea    -0x110(%rbp),%rdx
lea    0xdf7(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x118(%rbp)
mov    -0x118(%rbp),%eax
cmp    -0x114(%rbp),%eax
jge    129d <run_code+0x13d>
mov    -0x118(%rbp),%esi
movslq -0x118(%rbp),%rax
movzbl -0x110(%rbp,%rax,1),%edx
ff
lea    0xdcf(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x118(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x118(%rbp)
jmp    1254 <run_code+0xf4>
cmpl   $0x0,-0x114(%rbp)
jle    12d2 <run_code+0x172>
mov    -0x114(%rbp),%eax
sub    $0x1,%eax
cltq
movsbl -0x110(%rbp,%rax,1),%eax
ff
cmp    $0x3e,%eax
jne    12d2 <run_code+0x172>
movl   $0x0,-0x4(%rbp)
jmp    1314 <run_code+0x1b4>
cmpl   $0x0,-0x8(%rbp)
jne    1308 <run_code+0x1a8>
movsbl -0x110(%rbp),%eax
cmp    $0xd,%eax
je     12fc <run_code+0x19c>
movsbl -0x110(%rbp),%eax
cmp    $0xa,%eax
jne    1308 <run_code+0x1a8>
movl   $0x0,-0x4(%rbp)
jmp    1314 <run_code+0x1b4>
jmp    130d <run_code+0x1ad>
movl   $0xfffffffe,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<run_code>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x108,%rsp
mov    %edi,%r14d
xor    %eax,%eax
call   1080 <input_flush_serial_port@plt>
lea    0xe8a(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x100,%esi
mov    %r14d,%ecx
call   1060 <snprintf@plt>
lea    0xe75(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %rbx,%rdi
mov    %eax,%esi
call   1030 <write_serial_port@plt>
lea    0xe68(%rip),%rdx
movsd  0xe3d(%rip),%xmm0
mov    %rbx,%rdi
mov    $0xff,%esi
call   1070 <read_serial_port_timeout@plt>
mov    %eax,%ebx
test   %eax,%eax
js     1249 <run_code+0xe9>
movb   $0x0,(%rsp,%rbx,1)
lea    0xe59(%rip),%rdi
mov    %rsp,%rdx
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <printf@plt>
test   %rbx,%rbx
je     1228 <run_code+0xc8>
lea    0xe58(%rip),%r15
xor    %ebp,%ebp
nopl   0x0(%rax,%rax,1)
movzbl (%rsp,%rbp,1),%edx
mov    %r15,%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <printf@plt>
add    $0x1,%rbp
cmp    %rbp,%rbx
jne    1200 <run_code+0xa0>
test   %ebx,%ebx
jle    1228 <run_code+0xc8>
add    $0xffffffff,%ebx
xor    %eax,%eax
cmpb   $0x3e,(%rsp,%rbx,1)
je     125e <run_code+0xfe>
mov    $0xfffffffe,%eax
test   %r14d,%r14d
jne    125e <run_code+0xfe>
mov    (%rsp),%al
xor    %ecx,%ecx
cmp    $0xa,%al
sete   %cl
xor    %edx,%edx
cmp    $0xd,%al
lea    -0x2(%rcx,%rcx,1),%eax
cmove  %edx,%eax
jmp    125e <run_code+0xfe>
lea    0xdd5(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xffffffff,%eax
add    $0x108,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"G%X#\00"
@.str.1 = [22 x i8] c"sending command \22%s\22\0A\00"
@.str.2 = [2 x i8] c">\00"
@.str.3 = [21 x i8] c"error reading, n=%d\0A\00"
@.str.4 = [23 x i8] c"received %d chars: %s\0A\00"
@.str.5 = [16 x i8] c"buf[%d] = %02X\0A\00"
define i32 @run_code(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [256 x i8]
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %3
%7 = call i32 (...) @input_flush_serial_port()
%8 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%9 = load i32, i32* %3
%10 = call i32 @snprintf(i8* %8, i32 256, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %11)
%13 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%14 = getelementptr inbounds [256 x i8], [256 x i8]* %4, 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]* %4, 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.2, i64 0, i64 0), double 1.500000e+00)
store i32 %18, i32* %5
%19 = load i32, i32* %5
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %1
%22 = load i32, i32* %5
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i32 %22)
store i32 -1, i32* %2
br label %74
24: ; preds = %1
%25 = load i32, i32* %5
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 %26
store i8 0, i8* %27
%28 = load i32, i32* %5
%29 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i32 %28, i8* %29)
store i32 0, i32* %6
br label %31
31: ; preds = %43, %24
%32 = load i32, i32* %6
%33 = load i32, i32* %5
%34 = icmp slt i32 %32, %33
br i1 %34, label %35, label %46
35: ; preds = %31
%36 = load i32, i32* %6
%37 = load i32, i32* %6
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 %38
%40 = load i8, i8* %39
%41 = zext i8 %40 to i32
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0), i32 %36, i32 %41)
br label %43
43: ; preds = %35
%44 = load i32, i32* %6
%45 = add nsw i32 %44, 1
store i32 %45, i32* %6
br label %31
46: ; preds = %31
%47 = load i32, i32* %5
%48 = icmp sgt i32 %47, 0
br i1 %48, label %49, label %58
49: ; preds = %46
%50 = load i32, i32* %5
%51 = sub nsw i32 %50, 1
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 %52
%54 = load i8, i8* %53
%55 = sext i8 %54 to i32
%56 = icmp eq i32 %55, 62
br i1 %56, label %57, label %58
57: ; preds = %49
store i32 0, i32* %2
br label %74
58: ; preds = %49, %46
%59 = load i32, i32* %3
%60 = icmp eq i32 %59, 0
br i1 %60, label %61, label %72
61: ; preds = %58
%62 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%63 = load i8, i8* %62
%64 = sext i8 %63 to i32
%65 = icmp eq i32 %64, 13
br i1 %65, label %71, label %66
66: ; preds = %61
%67 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%68 = load i8, i8* %67
%69 = sext i8 %68 to i32
%70 = icmp eq i32 %69, 10
br i1 %70, label %71, label %72
71: ; preds = %66, %61
store i32 0, i32* %2
br label %74
72: ; preds = %66, %58
br label %73
73: ; preds = %72
store i32 -2, i32* %2
br label %74
74: ; preds = %73, %71, %57, %21
%75 = load i32, i32* %2
ret i32 %75
}

LLVM IR (-O2):
@.str = [5 x i8] c"G%X#\00"
@.str.1 = [22 x i8] c"sending command \22%s\22\0A\00"
@.str.2 = [2 x i8] c">\00"
@.str.3 = [21 x i8] c"error reading, n=%d\0A\00"
@.str.4 = [23 x i8] c"received %d chars: %s\0A\00"
@.str.5 = [16 x i8] c"buf[%d] = %02X\0A\00"
define i32 @run_code(i32 %0) {
%2 = alloca [256 x i8]
%3 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%4 = tail call i32 (...) @input_flush_serial_port()
%5 = call i32 @snprintf(i8* %3, i32 256, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %0)
%6 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %3)
%7 = call i32 @strlen(i8* %3)
%8 = call i32 @write_serial_port(i8* %3, i32 %7)
%9 = call i32 @read_serial_port_timeout(i8* %3, i32 255, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), double 1.500000e+00)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %1
%12 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i32 %9)
br label %43
13: ; preds = %1
%14 = zext i32 %9 to i64
%15 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 %14
store i8 0, i8* %15
%16 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i32 %9, i8* %3)
%17 = icmp eq i32 %9, 0
br i1 %17, label %35, label %18
18: ; preds = %13, %18
%19 = phi i64 [ %25, %18 ], [ 0, %13 ]
%20 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 %19
%21 = load i8, i8* %20
%22 = zext i8 %21 to i32
%23 = trunc i64 %19 to i32
%24 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0), i32 %23, i32 %22)
%25 = add nuw nsw i64 %19, 1
%26 = icmp eq i64 %25, %14
br i1 %26, label %27, label %18
27: ; preds = %18
%28 = icmp sgt i32 %9, 0
br i1 %28, label %29, label %35
29: ; preds = %27
%30 = add nsw i32 %9, -1
%31 = zext i32 %30 to i64
%32 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 %31
%33 = load i8, i8* %32
%34 = icmp eq i8 %33, 62
br i1 %34, label %43, label %35
35: ; preds = %13, %29, %27
%36 = icmp eq i32 %0, 0
br i1 %36, label %37, label %43
37: ; preds = %35
%38 = load i8, i8* %3
%39 = icmp eq i8 %38, 13
%40 = icmp eq i8 %38, 10
%41 = or i1 %39, %40
%42 = select i1 %41, i32 0, i32 -2
br label %43
43: ; preds = %35, %37, %29, %11
%44 = phi i32 [ -1, %11 ], [ 0, %29 ], [ %42, %37 ], [ -2, %35 ]
ret i32 %44
}