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

/* Variables and functions */
int /*<<< orphan*/ TerminateTask();
int /*<<< orphan*/ __startrek_assert_i0(int);
unsigned char __startrek_read_R_state();
unsigned short __startrek_read_threshold();
int /*<<< orphan*/ __startrek_write_R_state(int /*<<< orphan*/);
int /*<<< orphan*/ __startrek_write_input(int);
int /*<<< orphan*/ __startrek_write_need_to_read(int /*<<< orphan*/);
int /*<<< orphan*/ __startrek_write_need_to_run_nxtbg(int);
int /*<<< orphan*/ bg_nxtcolorsensor(int /*<<< orphan*/);
unsigned char ecrobot_get_nxtcolorsensor_light(int /*<<< orphan*/);
unsigned char ecrobot_get_nxtcolorsensor_mode(int /*<<< orphan*/);
int /*<<< orphan*/ ecrobot_set_nxtcolorsensor(int /*<<< orphan*/, int);
_Bool move_motor(int /*<<< orphan*/, int, int, int);
void Reader(void) {
  unsigned short color;
  unsigned char tmp;
  _Bool tmp___0;
  _Bool tmp___1;
  unsigned char tmp___2;
  unsigned char tmp___3;
  unsigned char tmp___4;
  int tmp___6;
  unsigned short tmp___7;
  {
    tmp = __startrek_read_R_state();
    switch (tmp) {
    case 0:
      break;
    case 1:
      tmp___0 = move_motor(0, 15, 45, 1);
      if (tmp___0) {
        __startrek_write_R_state(0);
      }
      break;
    case 2:
      tmp___1 = move_motor(0, 15, 0, -1);
      if (tmp___1) {
        __startrek_write_R_state(0);
      }
      break;
    case 3:
      tmp___2 = ecrobot_get_nxtcolorsensor_mode(0);
      if ((int)tmp___2 != 2) {
        ecrobot_set_nxtcolorsensor(0, 2);
        __startrek_write_need_to_run_nxtbg(1);
      }
      tmp___3 = ecrobot_get_nxtcolorsensor_mode(0);
      __startrek_assert_i0((int)tmp___3 == 2);
      bg_nxtcolorsensor(0);
      tmp___4 = ecrobot_get_nxtcolorsensor_light(0);
      color = tmp___4;
      tmp___7 = __startrek_read_threshold();
      if ((int)color < tmp___7) {
        tmp___6 = 1;
      } else {
        tmp___6 = 0;
      }
      __startrek_write_input(tmp___6);
      ecrobot_set_nxtcolorsensor(0, 5);
      __startrek_write_need_to_run_nxtbg(1);
      __startrek_write_R_state(0);
      __startrek_write_need_to_read(0);
      break;
    }
    TerminateTask();
    return;
  }
}


