int imcomp_nullscaledoubles(double *fdata, long tilelen, int *idata,
                            double scale, double zero, int nullcheck,
                            double nullflagval, int nullval, int *status) {
  long ii;
  double dvalue;
  if (nullcheck == 1) {
    for (ii = 0; ii < tilelen; ii++) {
      if (fdata[ii] == nullflagval)
        idata[ii] = nullval;
      else {
        dvalue = (fdata[ii] - zero) / scale;
        if (dvalue < -2147483648.49) {
          *status = -11;
          idata[ii] = (-2147483647 - 1);
        } else if (dvalue > 2147483647.49) {
          *status = -11;
          idata[ii] = 2147483647;
        } else {
          if (dvalue >= 0.)
            idata[ii] = (int)(dvalue + .5);
          else
            idata[ii] = (int)(dvalue - .5);
        }
      }
    }
  } else {
    for (ii = 0; ii < tilelen; ii++) {
      dvalue = (fdata[ii] - zero) / scale;
      if (dvalue < -2147483648.49) {
        *status = -11;
        idata[ii] = (-2147483647 - 1);
      } else if (dvalue > 2147483647.49) {
        *status = -11;
        idata[ii] = 2147483647;
      } else {
        if (dvalue >= 0.)
          idata[ii] = (int)(dvalue + .5);
        else
          idata[ii] = (int)(dvalue - .5);
      }
    }
  }
  return (*status);
}


