#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 sqrt(double);
void complex_magnitude(double *arr, int n_real) {
  int j;
  for (j = 0; j < n_real; j++) {
    double r = arr[2 * j];
    double i = arr[2 * j + 1];
    arr[j] = sqrt(r * r + i * i);
  }
}


x86 assembly (clang, -O0):
<complex_magnitude>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    119f <complex_magnitude+0x8f>
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%ecx
shl    $0x1,%ecx
movslq %ecx,%rcx
movsd  (%rax,%rcx,8),%xmm0
movsd  %xmm0,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%ecx
shl    $0x1,%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
movsd  (%rax,%rcx,8),%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  -0x18(%rbp),%xmm0
movsd  -0x18(%rbp),%xmm2
movsd  -0x20(%rbp),%xmm1
mulsd  -0x20(%rbp),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
call   1030 <sqrt@plt>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1126 <complex_magnitude+0x16>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<complex_magnitude>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
test   %esi,%esi
jle    120b <complex_magnitude+0xfb>
mov    %rdi,%r14
mov    %esi,%r15d
cmp    $0x1,%esi
jne    1170 <complex_magnitude+0x60>
xor    %r13d,%r13d
test   $0x1,%r15b
je     120b <complex_magnitude+0xfb>
mov    %r13,%rax
shl    $0x4,%rax
movsd  (%r14,%rax,1),%xmm0
movsd  0x8(%r14,%rax,1),%xmm1
mulsd  %xmm1,%xmm1
mulsd  %xmm0,%xmm0
addsd  %xmm1,%xmm0
xorpd  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jb     1200 <complex_magnitude+0xf0>
sqrtsd %xmm0,%xmm0
jmp    1205 <complex_magnitude+0xf5>
mov    %r15d,%r12d
and    $0xfffffffe,%r12d
lea    0x18(%r14),%rbx
xor    %r13d,%r13d
xorpd  %xmm2,%xmm2
jmp    11ad <complex_magnitude+0x9d>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
call   1030 <sqrt@plt>
xorpd  %xmm2,%xmm2
movsd  %xmm0,0x8(%r14,%r13,8)
add    $0x2,%r13
add    $0x20,%rbx
cmp    %r13,%r12
je     112f <complex_magnitude+0x1f>
movsd  -0x18(%rbx),%xmm0
movsd  -0x10(%rbx),%xmm1
mulsd  %xmm1,%xmm1
mulsd  %xmm0,%xmm0
addsd  %xmm1,%xmm0
ucomisd %xmm2,%xmm0
jb     11d0 <complex_magnitude+0xc0>
sqrtsd %xmm0,%xmm0
jmp    11d9 <complex_magnitude+0xc9>
nop
call   1030 <sqrt@plt>
xorpd  %xmm2,%xmm2
movsd  %xmm0,(%r14,%r13,8)
movsd  -0x8(%rbx),%xmm0
movsd  (%rbx),%xmm1
mulsd  %xmm1,%xmm1
mulsd  %xmm0,%xmm0
addsd  %xmm1,%xmm0
ucomisd %xmm2,%xmm0
jb     1190 <complex_magnitude+0x80>
sqrtsd %xmm0,%xmm0
jmp    1199 <complex_magnitude+0x89>
call   1030 <sqrt@plt>
movsd  %xmm0,(%r14,%r13,8)
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define void @complex_magnitude(double* %0, i32 %1) {
%3 = alloca double*
%4 = alloca i32
%5 = alloca i32
%6 = alloca double
%7 = alloca double
store double* %0, double** %3
store i32 %1, i32* %4
store i32 0, i32* %5
br label %8
8: ; preds = %37, %2
%9 = load i32, i32* %5
%10 = load i32, i32* %4
%11 = icmp slt i32 %9, %10
br i1 %11, label %12, label %40
12: ; preds = %8
%13 = load double*, double** %3
%14 = load i32, i32* %5
%15 = mul nsw i32 2, %14
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds double, double* %13, i64 %16
%18 = load double, double* %17
store double %18, double* %6
%19 = load double*, double** %3
%20 = load i32, i32* %5
%21 = mul nsw i32 2, %20
%22 = add nsw i32 %21, 1
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds double, double* %19, i64 %23
%25 = load double, double* %24
store double %25, double* %7
%26 = load double, double* %6
%27 = load double, double* %6
%28 = load double, double* %7
%29 = load double, double* %7
%30 = fmul double %28, %29
%31 = call double @llvm.fmuladd.f64(double %26, double %27, double %30)
%32 = call double @sqrt(double %31)
%33 = load double*, double** %3
%34 = load i32, i32* %5
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds double, double* %33, i64 %35
store double %32, double* %36
br label %37
37: ; preds = %12
%38 = load i32, i32* %5
%39 = add nsw i32 %38, 1
store i32 %39, i32* %5
br label %8
40: ; preds = %8
ret void
}

LLVM IR (-O2):
define void @complex_magnitude(double* nocapture %0, i32 %1) {
%3 = icmp sgt i32 %1, 0
br i1 %3, label %4, label %51
4: ; preds = %2
%5 = zext i32 %1 to i64
%6 = and i64 %5, 1
%7 = icmp eq i32 %1, 1
br i1 %7, label %37, label %8
8: ; preds = %4
%9 = and i64 %5, 4294967294
br label %10
10: ; preds = %10, %8
%11 = phi i64 [ 0, %8 ], [ %34, %10 ]
%12 = phi i64 [ 0, %8 ], [ %35, %10 ]
%13 = shl nuw nsw i64 %11, 1
%14 = getelementptr inbounds double, double* %0, i64 %13
%15 = load double, double* %14
%16 = or i64 %13, 1
%17 = getelementptr inbounds double, double* %0, i64 %16
%18 = load double, double* %17
%19 = fmul double %18, %18
%20 = tail call double @llvm.fmuladd.f64(double %15, double %15, double %19)
%21 = tail call double @sqrt(double %20)
%22 = getelementptr inbounds double, double* %0, i64 %11
store double %21, double* %22
%23 = or i64 %11, 1
%24 = shl nuw nsw i64 %23, 1
%25 = getelementptr inbounds double, double* %0, i64 %24
%26 = load double, double* %25
%27 = or i64 %24, 1
%28 = getelementptr inbounds double, double* %0, i64 %27
%29 = load double, double* %28
%30 = fmul double %29, %29
%31 = tail call double @llvm.fmuladd.f64(double %26, double %26, double %30)
%32 = tail call double @sqrt(double %31)
%33 = getelementptr inbounds double, double* %0, i64 %23
store double %32, double* %33
%34 = add nuw nsw i64 %11, 2
%35 = add nuw i64 %12, 2
%36 = icmp eq i64 %35, %9
br i1 %36, label %37, label %10
37: ; preds = %10, %4
%38 = phi i64 [ 0, %4 ], [ %34, %10 ]
%39 = icmp eq i64 %6, 0
br i1 %39, label %51, label %40
40: ; preds = %37
%41 = shl nuw nsw i64 %38, 1
%42 = getelementptr inbounds double, double* %0, i64 %41
%43 = load double, double* %42
%44 = or i64 %41, 1
%45 = getelementptr inbounds double, double* %0, i64 %44
%46 = load double, double* %45
%47 = fmul double %46, %46
%48 = tail call double @llvm.fmuladd.f64(double %43, double %43, double %47)
%49 = tail call double @sqrt(double %48)
%50 = getelementptr inbounds double, double* %0, i64 %38
store double %49, double* %50
br label %51
51: ; preds = %40, %37, %2
ret void
}