double max_abs(double velocities[][3], int amount) {
  int i, j;
  double max1 = 0;
  double max2 = 0;
  double c;
  for (i = 0; i < amount; i++) {
    for (j = 0; j < 3; j++) {
      c = velocities[i][j];
      max1 += c * c;
    }
    if (max1 >= max2) {
      max2 = max1;
      max1 = 0;
    } else {
      max1 = 0;
    }
  }
  return max2;
}


x86 assembly (clang, -O0):
<max_abs>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x20(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x28(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11c8 <max_abs+0xc8>
movl   $0x0,-0x14(%rbp)
cmpl   $0x3,-0x14(%rbp)
jge    1186 <max_abs+0x86>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
imul   $0x18,%rcx,%rcx
add    %rcx,%rax
movslq -0x14(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x30(%rbp),%xmm0
movsd  -0x30(%rbp),%xmm2
movsd  -0x20(%rbp),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x20(%rbp)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1135 <max_abs+0x35>
movsd  -0x20(%rbp),%xmm0
ucomisd -0x28(%rbp),%xmm0
jb     11ad <max_abs+0xad>
movsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x28(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x20(%rbp)
jmp    11b5 <max_abs+0xb5>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x20(%rbp)
jmp    11ba <max_abs+0xba>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1122 <max_abs+0x22>
movsd  -0x28(%rbp),%xmm0
pop    %rbp
ret

x86 assembly (clang, -O2):
<max_abs>:
test   %esi,%esi
jle    1168 <max_abs+0x68>
mov    %esi,%eax
shl    $0x3,%rax
lea    (%rax,%rax,2),%rax
xorpd  %xmm1,%xmm1
xor    %ecx,%ecx
xorpd  %xmm2,%xmm2
movapd %xmm2,%xmm0
nopl   0x0(%rax)
movsd  (%rdi,%rcx,1),%xmm3
movsd  0x8(%rdi,%rcx,1),%xmm4
mulsd  %xmm3,%xmm3
addsd  %xmm1,%xmm3
mulsd  %xmm4,%xmm4
addsd  %xmm3,%xmm4
movsd  0x10(%rdi,%rcx,1),%xmm3
mulsd  %xmm3,%xmm3
addsd  %xmm4,%xmm3
cmplesd %xmm3,%xmm0
andpd  %xmm0,%xmm3
andnpd %xmm2,%xmm0
orpd   %xmm3,%xmm0
add    $0x18,%rcx
movapd %xmm0,%xmm2
cmp    %rcx,%rax
jne    1120 <max_abs+0x20>
ret
xorps  %xmm0,%xmm0
ret

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

LLVM IR (-O2):
define double @max_abs([3 x double]* nocapture %0, i32 %1) {
%3 = icmp sgt i32 %1, 0
br i1 %3, label %4, label %22
4: ; preds = %2
%5 = zext i32 %1 to i64
br label %6
6: ; preds = %4, %6
%7 = phi i64 [ 0, %4 ], [ %20, %6 ]
%8 = phi double [ 0.000000e+00, %4 ], [ %19, %6 ]
%9 = getelementptr inbounds [3 x double], [3 x double]* %0, i64 %7, i64 0
%10 = load double, double* %9
%11 = tail call double @llvm.fmuladd.f64(double %10, double %10, double 0.000000e+00)
%12 = getelementptr inbounds [3 x double], [3 x double]* %0, i64 %7, i64 1
%13 = load double, double* %12
%14 = tail call double @llvm.fmuladd.f64(double %13, double %13, double %11)
%15 = getelementptr inbounds [3 x double], [3 x double]* %0, i64 %7, i64 2
%16 = load double, double* %15
%17 = tail call double @llvm.fmuladd.f64(double %16, double %16, double %14)
%18 = fcmp oge double %17, %8
%19 = select i1 %18, double %17, double %8
%20 = add nuw nsw i64 %7, 1
%21 = icmp eq i64 %20, %5
br i1 %21, label %22, label %6
22: ; preds = %6, %2
%23 = phi double [ 0.000000e+00, %2 ], [ %19, %6 ]
ret double %23
}