#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_R_speed(int);
int /*<<< orphan*/ __startrek_write_T_count(int);
int /*<<< orphan*/ __startrek_write_T_speed(int);
int /*<<< orphan*/ __startrek_write_W_count(int);
int /*<<< orphan*/ __startrek_write_W_speed(int);
void nxt_motor_set_speed(unsigned int n, int speed, _Bool b) {
  unsigned char diff;
  char prev_speed;
  int tmp;
  int tmp___0;
  int tmp___1;
  int tmp___2;
  int tmp___3;
  int tmp___4;
  {
    switch (n) {
    case 0:
      prev_speed = __startrek_read_R_speed();
      if (prev_speed != 0) {
        diff = __VERIFIER_nondet_U8();
        tmp = __startrek_read_R_count();
        if (prev_speed > 0) {
          tmp___0 = diff;
        } else {
          tmp___0 = -((int)diff);
        }
        __startrek_write_R_count(tmp + tmp___0);
      }
      __startrek_write_R_speed(speed);
      return;
    case 1:
      prev_speed = __startrek_read_W_speed();
      if (prev_speed != 0) {
        diff = __VERIFIER_nondet_U8();
        tmp___1 = __startrek_read_W_count();
        if (prev_speed > 0) {
          tmp___2 = diff;
        } else {
          tmp___2 = -((int)diff);
        }
        __startrek_write_W_count(tmp___1 + tmp___2);
      }
      __startrek_write_W_speed(speed);
      return;
    case 2:
      prev_speed = __startrek_read_T_speed();
      if (prev_speed != 0) {
        diff = __VERIFIER_nondet_U8();
        tmp___3 = __startrek_read_T_count();
        if (prev_speed > 0) {
          tmp___4 = diff;
        } else {
          tmp___4 = -((int)diff);
        }
        __startrek_write_T_count(tmp___3 + tmp___4);
      }
      __startrek_write_T_speed(speed);
      return;
    }
    return;
  }
}


