// File: /workspace/github_c_makefile_star2_O0/esheldon[P]xshear/src/shear.c
// Function: shear_procpair
void shear_procpair(Shear* self, 
                    Source* src, 
                    Lens* lens, 
                    Lensum* lensum) {

    ShearConfig* config=self->config;

    // make sure object is in a pair of unmasked adjacent
    // quadrants.  Using short-circuiting in if statement


    // checks against max radius
    int quadrant;
    double r_radians, posangle_radians;
    int keep=get_quad_info(lens, src,
                           &quadrant,
                           &r_radians,
                           &posangle_radians);

    if (!keep) {
        goto _procpair_bail;
    }

    if (config->mask_style == MASK_STYLE_EQ) {
        if (!quadeq_check_quadrant(lens->maskflags, quadrant)) {
            goto _procpair_bail;
        }
    } else if (config->mask_style == MASK_STYLE_SDSS) {
        if (!shear_test_quad_sdss(lens, src)) {
            goto _procpair_bail;
        }
    }

    double cos2pa = cos(2*posangle_radians);
    double sin2pa = sin(2*posangle_radians);


    // Philosophy:
    // Internal bookkeeping is the same, regardless of the input and intended output format.
    //
    // D=\sum_i(w_i*scinv_i^2*DeltaSigma_i)=\sum_i(w_i*scinv_i*g_i)
    // DeltaSigma=D/W where W=\sum_i(w_i*scinv_i^2) is a surface mass overdensity in physical units (Msol/Mpc^2)
    // g=D/S where S=\sum_i(w_i*scinv_i) is a unitless mean shear, weighted by (w_i*scinv_i)
    //
    // So we always output three columns: D, W=\sum_i(w_i*scinv_i^2) and S=\sum_i(w_i*scinv_i)
    //
    // We allow two different weighting style: config->weight_style=uniform and config->weight_style=optimal
    // 'optimal' will do what's written above, which maximizes the S/N
    // 'uniform' will use the weights w'_i = w_i/scinv_i; in that case, g is a unitless mean shear weighted by w_i 
    //           and DeltaSigma is still a (non-minimum variance) surface mass overdensity in physical units (Msol/Mpc^2)
    //
    // In the code below, we use the variable s for the weight on shear (i.e., D=\sum_i(s_i*g_i)).
    // In weight_style=optimal, s=w_i*scinv_i; in weight_style=uniform, s=w_i
    // 
    // The config->shear_units parameter controls the output format. "deltasig" and "shear" 
    // produce the backward-compatible output of D and W or, respectively, S
    // Output format "both" outputs both and also s_i and s_i*scinv_i-weighted sensitivities


    // (1) set sigma_crit^-1
    double scinv; // the sigma_crit^-1 that goes into S
    double zs;
    switch(config->scstyle) {
        case SIGMACRIT_STYLE_INTERP:
            scinv = interplin(src->zlens, src->scinv, lens->z);
            zs=-1.;
            break;
        case SIGMACRIT_STYLE_POINT:
            if ( (src->z - lens->z) < config->zdiff_min) {
                goto _procpair_bail;
            }
            double dcl = lens->da*(1.+lens->z);
            scinv = scinv_pre(lens->z, dcl, src->dc);
            zs=src->z;
            break;
        case SIGMACRIT_STYLE_SAMPLE:
            if(src->zs <= lens->z) {
                scinv=0.;
            } else {
                double dcl = lens->da*(1.+lens->z);
                scinv = scinv_pre(lens->z, dcl, src->dcs); // at sampled z of the source
            }
            zs=src->zs;
            break;
        default:
            fprintf(stderr, "unknown sigmacrit style %d\n", config->scstyle);
    }
    
    
    // (2) set shear weight s
    double s=src->weight;
    if(config->weight_style==WEIGHT_STYLE_OPTIMAL) {
        switch(config->scstyle) {
            case SIGMACRIT_STYLE_INTERP:
            case SIGMACRIT_STYLE_POINT:
                s *= scinv;
                break;
            case SIGMACRIT_STYLE_SAMPLE:
                if ( (src->z - lens->z) < config->zdiff_min) {
                    goto _procpair_bail;
                }
                double dcl = lens->da*(1.+lens->z);
                s *= scinv_pre(lens->z, dcl, src->dc); // src->dc is dc at the <z>, not the sampled z of the source
                break;
            default:
                fprintf(stderr, "unknown sigmacrit style %ld\n", config->scstyle);
        }
    }


    // (3) determine radius
    double r;
    if (config->r_units==UNITS_MPC) {
        // Mpc
        r = r_radians*lens->da;
    } else {
        // arcmin
        r = r_radians*R2D*60.;
    }

    double logr = log10(r);
    if (logr < config->log_rmin || logr > config->log_rmax) {
        goto _procpair_bail;
    }

    int rbin = (int)( (logr-config->log_rmin)/config->log_binsize );
    if (rbin < 0 || rbin >= config->nbin) {
        goto _procpair_bail;
    }

    // sign here depends on convention
    //double gt = -(src->g1*cos2pa + src->g2*sin2pa);
    //double gx =  (src->g1*sin2pa - src->g2*cos2pa);
    double gt = -(src->g1*cos2pa - src->g2*sin2pa);
    double gx =  (src->g1*sin2pa + src->g2*cos2pa);

    lensum->weight += s*scinv;
    lensum->totpairs += 1;
    
    lensum->npair[rbin] += 1;
    lensum->wsum[rbin] += s*scinv;
    lensum->ssum[rbin] += s;
    
    lensum->dsum[rbin] += s*gt;
    lensum->osum[rbin] += s*gx;

    lensum->rsum[rbin] += s*scinv*r;

    // sensitivity of gt/gx to shear in that direction
    double gsens_t=1.;
    double gsens_x=1.;

    if (config->shear_style==SHEAR_STYLE_LENSFIT) {
        // sensitivity of gt is
        // dgt/dgammat=-dg1/dgammat*cos2pa-dg2/dgammat*sin2pa
        //            =-dg1/dgamma1*dgamma1/dgammat*cos2pa-dg2/dgamma2*dgamma2/dgammat*sin2pa
        //            =g1sens*cos2pa^2+g2sens*sin2pa^2
    
        // the alternative is to just average them
        // double gsens = 0.5*(src->g1sens + src->g2sens); 
        // the only reason to not do that are strong-ish shears, where gsens is significantly
        // different along the tangential direction because tangential shears aren't small
        
        gsens_t=src->g1sens*cos2pa*cos2pa+src->g2sens*sin2pa*sin2pa;
        gsens_x=src->g1sens*sin2pa*sin2pa+src->g2sens*cos2pa*cos2pa;
    }
    
    // will have to implement something more complex here for metacal, maybe
        
    lensum->dsensum_w[rbin] += s*scinv*gsens_t;
    lensum->osensum_w[rbin] += s*scinv*gsens_x;
    lensum->dsensum_s[rbin] += s*gsens_t;
    lensum->osensum_s[rbin] += s*gsens_x;

    if(rbin<config->pairlog_rmax && rbin>=config->pairlog_rmin) {
      fprintf(config->pair_fd, "%ld %ld %d %le %le %le %le\n", lens->index, src->index, rbin, s, scinv, gsens_t, zs);
    }

_procpair_bail:

    return;

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xd0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x18(%rbp), %rdi
movq -0x10(%rbp), %rsi
leaq -0x2c(%rbp), %rdx
leaq -0x38(%rbp), %rcx
leaq -0x40(%rbp), %r8
callq 0xb0c0
movl %eax, -0x44(%rbp)
cmpl $0x0, -0x44(%rbp)
jne 0xa98d
jmp 0xb0a9
movq -0x28(%rbp), %rax
cmpl $0x3, 0x1c(%rax)
jne 0xa9b3
movq -0x18(%rbp), %rax
movq 0x48(%rax), %rdi
movl -0x2c(%rbp), %esi
callq 0xe0e0
cmpl $0x0, %eax
jne 0xa9b1
jmp 0xb0a9
jmp 0xa9d8
movq -0x28(%rbp), %rax
cmpl $0x2, 0x1c(%rax)
jne 0xa9d6
movq -0x18(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0xb2e0
cmpl $0x0, %eax
jne 0xa9d4
jmp 0xb0a9
jmp 0xa9d6
jmp 0xa9d8
movsd -0x40(%rbp), %xmm0
addsd %xmm0, %xmm0
callq 0x2120
movsd %xmm0, -0x50(%rbp)
movsd -0x40(%rbp), %xmm0
addsd %xmm0, %xmm0
callq 0x2220
movsd %xmm0, -0x58(%rbp)
movq -0x28(%rbp), %rax
movl 0x20(%rax), %eax
movl %eax, -0xc4(%rbp)
subl $0x1, %eax
je 0xaa68
jmp 0xaa12
movl -0xc4(%rbp), %eax
subl $0x2, %eax
je 0xaa33
jmp 0xaa1f
movl -0xc4(%rbp), %eax
subl $0x3, %eax
je 0xaae8
jmp 0xab61
movq -0x10(%rbp), %rax
movq 0x60(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rsi
movq -0x18(%rbp), %rax
movsd 0x18(%rax), %xmm0
callq 0xd3a0
movsd %xmm0, -0x60(%rbp)
movsd 0x5312(%rip), %xmm0     # 0xfd70
movsd %xmm0, -0x68(%rbp)
jmp 0xab80
movq -0x10(%rbp), %rax
movsd 0x68(%rax), %xmm1
movq -0x18(%rbp), %rax
subsd 0x18(%rax), %xmm1
movq -0x28(%rbp), %rax
movsd 0x98(%rax), %xmm0
ucomisd %xmm1, %xmm0
jbe 0xaa91
jmp 0xb0a9
movq -0x18(%rbp), %rax
movsd 0x20(%rax), %xmm0
movq -0x18(%rbp), %rax
movsd 0x4e2a(%rip), %xmm1     # 0xf8d0
addsd 0x18(%rax), %xmm1
mulsd %xmm1, %xmm0
movsd %xmm0, -0x70(%rbp)
movq -0x18(%rbp), %rax
movsd 0x18(%rax), %xmm0
movsd -0x70(%rbp), %xmm1
movq -0x10(%rbp), %rax
movsd 0x70(%rax), %xmm2
callq 0x8ee0
movsd %xmm0, -0x60(%rbp)
movq -0x10(%rbp), %rax
movsd 0x68(%rax), %xmm0
movsd %xmm0, -0x68(%rbp)
jmp 0xab80
movq -0x10(%rbp), %rax
movsd 0x78(%rax), %xmm1
movq -0x18(%rbp), %rax
movsd 0x18(%rax), %xmm0
ucomisd %xmm1, %xmm0
jb 0xab0a
xorps %xmm0, %xmm0
movsd %xmm0, -0x60(%rbp)
jmp 0xab51
movq -0x18(%rbp), %rax
movsd 0x20(%rax), %xmm0
movq -0x18(%rbp), %rax
movsd 0x4db1(%rip), %xmm1     # 0xf8d0
addsd 0x18(%rax), %xmm1
mulsd %xmm1, %xmm0
movsd %xmm0, -0x78(%rbp)
movq -0x18(%rbp), %rax
movsd 0x18(%rax), %xmm0
movsd -0x78(%rbp), %xmm1
movq -0x10(%rbp), %rax
movsd 0x80(%rax), %xmm2
callq 0x8ee0
movsd %xmm0, -0x60(%rbp)
movq -0x10(%rbp), %rax
movsd 0x78(%rax), %xmm0
movsd %xmm0, -0x68(%rbp)
jmp 0xab80
movq 0x9490(%rip), %rax      # 0x13ff8
movq (%rax), %rdi
movq -0x28(%rbp), %rax
movl 0x20(%rax), %edx
leaq 0x54fd(%rip), %rsi      # 0x10076
movb $0x0, %al
callq 0x2170
movq -0x10(%rbp), %rax
movsd 0x48(%rax), %xmm0
movsd %xmm0, -0x80(%rbp)
movq -0x28(%rbp), %rax
cmpl $0x1, 0x28(%rax)
jne 0xac71
movq -0x28(%rbp), %rax
movl 0x20(%rax), %eax
movl %eax, -0xc8(%rbp)
decl %eax
subl $0x2, %eax
jb 0xabc2
jmp 0xabb2
movl -0xc8(%rbp), %eax
subl $0x3, %eax
je 0xabd6
jmp 0xac50
movsd -0x60(%rbp), %xmm0
mulsd -0x80(%rbp), %xmm0
movsd %xmm0, -0x80(%rbp)
jmp 0xac6f
movq -0x10(%rbp), %rax
movsd 0x68(%rax), %xmm1
movq -0x18(%rbp), %rax
subsd 0x18(%rax), %xmm1
movq -0x28(%rbp), %rax
movsd 0x98(%rax), %xmm0
ucomisd %xmm1, %xmm0
jbe 0xabff
jmp 0xb0a9
movq -0x18(%rbp), %rax
movsd 0x20(%rax), %xmm0
movq -0x18(%rbp), %rax
movsd 0x4cbc(%rip), %xmm1     # 0xf8d0
addsd 0x18(%rax), %xmm1
mulsd %xmm1, %xmm0
movsd %xmm0, -0x88(%rbp)
movq -0x18(%rbp), %rax
movsd 0x18(%rax), %xmm0
movsd -0x88(%rbp), %xmm1
movq -0x10(%rbp), %rax
movsd 0x70(%rax), %xmm2
callq 0x8ee0
mulsd -0x80(%rbp), %xmm0
movsd %xmm0, -0x80(%rbp)
jmp 0xac6f
movq 0x93a1(%rip), %rax      # 0x13ff8
movq (%rax), %rdi
movq -0x28(%rbp), %rax
movl 0x20(%rax), %edx
leaq 0x542a(%rip), %rsi      # 0x10092
movb $0x0, %al
callq 0x2170
jmp 0xac71
movq -0x28(%rbp), %rax
cmpl $0x1, 0x48(%rax)
jne 0xac93
movsd -0x38(%rbp), %xmm0
movq -0x18(%rbp), %rax
mulsd 0x20(%rax), %xmm0
movsd %xmm0, -0x90(%rbp)
jmp 0xacb4
movsd 0x528d(%rip), %xmm0     # 0xff28
mulsd -0x38(%rbp), %xmm0
movsd 0x5278(%rip), %xmm1     # 0xff20
mulsd %xmm1, %xmm0
movsd %xmm0, -0x90(%rbp)
movsd -0x90(%rbp), %xmm0
callq 0x2160
movsd %xmm0, -0x98(%rbp)
movsd -0x98(%rbp), %xmm1
movq -0x28(%rbp), %rax
movsd 0x78(%rax), %xmm0
ucomisd %xmm1, %xmm0
ja 0xacf6
movsd -0x98(%rbp), %xmm0
movq -0x28(%rbp), %rax
ucomisd 0x80(%rax), %xmm0
jbe 0xacfb
jmp 0xb0a9
movsd -0x98(%rbp), %xmm0
movq -0x28(%rbp), %rax
subsd 0x78(%rax), %xmm0
movq -0x28(%rbp), %rax
divsd 0x88(%rax), %xmm0
cvttsd2si %xmm0, %eax
movl %eax, -0x9c(%rbp)
cmpl $0x0, -0x9c(%rbp)
jl 0xad3c
movslq -0x9c(%rbp), %rax
movq -0x28(%rbp), %rcx
cmpq 0x40(%rcx), %rax
jl 0xad41
jmp 0xb0a9
movq -0x10(%rbp), %rax
movsd 0x28(%rax), %xmm0
movsd -0x50(%rbp), %xmm2
movq -0x10(%rbp), %rax
movsd 0x30(%rax), %xmm1
mulsd -0x58(%rbp), %xmm1
movq %xmm1, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movsd %xmm0, -0xa8(%rbp)
movq -0x10(%rbp), %rax
movsd 0x28(%rax), %xmm0
movsd -0x58(%rbp), %xmm2
movq -0x10(%rbp), %rax
movsd 0x30(%rax), %xmm1
mulsd -0x50(%rbp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0xb0(%rbp)
movsd -0x80(%rbp), %xmm0
movsd -0x60(%rbp), %xmm2
movq -0x20(%rbp), %rax
movsd 0x18(%rax), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x18(%rax)
movq -0x20(%rbp), %rax
movq 0x20(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x20(%rax)
movq -0x20(%rbp), %rax
movq 0x30(%rax), %rax
movslq -0x9c(%rbp), %rcx
movq (%rax,%rcx,8), %rdx
addq $0x1, %rdx
movq %rdx, (%rax,%rcx,8)
movsd -0x80(%rbp), %xmm0
movsd -0x60(%rbp), %xmm2
movq -0x20(%rbp), %rax
movq 0x40(%rax), %rax
movslq -0x9c(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movsd -0x80(%rbp), %xmm0
movq -0x20(%rbp), %rax
movq 0x48(%rax), %rax
movslq -0x9c(%rbp), %rcx
addsd (%rax,%rcx,8), %xmm0
movsd %xmm0, (%rax,%rcx,8)
movsd -0x80(%rbp), %xmm0
movsd -0xa8(%rbp), %xmm2
movq -0x20(%rbp), %rax
movq 0x50(%rax), %rax
movslq -0x9c(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movsd -0x80(%rbp), %xmm0
movsd -0xb0(%rbp), %xmm2
movq -0x20(%rbp), %rax
movq 0x58(%rax), %rax
movslq -0x9c(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movsd -0x80(%rbp), %xmm0
mulsd -0x60(%rbp), %xmm0
movsd -0x90(%rbp), %xmm2
movq -0x20(%rbp), %rax
movq 0x38(%rax), %rax
movslq -0x9c(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movsd 0x49de(%rip), %xmm0     # 0xf8d0
movsd %xmm0, -0xb8(%rbp)
movsd 0x49ce(%rip), %xmm0     # 0xf8d0
movsd %xmm0, -0xc0(%rbp)
movq -0x28(%rbp), %rax
cmpl $0x2, 0x18(%rax)
jne 0xaf80
movq -0x10(%rbp), %rax
movsd 0x38(%rax), %xmm0
mulsd -0x50(%rbp), %xmm0
movsd -0x50(%rbp), %xmm2
movq -0x10(%rbp), %rax
movsd 0x40(%rax), %xmm1
mulsd -0x58(%rbp), %xmm1
mulsd -0x58(%rbp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0xb8(%rbp)
movq -0x10(%rbp), %rax
movsd 0x38(%rax), %xmm0
mulsd -0x58(%rbp), %xmm0
movsd -0x58(%rbp), %xmm2
movq -0x10(%rbp), %rax
movsd 0x40(%rax), %xmm1
mulsd -0x50(%rbp), %xmm1
mulsd -0x50(%rbp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0xc0(%rbp)
movsd -0x80(%rbp), %xmm0
mulsd -0x60(%rbp), %xmm0
movsd -0xb8(%rbp), %xmm2
movq -0x20(%rbp), %rax
movq 0x60(%rax), %rax
movslq -0x9c(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movsd -0x80(%rbp), %xmm0
mulsd -0x60(%rbp), %xmm0
movsd -0xc0(%rbp), %xmm2
movq -0x20(%rbp), %rax
movq 0x68(%rax), %rax
movslq -0x9c(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movsd -0x80(%rbp), %xmm0
movsd -0xb8(%rbp), %xmm2
movq -0x20(%rbp), %rax
movq 0x70(%rax), %rax
movslq -0x9c(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movsd -0x80(%rbp), %xmm0
movsd -0xc0(%rbp), %xmm2
movq -0x20(%rbp), %rax
movq 0x78(%rax), %rax
movslq -0x9c(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movslq -0x9c(%rbp), %rax
movq -0x28(%rbp), %rcx
cmpq 0x50(%rcx), %rax
jge 0xb0a7
movslq -0x9c(%rbp), %rax
movq -0x28(%rbp), %rcx
cmpq 0x58(%rcx), %rax
jl 0xb0a7
movq -0x28(%rbp), %rax
movq 0x60(%rax), %rdi
movq -0x18(%rbp), %rax
movq (%rax), %rdx
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rcx
movl -0x9c(%rbp), %r8d
movsd -0x80(%rbp), %xmm0
movsd -0x60(%rbp), %xmm1
movsd -0xb8(%rbp), %xmm2
movsd -0x68(%rbp), %xmm3
leaq 0x500f(%rip), %rsi      # 0x100af
movb $0x4, %al
callq 0x2170
jmp 0xb0a9
addq $0xd0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)