#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 */
unsigned char __VERIFIER_nondet_U8();
int __startrek_read_R_count();
char __startrek_read_R_speed();
int __startrek_read_T_count();
char __startrek_read_T_speed();
int __startrek_read_W_count();
char __startrek_read_W_speed();
int /*<<< orphan*/ __startrek_write_R_count(int);
int /*<<< orphan*/ __startrek_write_T_count(int);
int /*<<< orphan*/ __startrek_write_W_count(int);
int nxt_motor_get_count(unsigned int n) {
  unsigned char delta;
  int new_count;
  int count;
  char speed;
  char tmp;
  char tmp___0;
  char tmp___1;
  {
    count = 0;
    speed = 0;
    switch (n) {
    case 0:
      tmp = __startrek_read_R_speed();
      speed = tmp;
      count = __startrek_read_R_count();
      if ((int)speed == 0) {
        return (count);
      } else {
        delta = __VERIFIER_nondet_U8();
        if (count + (int)speed > 0) {
          new_count = delta;
        } else {
          new_count = -((int)delta);
        }
        __startrek_write_R_count(new_count);
      }
      break;
    case 1:
      tmp___0 = __startrek_read_W_speed();
      speed = tmp___0;
      count = __startrek_read_W_count();
      if ((int)speed == 0) {
        return (count);
      } else {
        delta = __VERIFIER_nondet_U8();
        if (count + (int)speed > 0) {
          new_count = delta;
        } else {
          new_count = -((int)delta);
        }
        __startrek_write_W_count(new_count);
      }
      break;
    case 2:
      tmp___1 = __startrek_read_T_speed();
      speed = tmp___1;
      count = __startrek_read_T_count();
      if ((int)speed == 0) {
        return (count);
      } else {
        delta = __VERIFIER_nondet_U8();
        if (count + (int)speed > 0) {
          new_count = delta;
        } else {
          new_count = -((int)delta);
        }
        __startrek_write_T_count(new_count);
      }
      break;
    default:;
    }
    return (new_count);
  }
}


