int psd_tonemasker(double *psd, int psd_len, int splnum1, int splnum2,
                   double *ptm, int *tone_flag) {
  int k;
  memset(ptm, 0, sizeof(double) * psd_len);
  memset(tone_flag, 0, sizeof(int) * psd_len);
  for (k = 0; k < psd_len; k++) {
    if (istone(psd, psd_len, k, splnum1, splnum2, tone_flag)) {
      ptm[k] = db_pos(db_inv(psd[k - 1]) + db_inv(psd[k]) + db_inv(psd[k + 1]));
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<psd_tonemasker>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
mov    %r8,-0x20(%rbp)
mov    %r9,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
movslq -0xc(%rbp),%rdx
shl    $0x3,%rdx
xor    %esi,%esi
call   1030 <memset@plt>
mov    -0x28(%rbp),%rdi
movslq -0xc(%rbp),%rdx
shl    $0x2,%rdx
xor    %esi,%esi
call   1030 <memset@plt>
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    123e <psd_tonemasker+0xfe>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x2c(%rbp),%edx
mov    -0x10(%rbp),%ecx
mov    -0x14(%rbp),%r8d
mov    -0x28(%rbp),%r9
mov    $0x0,%al
call   1050 <istone@plt>
cmp    $0x0,%eax
je     122b <psd_tonemasker+0xeb>
mov    -0x8(%rbp),%rax
mov    -0x2c(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsd  (%rax,%rcx,8),%xmm0
mov    $0x1,%al
call   1060 <db_inv@plt>
mov    %eax,-0x34(%rbp)
mov    -0x8(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
mov    $0x1,%al
call   1060 <db_inv@plt>
mov    %eax,%ecx
mov    -0x34(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x30(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x2c(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
movsd  (%rax,%rcx,8),%xmm0
mov    $0x1,%al
call   1060 <db_inv@plt>
mov    -0x30(%rbp),%edi
add    %eax,%edi
mov    $0x0,%al
call   1040 <db_pos@plt>
cvtsi2sd %eax,%xmm0
mov    -0x20(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movsd  %xmm0,(%rax,%rcx,8)
jmp    1230 <psd_tonemasker+0xf0>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    118a <psd_tonemasker+0x4a>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<psd_tonemasker>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r9,%r15
mov    %ecx,0xc(%rsp)
mov    %edx,0x8(%rsp)
mov    %rdi,%rbx
mov    %esi,0x4(%rsp)
movslq %esi,%r14
lea    0x0(,%r14,8),%rdx
mov    %r8,0x10(%rsp)
mov    %r8,%rdi
xor    %esi,%esi
call   1030 <memset@plt>
lea    0x0(,%r14,4),%rdx
mov    %r15,%rbp
mov    %r15,%rdi
xor    %esi,%esi
call   1030 <memset@plt>
test   %r14d,%r14d
jle    1234 <psd_tonemasker+0xf4>
mov    0x4(%rsp),%r12d
xor    %r14d,%r14d
jmp    120a <psd_tonemasker+0xca>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movsd  -0x8(%rbx,%r14,8),%xmm0
mov    $0x1,%al
call   1060 <db_inv@plt>
mov    %eax,%r15d
movsd  (%rbx,%r14,8),%xmm0
mov    $0x1,%al
call   1060 <db_inv@plt>
add    %eax,%r15d
lea    0x1(%r14),%r13
movsd  0x8(%rbx,%r14,8),%xmm0
mov    $0x1,%al
call   1060 <db_inv@plt>
add    %eax,%r15d
mov    %r15d,%edi
xor    %eax,%eax
call   1040 <db_pos@plt>
xorps  %xmm0,%xmm0
cvtsi2sd %eax,%xmm0
mov    0x10(%rsp),%rax
movsd  %xmm0,(%rax,%r14,8)
mov    %r13,%r14
cmp    %r12,%r14
je     1234 <psd_tonemasker+0xf4>
mov    %rbx,%rdi
mov    0x4(%rsp),%esi
mov    %r14d,%edx
mov    0x8(%rsp),%ecx
mov    0xc(%rsp),%r8d
mov    %rbp,%r9
xor    %eax,%eax
call   1050 <istone@plt>
test   %eax,%eax
jne    11b0 <psd_tonemasker+0x70>
add    $0x1,%r14
cmp    %r12,%r14
jne    120a <psd_tonemasker+0xca>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @psd_tonemasker(double* %0, i32 %1, i32 %2, i32 %3, double* %4, i32* %5) {
%7 = alloca double*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca double*
%12 = alloca i32*
%13 = alloca i32
store double* %0, double** %7
store i32 %1, i32* %8
store i32 %2, i32* %9
store i32 %3, i32* %10
store double* %4, double** %11
store i32* %5, i32** %12
%14 = load double*, double** %11
%15 = bitcast double* %14 to i8*
%16 = load i32, i32* %8
%17 = sext i32 %16 to i64
%18 = mul i64 8, %17
%19 = load i32*, i32** %12
%20 = bitcast i32* %19 to i8*
%21 = load i32, i32* %8
%22 = sext i32 %21 to i64
%23 = mul i64 4, %22
store i32 0, i32* %13
br label %24
24: ; preds = %67, %6
%25 = load i32, i32* %13
%26 = load i32, i32* %8
%27 = icmp slt i32 %25, %26
br i1 %27, label %28, label %70
28: ; preds = %24
%29 = load double*, double** %7
%30 = load i32, i32* %8
%31 = load i32, i32* %13
%32 = load i32, i32* %9
%33 = load i32, i32* %10
%34 = load i32*, i32** %12
%35 = call i32 (double*, i32, i32, i32, i32, i32*, ...) bitcast (i32 (...)* @istone to i32 (double*, i32, i32, i32, i32, i32*, ...)*)(double* %29, i32 %30, i32 %31, i32 %32, i32 %33, i32* %34)
%36 = icmp ne i32 %35, 0
br i1 %36, label %37, label %66
37: ; preds = %28
%38 = load double*, double** %7
%39 = load i32, i32* %13
%40 = sub nsw i32 %39, 1
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds double, double* %38, i64 %41
%43 = load double, double* %42
%44 = call i32 (double, ...) bitcast (i32 (...)* @db_inv to i32 (double, ...)*)(double %43)
%45 = load double*, double** %7
%46 = load i32, i32* %13
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds double, double* %45, i64 %47
%49 = load double, double* %48
%50 = call i32 (double, ...) bitcast (i32 (...)* @db_inv to i32 (double, ...)*)(double %49)
%51 = add nsw i32 %44, %50
%52 = load double*, double** %7
%53 = load i32, i32* %13
%54 = add nsw i32 %53, 1
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds double, double* %52, i64 %55
%57 = load double, double* %56
%58 = call i32 (double, ...) bitcast (i32 (...)* @db_inv to i32 (double, ...)*)(double %57)
%59 = add nsw i32 %51, %58
%60 = call i32 (i32, ...) bitcast (i32 (...)* @db_pos to i32 (i32, ...)*)(i32 %59)
%61 = sitofp i32 %60 to double
%62 = load double*, double** %11
%63 = load i32, i32* %13
%64 = sext i32 %63 to i64
%65 = getelementptr inbounds double, double* %62, i64 %64
store double %61, double* %65
br label %66
66: ; preds = %37, %28
br label %67
67: ; preds = %66
%68 = load i32, i32* %13
%69 = add nsw i32 %68, 1
store i32 %69, i32* %13
br label %24
70: ; preds = %24
ret i32 0
}

LLVM IR (-O2):
define i32 @psd_tonemasker(double* %0, i32 %1, i32 %2, i32 %3, double* nocapture writeonly %4, i32* %5) {
%7 = bitcast double* %4 to i8*
%8 = sext i32 %1 to i64
%9 = shl nsw i64 %8, 3
tail call void @llvm.memset.p0i8.i64(i8* align 8 %7, i8 0, i64 %9, i1 false)
%10 = bitcast i32* %5 to i8*
%11 = shl nsw i64 %8, 2
tail call void @llvm.memset.p0i8.i64(i8* align 4 %10, i8 0, i64 %11, i1 false)
%12 = icmp sgt i32 %1, 0
br i1 %12, label %13, label %42
13: ; preds = %6
%14 = zext i32 %1 to i64
br label %15
15: ; preds = %13, %39
%16 = phi i64 [ 0, %13 ], [ %40, %39 ]
%17 = trunc i64 %16 to i32
%18 = tail call i32 (double*, i32, i32, i32, i32, i32*, ...) bitcast (i32 (...)* @istone to i32 (double*, i32, i32, i32, i32, i32*, ...)*)(double* %0, i32 %1, i32 %17, i32 %2, i32 %3, i32* %5)
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %15
%21 = add nuw nsw i64 %16, 1
br label %39
22: ; preds = %15
%23 = add nsw i64 %16, -1
%24 = getelementptr inbounds double, double* %0, i64 %23
%25 = load double, double* %24
%26 = tail call i32 (double, ...) bitcast (i32 (...)* @db_inv to i32 (double, ...)*)(double %25)
%27 = getelementptr inbounds double, double* %0, i64 %16
%28 = load double, double* %27
%29 = tail call i32 (double, ...) bitcast (i32 (...)* @db_inv to i32 (double, ...)*)(double %28)
%30 = add nsw i32 %29, %26
%31 = add nuw nsw i64 %16, 1
%32 = getelementptr inbounds double, double* %0, i64 %31
%33 = load double, double* %32
%34 = tail call i32 (double, ...) bitcast (i32 (...)* @db_inv to i32 (double, ...)*)(double %33)
%35 = add nsw i32 %30, %34
%36 = tail call i32 (i32, ...) bitcast (i32 (...)* @db_pos to i32 (i32, ...)*)(i32 %35)
%37 = sitofp i32 %36 to double
%38 = getelementptr inbounds double, double* %4, i64 %16
store double %37, double* %38
br label %39
39: ; preds = %20, %22
%40 = phi i64 [ %21, %20 ], [ %31, %22 ]
%41 = icmp eq i64 %40, %14
br i1 %41, label %42, label %15
42: ; preds = %39, %6
ret i32 0
}