#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 gsw_alpha(double, double, double);
double gsw_beta(double, double, double);
double gsw_grav(double, double);
double gsw_rho(double, double, double);
void gsw_nsquared(double *sa, double *ct, double *p, double *lat, int nz,
                  double *n2, double *p_mid) {
  int k;
  double db2pa = 1e4;
  double grav_local, dsa, sa_mid, dct, ct_mid, dp, rho_mid;
  double alpha_mid, beta_mid;
  for (k = 0; k < nz - 1; k++) {
    grav_local =
        0.5 * (gsw_grav(lat[k], p[k]) + gsw_grav(lat[k + 1], p[k + 1]));
    dsa = (sa[k + 1] - sa[k]);
    sa_mid = 0.5 * (sa[k] + sa[k + 1]);
    dct = (ct[k + 1] - ct[k]);
    ct_mid = 0.5 * (ct[k] + ct[k + 1]);
    dp = (p[k + 1] - p[k]);
    p_mid[k] = 0.5 * (p[k] + p[k + 1]);
    rho_mid = gsw_rho(sa_mid, ct_mid, p_mid[k]);
    alpha_mid = gsw_alpha(sa_mid, ct_mid, p_mid[k]);
    beta_mid = gsw_beta(sa_mid, ct_mid, p_mid[k]);
    n2[k] = (grav_local * grav_local) * (rho_mid / (db2pa * dp)) *
            (beta_mid * dsa - alpha_mid * dct);
  }
}


