#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 */
scalar_t__ sqrt(double);
double poly_perim(unsigned ndims, double *xcoords, double *ycoords) {
  double retval = 0;
  for (unsigned i = 0; i < ndims; i++) {
    double left = xcoords[(i + 1) % ndims] - xcoords[i];
    double right = ycoords[(i + 1) % ndims] - ycoords[i];
    retval += sqrt(left * left + right * right);
  }
  return retval;
}


x86 assembly (clang, -O0):
<poly_perim>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x4(%rbp),%eax
jae    11da <poly_perim+0xca>
mov    -0x10(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
xor    %edx,%edx
divl   -0x4(%rbp)
mov    -0x48(%rbp),%rax
mov    %edx,%ecx
movsd  (%rax,%rcx,8),%xmm0
mov    -0x10(%rbp),%rax
mov    -0x24(%rbp),%ecx
subsd  (%rax,%rcx,8),%xmm0
movsd  %xmm0,-0x30(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
xor    %edx,%edx
divl   -0x4(%rbp)
mov    -0x40(%rbp),%rax
mov    %edx,%ecx
movsd  (%rax,%rcx,8),%xmm0
mov    -0x18(%rbp),%rax
mov    -0x24(%rbp),%ecx
subsd  (%rax,%rcx,8),%xmm0
movsd  %xmm0,-0x38(%rbp)
movsd  -0x30(%rbp),%xmm0
movsd  -0x30(%rbp),%xmm2
movsd  -0x38(%rbp),%xmm1
mulsd  -0x38(%rbp),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
call   1030 <sqrt@plt>
cvtsi2sd %rax,%xmm0
addsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x20(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1132 <poly_perim+0x22>
movsd  -0x20(%rbp),%xmm0
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<poly_perim>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
test   %edi,%edi
je     11b0 <poly_perim+0xa0>
mov    %rdx,%r14
mov    %rsi,%r15
mov    %edi,%r12d
neg    %r12
xorps  %xmm0,%xmm0
mov    $0x1,%ebx
xor    %r13d,%r13d
nopl   0x0(%rax)
movsd  %xmm0,0x8(%rsp)
mov    %r12,%rax
add    %rbx,%rax
mov    %rbx,%rax
cmove  %r13,%rax
movsd  (%r15,%rax,8),%xmm0
subsd  -0x8(%r15,%rbx,8),%xmm0
movsd  (%r14,%rax,8),%xmm1
subsd  -0x8(%r14,%rbx,8),%xmm1
mulsd  %xmm1,%xmm1
mulsd  %xmm0,%xmm0
addsd  %xmm1,%xmm0
call   1030 <sqrt@plt>
xorps  %xmm0,%xmm0
cvtsi2sd %rax,%xmm0
movsd  0x8(%rsp),%xmm1
addsd  %xmm0,%xmm1
movsd  %xmm1,0x8(%rsp)
movsd  0x8(%rsp),%xmm0
lea    (%r12,%rbx,1),%rax
add    $0x1,%rax
add    $0x1,%rbx
cmp    $0x1,%rax
jne    1140 <poly_perim+0x30>
jmp    11b3 <poly_perim+0xa3>
xorps  %xmm0,%xmm0
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define double @poly_perim(i32 %0, double* %1, double* %2) {
%4 = alloca i32
%5 = alloca double*
%6 = alloca double*
%7 = alloca double
%8 = alloca i32
%9 = alloca double
%10 = alloca double
store i32 %0, i32* %4
store double* %1, double** %5
store double* %2, double** %6
store double 0.000000e+00, double* %7
store i32 0, i32* %8
br label %11
11: ; preds = %54, %3
%12 = load i32, i32* %8
%13 = load i32, i32* %4
%14 = icmp ult i32 %12, %13
br i1 %14, label %15, label %57
15: ; preds = %11
%16 = load double*, double** %5
%17 = load i32, i32* %8
%18 = add i32 %17, 1
%19 = load i32, i32* %4
%20 = urem i32 %18, %19
%21 = zext i32 %20 to i64
%22 = getelementptr inbounds double, double* %16, i64 %21
%23 = load double, double* %22
%24 = load double*, double** %5
%25 = load i32, i32* %8
%26 = zext i32 %25 to i64
%27 = getelementptr inbounds double, double* %24, i64 %26
%28 = load double, double* %27
%29 = fsub double %23, %28
store double %29, double* %9
%30 = load double*, double** %6
%31 = load i32, i32* %8
%32 = add i32 %31, 1
%33 = load i32, i32* %4
%34 = urem i32 %32, %33
%35 = zext i32 %34 to i64
%36 = getelementptr inbounds double, double* %30, i64 %35
%37 = load double, double* %36
%38 = load double*, double** %6
%39 = load i32, i32* %8
%40 = zext i32 %39 to i64
%41 = getelementptr inbounds double, double* %38, i64 %40
%42 = load double, double* %41
%43 = fsub double %37, %42
store double %43, double* %10
%44 = load double, double* %9
%45 = load double, double* %9
%46 = load double, double* %10
%47 = load double, double* %10
%48 = fmul double %46, %47
%49 = call double @llvm.fmuladd.f64(double %44, double %45, double %48)
%50 = call i64 @sqrt(double %49)
%51 = sitofp i64 %50 to double
%52 = load double, double* %7
%53 = fadd double %52, %51
store double %53, double* %7
br label %54
54: ; preds = %15
%55 = load i32, i32* %8
%56 = add i32 %55, 1
store i32 %56, i32* %8
br label %11
57: ; preds = %11
%58 = load double, double* %7
ret double %58
}

LLVM IR (-O2):
define double @poly_perim(i32 %0, double* nocapture %1, double* nocapture %2) {
%4 = icmp eq i32 %0, 0
br i1 %4, label %7, label %5
5: ; preds = %3
%6 = zext i32 %0 to i64
br label %9
7: ; preds = %9, %3
%8 = phi double [ 0.000000e+00, %3 ], [ %30, %9 ]
ret double %8
9: ; preds = %5, %9
%10 = phi i64 [ 0, %5 ], [ %12, %9 ]
%11 = phi double [ 0.000000e+00, %5 ], [ %30, %9 ]
%12 = add nuw nsw i64 %10, 1
%13 = icmp eq i64 %12, %6
%14 = and i64 %12, 4294967295
%15 = select i1 %13, i64 0, i64 %14
%16 = getelementptr inbounds double, double* %1, i64 %15
%17 = load double, double* %16
%18 = getelementptr inbounds double, double* %1, i64 %10
%19 = load double, double* %18
%20 = fsub double %17, %19
%21 = getelementptr inbounds double, double* %2, i64 %15
%22 = load double, double* %21
%23 = getelementptr inbounds double, double* %2, i64 %10
%24 = load double, double* %23
%25 = fsub double %22, %24
%26 = fmul double %25, %25
%27 = tail call double @llvm.fmuladd.f64(double %20, double %20, double %26)
%28 = tail call i64 @sqrt(double %27)
%29 = sitofp i64 %28 to double
%30 = fadd double %11, %29
%31 = icmp eq i64 %12, %6
br i1 %31, label %7, label %9
}