#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 int64_t;

/* Variables and functions */
double fabs(float);
float calc_distance2(float *p1, float *p2, float box_size) {
  double ds = 0, dx;
  for (int64_t i = 0; i < 3; i++) {
    dx = fabs(p1[i] - p2[i]);
    if (dx > box_size / 2.0)
      dx = box_size - dx;
    ds += dx * dx;
  }
  return ds;
}


x86 assembly (clang, -O0):
<calc_distance2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movss  %xmm0,-0x14(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x20(%rbp)
movl   $0x0,-0x2c(%rbp)
cmpl   $0x3,-0x2c(%rbp)
jge    11c3 <calc_distance2+0xb3>
mov    -0x8(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movss  (%rax,%rcx,4),%xmm0
mov    -0x10(%rbp),%rax
movslq -0x2c(%rbp),%rcx
subss  (%rax,%rcx,4),%xmm0
call   1030 <fabs@plt>
movsd  %xmm0,-0x28(%rbp)
movsd  -0x28(%rbp),%xmm0
movss  -0x14(%rbp),%xmm1
cvtss2sd %xmm1,%xmm1
movsd  0xe88(%rip),%xmm2
divsd  %xmm2,%xmm1
ucomisd %xmm1,%xmm0
jbe    1199 <calc_distance2+0x89>
movss  -0x14(%rbp),%xmm0
cvtss2sd %xmm0,%xmm0
subsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  -0x28(%rbp),%xmm0
movsd  -0x28(%rbp),%xmm2
movsd  -0x20(%rbp),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x20(%rbp)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1134 <calc_distance2+0x24>
movsd  -0x20(%rbp),%xmm0
cvtsd2ss %xmm0,%xmm0
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<calc_distance2>:
push   %r14
push   %rbx
sub    $0x38,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
cvtss2sd %xmm0,%xmm1
movaps %xmm1,0x10(%rsp)
movsd  0xed2(%rip),%xmm0
mulsd  %xmm1,%xmm0
movapd %xmm0,0x20(%rsp)
movss  (%rdi),%xmm0
subss  (%rsi),%xmm0
call   1030 <fabs@plt>
movapd 0x10(%rsp),%xmm1
subsd  %xmm0,%xmm1
movapd 0x20(%rsp),%xmm2
cmpltsd %xmm0,%xmm2
andpd  %xmm2,%xmm1
andnpd %xmm0,%xmm2
orpd   %xmm1,%xmm2
mulsd  %xmm2,%xmm2
xorpd  %xmm0,%xmm0
addsd  %xmm2,%xmm0
movsd  %xmm0,(%rsp)
movss  0x4(%rbx),%xmm0
subss  0x4(%r14),%xmm0
call   1030 <fabs@plt>
movapd 0x10(%rsp),%xmm1
subsd  %xmm0,%xmm1
movapd 0x20(%rsp),%xmm2
cmpltsd %xmm0,%xmm2
andpd  %xmm2,%xmm1
andnpd %xmm0,%xmm2
orpd   %xmm1,%xmm2
mulsd  %xmm2,%xmm2
addsd  (%rsp),%xmm2
movapd %xmm2,(%rsp)
movss  0x8(%rbx),%xmm0
subss  0x8(%r14),%xmm0
call   1030 <fabs@plt>
movapd 0x10(%rsp),%xmm2
subsd  %xmm0,%xmm2
movapd 0x20(%rsp),%xmm1
cmpltsd %xmm0,%xmm1
andpd  %xmm1,%xmm2
andnpd %xmm0,%xmm1
orpd   %xmm2,%xmm1
mulsd  %xmm1,%xmm1
addsd  (%rsp),%xmm1
xorps  %xmm0,%xmm0
cvtsd2ss %xmm1,%xmm0
add    $0x38,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define float @calc_distance2(float* %0, float* %1, float %2) {
%4 = alloca float*
%5 = alloca float*
%6 = alloca float
%7 = alloca double
%8 = alloca double
%9 = alloca i32
store float* %0, float** %4
store float* %1, float** %5
store float %2, float* %6
store double 0.000000e+00, double* %7
store i32 0, i32* %9
br label %10
10: ; preds = %41, %3
%11 = load i32, i32* %9
%12 = icmp slt i32 %11, 3
br i1 %12, label %13, label %44
13: ; preds = %10
%14 = load float*, float** %4
%15 = load i32, i32* %9
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds float, float* %14, i64 %16
%18 = load float, float* %17
%19 = load float*, float** %5
%20 = load i32, i32* %9
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds float, float* %19, i64 %21
%23 = load float, float* %22
%24 = fsub float %18, %23
%25 = call double @fabs(float %24)
store double %25, double* %8
%26 = load double, double* %8
%27 = load float, float* %6
%28 = fpext float %27 to double
%29 = fdiv double %28, 2.000000e+00
%30 = fcmp ogt double %26, %29
br i1 %30, label %31, label %36
31: ; preds = %13
%32 = load float, float* %6
%33 = fpext float %32 to double
%34 = load double, double* %8
%35 = fsub double %33, %34
store double %35, double* %8
br label %36
36: ; preds = %31, %13
%37 = load double, double* %8
%38 = load double, double* %8
%39 = load double, double* %7
%40 = call double @llvm.fmuladd.f64(double %37, double %38, double %39)
store double %40, double* %7
br label %41
41: ; preds = %36
%42 = load i32, i32* %9
%43 = add nsw i32 %42, 1
store i32 %43, i32* %9
br label %10
44: ; preds = %10
%45 = load double, double* %7
%46 = fptrunc double %45 to float
ret float %46
}

LLVM IR (-O2):
define float @calc_distance2(float* nocapture %0, float* nocapture %1, float %2) {
%4 = fpext float %2 to double
%5 = fmul double %4, 5.000000e-01
%6 = load float, float* %0
%7 = load float, float* %1
%8 = fsub float %6, %7
%9 = tail call double @fabs(float %8)
%10 = fcmp ogt double %9, %5
%11 = fsub double %4, %9
%12 = select i1 %10, double %11, double %9
%13 = tail call double @llvm.fmuladd.f64(double %12, double %12, double 0.000000e+00)
%14 = getelementptr inbounds float, float* %0, i64 1
%15 = load float, float* %14
%16 = getelementptr inbounds float, float* %1, i64 1
%17 = load float, float* %16
%18 = fsub float %15, %17
%19 = tail call double @fabs(float %18)
%20 = fcmp ogt double %19, %5
%21 = fsub double %4, %19
%22 = select i1 %20, double %21, double %19
%23 = tail call double @llvm.fmuladd.f64(double %22, double %22, double %13)
%24 = getelementptr inbounds float, float* %0, i64 2
%25 = load float, float* %24
%26 = getelementptr inbounds float, float* %1, i64 2
%27 = load float, float* %26
%28 = fsub float %25, %27
%29 = tail call double @fabs(float %28)
%30 = fcmp ogt double %29, %5
%31 = fsub double %4, %29
%32 = select i1 %30, double %31, double %29
%33 = tail call double @llvm.fmuladd.f64(double %32, double %32, double %23)
%34 = fptrunc double %33 to float
ret float %34
}