x86 assembly (clang, -O0):
<nxt_motor_get_count>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x0,-0x14(%rbp)
movb   $0x0,-0x15(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x1c(%rbp)
test   %eax,%eax
je     11eb <nxt_motor_get_count+0x4b>
jmp    11c9 <nxt_motor_get_count+0x29>
mov    -0x1c(%rbp),%eax
sub    $0x1,%eax
je     125d <nxt_motor_get_count+0xbd>
jmp    11da <nxt_motor_get_count+0x3a>
mov    -0x1c(%rbp),%eax
sub    $0x2,%eax
je     12cf <nxt_motor_get_count+0x12f>
jmp    1341 <nxt_motor_get_count+0x1a1>
mov    $0x0,%al
call   1080 <__startrek_read_R_speed@plt>
mov    %al,-0x16(%rbp)
mov    -0x16(%rbp),%al
mov    %al,-0x15(%rbp)
mov    $0x0,%al
call   1060 <__startrek_read_R_count@plt>
mov    %eax,-0x14(%rbp)
movsbl -0x15(%rbp),%eax
cmp    $0x0,%eax
jne    121d <nxt_motor_get_count+0x7d>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    134c <nxt_motor_get_count+0x1ac>
mov    $0x0,%al
call   1040 <__VERIFIER_nondet_U8@plt>
mov    %al,-0x9(%rbp)
mov    -0x14(%rbp),%eax
movsbl -0x15(%rbp),%ecx
add    %ecx,%eax
cmp    $0x0,%eax
jle    1245 <nxt_motor_get_count+0xa5>
movzbl -0x9(%rbp),%eax
mov    %eax,-0x10(%rbp)
jmp    1250 <nxt_motor_get_count+0xb0>
movzbl -0x9(%rbp),%ecx
xor    %eax,%eax
sub    %ecx,%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%edi
call   1030 <__startrek_write_R_count@plt>
jmp    1346 <nxt_motor_get_count+0x1a6>
mov    $0x0,%al
call   1090 <__startrek_read_W_speed@plt>
mov    %al,-0x17(%rbp)
mov    -0x17(%rbp),%al
mov    %al,-0x15(%rbp)
mov    $0x0,%al
call   10c0 <__startrek_read_W_count@plt>
mov    %eax,-0x14(%rbp)
movsbl -0x15(%rbp),%eax
cmp    $0x0,%eax
jne    128f <nxt_motor_get_count+0xef>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    134c <nxt_motor_get_count+0x1ac>
mov    $0x0,%al
call   1040 <__VERIFIER_nondet_U8@plt>
mov    %al,-0x9(%rbp)
mov    -0x14(%rbp),%eax
movsbl -0x15(%rbp),%ecx
add    %ecx,%eax
cmp    $0x0,%eax
jle    12b7 <nxt_motor_get_count+0x117>
movzbl -0x9(%rbp),%eax
mov    %eax,-0x10(%rbp)
jmp    12c2 <nxt_motor_get_count+0x122>
movzbl -0x9(%rbp),%ecx
xor    %eax,%eax
sub    %ecx,%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%edi
call   1070 <__startrek_write_W_count@plt>
jmp    1346 <nxt_motor_get_count+0x1a6>
mov    $0x0,%al
call   10a0 <__startrek_read_T_speed@plt>
mov    %al,-0x18(%rbp)
mov    -0x18(%rbp),%al
mov    %al,-0x15(%rbp)
mov    $0x0,%al
call   10b0 <__startrek_read_T_count@plt>
mov    %eax,-0x14(%rbp)
movsbl -0x15(%rbp),%eax
cmp    $0x0,%eax
jne    1301 <nxt_motor_get_count+0x161>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    134c <nxt_motor_get_count+0x1ac>
mov    $0x0,%al
call   1040 <__VERIFIER_nondet_U8@plt>
mov    %al,-0x9(%rbp)
mov    -0x14(%rbp),%eax
movsbl -0x15(%rbp),%ecx
add    %ecx,%eax
cmp    $0x0,%eax
jle    1329 <nxt_motor_get_count+0x189>
movzbl -0x9(%rbp),%eax
mov    %eax,-0x10(%rbp)
jmp    1334 <nxt_motor_get_count+0x194>
movzbl -0x9(%rbp),%ecx
xor    %eax,%eax
sub    %ecx,%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%edi
call   1050 <__startrek_write_T_count@plt>
jmp    1346 <nxt_motor_get_count+0x1a6>
jmp    1346 <nxt_motor_get_count+0x1a6>
mov    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nxt_motor_get_count>:
push   %rbp
push   %r14
push   %rbx
cmp    $0x2,%edi
je     11f2 <nxt_motor_get_count+0x52>
cmp    $0x1,%edi
je     122a <nxt_motor_get_count+0x8a>
test   %edi,%edi
jne    1262 <nxt_motor_get_count+0xc2>
xor    %eax,%eax
call   1080 <__startrek_read_R_speed@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1060 <__startrek_read_R_count@plt>
mov    %eax,%ebx
test   %bpl,%bpl
je     1262 <nxt_motor_get_count+0xc2>
movsbl %bpl,%r14d
xor    %eax,%eax
call   1040 <__VERIFIER_nondet_U8@plt>
movzbl %al,%eax
mov    %eax,%ebp
neg    %ebp
add    %r14d,%ebx
cmovg  %eax,%ebp
mov    %ebp,%edi
call   1030 <__startrek_write_R_count@plt>
jmp    1260 <nxt_motor_get_count+0xc0>
xor    %eax,%eax
call   10a0 <__startrek_read_T_speed@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   10b0 <__startrek_read_T_count@plt>
mov    %eax,%ebx
test   %bpl,%bpl
je     1262 <nxt_motor_get_count+0xc2>
movsbl %bpl,%r14d
xor    %eax,%eax
call   1040 <__VERIFIER_nondet_U8@plt>
movzbl %al,%eax
mov    %eax,%ebp
neg    %ebp
add    %r14d,%ebx
cmovg  %eax,%ebp
mov    %ebp,%edi
call   1050 <__startrek_write_T_count@plt>
jmp    1260 <nxt_motor_get_count+0xc0>
xor    %eax,%eax
call   1090 <__startrek_read_W_speed@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   10c0 <__startrek_read_W_count@plt>
mov    %eax,%ebx
test   %bpl,%bpl
je     1262 <nxt_motor_get_count+0xc2>
movsbl %bpl,%r14d
xor    %eax,%eax
call   1040 <__VERIFIER_nondet_U8@plt>
movzbl %al,%eax
mov    %eax,%ebp
neg    %ebp
add    %r14d,%ebx
cmovg  %eax,%ebp
mov    %ebp,%edi
call   1070 <__startrek_write_W_count@plt>
mov    %ebp,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @nxt_motor_get_count(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8
%8 = alloca i8
%9 = alloca i8
%10 = alloca i8
store i32 %0, i32* %3
store i32 0, i32* %6
store i8 0, i8* %7
%11 = load i32, i32* %3
switch i32 %11, label %93 [
i32 0, label %12
i32 1, label %39
i32 2, label %66
]
12: ; preds = %1
%13 = call signext i8 (...) @__startrek_read_R_speed()
store i8 %13, i8* %8
%14 = load i8, i8* %8
store i8 %14, i8* %7
%15 = call i32 (...) @__startrek_read_R_count()
store i32 %15, i32* %6
%16 = load i8, i8* %7
%17 = sext i8 %16 to i32
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %12
%20 = load i32, i32* %6
store i32 %20, i32* %2
br label %96
21: ; preds = %12
%22 = call zeroext i8 (...) @__VERIFIER_nondet_U8()
store i8 %22, i8* %4
%23 = load i32, i32* %6
%24 = load i8, i8* %7
%25 = sext i8 %24 to i32
%26 = add nsw i32 %23, %25
%27 = icmp sgt i32 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %21
%29 = load i8, i8* %4
%30 = zext i8 %29 to i32
store i32 %30, i32* %5
br label %35
31: ; preds = %21
%32 = load i8, i8* %4
%33 = zext i8 %32 to i32
%34 = sub nsw i32 0, %33
store i32 %34, i32* %5
br label %35
35: ; preds = %31, %28
%36 = load i32, i32* %5
%37 = call i32 @__startrek_write_R_count(i32 %36)
br label %38
38: ; preds = %35
br label %94
39: ; preds = %1
%40 = call signext i8 (...) @__startrek_read_W_speed()
store i8 %40, i8* %9
%41 = load i8, i8* %9
store i8 %41, i8* %7
%42 = call i32 (...) @__startrek_read_W_count()
store i32 %42, i32* %6
%43 = load i8, i8* %7
%44 = sext i8 %43 to i32
%45 = icmp eq i32 %44, 0
br i1 %45, label %46, label %48
46: ; preds = %39
%47 = load i32, i32* %6
store i32 %47, i32* %2
br label %96
48: ; preds = %39
%49 = call zeroext i8 (...) @__VERIFIER_nondet_U8()
store i8 %49, i8* %4
%50 = load i32, i32* %6
%51 = load i8, i8* %7
%52 = sext i8 %51 to i32
%53 = add nsw i32 %50, %52
%54 = icmp sgt i32 %53, 0
br i1 %54, label %55, label %58
55: ; preds = %48
%56 = load i8, i8* %4
%57 = zext i8 %56 to i32
store i32 %57, i32* %5
br label %62
58: ; preds = %48
%59 = load i8, i8* %4
%60 = zext i8 %59 to i32
%61 = sub nsw i32 0, %60
store i32 %61, i32* %5
br label %62
62: ; preds = %58, %55
%63 = load i32, i32* %5
%64 = call i32 @__startrek_write_W_count(i32 %63)
br label %65
65: ; preds = %62
br label %94
66: ; preds = %1
%67 = call signext i8 (...) @__startrek_read_T_speed()
store i8 %67, i8* %10
%68 = load i8, i8* %10
store i8 %68, i8* %7
%69 = call i32 (...) @__startrek_read_T_count()
store i32 %69, i32* %6
%70 = load i8, i8* %7
%71 = sext i8 %70 to i32
%72 = icmp eq i32 %71, 0
br i1 %72, label %73, label %75
73: ; preds = %66
%74 = load i32, i32* %6
store i32 %74, i32* %2
br label %96
75: ; preds = %66
%76 = call zeroext i8 (...) @__VERIFIER_nondet_U8()
store i8 %76, i8* %4
%77 = load i32, i32* %6
%78 = load i8, i8* %7
%79 = sext i8 %78 to i32
%80 = add nsw i32 %77, %79
%81 = icmp sgt i32 %80, 0
br i1 %81, label %82, label %85
82: ; preds = %75
%83 = load i8, i8* %4
%84 = zext i8 %83 to i32
store i32 %84, i32* %5
br label %89
85: ; preds = %75
%86 = load i8, i8* %4
%87 = zext i8 %86 to i32
%88 = sub nsw i32 0, %87
store i32 %88, i32* %5
br label %89
89: ; preds = %85, %82
%90 = load i32, i32* %5
%91 = call i32 @__startrek_write_T_count(i32 %90)
br label %92
92: ; preds = %89
br label %94
93: ; preds = %1
br label %94
94: ; preds = %93, %92, %65, %38
%95 = load i32, i32* %5
store i32 %95, i32* %2
br label %96
96: ; preds = %94, %73, %46, %19
%97 = load i32, i32* %2
ret i32 %97
}

LLVM IR (-O2):
define i32 @nxt_motor_get_count(i32 %0) {
switch i32 %0, label %41 [
i32 0, label %2
i32 1, label %15
i32 2, label %28
]
2: ; preds = %1
%3 = tail call signext i8 (...) @__startrek_read_R_speed()
%4 = tail call i32 (...) @__startrek_read_R_count()
%5 = icmp eq i8 %3, 0
br i1 %5, label %41, label %6
6: ; preds = %2
%7 = sext i8 %3 to i32
%8 = tail call zeroext i8 (...) @__VERIFIER_nondet_U8()
%9 = add nsw i32 %4, %7
%10 = icmp sgt i32 %9, 0
%11 = zext i8 %8 to i32
%12 = sub nsw i32 0, %11
%13 = select i1 %10, i32 %11, i32 %12
%14 = tail call i32 @__startrek_write_R_count(i32 %13)
br label %41
15: ; preds = %1
%16 = tail call signext i8 (...) @__startrek_read_W_speed()
%17 = tail call i32 (...) @__startrek_read_W_count()
%18 = icmp eq i8 %16, 0
br i1 %18, label %41, label %19
19: ; preds = %15
%20 = sext i8 %16 to i32
%21 = tail call zeroext i8 (...) @__VERIFIER_nondet_U8()
%22 = add nsw i32 %17, %20
%23 = icmp sgt i32 %22, 0
%24 = zext i8 %21 to i32
%25 = sub nsw i32 0, %24
%26 = select i1 %23, i32 %24, i32 %25
%27 = tail call i32 @__startrek_write_W_count(i32 %26)
br label %41
28: ; preds = %1
%29 = tail call signext i8 (...) @__startrek_read_T_speed()
%30 = tail call i32 (...) @__startrek_read_T_count()
%31 = icmp eq i8 %29, 0
br i1 %31, label %41, label %32
32: ; preds = %28
%33 = sext i8 %29 to i32
%34 = tail call zeroext i8 (...) @__VERIFIER_nondet_U8()
%35 = add nsw i32 %30, %33
%36 = icmp sgt i32 %35, 0
%37 = zext i8 %34 to i32
%38 = sub nsw i32 0, %37
%39 = select i1 %36, i32 %37, i32 %38
%40 = tail call i32 @__startrek_write_T_count(i32 %39)
br label %41
41: ; preds = %6, %19, %32, %1, %28, %15, %2
%42 = phi i32 [ %4, %2 ], [ %17, %15 ], [ %30, %28 ], [ undef, %1 ], [ %39, %32 ], [ %26, %19 ], [ %13, %6 ]
ret i32 %42
}