#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 acos(double);
double vector_dot_2d(double const *, double const *);
double vector_magnitude_2d(double const *);
double vector_angle_2d(const double v1[2], const double v2[2]) {
  double mag1 = vector_magnitude_2d(v1);
  double mag2 = vector_magnitude_2d(v2);
  double dot = vector_dot_2d(v1, v2);
  double costheta = dot / (mag1 * mag2);
  if (costheta > 1)
    return 0;
  double theta = acos(costheta);
  if ((v1[0] * v2[1] - v1[1] * v2[0]) < 0) {
    return -theta;
  }
  return theta;
}


x86 assembly (clang, -O0):
<vector_angle_2d>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <vector_magnitude_2d@plt>
movsd  %xmm0,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <vector_magnitude_2d@plt>
movsd  %xmm0,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <vector_dot_2d@plt>
movsd  %xmm0,-0x30(%rbp)
movsd  -0x30(%rbp),%xmm0
movsd  -0x20(%rbp),%xmm1
mulsd  -0x28(%rbp),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x38(%rbp)
movsd  -0x38(%rbp),%xmm0
movsd  0xe6d(%rip),%xmm1
ucomisd %xmm1,%xmm0
jbe    11aa <vector_angle_2d+0x7a>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    1237 <vector_angle_2d+0x107>
movsd  -0x38(%rbp),%xmm0
call   1040 <acos@plt>
movsd  %xmm0,-0x40(%rbp)
mov    -0x10(%rbp),%rax
movsd  (%rax),%xmm1
mov    -0x18(%rbp),%rax
movsd  0x8(%rax),%xmm2
mov    -0x10(%rbp),%rax
movsd  0x8(%rax),%xmm0
mov    -0x18(%rbp),%rax
mulsd  (%rax),%xmm0
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
mulsd  %xmm2,%xmm1
addsd  %xmm0,%xmm1
xorps  %xmm0,%xmm0
ucomisd %xmm1,%xmm0
jbe    122d <vector_angle_2d+0xfd>
movsd  -0x40(%rbp),%xmm0
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    1237 <vector_angle_2d+0x107>
movsd  -0x40(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<vector_angle_2d>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
call   1030 <vector_magnitude_2d@plt>
movsd  %xmm0,0x10(%rsp)
mov    %r14,%rdi
call   1030 <vector_magnitude_2d@plt>
movsd  %xmm0,0x8(%rsp)
mov    %rbx,%rdi
mov    %r14,%rsi
call   1050 <vector_dot_2d@plt>
movapd %xmm0,%xmm1
movsd  0x8(%rsp),%xmm0
mulsd  0x10(%rsp),%xmm0
divsd  %xmm0,%xmm1
xorpd  %xmm0,%xmm0
ucomisd 0xe7f(%rip),%xmm1
ja     11c9 <vector_angle_2d+0x99>
movapd %xmm1,%xmm0
call   1040 <acos@plt>
movapd %xmm0,%xmm1
movsd  (%rbx),%xmm0
movsd  (%r14),%xmm2
mulsd  0x8(%rbx),%xmm2
mulsd  0x8(%r14),%xmm0
subsd  %xmm2,%xmm0
movapd 0xe60(%rip),%xmm2
xorpd  %xmm1,%xmm2
xorpd  %xmm3,%xmm3
cmpltsd %xmm3,%xmm0
andpd  %xmm0,%xmm2
andnpd %xmm1,%xmm0
orpd   %xmm2,%xmm0
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define double @vector_angle_2d(double* %0, double* %1) {
%3 = alloca double
%4 = alloca double*
%5 = alloca double*
%6 = alloca double
%7 = alloca double
%8 = alloca double
%9 = alloca double
%10 = alloca double
store double* %0, double** %4
store double* %1, double** %5
%11 = load double*, double** %4
%12 = call double @vector_magnitude_2d(double* %11)
store double %12, double* %6
%13 = load double*, double** %5
%14 = call double @vector_magnitude_2d(double* %13)
store double %14, double* %7
%15 = load double*, double** %4
%16 = load double*, double** %5
%17 = call double @vector_dot_2d(double* %15, double* %16)
store double %17, double* %8
%18 = load double, double* %8
%19 = load double, double* %6
%20 = load double, double* %7
%21 = fmul double %19, %20
%22 = fdiv double %18, %21
store double %22, double* %9
%23 = load double, double* %9
%24 = fcmp ogt double %23, 1.000000e+00
br i1 %24, label %25, label %26
25: ; preds = %2
store double 0.000000e+00, double* %3
br label %50
26: ; preds = %2
%27 = load double, double* %9
%28 = call double @acos(double %27)
store double %28, double* %10
%29 = load double*, double** %4
%30 = getelementptr inbounds double, double* %29, i64 0
%31 = load double, double* %30
%32 = load double*, double** %5
%33 = getelementptr inbounds double, double* %32, i64 1
%34 = load double, double* %33
%35 = load double*, double** %4
%36 = getelementptr inbounds double, double* %35, i64 1
%37 = load double, double* %36
%38 = load double*, double** %5
%39 = getelementptr inbounds double, double* %38, i64 0
%40 = load double, double* %39
%41 = fmul double %37, %40
%42 = fneg double %41
%43 = call double @llvm.fmuladd.f64(double %31, double %34, double %42)
%44 = fcmp olt double %43, 0.000000e+00
br i1 %44, label %45, label %48
45: ; preds = %26
%46 = load double, double* %10
%47 = fneg double %46
store double %47, double* %3
br label %50
48: ; preds = %26
%49 = load double, double* %10
store double %49, double* %3
br label %50
50: ; preds = %48, %45, %25
%51 = load double, double* %3
ret double %51
}

LLVM IR (-O2):
define double @vector_angle_2d(double* %0, double* %1) {
%3 = tail call double @vector_magnitude_2d(double* %0)
%4 = tail call double @vector_magnitude_2d(double* %1)
%5 = tail call double @vector_dot_2d(double* %0, double* %1)
%6 = fmul double %3, %4
%7 = fdiv double %5, %6
%8 = fcmp ogt double %7, 1.000000e+00
br i1 %8, label %23, label %9
9: ; preds = %2
%10 = tail call double @acos(double %7)
%11 = load double, double* %0
%12 = getelementptr inbounds double, double* %1, i64 1
%13 = load double, double* %12
%14 = getelementptr inbounds double, double* %0, i64 1
%15 = load double, double* %14
%16 = load double, double* %1
%17 = fneg double %15
%18 = fmul double %16, %17
%19 = tail call double @llvm.fmuladd.f64(double %11, double %13, double %18)
%20 = fcmp olt double %19, 0.000000e+00
%21 = fneg double %10
%22 = select i1 %20, double %21, double %10
br label %23
23: ; preds = %2, %9
%24 = phi double [ %22, %9 ], [ 0.000000e+00, %2 ]
ret double %24
}