x86 assembly (clang, -O0):
<nxt_motor_set_speed>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %dl,%al
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
and    $0x1,%al
mov    %al,-0x9(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x28(%rbp)
test   %eax,%eax
je     121a <nxt_motor_set_speed+0x4a>
jmp    11f8 <nxt_motor_set_speed+0x28>
mov    -0x28(%rbp),%eax
sub    $0x1,%eax
je     1281 <nxt_motor_set_speed+0xb1>
jmp    1209 <nxt_motor_set_speed+0x39>
mov    -0x28(%rbp),%eax
sub    $0x2,%eax
je     12e8 <nxt_motor_set_speed+0x118>
jmp    134f <nxt_motor_set_speed+0x17f>
mov    $0x0,%al
call   10a0 <__startrek_read_R_speed@plt>
mov    %al,-0xb(%rbp)
movsbl -0xb(%rbp),%eax
cmp    $0x0,%eax
je     1274 <nxt_motor_set_speed+0xa4>
mov    $0x0,%al
call   1050 <__VERIFIER_nondet_U8@plt>
mov    %al,-0xa(%rbp)
mov    $0x0,%al
call   1080 <__startrek_read_R_count@plt>
mov    %eax,-0x10(%rbp)
movsbl -0xb(%rbp),%eax
cmp    $0x0,%eax
jle    125e <nxt_motor_set_speed+0x8e>
movzbl -0xa(%rbp),%eax
mov    %eax,-0x14(%rbp)
jmp    1269 <nxt_motor_set_speed+0x99>
movzbl -0xa(%rbp),%ecx
xor    %eax,%eax
sub    %ecx,%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%edi
add    -0x14(%rbp),%edi
call   1030 <__startrek_write_R_count@plt>
mov    -0x8(%rbp),%edi
call   10f0 <__startrek_write_R_speed@plt>
jmp    1354 <nxt_motor_set_speed+0x184>
mov    $0x0,%al
call   10b0 <__startrek_read_W_speed@plt>
mov    %al,-0xb(%rbp)
movsbl -0xb(%rbp),%eax
cmp    $0x0,%eax
je     12db <nxt_motor_set_speed+0x10b>
mov    $0x0,%al
call   1050 <__VERIFIER_nondet_U8@plt>
mov    %al,-0xa(%rbp)
mov    $0x0,%al
call   10e0 <__startrek_read_W_count@plt>
mov    %eax,-0x18(%rbp)
movsbl -0xb(%rbp),%eax
cmp    $0x0,%eax
jle    12c5 <nxt_motor_set_speed+0xf5>
movzbl -0xa(%rbp),%eax
mov    %eax,-0x1c(%rbp)
jmp    12d0 <nxt_motor_set_speed+0x100>
movzbl -0xa(%rbp),%ecx
xor    %eax,%eax
sub    %ecx,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%edi
add    -0x1c(%rbp),%edi
call   1090 <__startrek_write_W_count@plt>
mov    -0x8(%rbp),%edi
call   1060 <__startrek_write_W_speed@plt>
jmp    1354 <nxt_motor_set_speed+0x184>
mov    $0x0,%al
call   10c0 <__startrek_read_T_speed@plt>
mov    %al,-0xb(%rbp)
movsbl -0xb(%rbp),%eax
cmp    $0x0,%eax
je     1342 <nxt_motor_set_speed+0x172>
mov    $0x0,%al
call   1050 <__VERIFIER_nondet_U8@plt>
mov    %al,-0xa(%rbp)
mov    $0x0,%al
call   10d0 <__startrek_read_T_count@plt>
mov    %eax,-0x20(%rbp)
movsbl -0xb(%rbp),%eax
cmp    $0x0,%eax
jle    132c <nxt_motor_set_speed+0x15c>
movzbl -0xa(%rbp),%eax
mov    %eax,-0x24(%rbp)
jmp    1337 <nxt_motor_set_speed+0x167>
movzbl -0xa(%rbp),%ecx
xor    %eax,%eax
sub    %ecx,%eax
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%edi
add    -0x24(%rbp),%edi
call   1070 <__startrek_write_T_count@plt>
mov    -0x8(%rbp),%edi
call   1040 <__startrek_write_T_speed@plt>
jmp    1354 <nxt_motor_set_speed+0x184>
jmp    1354 <nxt_motor_set_speed+0x184>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nxt_motor_set_speed>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%ebp
cmp    $0x2,%edi
je     1229 <nxt_motor_set_speed+0x59>
cmp    $0x1,%edi
je     1266 <nxt_motor_set_speed+0x96>
test   %edi,%edi
jne    12a3 <nxt_motor_set_speed+0xd3>
xor    %eax,%eax
call   10a0 <__startrek_read_R_speed@plt>
test   %al,%al
je     121e <nxt_motor_set_speed+0x4e>
mov    %eax,%ebx
xor    %eax,%eax
call   1050 <__VERIFIER_nondet_U8@plt>
mov    %eax,%r14d
xor    %eax,%eax
call   1080 <__startrek_read_R_count@plt>
movzbl %r14b,%ecx
mov    %ecx,%edi
neg    %edi
test   %bl,%bl
cmovg  %ecx,%edi
add    %eax,%edi
call   1030 <__startrek_write_R_count@plt>
mov    %ebp,%edi
pop    %rbx
pop    %r14
pop    %rbp
jmp    10f0 <__startrek_write_R_speed@plt>
xor    %eax,%eax
call   10c0 <__startrek_read_T_speed@plt>
test   %al,%al
je     125b <nxt_motor_set_speed+0x8b>
mov    %eax,%ebx
xor    %eax,%eax
call   1050 <__VERIFIER_nondet_U8@plt>
mov    %eax,%r14d
xor    %eax,%eax
call   10d0 <__startrek_read_T_count@plt>
movzbl %r14b,%ecx
mov    %ecx,%edi
neg    %edi
test   %bl,%bl
cmovg  %ecx,%edi
add    %eax,%edi
call   1070 <__startrek_write_T_count@plt>
mov    %ebp,%edi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1040 <__startrek_write_T_speed@plt>
xor    %eax,%eax
call   10b0 <__startrek_read_W_speed@plt>
test   %al,%al
je     1298 <nxt_motor_set_speed+0xc8>
mov    %eax,%ebx
xor    %eax,%eax
call   1050 <__VERIFIER_nondet_U8@plt>
mov    %eax,%r14d
xor    %eax,%eax
call   10e0 <__startrek_read_W_count@plt>
movzbl %r14b,%ecx
mov    %ecx,%edi
neg    %edi
test   %bl,%bl
cmovg  %ecx,%edi
add    %eax,%edi
call   1090 <__startrek_write_W_count@plt>
mov    %ebp,%edi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1060 <__startrek_write_W_speed@plt>
pop    %rbx
pop    %r14
pop    %rbp
ret

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

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