#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 */
double fabs(int);
int /*<<< orphan*/ get_motor_encoders(int *, int *);
int /*<<< orphan*/ log_trail();
int /*<<< orphan*/ reset_motor_encoders();
int /*<<< orphan*/ set_motors(int, int);
int /*<<< orphan*/ usleep(int);
void turn(float targetAngle, int direction) {
  int slowSpd = 15;
  int fastSpd = 40 - 15;
  int currentSpd;
  double percentTurned = 0;
  int leftenc = 0;
  int rightenc = 0;
  int target = (double)(210 * targetAngle) / 90;
  reset_motor_encoders();
  while (fabs(leftenc) < target && fabs(rightenc) < target) {
    get_motor_encoders(&leftenc, &rightenc);
    percentTurned = fabs(leftenc) / (double)target;
    if (percentTurned <= 0.5) {
      currentSpd = slowSpd + (int)(2 * percentTurned * (double)fastSpd);
    } else {
      currentSpd = slowSpd + (int)((1 - percentTurned) * (double)fastSpd);
    }
    if (direction == 1) {
      set_motors(currentSpd, -currentSpd);
    } else {
      set_motors(-currentSpd, currentSpd);
    }
    log_trail();
  }
  set_motors(0, 0);
  usleep(40000);
}


x86 assembly (clang, -O0):
<turn>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movss  %xmm0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
movl   $0xf,-0xc(%rbp)
movl   $0x19,-0x10(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
movss  0xe84(%rip),%xmm0
mulss  -0x4(%rbp),%xmm0
cvtss2sd %xmm0,%xmm0
movsd  0xe53(%rip),%xmm1
divsd  %xmm1,%xmm0
cvttsd2si %xmm0,%eax
mov    %eax,-0x2c(%rbp)
mov    $0x0,%al
call   1030 <reset_motor_encoders@plt>
mov    -0x24(%rbp),%edi
call   1050 <fabs@plt>
movaps %xmm0,%xmm1
cvtsi2sdl -0x2c(%rbp),%xmm0
xor    %eax,%eax
ucomisd %xmm1,%xmm0
mov    %al,-0x2d(%rbp)
jbe    11f8 <turn+0x98>
mov    -0x28(%rbp),%edi
call   1050 <fabs@plt>
movaps %xmm0,%xmm1
cvtsi2sdl -0x2c(%rbp),%xmm0
ucomisd %xmm1,%xmm0
seta   %al
mov    %al,-0x2d(%rbp)
mov    -0x2d(%rbp),%al
test   $0x1,%al
jne    1208 <turn+0xa8>
jmp    12bc <turn+0x15c>
lea    -0x24(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1060 <get_motor_encoders@plt>
mov    -0x24(%rbp),%edi
call   1050 <fabs@plt>
cvtsi2sdl -0x2c(%rbp),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  0xdd5(%rip),%xmm0
ucomisd -0x20(%rbp),%xmm0
jb     1265 <turn+0x105>
mov    -0xc(%rbp),%eax
movsd  0xdcf(%rip),%xmm0
mulsd  -0x20(%rbp),%xmm0
cvtsi2sdl -0x10(%rbp),%xmm1
mulsd  %xmm1,%xmm0
cvttsd2si %xmm0,%ecx
add    %ecx,%eax
mov    %eax,-0x14(%rbp)
jmp    1287 <turn+0x127>
mov    -0xc(%rbp),%eax
movsd  0xda0(%rip),%xmm0
subsd  -0x20(%rbp),%xmm0
cvtsi2sdl -0x10(%rbp),%xmm1
mulsd  %xmm1,%xmm0
cvttsd2si %xmm0,%ecx
add    %ecx,%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x1,-0x8(%rbp)
jne    12a3 <turn+0x143>
mov    -0x14(%rbp),%edi
xor    %esi,%esi
sub    -0x14(%rbp),%esi
call   1070 <set_motors@plt>
jmp    12b0 <turn+0x150>
xor    %edi,%edi
sub    -0x14(%rbp),%edi
mov    -0x14(%rbp),%esi
call   1070 <set_motors@plt>
mov    $0x0,%al
call   1040 <log_trail@plt>
jmp    11bf <turn+0x5f>
xor    %esi,%esi
mov    %esi,%edi
call   1070 <set_motors@plt>
mov    $0x9c40,%edi
call   1080 <usleep@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<turn>:
push   %r15
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %edi,%ebx
movl   $0x0,0x8(%rsp)
mulss  0xe85(%rip),%xmm0
movl   $0x0,0xc(%rsp)
cvtss2sd %xmm0,%xmm0
divsd  0xe79(%rip),%xmm0
movapd %xmm0,0x10(%rsp)
xor    %eax,%eax
call   1030 <reset_motor_encoders@plt>
xor    %edi,%edi
call   1050 <fabs@plt>
cvttpd2dq 0x10(%rsp),%xmm1
cvtdq2pd %xmm1,%xmm1
ucomisd %xmm0,%xmm1
jbe    128c <turn+0x12c>
xor    %edi,%edi
movapd %xmm1,0x10(%rsp)
call   1050 <fabs@plt>
movapd 0x10(%rsp),%xmm1
ucomisd %xmm0,%xmm1
jbe    128c <turn+0x12c>
lea    0x8(%rsp),%r14
lea    0xc(%rsp),%r15
xchg   %ax,%ax
mov    %r14,%rdi
mov    %r15,%rsi
call   1060 <get_motor_encoders@plt>
mov    0x8(%rsp),%edi
call   1050 <fabs@plt>
divsd  0x10(%rsp),%xmm0
movapd %xmm0,%xmm1
addsd  %xmm0,%xmm1
movsd  0xe06(%rip),%xmm2
subsd  %xmm0,%xmm2
cmpnlesd 0xe01(%rip),%xmm0
andpd  %xmm0,%xmm2
andnpd %xmm1,%xmm0
orpd   %xmm2,%xmm0
mulsd  0xdf5(%rip),%xmm0
cvttsd2si %xmm0,%esi
mov    $0xfffffff1,%eax
sub    %esi,%eax
add    $0xf,%esi
cmp    $0x1,%ebx
jne    1250 <turn+0xf0>
mov    %esi,%edi
mov    %eax,%esi
jmp    1252 <turn+0xf2>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %eax,%edi
call   1070 <set_motors@plt>
xor    %eax,%eax
call   1040 <log_trail@plt>
mov    0x8(%rsp),%edi
call   1050 <fabs@plt>
movapd 0x10(%rsp),%xmm1
ucomisd %xmm0,%xmm1
jbe    128c <turn+0x12c>
mov    0xc(%rsp),%edi
call   1050 <fabs@plt>
movapd 0x10(%rsp),%xmm1
ucomisd %xmm0,%xmm1
ja     11e0 <turn+0x80>
xor    %edi,%edi
xor    %esi,%esi
call   1070 <set_motors@plt>
mov    $0x9c40,%edi
call   1080 <usleep@plt>
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define void @turn(float %0, i32 %1) {
%3 = alloca float
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca double
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store float %0, float* %3
store i32 %1, i32* %4
store i32 15, i32* %5
store i32 25, i32* %6
store double 0.000000e+00, double* %8
store i32 0, i32* %9
store i32 0, i32* %10
%12 = load float, float* %3
%13 = fmul float 2.100000e+02, %12
%14 = fpext float %13 to double
%15 = fdiv double %14, 9.000000e+01
%16 = fptosi double %15 to i32
store i32 %16, i32* %11
%17 = call i32 (...) @reset_motor_encoders()
br label %18
18: ; preds = %72, %2
%19 = load i32, i32* %9
%20 = call double @fabs(i32 %19)
%21 = load i32, i32* %11
%22 = sitofp i32 %21 to double
%23 = fcmp olt double %20, %22
br i1 %23, label %24, label %30
24: ; preds = %18
%25 = load i32, i32* %10
%26 = call double @fabs(i32 %25)
%27 = load i32, i32* %11
%28 = sitofp i32 %27 to double
%29 = fcmp olt double %26, %28
br label %30
30: ; preds = %24, %18
%31 = phi i1 [ false, %18 ], [ %29, %24 ]
br i1 %31, label %32, label %74
32: ; preds = %30
%33 = call i32 @get_motor_encoders(i32* %9, i32* %10)
%34 = load i32, i32* %9
%35 = call double @fabs(i32 %34)
%36 = load i32, i32* %11
%37 = sitofp i32 %36 to double
%38 = fdiv double %35, %37
store double %38, double* %8
%39 = load double, double* %8
%40 = fcmp ole double %39, 5.000000e-01
br i1 %40, label %41, label %50
41: ; preds = %32
%42 = load i32, i32* %5
%43 = load double, double* %8
%44 = fmul double 2.000000e+00, %43
%45 = load i32, i32* %6
%46 = sitofp i32 %45 to double
%47 = fmul double %44, %46
%48 = fptosi double %47 to i32
%49 = add nsw i32 %42, %48
store i32 %49, i32* %7
br label %59
50: ; preds = %32
%51 = load i32, i32* %5
%52 = load double, double* %8
%53 = fsub double 1.000000e+00, %52
%54 = load i32, i32* %6
%55 = sitofp i32 %54 to double
%56 = fmul double %53, %55
%57 = fptosi double %56 to i32
%58 = add nsw i32 %51, %57
store i32 %58, i32* %7
br label %59
59: ; preds = %50, %41
%60 = load i32, i32* %4
%61 = icmp eq i32 %60, 1
br i1 %61, label %62, label %67
62: ; preds = %59
%63 = load i32, i32* %7
%64 = load i32, i32* %7
%65 = sub nsw i32 0, %64
%66 = call i32 @set_motors(i32 %63, i32 %65)
br label %72
67: ; preds = %59
%68 = load i32, i32* %7
%69 = sub nsw i32 0, %68
%70 = load i32, i32* %7
%71 = call i32 @set_motors(i32 %69, i32 %70)
br label %72
72: ; preds = %67, %62
%73 = call i32 (...) @log_trail()
br label %18
74: ; preds = %30
%75 = call i32 @set_motors(i32 0, i32 0)
%76 = call i32 @usleep(i32 40000)
ret void
}

LLVM IR (-O2):
define void @turn(float %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = bitcast i32* %3 to i8*
store i32 0, i32* %3
%6 = bitcast i32* %4 to i8*
store i32 0, i32* %4
%7 = fmul float %0, 2.100000e+02
%8 = fpext float %7 to double
%9 = fdiv double %8, 9.000000e+01
%10 = fptosi double %9 to i32
%11 = tail call i32 (...) @reset_motor_encoders()
%12 = call double @fabs(i32 0)
%13 = sitofp i32 %10 to double
%14 = fcmp olt double %12, %13
br i1 %14, label %15, label %45
15: ; preds = %2
%16 = icmp eq i32 %1, 1
%17 = call double @fabs(i32 0)
%18 = fcmp olt double %17, %13
br i1 %18, label %19, label %45
19: ; preds = %15, %41
%20 = call i32 @get_motor_encoders(i32* %3, i32* %4)
%21 = load i32, i32* %3
%22 = call double @fabs(i32 %21)
%23 = fdiv double %22, %13
%24 = fcmp ugt double %23, 5.000000e-01
%25 = fmul double %23, 2.000000e+00
%26 = fsub double 1.000000e+00, %23
%27 = select i1 %24, double %26, double %25
%28 = fmul double %27, 2.500000e+01
%29 = fptosi double %28 to i32
%30 = add nsw i32 %29, 15
%31 = sub i32 -15, %29
br i1 %16, label %32, label %34
32: ; preds = %19
%33 = call i32 @set_motors(i32 %30, i32 %31)
br label %36
34: ; preds = %19
%35 = call i32 @set_motors(i32 %31, i32 %30)
br label %36
36: ; preds = %34, %32
%37 = call i32 (...) @log_trail()
%38 = load i32, i32* %3
%39 = call double @fabs(i32 %38)
%40 = fcmp olt double %39, %13
br i1 %40, label %41, label %45
41: ; preds = %36
%42 = load i32, i32* %4
%43 = call double @fabs(i32 %42)
%44 = fcmp olt double %43, %13
br i1 %44, label %19, label %45
45: ; preds = %36, %41, %15, %2
%46 = call i32 @set_motors(i32 0, i32 0)
%47 = call i32 @usleep(i32 40000)
ret void
}