x86 assembly (clang, -O0):
<gsw_nsquared>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    0x10(%rbp),%rax
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
mov    %r9,-0x30(%rbp)
movsd  0xe91(%rip),%xmm0
movsd  %xmm0,-0x40(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
mov    -0x24(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    13d9 <gsw_nsquared+0x299>
mov    -0x20(%rbp),%rax
movslq -0x34(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
mov    -0x18(%rbp),%rax
movslq -0x34(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm1
call   1040 <gsw_grav@plt>
movsd  %xmm0,-0x90(%rbp)
ff
mov    -0x20(%rbp),%rax
mov    -0x34(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
movsd  (%rax,%rcx,8),%xmm0
mov    -0x18(%rbp),%rax
mov    -0x34(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
movsd  (%rax,%rcx,8),%xmm1
call   1040 <gsw_grav@plt>
movsd  -0x90(%rbp),%xmm1
ff
addsd  %xmm0,%xmm1
movsd  0xe18(%rip),%xmm0
mulsd  %xmm1,%xmm0
movsd  %xmm0,-0x48(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x34(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
movsd  (%rax,%rcx,8),%xmm0
mov    -0x8(%rbp),%rax
movslq -0x34(%rbp),%rcx
subsd  (%rax,%rcx,8),%xmm0
movsd  %xmm0,-0x50(%rbp)
mov    -0x8(%rbp),%rax
movslq -0x34(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm1
mov    -0x8(%rbp),%rax
mov    -0x34(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
addsd  (%rax,%rcx,8),%xmm1
movsd  0xdc4(%rip),%xmm0
mulsd  %xmm1,%xmm0
movsd  %xmm0,-0x58(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x34(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
movsd  (%rax,%rcx,8),%xmm0
mov    -0x10(%rbp),%rax
movslq -0x34(%rbp),%rcx
subsd  (%rax,%rcx,8),%xmm0
movsd  %xmm0,-0x60(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x34(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm1
mov    -0x10(%rbp),%rax
mov    -0x34(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
addsd  (%rax,%rcx,8),%xmm1
movsd  0xd70(%rip),%xmm0
mulsd  %xmm1,%xmm0
movsd  %xmm0,-0x68(%rbp)
mov    -0x18(%rbp),%rax
mov    -0x34(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
movsd  (%rax,%rcx,8),%xmm0
mov    -0x18(%rbp),%rax
movslq -0x34(%rbp),%rcx
subsd  (%rax,%rcx,8),%xmm0
movsd  %xmm0,-0x70(%rbp)
mov    -0x18(%rbp),%rax
movslq -0x34(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm1
mov    -0x18(%rbp),%rax
mov    -0x34(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
addsd  (%rax,%rcx,8),%xmm1
movsd  0xd1c(%rip),%xmm0
mulsd  %xmm1,%xmm0
mov    0x10(%rbp),%rax
movslq -0x34(%rbp),%rcx
movsd  %xmm0,(%rax,%rcx,8)
movsd  -0x58(%rbp),%xmm0
movsd  -0x68(%rbp),%xmm1
mov    0x10(%rbp),%rax
movslq -0x34(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm2
call   1060 <gsw_rho@plt>
movsd  %xmm0,-0x78(%rbp)
movsd  -0x58(%rbp),%xmm0
movsd  -0x68(%rbp),%xmm1
mov    0x10(%rbp),%rax
movslq -0x34(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm2
call   1050 <gsw_alpha@plt>
movsd  %xmm0,-0x80(%rbp)
movsd  -0x58(%rbp),%xmm0
movsd  -0x68(%rbp),%xmm1
mov    0x10(%rbp),%rax
movslq -0x34(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm2
call   1030 <gsw_beta@plt>
movsd  %xmm0,-0x88(%rbp)
ff
movsd  -0x48(%rbp),%xmm0
mulsd  -0x48(%rbp),%xmm0
movsd  -0x78(%rbp),%xmm1
movsd  -0x40(%rbp),%xmm2
mulsd  -0x70(%rbp),%xmm2
divsd  %xmm2,%xmm1
mulsd  %xmm1,%xmm0
movsd  -0x88(%rbp),%xmm1
ff
movsd  -0x50(%rbp),%xmm3
movsd  -0x80(%rbp),%xmm2
mulsd  -0x60(%rbp),%xmm2
movq   %xmm2,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm2
mulsd  %xmm3,%xmm1
addsd  %xmm2,%xmm1
mulsd  %xmm1,%xmm0
mov    -0x30(%rbp),%rax
movslq -0x34(%rbp),%rcx
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    117b <gsw_nsquared+0x3b>
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gsw_nsquared>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x58,%rsp
mov    %rsi,0x20(%rsp)
mov    %r8,0x8(%rsp)
cmp    $0x2,%r8d
jl     12f1 <gsw_nsquared+0x1b1>
mov    %rcx,%r13
mov    %rdx,%rbx
mov    %rdi,%r12
mov    0x8(%rsp),%rax
add    $0xffffffff,%eax
mov    %rax,0x8(%rsp)
xor    %r15d,%r15d
mov    %r9,0x18(%rsp)
movsd  0x0(%r13,%r15,8),%xmm0
movsd  (%rbx,%r15,8),%xmm1
call   1040 <gsw_grav@plt>
movsd  %xmm0,(%rsp)
lea    0x1(%r15),%r14
movsd  0x8(%r13,%r15,8),%xmm0
movsd  0x8(%rbx,%r15,8),%xmm1
call   1040 <gsw_grav@plt>
movapd %xmm0,%xmm2
addsd  (%rsp),%xmm2
movsd  0xe41(%rip),%xmm5
mulsd  %xmm5,%xmm2
movsd  %xmm2,0x48(%rsp)
movsd  (%r12,%r15,8),%xmm0
movsd  0x8(%r12,%r15,8),%xmm3
movapd %xmm3,%xmm2
subsd  %xmm0,%xmm2
movsd  %xmm2,0x50(%rsp)
addsd  %xmm0,%xmm3
mulsd  %xmm5,%xmm3
movapd %xmm3,%xmm0
movsd  %xmm3,(%rsp)
mov    0x20(%rsp),%rax
movsd  (%rax,%r15,8),%xmm3
movsd  0x8(%rax,%r15,8),%xmm4
movapd %xmm4,%xmm2
subsd  %xmm3,%xmm2
movsd  %xmm2,0x40(%rsp)
addsd  %xmm3,%xmm4
mulsd  %xmm5,%xmm4
movapd %xmm4,%xmm1
movsd  %xmm4,0x10(%rsp)
movsd  (%rbx,%r15,8),%xmm4
movsd  0x8(%rbx,%r15,8),%xmm2
movapd %xmm2,%xmm3
subsd  %xmm4,%xmm3
movsd  %xmm3,0x38(%rsp)
addsd  %xmm4,%xmm2
mulsd  %xmm5,%xmm2
mov    0x90(%rsp),%rbp
movsd  %xmm2,0x0(%rbp,%r15,8)
call   1060 <gsw_rho@plt>
movsd  %xmm0,0x30(%rsp)
movsd  0x0(%rbp,%r15,8),%xmm2
movsd  (%rsp),%xmm0
movsd  0x10(%rsp),%xmm1
call   1050 <gsw_alpha@plt>
movsd  %xmm0,0x28(%rsp)
movsd  0x0(%rbp,%r15,8),%xmm2
movsd  (%rsp),%xmm0
movsd  0x10(%rsp),%xmm1
call   1030 <gsw_beta@plt>
mov    0x18(%rsp),%r9
movsd  0x48(%rsp),%xmm1
mulsd  %xmm1,%xmm1
movsd  0x38(%rsp),%xmm2
mulsd  0xd53(%rip),%xmm2
movsd  0x30(%rsp),%xmm3
divsd  %xmm2,%xmm3
mulsd  %xmm1,%xmm3
movsd  0x28(%rsp),%xmm1
mulsd  0x40(%rsp),%xmm1
mulsd  0x50(%rsp),%xmm0
subsd  %xmm1,%xmm0
mulsd  %xmm3,%xmm0
movsd  %xmm0,(%r9,%r15,8)
mov    %r14,%r15
cmp    %r14,0x8(%rsp)
jne    1180 <gsw_nsquared+0x40>
add    $0x58,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @gsw_nsquared(double* %0, double* %1, double* %2, double* %3, i32 %4, double* %5, double* %6) {
%8 = alloca double*
%9 = alloca double*
%10 = alloca double*
%11 = alloca double*
%12 = alloca i32
%13 = alloca double*
%14 = alloca double*
%15 = alloca i32
%16 = alloca double
%17 = alloca double
%18 = alloca double
%19 = alloca double
%20 = alloca double
%21 = alloca double
%22 = alloca double
%23 = alloca double
%24 = alloca double
%25 = alloca double
store double* %0, double** %8
store double* %1, double** %9
store double* %2, double** %10
store double* %3, double** %11
store i32 %4, i32* %12
store double* %5, double** %13
store double* %6, double** %14
store double 1.000000e+04, double* %16
store i32 0, i32* %15
br label %26
26: ; preds = %182, %7
%27 = load i32, i32* %15
%28 = load i32, i32* %12
%29 = sub nsw i32 %28, 1
%30 = icmp slt i32 %27, %29
br i1 %30, label %31, label %185
31: ; preds = %26
%32 = load double*, double** %11
%33 = load i32, i32* %15
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds double, double* %32, i64 %34
%36 = load double, double* %35
%37 = load double*, double** %10
%38 = load i32, i32* %15
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds double, double* %37, i64 %39
%41 = load double, double* %40
%42 = call double @gsw_grav(double %36, double %41)
%43 = load double*, double** %11
%44 = load i32, i32* %15
%45 = add nsw i32 %44, 1
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds double, double* %43, i64 %46
%48 = load double, double* %47
%49 = load double*, double** %10
%50 = load i32, i32* %15
%51 = add nsw i32 %50, 1
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds double, double* %49, i64 %52
%54 = load double, double* %53
%55 = call double @gsw_grav(double %48, double %54)
%56 = fadd double %42, %55
%57 = fmul double 5.000000e-01, %56
store double %57, double* %17
%58 = load double*, double** %8
%59 = load i32, i32* %15
%60 = add nsw i32 %59, 1
%61 = sext i32 %60 to i64
%62 = getelementptr inbounds double, double* %58, i64 %61
%63 = load double, double* %62
%64 = load double*, double** %8
%65 = load i32, i32* %15
%66 = sext i32 %65 to i64
%67 = getelementptr inbounds double, double* %64, i64 %66
%68 = load double, double* %67
%69 = fsub double %63, %68
store double %69, double* %18
%70 = load double*, double** %8
%71 = load i32, i32* %15
%72 = sext i32 %71 to i64
%73 = getelementptr inbounds double, double* %70, i64 %72
%74 = load double, double* %73
%75 = load double*, double** %8
%76 = load i32, i32* %15
%77 = add nsw i32 %76, 1
%78 = sext i32 %77 to i64
%79 = getelementptr inbounds double, double* %75, i64 %78
%80 = load double, double* %79
%81 = fadd double %74, %80
%82 = fmul double 5.000000e-01, %81
store double %82, double* %19
%83 = load double*, double** %9
%84 = load i32, i32* %15
%85 = add nsw i32 %84, 1
%86 = sext i32 %85 to i64
%87 = getelementptr inbounds double, double* %83, i64 %86
%88 = load double, double* %87
%89 = load double*, double** %9
%90 = load i32, i32* %15
%91 = sext i32 %90 to i64
%92 = getelementptr inbounds double, double* %89, i64 %91
%93 = load double, double* %92
%94 = fsub double %88, %93
store double %94, double* %20
%95 = load double*, double** %9
%96 = load i32, i32* %15
%97 = sext i32 %96 to i64
%98 = getelementptr inbounds double, double* %95, i64 %97
%99 = load double, double* %98
%100 = load double*, double** %9
%101 = load i32, i32* %15
%102 = add nsw i32 %101, 1
%103 = sext i32 %102 to i64
%104 = getelementptr inbounds double, double* %100, i64 %103
%105 = load double, double* %104
%106 = fadd double %99, %105
%107 = fmul double 5.000000e-01, %106
store double %107, double* %21
%108 = load double*, double** %10
%109 = load i32, i32* %15
%110 = add nsw i32 %109, 1
%111 = sext i32 %110 to i64
%112 = getelementptr inbounds double, double* %108, i64 %111
%113 = load double, double* %112
%114 = load double*, double** %10
%115 = load i32, i32* %15
%116 = sext i32 %115 to i64
%117 = getelementptr inbounds double, double* %114, i64 %116
%118 = load double, double* %117
%119 = fsub double %113, %118
store double %119, double* %22
%120 = load double*, double** %10
%121 = load i32, i32* %15
%122 = sext i32 %121 to i64
%123 = getelementptr inbounds double, double* %120, i64 %122
%124 = load double, double* %123
%125 = load double*, double** %10
%126 = load i32, i32* %15
%127 = add nsw i32 %126, 1
%128 = sext i32 %127 to i64
%129 = getelementptr inbounds double, double* %125, i64 %128
%130 = load double, double* %129
%131 = fadd double %124, %130
%132 = fmul double 5.000000e-01, %131
%133 = load double*, double** %14
%134 = load i32, i32* %15
%135 = sext i32 %134 to i64
%136 = getelementptr inbounds double, double* %133, i64 %135
store double %132, double* %136
%137 = load double, double* %19
%138 = load double, double* %21
%139 = load double*, double** %14
%140 = load i32, i32* %15
%141 = sext i32 %140 to i64
%142 = getelementptr inbounds double, double* %139, i64 %141
%143 = load double, double* %142
%144 = call double @gsw_rho(double %137, double %138, double %143)
store double %144, double* %23
%145 = load double, double* %19
%146 = load double, double* %21
%147 = load double*, double** %14
%148 = load i32, i32* %15
%149 = sext i32 %148 to i64
%150 = getelementptr inbounds double, double* %147, i64 %149
%151 = load double, double* %150
%152 = call double @gsw_alpha(double %145, double %146, double %151)
store double %152, double* %24
%153 = load double, double* %19
%154 = load double, double* %21
%155 = load double*, double** %14
%156 = load i32, i32* %15
%157 = sext i32 %156 to i64
%158 = getelementptr inbounds double, double* %155, i64 %157
%159 = load double, double* %158
%160 = call double @gsw_beta(double %153, double %154, double %159)
store double %160, double* %25
%161 = load double, double* %17
%162 = load double, double* %17
%163 = fmul double %161, %162
%164 = load double, double* %23
%165 = load double, double* %16
%166 = load double, double* %22
%167 = fmul double %165, %166
%168 = fdiv double %164, %167
%169 = fmul double %163, %168
%170 = load double, double* %25
%171 = load double, double* %18
%172 = load double, double* %24
%173 = load double, double* %20
%174 = fmul double %172, %173
%175 = fneg double %174
%176 = call double @llvm.fmuladd.f64(double %170, double %171, double %175)
%177 = fmul double %169, %176
%178 = load double*, double** %13
%179 = load i32, i32* %15
%180 = sext i32 %179 to i64
%181 = getelementptr inbounds double, double* %178, i64 %180
store double %177, double* %181
br label %182
182: ; preds = %31
%183 = load i32, i32* %15
%184 = add nsw i32 %183, 1
store i32 %184, i32* %15
br label %26
185: ; preds = %26
ret void
}

LLVM IR (-O2):
define void @gsw_nsquared(double* nocapture %0, double* nocapture %1, double* nocapture %2, double* nocapture %3, i32 %4, double* nocapture writeonly %5, double* nocapture %6) {
%8 = icmp sgt i32 %4, 1
br i1 %8, label %9, label %62
9: ; preds = %7
%10 = add nsw i32 %4, -1
%11 = zext i32 %10 to i64
br label %12
12: ; preds = %9, %12
%13 = phi i64 [ 0, %9 ], [ %19, %12 ]
%14 = getelementptr inbounds double, double* %3, i64 %13
%15 = load double, double* %14
%16 = getelementptr inbounds double, double* %2, i64 %13
%17 = load double, double* %16
%18 = tail call double @gsw_grav(double %15, double %17)
%19 = add nuw nsw i64 %13, 1
%20 = getelementptr inbounds double, double* %3, i64 %19
%21 = load double, double* %20
%22 = getelementptr inbounds double, double* %2, i64 %19
%23 = load double, double* %22
%24 = tail call double @gsw_grav(double %21, double %23)
%25 = fadd double %18, %24
%26 = fmul double %25, 5.000000e-01
%27 = getelementptr inbounds double, double* %0, i64 %19
%28 = load double, double* %27
%29 = getelementptr inbounds double, double* %0, i64 %13
%30 = load double, double* %29
%31 = fsub double %28, %30
%32 = fadd double %28, %30
%33 = fmul double %32, 5.000000e-01
%34 = getelementptr inbounds double, double* %1, i64 %19
%35 = load double, double* %34
%36 = getelementptr inbounds double, double* %1, i64 %13
%37 = load double, double* %36
%38 = fsub double %35, %37
%39 = fadd double %35, %37
%40 = fmul double %39, 5.000000e-01
%41 = load double, double* %22
%42 = load double, double* %16
%43 = fsub double %41, %42
%44 = fadd double %41, %42
%45 = fmul double %44, 5.000000e-01
%46 = getelementptr inbounds double, double* %6, i64 %13
store double %45, double* %46
%47 = tail call double @gsw_rho(double %33, double %40, double %45)
%48 = load double, double* %46
%49 = tail call double @gsw_alpha(double %33, double %40, double %48)
%50 = load double, double* %46
%51 = tail call double @gsw_beta(double %33, double %40, double %50)
%52 = fmul double %26, %26
%53 = fmul double %43, 1.000000e+04
%54 = fdiv double %47, %53
%55 = fmul double %52, %54
%56 = fneg double %49
%57 = fmul double %38, %56
%58 = tail call double @llvm.fmuladd.f64(double %51, double %31, double %57)
%59 = fmul double %55, %58
%60 = getelementptr inbounds double, double* %5, i64 %13
store double %59, double* %60
%61 = icmp eq i64 %19, %11
br i1 %61, label %62, label %12
62: ; preds = %12, %7
ret void
}