x86 assembly (clang, -O0):
<Reader>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
xor    %eax,%eax
call   1050 <__startrek_read_R_state@plt>
mov    %al,-0x3(%rbp)
movzbl -0x3(%rbp),%eax
mov    %rax,-0x18(%rbp)
sub    $0x3,%rax
ja     132a <Reader+0x15a>
mov    -0x18(%rbp),%rax
lea    0xe01(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
jmp    132a <Reader+0x15a>
xor    %edi,%edi
mov    $0xf,%esi
mov    $0x2d,%edx
mov    $0x1,%ecx
call   10a0 <move_motor@plt>
and    $0x1,%al
mov    %al,-0x4(%rbp)
testb  $0x1,-0x4(%rbp)
je     1239 <Reader+0x69>
xor    %edi,%edi
call   10c0 <__startrek_write_R_state@plt>
jmp    132a <Reader+0x15a>
xor    %edx,%edx
mov    $0xf,%esi
mov    $0xffffffff,%ecx
mov    %edx,%edi
call   10a0 <move_motor@plt>
and    $0x1,%al
mov    %al,-0x5(%rbp)
testb  $0x1,-0x5(%rbp)
je     1267 <Reader+0x97>
xor    %edi,%edi
call   10c0 <__startrek_write_R_state@plt>
jmp    132a <Reader+0x15a>
xor    %edi,%edi
call   10e0 <ecrobot_get_nxtcolorsensor_mode@plt>
mov    %al,-0x6(%rbp)
movzbl -0x6(%rbp),%eax
cmp    $0x2,%eax
je     1299 <Reader+0xc9>
xor    %edi,%edi
mov    $0x2,%esi
call   1060 <ecrobot_set_nxtcolorsensor@plt>
mov    $0x1,%edi
call   1030 <__startrek_write_need_to_run_nxtbg@plt>
xor    %edi,%edi
call   10e0 <ecrobot_get_nxtcolorsensor_mode@plt>
mov    %al,-0x7(%rbp)
movzbl -0x7(%rbp),%eax
cmp    $0x2,%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   10d0 <__startrek_assert_i0@plt>
xor    %edi,%edi
call   10b0 <bg_nxtcolorsensor@plt>
xor    %edi,%edi
call   1040 <ecrobot_get_nxtcolorsensor_light@plt>
mov    %al,-0x8(%rbp)
movzbl -0x8(%rbp),%eax
mov    %ax,-0x2(%rbp)
mov    $0x0,%al
call   1080 <__startrek_read_threshold@plt>
mov    %ax,-0xe(%rbp)
movzwl -0x2(%rbp),%eax
movzwl -0xe(%rbp),%ecx
cmp    %ecx,%eax
jge    12f7 <Reader+0x127>
movl   $0x1,-0xc(%rbp)
jmp    12fe <Reader+0x12e>
movl   $0x0,-0xc(%rbp)
mov    -0xc(%rbp),%edi
call   1090 <__startrek_write_input@plt>
xor    %edi,%edi
mov    $0x5,%esi
call   1060 <ecrobot_set_nxtcolorsensor@plt>
mov    $0x1,%edi
call   1030 <__startrek_write_need_to_run_nxtbg@plt>
xor    %edi,%edi
call   10c0 <__startrek_write_R_state@plt>
xor    %edi,%edi
call   1070 <__startrek_write_need_to_read@plt>
mov    $0x0,%al
call   10f0 <TerminateTask@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<Reader>:
push   %rbx
xor    %eax,%eax
call   1050 <__startrek_read_R_state@plt>
cmp    $0x3,%al
je     1222 <Reader+0x52>
cmp    $0x2,%al
je     11fb <Reader+0x2b>
cmp    $0x1,%al
jne    12a3 <Reader+0xd3>
xor    %edi,%edi
mov    $0xf,%esi
mov    $0x2d,%edx
mov    $0x1,%ecx
jmp    1209 <Reader+0x39>
xor    %edi,%edi
mov    $0xf,%esi
xor    %edx,%edx
mov    $0xffffffff,%ecx
call   10a0 <move_motor@plt>
test   %al,%al
je     12a3 <Reader+0xd3>
xor    %edi,%edi
call   10c0 <__startrek_write_R_state@plt>
jmp    12a3 <Reader+0xd3>
xor    %edi,%edi
call   10e0 <ecrobot_get_nxtcolorsensor_mode@plt>
cmp    $0x2,%al
je     1243 <Reader+0x73>
xor    %edi,%edi
mov    $0x2,%esi
call   1060 <ecrobot_set_nxtcolorsensor@plt>
mov    $0x1,%edi
call   1030 <__startrek_write_need_to_run_nxtbg@plt>
xor    %edi,%edi
call   10e0 <ecrobot_get_nxtcolorsensor_mode@plt>
xor    %edi,%edi
cmp    $0x2,%al
sete   %dil
call   10d0 <__startrek_assert_i0@plt>
xor    %edi,%edi
call   10b0 <bg_nxtcolorsensor@plt>
xor    %edi,%edi
call   1040 <ecrobot_get_nxtcolorsensor_light@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1080 <__startrek_read_threshold@plt>
movzbl %bl,%ecx
xor    %edi,%edi
cmp    %cx,%ax
seta   %dil
call   1090 <__startrek_write_input@plt>
xor    %edi,%edi
mov    $0x5,%esi
call   1060 <ecrobot_set_nxtcolorsensor@plt>
mov    $0x1,%edi
call   1030 <__startrek_write_need_to_run_nxtbg@plt>
xor    %edi,%edi
call   10c0 <__startrek_write_R_state@plt>
xor    %edi,%edi
call   1070 <__startrek_write_need_to_read@plt>
xor    %eax,%eax
pop    %rbx
jmp    10f0 <TerminateTask@plt>

LLVM IR (-O0):
define void @Reader() {
%1 = alloca i16
%2 = alloca i8
%3 = alloca i8
%4 = alloca i8
%5 = alloca i8
%6 = alloca i8
%7 = alloca i8
%8 = alloca i32
%9 = alloca i16
%10 = call zeroext i8 (...) @__startrek_read_R_state()
store i8 %10, i8* %2
%11 = load i8, i8* %2
%12 = zext i8 %11 to i32
switch i32 %12, label %64 [
i32 0, label %13
i32 1, label %14
i32 2, label %22
i32 3, label %30
]
13: ; preds = %0
br label %64
14: ; preds = %0
%15 = call zeroext i1 @move_motor(i32 0, i32 15, i32 45, i32 1)
%16 = zext i1 %15 to i8
store i8 %16, i8* %3
%17 = load i8, i8* %3
%18 = trunc i8 %17 to i1
br i1 %18, label %19, label %21
19: ; preds = %14
%20 = call i32 @__startrek_write_R_state(i32 0)
br label %21
21: ; preds = %19, %14
br label %64
22: ; preds = %0
%23 = call zeroext i1 @move_motor(i32 0, i32 15, i32 0, i32 -1)
%24 = zext i1 %23 to i8
store i8 %24, i8* %4
%25 = load i8, i8* %4
%26 = trunc i8 %25 to i1
br i1 %26, label %27, label %29
27: ; preds = %22
%28 = call i32 @__startrek_write_R_state(i32 0)
br label %29
29: ; preds = %27, %22
br label %64
30: ; preds = %0
%31 = call zeroext i8 @ecrobot_get_nxtcolorsensor_mode(i32 0)
store i8 %31, i8* %5
%32 = load i8, i8* %5
%33 = zext i8 %32 to i32
%34 = icmp ne i32 %33, 2
br i1 %34, label %35, label %38
35: ; preds = %30
%36 = call i32 @ecrobot_set_nxtcolorsensor(i32 0, i32 2)
%37 = call i32 @__startrek_write_need_to_run_nxtbg(i32 1)
br label %38
38: ; preds = %35, %30
%39 = call zeroext i8 @ecrobot_get_nxtcolorsensor_mode(i32 0)
store i8 %39, i8* %6
%40 = load i8, i8* %6
%41 = zext i8 %40 to i32
%42 = icmp eq i32 %41, 2
%43 = zext i1 %42 to i32
%44 = call i32 @__startrek_assert_i0(i32 %43)
%45 = call i32 @bg_nxtcolorsensor(i32 0)
%46 = call zeroext i8 @ecrobot_get_nxtcolorsensor_light(i32 0)
store i8 %46, i8* %7
%47 = load i8, i8* %7
%48 = zext i8 %47 to i16
store i16 %48, i16* %1
%49 = call zeroext i16 (...) @__startrek_read_threshold()
store i16 %49, i16* %9
%50 = load i16, i16* %1
%51 = zext i16 %50 to i32
%52 = load i16, i16* %9
%53 = zext i16 %52 to i32
%54 = icmp slt i32 %51, %53
br i1 %54, label %55, label %56
55: ; preds = %38
store i32 1, i32* %8
br label %57
56: ; preds = %38
store i32 0, i32* %8
br label %57
57: ; preds = %56, %55
%58 = load i32, i32* %8
%59 = call i32 @__startrek_write_input(i32 %58)
%60 = call i32 @ecrobot_set_nxtcolorsensor(i32 0, i32 5)
%61 = call i32 @__startrek_write_need_to_run_nxtbg(i32 1)
%62 = call i32 @__startrek_write_R_state(i32 0)
%63 = call i32 @__startrek_write_need_to_read(i32 0)
br label %64
64: ; preds = %0, %57, %29, %21, %13
%65 = call i32 (...) @TerminateTask()
ret void
}

LLVM IR (-O2):
define void @Reader() {
%1 = tail call zeroext i8 (...) @__startrek_read_R_state()
switch i8 %1, label %32 [
i8 3, label %10
i8 1, label %2
i8 2, label %6
]
2: ; preds = %0
%3 = tail call zeroext i1 @move_motor(i32 0, i32 15, i32 45, i32 1)
br i1 %3, label %4, label %32
4: ; preds = %2
%5 = tail call i32 @__startrek_write_R_state(i32 0)
br label %32
6: ; preds = %0
%7 = tail call zeroext i1 @move_motor(i32 0, i32 15, i32 0, i32 -1)
br i1 %7, label %8, label %32
8: ; preds = %6
%9 = tail call i32 @__startrek_write_R_state(i32 0)
br label %32
10: ; preds = %0
%11 = tail call zeroext i8 @ecrobot_get_nxtcolorsensor_mode(i32 0)
%12 = icmp eq i8 %11, 2
br i1 %12, label %16, label %13
13: ; preds = %10
%14 = tail call i32 @ecrobot_set_nxtcolorsensor(i32 0, i32 2)
%15 = tail call i32 @__startrek_write_need_to_run_nxtbg(i32 1)
br label %16
16: ; preds = %13, %10
%17 = tail call zeroext i8 @ecrobot_get_nxtcolorsensor_mode(i32 0)
%18 = icmp eq i8 %17, 2
%19 = zext i1 %18 to i32
%20 = tail call i32 @__startrek_assert_i0(i32 %19)
%21 = tail call i32 @bg_nxtcolorsensor(i32 0)
%22 = tail call zeroext i8 @ecrobot_get_nxtcolorsensor_light(i32 0)
%23 = tail call zeroext i16 (...) @__startrek_read_threshold()
%24 = zext i8 %22 to i16
%25 = icmp ugt i16 %23, %24
%26 = zext i1 %25 to i32
%27 = tail call i32 @__startrek_write_input(i32 %26)
%28 = tail call i32 @ecrobot_set_nxtcolorsensor(i32 0, i32 5)
%29 = tail call i32 @__startrek_write_need_to_run_nxtbg(i32 1)
%30 = tail call i32 @__startrek_write_R_state(i32 0)
%31 = tail call i32 @__startrek_write_need_to_read(i32 0)
br label %32
32: ; preds = %6, %8, %2, %4, %0, %16
%33 = tail call i32 (...) @TerminateTask()
ret void
}