#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*/ tio;
struct termios {
  int c_cflag;
  int *c_cc;
  scalar_t__ c_oflag;
  int /*<<< orphan*/ c_iflag;
};

/* Variables and functions */
int B1200;
int CLOCAL;
int CREAD;
int CS8;
int /*<<< orphan*/ IGNPAR;
int O_NOCTTY;
int O_NONBLOCK;
int O_RDWR;
int /*<<< orphan*/ TCIOFLUSH;
int /*<<< orphan*/ TCSANOW;
size_t VMIN;
int /*<<< orphan*/ bzero(struct termios *, int);
int /*<<< orphan*/ exit(int);
int getAim();
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int readStatus(int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ tcflush(int, int /*<<< orphan*/);
int /*<<< orphan*/ tcgetattr(int, struct termios *);
int /*<<< orphan*/ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int /*<<< orphan*/ writeCurr(int, int);
int /*<<< orphan*/ writeDeg(int, int);
int /*<<< orphan*/ writeStatus(int);
int /*<<< orphan*/ writeStop(int);
int main(int argc, char *argv[]) {
  int fd, c, res, i;
  struct termios oldtio, tio;
  char buf[255];
  fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NONBLOCK);
  if (fd < 0) {
    perror("/dev/ttyUSB0");
    exit(-1);
  }
  tcgetattr(fd, &oldtio);
  bzero(&tio, sizeof(tio));
  tio.c_iflag = IGNPAR;
  tio.c_cflag = B1200 | CLOCAL | CS8 | CREAD;
  tio.c_oflag = 0;
  tio.c_cc[VMIN] = 5;
  tcflush(fd, TCIOFLUSH);
  tcsetattr(fd, TCSANOW, &tio);
  if (argc > 1) {
    writeStop(fd);
    printf("StopStatus: %d\n", readStatus(fd));
    return 0;
  }
  writeStop(fd);
  int start = readStatus(fd);
  if (start == -1) {
    printf("Failed to stop\n");
    return 1;
  }
  writeCurr(start, start);
  while (1) {
    int aim = getAim();
    if (aim == -1 || aim == start) {
      sleep(1);
      continue;
    }
    writeCurr(start, aim);
    int small_aim = aim;
    if (((start - aim) < 0 ? -(start - aim) : (start - aim)) > 45) {
      if (start > aim) {
        small_aim = start - 45;
      } else {
        small_aim = start + 45;
      }
    }
    printf("Start: %d\n", start);
    printf("Real Aim: %d\n", aim);
    printf("Small Aim: %d\n", small_aim);
    writeDeg(fd, small_aim);
    int curr = -1;
    int last = -1;
    int stop = 0;
    while (small_aim != curr) {
      sleep(1);
      if (getAim() == -1) {
        printf("stop\n");
        stop = 1;
        break;
      }
      writeStatus(fd);
      curr = readStatus(fd);
      writeCurr(curr, aim);
      printf("curr: %d (%d > %d == bad)\n", curr,
             ((curr - aim) < 0 ? -(curr - aim) : (curr - aim)),
             ((start - aim) < 0 ? -(start - aim) : (start - aim)));
      if (((curr - aim) < 0 ? -(curr - aim) : (curr - aim)) >
          ((start - aim) < 0 ? -(start - aim) : (start - aim))) {
        printf("Wrong dir... exit\n");
        break;
      }
      int c = 2;
      while (curr == last) {
        if (c-- > 0) {
          printf("not turning... retry\n");
          sleep(1);
          writeStatus(fd);
          curr = readStatus(fd);
          writeCurr(curr, aim);
          if (curr != last) {
            break;
          }
        }
        break;
      }
      if (curr == last) {
        break;
      }
      last = curr;
    }
    sleep(1);
    writeStop(fd);
    sleep(1);
    start = readStatus(fd);
    if (stop) {
      aim = start;
    }
    writeCurr(start, aim);
    printf("StopStatus: %d\n", start);
  }
  tcsetattr(fd, TCSANOW, &oldtio);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1a0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2dd8(%rip),%rax
mov    (%rax),%esi
mov    0x2d97(%rip),%rax
or     (%rax),%esi
mov    0x2dbe(%rip),%rax
or     (%rax),%esi
lea    0xdd5(%rip),%rdi
call   10d0 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1253 <main+0x63>
lea    0xdbc(%rip),%rdi
call   10e0 <perror@plt>
mov    $0xffffffff,%edi
call   1100 <exit@plt>
mov    -0x14(%rbp),%edi
lea    -0x40(%rbp),%rsi
call   10b0 <tcgetattr@plt>
lea    -0x60(%rbp),%rdi
mov    $0x20,%esi
call   10f0 <bzero@plt>
mov    0x2d24(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x48(%rbp)
mov    0x2d20(%rip),%rax
mov    (%rax),%eax
mov    0x2cff(%rip),%rcx
or     (%rcx),%eax
mov    0x2d36(%rip),%rcx
or     (%rcx),%eax
mov    0x2d35(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x60(%rbp)
movq   $0x0,-0x50(%rbp)
mov    -0x58(%rbp),%rax
mov    0x2d0d(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x5,(%rax,%rcx,4)
mov    -0x14(%rbp),%edi
mov    0x2ce1(%rip),%rax
mov    (%rax),%esi
call   1060 <tcflush@plt>
mov    -0x14(%rbp),%edi
mov    0x2d00(%rip),%rax
mov    (%rax),%esi
lea    -0x60(%rbp),%rdx
call   10c0 <tcsetattr@plt>
cmpl   $0x1,-0x8(%rbp)
jle    1319 <main+0x129>
mov    -0x14(%rbp),%edi
call   1080 <writeStop@plt>
mov    -0x14(%rbp),%edi
call   10a0 <readStatus@plt>
mov    %eax,%esi
lea    0xd07(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    17c0 <main+0x5d0>
mov    -0x14(%rbp),%edi
call   1080 <writeStop@plt>
mov    -0x14(%rbp),%edi
call   10a0 <readStatus@plt>
mov    %eax,-0x164(%rbp)
cmpl   $0xffffffff,-0x164(%rbp)
jne    1356 <main+0x166>
lea    0xcda(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    17c0 <main+0x5d0>
mov    -0x164(%rbp),%edi
mov    -0x164(%rbp),%esi
call   1090 <writeCurr@plt>
mov    $0x0,%al
call   1040 <getAim@plt>
mov    %eax,-0x168(%rbp)
cmpl   $0xffffffff,-0x168(%rbp)
je     1393 <main+0x1a3>
mov    -0x168(%rbp),%eax
cmp    -0x164(%rbp),%eax
jne    13a2 <main+0x1b2>
mov    $0x1,%edi
call   1110 <sleep@plt>
jmp    1367 <main+0x177>
mov    -0x164(%rbp),%edi
mov    -0x168(%rbp),%esi
call   1090 <writeCurr@plt>
mov    -0x168(%rbp),%eax
mov    %eax,-0x16c(%rbp)
mov    -0x164(%rbp),%eax
sub    -0x168(%rbp),%eax
cmp    $0x0,%eax
jge    13ef <main+0x1ff>
mov    -0x164(%rbp),%ecx
sub    -0x168(%rbp),%ecx
xor    %eax,%eax
sub    %ecx,%eax
mov    %eax,-0x180(%rbp)
jmp    1401 <main+0x211>
mov    -0x164(%rbp),%eax
sub    -0x168(%rbp),%eax
mov    %eax,-0x180(%rbp)
mov    -0x180(%rbp),%eax
cmp    $0x2d,%eax
jle    144a <main+0x25a>
mov    -0x164(%rbp),%eax
cmp    -0x168(%rbp),%eax
jle    1436 <main+0x246>
mov    -0x164(%rbp),%eax
sub    $0x2d,%eax
mov    %eax,-0x16c(%rbp)
jmp    1445 <main+0x255>
mov    -0x164(%rbp),%eax
add    $0x2d,%eax
mov    %eax,-0x16c(%rbp)
jmp    144a <main+0x25a>
mov    -0x164(%rbp),%esi
lea    0xbd6(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x168(%rbp),%esi
lea    0xbcd(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x16c(%rbp),%esi
lea    0xbc7(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x14(%rbp),%edi
mov    -0x16c(%rbp),%esi
call   1050 <writeDeg@plt>
movl   $0xffffffff,-0x170(%rbp)
ff ff ff
movl   $0xffffffff,-0x174(%rbp)
ff ff ff
movl   $0x0,-0x178(%rbp)
mov    -0x16c(%rbp),%eax
cmp    -0x170(%rbp),%eax
je     1753 <main+0x563>
mov    $0x1,%edi
call   1110 <sleep@plt>
mov    $0x0,%al
call   1040 <getAim@plt>
cmp    $0xffffffff,%eax
jne    14fb <main+0x30b>
lea    0xb42(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x1,-0x178(%rbp)
jmp    1753 <main+0x563>
mov    -0x14(%rbp),%edi
call   1030 <writeStatus@plt>
mov    -0x14(%rbp),%edi
call   10a0 <readStatus@plt>
mov    %eax,-0x170(%rbp)
mov    -0x170(%rbp),%edi
mov    -0x168(%rbp),%esi
call   1090 <writeCurr@plt>
mov    -0x170(%rbp),%eax
mov    %eax,-0x184(%rbp)
mov    -0x170(%rbp),%eax
sub    -0x168(%rbp),%eax
cmp    $0x0,%eax
jge    155e <main+0x36e>
mov    -0x170(%rbp),%ecx
sub    -0x168(%rbp),%ecx
xor    %eax,%eax
sub    %ecx,%eax
mov    %eax,-0x188(%rbp)
jmp    1570 <main+0x380>
mov    -0x170(%rbp),%eax
sub    -0x168(%rbp),%eax
mov    %eax,-0x188(%rbp)
mov    -0x188(%rbp),%eax
mov    %eax,-0x18c(%rbp)
mov    -0x164(%rbp),%eax
sub    -0x168(%rbp),%eax
cmp    $0x0,%eax
jge    15ac <main+0x3bc>
mov    -0x164(%rbp),%ecx
sub    -0x168(%rbp),%ecx
xor    %eax,%eax
sub    %ecx,%eax
mov    %eax,-0x190(%rbp)
jmp    15be <main+0x3ce>
mov    -0x164(%rbp),%eax
sub    -0x168(%rbp),%eax
mov    %eax,-0x190(%rbp)
mov    -0x18c(%rbp),%edx
mov    -0x184(%rbp),%esi
mov    -0x190(%rbp),%ecx
lea    0xa7e(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x170(%rbp),%eax
sub    -0x168(%rbp),%eax
cmp    $0x0,%eax
jge    160e <main+0x41e>
mov    -0x170(%rbp),%ecx
sub    -0x168(%rbp),%ecx
xor    %eax,%eax
sub    %ecx,%eax
mov    %eax,-0x194(%rbp)
jmp    1620 <main+0x430>
mov    -0x170(%rbp),%eax
sub    -0x168(%rbp),%eax
mov    %eax,-0x194(%rbp)
mov    -0x194(%rbp),%eax
mov    %eax,-0x198(%rbp)
mov    -0x164(%rbp),%eax
sub    -0x168(%rbp),%eax
cmp    $0x0,%eax
jge    165c <main+0x46c>
mov    -0x164(%rbp),%ecx
sub    -0x168(%rbp),%ecx
xor    %eax,%eax
sub    %ecx,%eax
mov    %eax,-0x19c(%rbp)
jmp    166e <main+0x47e>
mov    -0x164(%rbp),%eax
sub    -0x168(%rbp),%eax
mov    %eax,-0x19c(%rbp)
mov    -0x198(%rbp),%eax
mov    -0x19c(%rbp),%ecx
cmp    %ecx,%eax
jle    1695 <main+0x4a5>
lea    0x9e7(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
jmp    1753 <main+0x563>
movl   $0x2,-0x17c(%rbp)
mov    -0x170(%rbp),%eax
cmp    -0x174(%rbp),%eax
jne    172b <main+0x53b>
mov    -0x17c(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x17c(%rbp)
cmp    $0x0,%eax
jle    1726 <main+0x536>
lea    0x9b1(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    $0x1,%edi
call   1110 <sleep@plt>
mov    -0x14(%rbp),%edi
call   1030 <writeStatus@plt>
mov    -0x14(%rbp),%edi
call   10a0 <readStatus@plt>
mov    %eax,-0x170(%rbp)
mov    -0x170(%rbp),%edi
mov    -0x168(%rbp),%esi
call   1090 <writeCurr@plt>
mov    -0x170(%rbp),%eax
cmp    -0x174(%rbp),%eax
je     1721 <main+0x531>
jmp    172b <main+0x53b>
jmp    1726 <main+0x536>
jmp    172b <main+0x53b>
mov    -0x170(%rbp),%eax
cmp    -0x174(%rbp),%eax
jne    1742 <main+0x552>
jmp    1753 <main+0x563>
mov    -0x170(%rbp),%eax
mov    %eax,-0x174(%rbp)
jmp    14b2 <main+0x2c2>
mov    $0x1,%edi
call   1110 <sleep@plt>
mov    -0x14(%rbp),%edi
call   1080 <writeStop@plt>
mov    $0x1,%edi
call   1110 <sleep@plt>
mov    -0x14(%rbp),%edi
call   10a0 <readStatus@plt>
mov    %eax,-0x164(%rbp)
cmpl   $0x0,-0x178(%rbp)
je     1796 <main+0x5a6>
mov    -0x164(%rbp),%eax
mov    %eax,-0x168(%rbp)
mov    -0x164(%rbp),%edi
mov    -0x168(%rbp),%esi
call   1090 <writeCurr@plt>
mov    -0x164(%rbp),%esi
lea    0x859(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
jmp    1367 <main+0x177>
mov    -0x4(%rbp),%eax
add    $0x1a0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x58,%rsp
mov    0x2dd3(%rip),%rax
mov    0x2d94(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %edi,%r15d
mov    0x2db6(%rip),%rax
or     (%rax),%esi
lea    0xdcd(%rip),%rdi
call   10e0 <open@plt>
test   %eax,%eax
js     1524 <main+0x324>
mov    %eax,%ebp
lea    0x38(%rsp),%rsi
mov    %eax,%edi
call   10c0 <tcgetattr@plt>
lea    0x18(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x20,%esi
call   1100 <bzero@plt>
mov    0x2d39(%rip),%rax
mov    0x2d1a(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2d4f(%rip),%rax
or     (%rax),%ecx
mov    0x2d16(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x30(%rsp)
mov    0x2d41(%rip),%rax
or     (%rax),%ecx
mov    %ecx,0x18(%rsp)
movq   $0x0,0x28(%rsp)
mov    0x20(%rsp),%rax
mov    0x2d16(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x5,(%rax,%rcx,4)
mov    0x2ced(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1070 <tcflush@plt>
mov    0x2d0d(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %rbx,%rdx
call   10d0 <tcsetattr@plt>
mov    %ebp,%edi
call   1090 <writeStop@plt>
mov    %ebp,0xc(%rsp)
mov    %ebp,%edi
call   10b0 <readStatus@plt>
mov    %eax,%r14d
cmp    $0x2,%r15d
jl     130a <main+0x10a>
lea    0xd14(%rip),%rdi
xor    %ebx,%ebx
mov    %r14d,%esi
xor    %eax,%eax
call   1080 <printf@plt>
jmp    1513 <main+0x313>
cmp    $0xffffffff,%r14d
je     1502 <main+0x302>
mov    %r14d,%edi
mov    %r14d,%esi
call   10a0 <writeCurr@plt>
mov    0xc(%rsp),%ebp
jmp    133a <main+0x13a>
cs nopw 0x0(%rax,%rax,1)
nop
mov    $0x1,%edi
call   1120 <sleep@plt>
xor    %eax,%eax
call   1050 <getAim@plt>
cmp    $0xffffffff,%eax
je     1330 <main+0x130>
mov    %eax,%r15d
mov    %r14d,%ebx
sub    %eax,%ebx
je     1330 <main+0x130>
mov    %r14d,%edi
mov    %r15d,%esi
call   10a0 <writeCurr@plt>
mov    %ebx,%eax
neg    %eax
cmovs  %ebx,%eax
mov    %r15d,%ebx
mov    %eax,0x14(%rsp)
cmp    $0x2e,%eax
jb     137d <main+0x17d>
cmp    %r15d,%r14d
jle    1379 <main+0x179>
lea    -0x2d(%r14),%ebx
jmp    137d <main+0x17d>
lea    0x2d(%r14),%ebx
lea    0xc99(%rip),%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1080 <printf@plt>
lea    0xc93(%rip),%rdi
mov    %r15d,%esi
xor    %eax,%eax
call   1080 <printf@plt>
lea    0xc90(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1080 <printf@plt>
mov    %ebp,%edi
mov    %ebx,%esi
call   1060 <writeDeg@plt>
mov    $0xffffffff,%r12d
mov    %ebx,0x10(%rsp)
jmp    13e0 <main+0x1e0>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
cmp    %r12d,%ebx
mov    %ebx,%r12d
mov    0x10(%rsp),%ebx
je     14b4 <main+0x2b4>
movb   $0x1,0xb(%rsp)
cmp    %r12d,%ebx
je     14b4 <main+0x2b4>
mov    $0x1,%edi
call   1120 <sleep@plt>
xor    %eax,%eax
call   1050 <getAim@plt>
mov    %eax,%r14d
cmp    $0xffffffff,%eax
je     1492 <main+0x292>
mov    %ebp,%edi
call   1030 <writeStatus@plt>
mov    %ebp,%edi
call   10b0 <readStatus@plt>
mov    %eax,%ebx
mov    %eax,%edi
mov    %r15d,%esi
call   10a0 <writeCurr@plt>
mov    %ebx,%eax
sub    %r15d,%eax
mov    %eax,%ebp
neg    %ebp
cmovs  %eax,%ebp
lea    0xc0d(%rip),%rdi
mov    %ebx,%esi
mov    %ebp,%edx
mov    0x14(%rsp),%r13d
mov    %r13d,%ecx
xor    %eax,%eax
call   1080 <printf@plt>
cmp    %r13d,%ebp
ja     149b <main+0x29b>
cmp    %r12d,%ebx
mov    0xc(%rsp),%ebp
jne    13d0 <main+0x1d0>
lea    0xbfc(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   1120 <sleep@plt>
mov    %ebp,%edi
call   1030 <writeStatus@plt>
mov    %ebp,%edi
call   10b0 <readStatus@plt>
mov    %eax,%ebx
mov    %eax,%edi
mov    %r15d,%esi
call   10a0 <writeCurr@plt>
jmp    13d0 <main+0x1d0>
lea    0xbf8(%rip),%rdi
jmp    14a6 <main+0x2a6>
lea    0xbd3(%rip),%rdi
mov    0xc(%rsp),%ebp
cmp    $0xffffffff,%r14d
setne  0xb(%rsp)
call   1040 <puts@plt>
mov    $0x1,%edi
call   1120 <sleep@plt>
mov    %ebp,%edi
call   1090 <writeStop@plt>
mov    $0x1,%edi
call   1120 <sleep@plt>
mov    %ebp,%edi
call   10b0 <readStatus@plt>
mov    %eax,%r14d
cmpb   $0x0,0xb(%rsp)
cmove  %eax,%r15d
mov    %eax,%edi
mov    %r15d,%esi
call   10a0 <writeCurr@plt>
lea    0xb1a(%rip),%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1080 <printf@plt>
jmp    133a <main+0x13a>
lea    0xb7e(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x58,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xad5(%rip),%rdi
call   10f0 <perror@plt>
mov    $0xffffffff,%edi
call   1110 <exit@plt>

LLVM IR (-O0):
%struct.termios = type { i32, i32*, i64, i32 }
@.str = [13 x i8] c"/dev/ttyUSB0\00"
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_NONBLOCK = global i32 0
@IGNPAR = global i32 0
@B1200 = global i32 0
@CLOCAL = global i32 0
@CS8 = global i32 0
@CREAD = global i32 0
@VMIN = global i64 0
@TCIOFLUSH = global i32 0
@TCSANOW = global i32 0
@.str.1 = [16 x i8] c"StopStatus: %d\0A\00"
@.str.2 = [16 x i8] c"Failed to stop\0A\00"
@.str.3 = [11 x i8] c"Start: %d\0A\00"
@.str.4 = [14 x i8] c"Real Aim: %d\0A\00"
@.str.5 = [15 x i8] c"Small Aim: %d\0A\00"
@.str.6 = [6 x i8] c"stop\0A\00"
@.str.7 = [27 x i8] c"curr: %d (%d > %d == bad)\0A\00"
@.str.8 = [19 x i8] c"Wrong dir... exit\0A\00"
@.str.9 = [22 x i8] c"not turning... retry\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.termios
%11 = alloca %struct.termios
%12 = alloca [255 x i8]
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
%17 = alloca i32
%18 = alloca i32
%19 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%20 = load i32, i32* @O_RDWR
%21 = load i32, i32* @O_NOCTTY
%22 = or i32 %20, %21
%23 = load i32, i32* @O_NONBLOCK
%24 = or i32 %22, %23
%25 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %24)
store i32 %25, i32* %6
%26 = load i32, i32* %6
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %2
%29 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%30 = call i32 @exit(i32 -1)
unreachable
31: ; preds = %2
%32 = load i32, i32* %6
%33 = call i32 @tcgetattr(i32 %32, %struct.termios* %10)
%34 = call i32 @bzero(%struct.termios* %11, i32 32)
%35 = load i32, i32* @IGNPAR
%36 = getelementptr inbounds %struct.termios, %struct.termios* %11, i32 0, i32 3
store i32 %35, i32* %36
%37 = load i32, i32* @B1200
%38 = load i32, i32* @CLOCAL
%39 = or i32 %37, %38
%40 = load i32, i32* @CS8
%41 = or i32 %39, %40
%42 = load i32, i32* @CREAD
%43 = or i32 %41, %42
%44 = getelementptr inbounds %struct.termios, %struct.termios* %11, i32 0, i32 0
store i32 %43, i32* %44
%45 = getelementptr inbounds %struct.termios, %struct.termios* %11, i32 0, i32 2
store i64 0, i64* %45
%46 = getelementptr inbounds %struct.termios, %struct.termios* %11, i32 0, i32 1
%47 = load i32*, i32** %46
%48 = load i64, i64* @VMIN
%49 = getelementptr inbounds i32, i32* %47, i64 %48
store i32 5, i32* %49
%50 = load i32, i32* %6
%51 = load i32, i32* @TCIOFLUSH
%52 = call i32 @tcflush(i32 %50, i32 %51)
%53 = load i32, i32* %6
%54 = load i32, i32* @TCSANOW
%55 = call i32 @tcsetattr(i32 %53, i32 %54, %struct.termios* %11)
%56 = load i32, i32* %4
%57 = icmp sgt i32 %56, 1
br i1 %57, label %58, label %64
58: ; preds = %31
%59 = load i32, i32* %6
%60 = call i32 @writeStop(i32 %59)
%61 = load i32, i32* %6
%62 = call i32 @readStatus(i32 %61)
%63 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %62)
store i32 0, i32* %3
br label %261
64: ; preds = %31
%65 = load i32, i32* %6
%66 = call i32 @writeStop(i32 %65)
%67 = load i32, i32* %6
%68 = call i32 @readStatus(i32 %67)
store i32 %68, i32* %13
%69 = load i32, i32* %13
%70 = icmp eq i32 %69, -1
br i1 %70, label %71, label %73
71: ; preds = %64
%72 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
br label %261
73: ; preds = %64
%74 = load i32, i32* %13
%75 = load i32, i32* %13
%76 = call i32 @writeCurr(i32 %74, i32 %75)
br label %77
77: ; preds = %73, %85, %255
%78 = call i32 (...) @getAim()
store i32 %78, i32* %14
%79 = load i32, i32* %14
%80 = icmp eq i32 %79, -1
br i1 %80, label %85, label %81
81: ; preds = %77
%82 = load i32, i32* %14
%83 = load i32, i32* %13
%84 = icmp eq i32 %82, %83
br i1 %84, label %85, label %87
85: ; preds = %81, %77
%86 = call i32 @sleep(i32 1)
br label %77
87: ; preds = %81
%88 = load i32, i32* %13
%89 = load i32, i32* %14
%90 = call i32 @writeCurr(i32 %88, i32 %89)
%91 = load i32, i32* %14
store i32 %91, i32* %15
%92 = load i32, i32* %13
%93 = load i32, i32* %14
%94 = sub nsw i32 %92, %93
%95 = icmp slt i32 %94, 0
br i1 %95, label %96, label %101
96: ; preds = %87
%97 = load i32, i32* %13
%98 = load i32, i32* %14
%99 = sub nsw i32 %97, %98
%100 = sub nsw i32 0, %99
br label %105
101: ; preds = %87
%102 = load i32, i32* %13
%103 = load i32, i32* %14
%104 = sub nsw i32 %102, %103
br label %105
105: ; preds = %101, %96
%106 = phi i32 [ %100, %96 ], [ %104, %101 ]
%107 = icmp sgt i32 %106, 45
br i1 %107, label %108, label %119
108: ; preds = %105
%109 = load i32, i32* %13
%110 = load i32, i32* %14
%111 = icmp sgt i32 %109, %110
br i1 %111, label %112, label %115
112: ; preds = %108
%113 = load i32, i32* %13
%114 = sub nsw i32 %113, 45
store i32 %114, i32* %15
br label %118
115: ; preds = %108
%116 = load i32, i32* %13
%117 = add nsw i32 %116, 45
store i32 %117, i32* %15
br label %118
118: ; preds = %115, %112
br label %119
119: ; preds = %118, %105
%120 = load i32, i32* %13
%121 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i32 %120)
%122 = load i32, i32* %14
%123 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i32 %122)
%124 = load i32, i32* %15
%125 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0), i32 %124)
%126 = load i32, i32* %6
%127 = load i32, i32* %15
%128 = call i32 @writeDeg(i32 %126, i32 %127)
store i32 -1, i32* %16
store i32 -1, i32* %17
store i32 0, i32* %18
br label %129
129: ; preds = %242, %119
%130 = load i32, i32* %15
%131 = load i32, i32* %16
%132 = icmp ne i32 %130, %131
br i1 %132, label %133, label %244
133: ; preds = %129
%134 = call i32 @sleep(i32 1)
%135 = call i32 (...) @getAim()
%136 = icmp eq i32 %135, -1
br i1 %136, label %137, label %139
137: ; preds = %133
%138 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0))
store i32 1, i32* %18
br label %244
139: ; preds = %133
%140 = load i32, i32* %6
%141 = call i32 @writeStatus(i32 %140)
%142 = load i32, i32* %6
%143 = call i32 @readStatus(i32 %142)
store i32 %143, i32* %16
%144 = load i32, i32* %16
%145 = load i32, i32* %14
%146 = call i32 @writeCurr(i32 %144, i32 %145)
%147 = load i32, i32* %16
%148 = load i32, i32* %16
%149 = load i32, i32* %14
%150 = sub nsw i32 %148, %149
%151 = icmp slt i32 %150, 0
br i1 %151, label %152, label %157
152: ; preds = %139
%153 = load i32, i32* %16
%154 = load i32, i32* %14
%155 = sub nsw i32 %153, %154
%156 = sub nsw i32 0, %155
br label %161
157: ; preds = %139
%158 = load i32, i32* %16
%159 = load i32, i32* %14
%160 = sub nsw i32 %158, %159
br label %161
161: ; preds = %157, %152
%162 = phi i32 [ %156, %152 ], [ %160, %157 ]
%163 = load i32, i32* %13
%164 = load i32, i32* %14
%165 = sub nsw i32 %163, %164
%166 = icmp slt i32 %165, 0
br i1 %166, label %167, label %172
167: ; preds = %161
%168 = load i32, i32* %13
%169 = load i32, i32* %14
%170 = sub nsw i32 %168, %169
%171 = sub nsw i32 0, %170
br label %176
172: ; preds = %161
%173 = load i32, i32* %13
%174 = load i32, i32* %14
%175 = sub nsw i32 %173, %174
br label %176
176: ; preds = %172, %167
%177 = phi i32 [ %171, %167 ], [ %175, %172 ]
%178 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.7, i64 0, i64 0), i32 %147, i32 %162, i32 %177)
%179 = load i32, i32* %16
%180 = load i32, i32* %14
%181 = sub nsw i32 %179, %180
%182 = icmp slt i32 %181, 0
br i1 %182, label %183, label %188
183: ; preds = %176
%184 = load i32, i32* %16
%185 = load i32, i32* %14
%186 = sub nsw i32 %184, %185
%187 = sub nsw i32 0, %186
br label %192
188: ; preds = %176
%189 = load i32, i32* %16
%190 = load i32, i32* %14
%191 = sub nsw i32 %189, %190
br label %192
192: ; preds = %188, %183
%193 = phi i32 [ %187, %183 ], [ %191, %188 ]
%194 = load i32, i32* %13
%195 = load i32, i32* %14
%196 = sub nsw i32 %194, %195
%197 = icmp slt i32 %196, 0
br i1 %197, label %198, label %203
198: ; preds = %192
%199 = load i32, i32* %13
%200 = load i32, i32* %14
%201 = sub nsw i32 %199, %200
%202 = sub nsw i32 0, %201
br label %207
203: ; preds = %192
%204 = load i32, i32* %13
%205 = load i32, i32* %14
%206 = sub nsw i32 %204, %205
br label %207
207: ; preds = %203, %198
%208 = phi i32 [ %202, %198 ], [ %206, %203 ]
%209 = icmp sgt i32 %193, %208
br i1 %209, label %210, label %212
210: ; preds = %207
%211 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.8, i64 0, i64 0))
br label %244
212: ; preds = %207
store i32 2, i32* %19
br label %213
213: ; preds = %212
%214 = load i32, i32* %16
%215 = load i32, i32* %17
%216 = icmp eq i32 %214, %215
br i1 %216, label %217, label %237
217: ; preds = %213
%218 = load i32, i32* %19
%219 = add nsw i32 %218, -1
store i32 %219, i32* %19
%220 = icmp sgt i32 %218, 0
br i1 %220, label %221, label %236
221: ; preds = %217
%222 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.9, i64 0, i64 0))
%223 = call i32 @sleep(i32 1)
%224 = load i32, i32* %6
%225 = call i32 @writeStatus(i32 %224)
%226 = load i32, i32* %6
%227 = call i32 @readStatus(i32 %226)
store i32 %227, i32* %16
%228 = load i32, i32* %16
%229 = load i32, i32* %14
%230 = call i32 @writeCurr(i32 %228, i32 %229)
%231 = load i32, i32* %16
%232 = load i32, i32* %17
%233 = icmp ne i32 %231, %232
br i1 %233, label %234, label %235
234: ; preds = %221
br label %237
235: ; preds = %221
br label %236
236: ; preds = %235, %217
br label %237
237: ; preds = %236, %234, %213
%238 = load i32, i32* %16
%239 = load i32, i32* %17
%240 = icmp eq i32 %238, %239
br i1 %240, label %241, label %242
241: ; preds = %237
br label %244
242: ; preds = %237
%243 = load i32, i32* %16
store i32 %243, i32* %17
br label %129
244: ; preds = %241, %210, %137, %129
%245 = call i32 @sleep(i32 1)
%246 = load i32, i32* %6
%247 = call i32 @writeStop(i32 %246)
%248 = call i32 @sleep(i32 1)
%249 = load i32, i32* %6
%250 = call i32 @readStatus(i32 %249)
store i32 %250, i32* %13
%251 = load i32, i32* %18
%252 = icmp ne i32 %251, 0
br i1 %252, label %253, label %255
253: ; preds = %244
%254 = load i32, i32* %13
store i32 %254, i32* %14
br label %255
255: ; preds = %253, %244
%256 = load i32, i32* %13
%257 = load i32, i32* %14
%258 = call i32 @writeCurr(i32 %256, i32 %257)
%259 = load i32, i32* %13
%260 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %259)
br label %77
261: ; preds = %71, %58
%262 = load i32, i32* %3
ret i32 %262
}

LLVM IR (-O2):
%struct.termios = type { i32, i32*, i64, i32 }
@.str = [13 x i8] c"/dev/ttyUSB0\00"
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_NONBLOCK = global i32 0
@IGNPAR = global i32 0
@B1200 = global i32 0
@CLOCAL = global i32 0
@CS8 = global i32 0
@CREAD = global i32 0
@VMIN = global i64 0
@TCIOFLUSH = global i32 0
@TCSANOW = global i32 0
@.str.1 = [16 x i8] c"StopStatus: %d\0A\00"
@.str.3 = [11 x i8] c"Start: %d\0A\00"
@.str.4 = [14 x i8] c"Real Aim: %d\0A\00"
@.str.5 = [15 x i8] c"Small Aim: %d\0A\00"
@.str.7 = [27 x i8] c"curr: %d (%d > %d == bad)\0A\00"
@str = [21 x i8] c"not turning... retry\00"
@str.10 = [18 x i8] c"Wrong dir... exit\00"
@str.11 = [5 x i8] c"stop\00"
@str.12 = [15 x i8] c"Failed to stop\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.termios
%4 = alloca %struct.termios
%5 = bitcast %struct.termios* %3 to i8*
%6 = bitcast %struct.termios* %4 to i8*
%7 = load i32, i32* @O_RDWR
%8 = load i32, i32* @O_NOCTTY
%9 = or i32 %8, %7
%10 = load i32, i32* @O_NONBLOCK
%11 = or i32 %9, %10
%12 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %11)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%16 = tail call i32 @exit(i32 -1)
unreachable
17: ; preds = %2
%18 = call i32 @tcgetattr(i32 %12, %struct.termios* %3)
%19 = call i32 @bzero(%struct.termios* %4, i32 32)
%20 = load i32, i32* @IGNPAR
%21 = getelementptr inbounds %struct.termios, %struct.termios* %4, i64 0, i32 3
store i32 %20, i32* %21
%22 = load i32, i32* @B1200
%23 = load i32, i32* @CLOCAL
%24 = or i32 %23, %22
%25 = load i32, i32* @CS8
%26 = or i32 %24, %25
%27 = load i32, i32* @CREAD
%28 = or i32 %26, %27
%29 = getelementptr inbounds %struct.termios, %struct.termios* %4, i64 0, i32 0
store i32 %28, i32* %29
%30 = getelementptr inbounds %struct.termios, %struct.termios* %4, i64 0, i32 2
store i64 0, i64* %30
%31 = getelementptr inbounds %struct.termios, %struct.termios* %4, i64 0, i32 1
%32 = load i32*, i32** %31
%33 = load i64, i64* @VMIN
%34 = getelementptr inbounds i32, i32* %32, i64 %33
store i32 5, i32* %34
%35 = load i32, i32* @TCIOFLUSH
%36 = call i32 @tcflush(i32 %12, i32 %35)
%37 = load i32, i32* @TCSANOW
%38 = call i32 @tcsetattr(i32 %12, i32 %37, %struct.termios* %4)
%39 = icmp sgt i32 %0, 1
%40 = call i32 @writeStop(i32 %12)
%41 = call i32 @readStatus(i32 %12)
br i1 %39, label %42, label %44
42: ; preds = %17
%43 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %41)
br label %115
44: ; preds = %17
%45 = icmp eq i32 %41, -1
br i1 %45, label %46, label %48
46: ; preds = %44
%47 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.12, i64 0, i64 0))
br label %115
48: ; preds = %44
%49 = call i32 @writeCurr(i32 %41, i32 %41)
br label %50
50: ; preds = %106, %48
%51 = phi i32 [ %111, %106 ], [ %41, %48 ]
br label %52
52: ; preds = %50, %57
%53 = call i32 (...) @getAim()
%54 = icmp eq i32 %53, -1
%55 = icmp eq i32 %51, %53
%56 = select i1 %54, i1 true, i1 %55
br i1 %56, label %57, label %59
57: ; preds = %52
%58 = call i32 @sleep(i32 1)
br label %52
59: ; preds = %52
%60 = call i32 @writeCurr(i32 %51, i32 %53)
%61 = sub nsw i32 %51, %53
%62 = call i32 @llvm.abs.i32(i32 %61, i1 true)
%63 = icmp ugt i32 %62, 45
br i1 %63, label %64, label %70
64: ; preds = %59
%65 = icmp sgt i32 %51, %53
br i1 %65, label %66, label %68
66: ; preds = %64
%67 = add nsw i32 %51, -45
br label %70
68: ; preds = %64
%69 = add nsw i32 %51, 45
br label %70
70: ; preds = %66, %68, %59
%71 = phi i32 [ %67, %66 ], [ %69, %68 ], [ %53, %59 ]
%72 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i32 %51)
%73 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i32 %53)
%74 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0), i32 %71)
%75 = call i32 @writeDeg(i32 %12, i32 %71)
br label %76
76: ; preds = %99, %70
%77 = phi i32 [ -1, %70 ], [ %100, %99 ]
%78 = icmp eq i32 %71, %77
br i1 %78, label %106, label %79
79: ; preds = %76
%80 = call i32 @sleep(i32 1)
%81 = call i32 (...) @getAim()
%82 = icmp eq i32 %81, -1
br i1 %82, label %102, label %83
83: ; preds = %79
%84 = call i32 @writeStatus(i32 %12)
%85 = call i32 @readStatus(i32 %12)
%86 = call i32 @writeCurr(i32 %85, i32 %53)
%87 = sub nsw i32 %85, %53
%88 = call i32 @llvm.abs.i32(i32 %87, i1 true)
%89 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.7, i64 0, i64 0), i32 %85, i32 %88, i32 %62)
%90 = icmp ugt i32 %88, %62
br i1 %90, label %102, label %91
91: ; preds = %83
%92 = icmp eq i32 %85, %77
br i1 %92, label %93, label %99
93: ; preds = %91
%94 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
%95 = call i32 @sleep(i32 1)
%96 = call i32 @writeStatus(i32 %12)
%97 = call i32 @readStatus(i32 %12)
%98 = call i32 @writeCurr(i32 %97, i32 %53)
br label %99
99: ; preds = %93, %91
%100 = phi i32 [ %97, %93 ], [ %85, %91 ]
%101 = icmp eq i32 %100, %77
br i1 %101, label %106, label %76
102: ; preds = %83, %79
%103 = phi i8* [ getelementptr inbounds ([5 x i8], [5 x i8]* @str.11, i64 0, i64 0), %79 ], [ getelementptr inbounds ([18 x i8], [18 x i8]* @str.10, i64 0, i64 0), %83 ]
%104 = xor i1 %82, true
%105 = call i32 @puts(i8* dereferenceable(1) %103)
br label %106
106: ; preds = %99, %76, %102
%107 = phi i1 [ %104, %102 ], [ true, %76 ], [ true, %99 ]
%108 = call i32 @sleep(i32 1)
%109 = call i32 @writeStop(i32 %12)
%110 = call i32 @sleep(i32 1)
%111 = call i32 @readStatus(i32 %12)
%112 = select i1 %107, i32 %53, i32 %111
%113 = call i32 @writeCurr(i32 %111, i32 %112)
%114 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %111)
br label %50
115: ; preds = %46, %42
%116 = phi i32 [ 0, %42 ], [ 1, %46 ]
ret i32 %116
}