x86 assembly (clang, -O0):
<imcomp_nullscaledoubles>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movsd  %xmm0,-0x20(%rbp)
movsd  %xmm1,-0x28(%rbp)
mov    %ecx,-0x2c(%rbp)
movsd  %xmm2,-0x38(%rbp)
mov    %r8d,-0x3c(%rbp)
mov    %r9,-0x48(%rbp)
cmpl   $0x1,-0x2c(%rbp)
jne    127a <imcomp_nullscaledoubles+0x17a>
movq   $0x0,-0x50(%rbp)
mov    -0x50(%rbp),%rax
cmp    -0x10(%rbp),%rax
jge    1275 <imcomp_nullscaledoubles+0x175>
mov    -0x8(%rbp),%rax
mov    -0x50(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
ucomisd -0x38(%rbp),%xmm0
jne    117b <imcomp_nullscaledoubles+0x7b>
jp     117b <imcomp_nullscaledoubles+0x7b>
mov    -0x3c(%rbp),%edx
mov    -0x18(%rbp),%rax
mov    -0x50(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
jmp    125f <imcomp_nullscaledoubles+0x15f>
mov    -0x8(%rbp),%rax
mov    -0x50(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
subsd  -0x28(%rbp),%xmm0
divsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x58(%rbp)
movsd  0xe61(%rip),%xmm0
ucomisd -0x58(%rbp),%xmm0
jbe    11c8 <imcomp_nullscaledoubles+0xc8>
mov    -0x48(%rbp),%rax
movl   $0xfffffff5,(%rax)
mov    -0x18(%rbp),%rax
mov    -0x50(%rbp),%rcx
movl   $0x80000000,(%rax,%rcx,4)
jmp    125a <imcomp_nullscaledoubles+0x15a>
movsd  -0x58(%rbp),%xmm0
movsd  0xe33(%rip),%xmm1
ucomisd %xmm1,%xmm0
jbe    11fd <imcomp_nullscaledoubles+0xfd>
mov    -0x48(%rbp),%rax
movl   $0xfffffff5,(%rax)
mov    -0x18(%rbp),%rax
mov    -0x50(%rbp),%rcx
movl   $0x7fffffff,(%rax,%rcx,4)
jmp    1255 <imcomp_nullscaledoubles+0x155>
movsd  -0x58(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jb     1230 <imcomp_nullscaledoubles+0x130>
movsd  0xdf9(%rip),%xmm0
addsd  -0x58(%rbp),%xmm0
cvttsd2si %xmm0,%edx
mov    -0x18(%rbp),%rax
mov    -0x50(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
jmp    1250 <imcomp_nullscaledoubles+0x150>
movsd  -0x58(%rbp),%xmm0
movsd  0xdd3(%rip),%xmm1
subsd  %xmm1,%xmm0
cvttsd2si %xmm0,%edx
mov    -0x18(%rbp),%rax
mov    -0x50(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
jmp    1255 <imcomp_nullscaledoubles+0x155>
jmp    125a <imcomp_nullscaledoubles+0x15a>
jmp    125f <imcomp_nullscaledoubles+0x15f>
jmp    1264 <imcomp_nullscaledoubles+0x164>
mov    -0x50(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x50(%rbp)
jmp    113c <imcomp_nullscaledoubles+0x3c>
jmp    138a <imcomp_nullscaledoubles+0x28a>
movq   $0x0,-0x50(%rbp)
mov    -0x50(%rbp),%rax
cmp    -0x10(%rbp),%rax
jge    1385 <imcomp_nullscaledoubles+0x285>
mov    -0x8(%rbp),%rax
mov    -0x50(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
subsd  -0x28(%rbp),%xmm0
divsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x58(%rbp)
movsd  0xd4c(%rip),%xmm0
ucomisd -0x58(%rbp),%xmm0
jbe    12dd <imcomp_nullscaledoubles+0x1dd>
mov    -0x48(%rbp),%rax
movl   $0xfffffff5,(%rax)
mov    -0x18(%rbp),%rax
mov    -0x50(%rbp),%rcx
movl   $0x80000000,(%rax,%rcx,4)
jmp    136f <imcomp_nullscaledoubles+0x26f>
movsd  -0x58(%rbp),%xmm0
movsd  0xd1e(%rip),%xmm1
ucomisd %xmm1,%xmm0
jbe    1312 <imcomp_nullscaledoubles+0x212>
mov    -0x48(%rbp),%rax
movl   $0xfffffff5,(%rax)
mov    -0x18(%rbp),%rax
mov    -0x50(%rbp),%rcx
movl   $0x7fffffff,(%rax,%rcx,4)
jmp    136a <imcomp_nullscaledoubles+0x26a>
movsd  -0x58(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jb     1345 <imcomp_nullscaledoubles+0x245>
movsd  0xce4(%rip),%xmm0
addsd  -0x58(%rbp),%xmm0
cvttsd2si %xmm0,%edx
mov    -0x18(%rbp),%rax
mov    -0x50(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
jmp    1365 <imcomp_nullscaledoubles+0x265>
movsd  -0x58(%rbp),%xmm0
movsd  0xcbe(%rip),%xmm1
subsd  %xmm1,%xmm0
cvttsd2si %xmm0,%edx
mov    -0x18(%rbp),%rax
mov    -0x50(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
jmp    136a <imcomp_nullscaledoubles+0x26a>
jmp    136f <imcomp_nullscaledoubles+0x26f>
jmp    1374 <imcomp_nullscaledoubles+0x274>
mov    -0x50(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x50(%rbp)
jmp    1282 <imcomp_nullscaledoubles+0x182>
jmp    138a <imcomp_nullscaledoubles+0x28a>
mov    -0x48(%rbp),%rax
mov    (%rax),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<imcomp_nullscaledoubles>:
cmp    $0x1,%ecx
jne    11a9 <imcomp_nullscaledoubles+0xa9>
test   %rsi,%rsi
jle    1236 <imcomp_nullscaledoubles+0x136>
xor    %eax,%eax
movsd  0xee4(%rip),%xmm3
movsd  0xee4(%rip),%xmm4
xorpd  %xmm5,%xmm5
movsd  0xee7(%rip),%xmm8
movsd  0xed7(%rip),%xmm7
jmp    1160 <imcomp_nullscaledoubles+0x60>
movl   $0xfffffff5,(%r9)
mov    $0x80000000,%ecx
nopw   0x0(%rax,%rax,1)
mov    %ecx,(%rdx,%rax,4)
add    $0x1,%rax
cmp    %rax,%rsi
je     1236 <imcomp_nullscaledoubles+0x136>
movsd  (%rdi,%rax,8),%xmm6
ucomisd %xmm2,%xmm6
mov    %r8d,%ecx
jne    1170 <imcomp_nullscaledoubles+0x70>
jnp    1150 <imcomp_nullscaledoubles+0x50>
subsd  %xmm1,%xmm6
divsd  %xmm0,%xmm6
ucomisd %xmm6,%xmm3
ja     113b <imcomp_nullscaledoubles+0x3b>
ucomisd %xmm4,%xmm6
jbe    1192 <imcomp_nullscaledoubles+0x92>
movl   $0xfffffff5,(%r9)
mov    $0x7fffffff,%ecx
jmp    1150 <imcomp_nullscaledoubles+0x50>
ucomisd %xmm5,%xmm6
jae    119f <imcomp_nullscaledoubles+0x9f>
addsd  %xmm8,%xmm6
jmp    11a3 <imcomp_nullscaledoubles+0xa3>
addsd  %xmm7,%xmm6
cvttsd2si %xmm6,%ecx
jmp    1150 <imcomp_nullscaledoubles+0x50>
test   %rsi,%rsi
jle    1236 <imcomp_nullscaledoubles+0x136>
xor    %eax,%eax
movsd  0xe44(%rip),%xmm2
movsd  0xe44(%rip),%xmm3
xorpd  %xmm4,%xmm4
movsd  0xe48(%rip),%xmm5
movsd  0xe38(%rip),%xmm6
jmp    11f8 <imcomp_nullscaledoubles+0xf8>
nopw   0x0(%rax,%rax,1)
movl   $0xfffffff5,(%r9)
mov    $0x80000000,%ecx
mov    %ecx,(%rdx,%rax,4)
add    $0x1,%rax
cmp    %rax,%rsi
je     1236 <imcomp_nullscaledoubles+0x136>
movsd  (%rdi,%rax,8),%xmm7
subsd  %xmm1,%xmm7
divsd  %xmm0,%xmm7
ucomisd %xmm7,%xmm2
ja     11e0 <imcomp_nullscaledoubles+0xe0>
ucomisd %xmm3,%xmm7
jbe    1220 <imcomp_nullscaledoubles+0x120>
movl   $0xfffffff5,(%r9)
mov    $0x7fffffff,%ecx
jmp    11ec <imcomp_nullscaledoubles+0xec>
nop
ucomisd %xmm4,%xmm7
jae    122c <imcomp_nullscaledoubles+0x12c>
addsd  %xmm5,%xmm7
jmp    1230 <imcomp_nullscaledoubles+0x130>
addsd  %xmm6,%xmm7
cvttsd2si %xmm7,%ecx
jmp    11ec <imcomp_nullscaledoubles+0xec>
mov    (%r9),%eax
ret

LLVM IR (-O0):
define i32 @imcomp_nullscaledoubles(double* %0, i64 %1, i32* %2, double %3, double %4, i32 %5, double %6, i32 %7, i32* %8) {
%10 = alloca double*
%11 = alloca i64
%12 = alloca i32*
%13 = alloca double
%14 = alloca double
%15 = alloca i32
%16 = alloca double
%17 = alloca i32
%18 = alloca i32*
%19 = alloca i64
%20 = alloca double
store double* %0, double** %10
store i64 %1, i64* %11
store i32* %2, i32** %12
store double %3, double* %13
store double %4, double* %14
store i32 %5, i32* %15
store double %6, double* %16
store i32 %7, i32* %17
store i32* %8, i32** %18
%21 = load i32, i32* %15
%22 = icmp eq i32 %21, 1
br i1 %22, label %23, label %89
23: ; preds = %9
store i64 0, i64* %19
br label %24
24: ; preds = %85, %23
%25 = load i64, i64* %19
%26 = load i64, i64* %11
%27 = icmp slt i64 %25, %26
br i1 %27, label %28, label %88
28: ; preds = %24
%29 = load double*, double** %10
%30 = load i64, i64* %19
%31 = getelementptr inbounds double, double* %29, i64 %30
%32 = load double, double* %31
%33 = load double, double* %16
%34 = fcmp oeq double %32, %33
br i1 %34, label %35, label %40
35: ; preds = %28
%36 = load i32, i32* %17
%37 = load i32*, i32** %12
%38 = load i64, i64* %19
%39 = getelementptr inbounds i32, i32* %37, i64 %38
store i32 %36, i32* %39
br label %84
40: ; preds = %28
%41 = load double*, double** %10
%42 = load i64, i64* %19
%43 = getelementptr inbounds double, double* %41, i64 %42
%44 = load double, double* %43
%45 = load double, double* %14
%46 = fsub double %44, %45
%47 = load double, double* %13
%48 = fdiv double %46, %47
store double %48, double* %20
%49 = load double, double* %20
%50 = fcmp olt double %49, 0xC1E00000000FAE14
br i1 %50, label %51, label %56
51: ; preds = %40
%52 = load i32*, i32** %18
store i32 -11, i32* %52
%53 = load i32*, i32** %12
%54 = load i64, i64* %19
%55 = getelementptr inbounds i32, i32* %53, i64 %54
store i32 -2147483648, i32* %55
br label %83
56: ; preds = %40
%57 = load double, double* %20
%58 = fcmp ogt double %57, 0x41DFFFFFFFDF5C29
br i1 %58, label %59, label %64
59: ; preds = %56
%60 = load i32*, i32** %18
store i32 -11, i32* %60
%61 = load i32*, i32** %12
%62 = load i64, i64* %19
%63 = getelementptr inbounds i32, i32* %61, i64 %62
store i32 2147483647, i32* %63
br label %82
64: ; preds = %56
%65 = load double, double* %20
%66 = fcmp oge double %65, 0.000000e+00
br i1 %66, label %67, label %74
67: ; preds = %64
%68 = load double, double* %20
%69 = fadd double %68, 5.000000e-01
%70 = fptosi double %69 to i32
%71 = load i32*, i32** %12
%72 = load i64, i64* %19
%73 = getelementptr inbounds i32, i32* %71, i64 %72
store i32 %70, i32* %73
br label %81
74: ; preds = %64
%75 = load double, double* %20
%76 = fsub double %75, 5.000000e-01
%77 = fptosi double %76 to i32
%78 = load i32*, i32** %12
%79 = load i64, i64* %19
%80 = getelementptr inbounds i32, i32* %78, i64 %79
store i32 %77, i32* %80
br label %81
81: ; preds = %74, %67
br label %82
82: ; preds = %81, %59
br label %83
83: ; preds = %82, %51
br label %84
84: ; preds = %83, %35
br label %85
85: ; preds = %84
%86 = load i64, i64* %19
%87 = add nsw i64 %86, 1
store i64 %87, i64* %19
br label %24
88: ; preds = %24
br label %142
89: ; preds = %9
store i64 0, i64* %19
br label %90
90: ; preds = %138, %89
%91 = load i64, i64* %19
%92 = load i64, i64* %11
%93 = icmp slt i64 %91, %92
br i1 %93, label %94, label %141
94: ; preds = %90
%95 = load double*, double** %10
%96 = load i64, i64* %19
%97 = getelementptr inbounds double, double* %95, i64 %96
%98 = load double, double* %97
%99 = load double, double* %14
%100 = fsub double %98, %99
%101 = load double, double* %13
%102 = fdiv double %100, %101
store double %102, double* %20
%103 = load double, double* %20
%104 = fcmp olt double %103, 0xC1E00000000FAE14
br i1 %104, label %105, label %110
105: ; preds = %94
%106 = load i32*, i32** %18
store i32 -11, i32* %106
%107 = load i32*, i32** %12
%108 = load i64, i64* %19
%109 = getelementptr inbounds i32, i32* %107, i64 %108
store i32 -2147483648, i32* %109
br label %137
110: ; preds = %94
%111 = load double, double* %20
%112 = fcmp ogt double %111, 0x41DFFFFFFFDF5C29
br i1 %112, label %113, label %118
113: ; preds = %110
%114 = load i32*, i32** %18
store i32 -11, i32* %114
%115 = load i32*, i32** %12
%116 = load i64, i64* %19
%117 = getelementptr inbounds i32, i32* %115, i64 %116
store i32 2147483647, i32* %117
br label %136
118: ; preds = %110
%119 = load double, double* %20
%120 = fcmp oge double %119, 0.000000e+00
br i1 %120, label %121, label %128
121: ; preds = %118
%122 = load double, double* %20
%123 = fadd double %122, 5.000000e-01
%124 = fptosi double %123 to i32
%125 = load i32*, i32** %12
%126 = load i64, i64* %19
%127 = getelementptr inbounds i32, i32* %125, i64 %126
store i32 %124, i32* %127
br label %135
128: ; preds = %118
%129 = load double, double* %20
%130 = fsub double %129, 5.000000e-01
%131 = fptosi double %130 to i32
%132 = load i32*, i32** %12
%133 = load i64, i64* %19
%134 = getelementptr inbounds i32, i32* %132, i64 %133
store i32 %131, i32* %134
br label %135
135: ; preds = %128, %121
br label %136
136: ; preds = %135, %113
br label %137
137: ; preds = %136, %105
br label %138
138: ; preds = %137
%139 = load i64, i64* %19
%140 = add nsw i64 %139, 1
store i64 %140, i64* %19
br label %90
141: ; preds = %90
br label %142
142: ; preds = %141, %88
%143 = load i32*, i32** %18
%144 = load i32, i32* %143
ret i32 %144
}

LLVM IR (-O2):
define i32 @imcomp_nullscaledoubles(double* nocapture %0, i64 %1, i32* nocapture writeonly %2, double %3, double %4, i32 %5, double %6, i32 %7, i32* nocapture %8) {
%10 = icmp eq i32 %5, 1
%11 = icmp sgt i64 %1, 0
br i1 %10, label %13, label %12
12: ; preds = %9
br i1 %11, label %40, label %64
13: ; preds = %9
br i1 %11, label %14, label %64
14: ; preds = %13, %35
%15 = phi i64 [ %38, %35 ], [ 0, %13 ]
%16 = getelementptr inbounds double, double* %0, i64 %15
%17 = load double, double* %16
%18 = fcmp oeq double %17, %6
br i1 %18, label %35, label %19
19: ; preds = %14
%20 = fsub double %17, %4
%21 = fdiv double %20, %3
%22 = fcmp olt double %21, 0xC1E00000000FAE14
br i1 %22, label %23, label %24
23: ; preds = %19
store i32 -11, i32* %8
br label %35
24: ; preds = %19
%25 = fcmp ogt double %21, 0x41DFFFFFFFDF5C29
br i1 %25, label %26, label %27
26: ; preds = %24
store i32 -11, i32* %8
br label %35
27: ; preds = %24
%28 = fcmp ult double %21, 0.000000e+00
br i1 %28, label %32, label %29
29: ; preds = %27
%30 = fadd double %21, 5.000000e-01
%31 = fptosi double %30 to i32
br label %35
32: ; preds = %27
%33 = fadd double %21, -5.000000e-01
%34 = fptosi double %33 to i32
br label %35
35: ; preds = %14, %26, %32, %29, %23
%36 = phi i32 [ 2147483647, %26 ], [ %34, %32 ], [ %31, %29 ], [ -2147483648, %23 ], [ %7, %14 ]
%37 = getelementptr inbounds i32, i32* %2, i64 %15
store i32 %36, i32* %37
%38 = add nuw nsw i64 %15, 1
%39 = icmp eq i64 %38, %1
br i1 %39, label %64, label %14
40: ; preds = %12, %59
%41 = phi i64 [ %62, %59 ], [ 0, %12 ]
%42 = getelementptr inbounds double, double* %0, i64 %41
%43 = load double, double* %42
%44 = fsub double %43, %4
%45 = fdiv double %44, %3
%46 = fcmp olt double %45, 0xC1E00000000FAE14
br i1 %46, label %47, label %48
47: ; preds = %40
store i32 -11, i32* %8
br label %59
48: ; preds = %40
%49 = fcmp ogt double %45, 0x41DFFFFFFFDF5C29
br i1 %49, label %50, label %51
50: ; preds = %48
store i32 -11, i32* %8
br label %59
51: ; preds = %48
%52 = fcmp ult double %45, 0.000000e+00
br i1 %52, label %56, label %53
53: ; preds = %51
%54 = fadd double %45, 5.000000e-01
%55 = fptosi double %54 to i32
br label %59
56: ; preds = %51
%57 = fadd double %45, -5.000000e-01
%58 = fptosi double %57 to i32
br label %59
59: ; preds = %47, %53, %56, %50
%60 = phi i32 [ -2147483648, %47 ], [ %55, %53 ], [ %58, %56 ], [ 2147483647, %50 ]
%61 = getelementptr inbounds i32, i32* %2, i64 %41
store i32 %60, i32* %61
%62 = add nuw nsw i64 %41, 1
%63 = icmp eq i64 %62, %1
br i1 %63, label %64, label %40
64: ; preds = %59, %35, %12, %13
%65 = load i32, i32* %8
ret i32 %65
}