// File: /workspace/github_c_makefile_star2_O2/esheldon[P]xshear/src/shear.c
// Function: shear_init
Shear* shear_init(const char* config_url, const char* lens_url) {

    Shear* shear = calloc(1, sizeof(Shear));
    if (shear == NULL) {
        wlog("Failed to alloc shear struct\n");
        exit(EXIT_FAILURE);
    }

    shear->config=sconfig_read(config_url);

    ShearConfig* config=shear->config;
    wlog("config structure:\n");
    sconfig_print(config);

    // now initialize the structures we need
    wlog("Initalizing cosmo in flat universe\n");
    int flat=1;
    double omega_k=0;
    shear->cosmo = cosmo_new(config->H0, flat, 
                             config->omega_m, 
                             1-config->omega_m, 
                             omega_k);

    wlog("cosmo structure:\n");
    cosmo_print(shear->cosmo);

    wlog("Initalizing healpix at nside: %lld\n", config->healpix_nside);
    shear->hpix = hpix_new(config->healpix_nside);

    shear->lcat = lcat_read(config, lens_url);

    // order is important here
    if (!config->Dlens_input) {
        wlog("Adding Da to lenses\n");
        lcat_add_da(shear->lcat, shear->cosmo);
    } else {

    }

    wlog("Adding cos(search_angle) to lenses\n");
    lcat_add_search_angle(shear->lcat, config->rmax);

    wlog("Intersecting all lenses with healpix at rmax: %lf\n", config->rmax);
    lcat_disc_intersect(shear->lcat, shear->hpix, config->rmax);

    wlog("Building hpix tree for lenses\n");
    lcat_build_hpix_tree(shear->hpix, shear->lcat);

    lcat_print_firstlast(shear->lcat);

    // this holds the sums for each lens
    shear->lensums = lensums_new(shear->lcat->size,
                                 config->nbin,
                                 config->shear_style,
                                 config->scstyle,
                                 config->shear_units);

    for (size_t i=0; i<shear->lensums->size; i++) {
        shear->lensums->data[i].index = shear->lcat->data[i].index;
    }

    if (config->scstyle == SIGMACRIT_STYLE_INTERP) {
        // interpolation region
        double* zl=config->zl->data;
        int64 nzl=config->nzl;
        shear->max_zlens = zl[nzl-1];
        shear->min_zlens = fmax(zl[0], shear->config->min_zlens_interp);
    } else {
        shear->min_zlens = 0;
        shear->max_zlens = 9999;
    }

    wlog("min_zlens: %lf  max_zlens: %lf\n", shear->min_zlens, shear->max_zlens);


    return shear;

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %r15
movq %rdi, %r14
pushq $0x1
popq %rdi
pushq $0x38
popq %rsi
callq 0x2180
testq %rax, %rax
je 0x6dac
movq %rax, %rbx
movq %r14, %rdi
callq 0x23dc
movq %rax, %r14
movq %rax, (%rbx)
movq 0x7457(%rip), %rbp      # 0xdff8
movq (%rbp), %rcx
leaq 0x33f2(%rip), %rdi      # 0x9f9e
pushq $0x12
popq %rsi
pushq $0x1
popq %r12
movq %r12, %rdx
callq 0x22a0
movq %r14, %rdi
callq 0x2c87
movq (%rbp), %rcx
leaq 0x33e3(%rip), %rdi      # 0x9fb1
pushq $0x23
popq %rsi
movq %r12, %rdx
callq 0x22a0
movsd (%r14), %xmm0
movsd 0x8(%r14), %xmm1
movsd 0x319c(%rip), %xmm2     # 0x9d88
subsd %xmm1, %xmm2
pushq $0x1
popq %rdi
xorps %xmm3, %xmm3
callq 0x58d8
movq %rax, %r13
movq %rax, 0x8(%rbx)
movq (%rbp), %rcx
leaq 0x33c8(%rip), %rdi      # 0x9fd5
pushq $0x11
popq %rsi
movq %r12, %rdx
callq 0x22a0
movq %r13, %rdi
callq 0x5bfe
movq (%rbp), %rdi
movq 0x10(%r14), %rdx
leaq 0x33b8(%rip), %rsi      # 0x9fe7
xorl %eax, %eax
callq 0x21b0
movq 0x10(%r14), %rdi
callq 0x5fdf
movq %rax, %r12
movq %rax, 0x10(%rbx)
movq %r14, %rdi
movq %r15, %rsi
callq 0x5309
movq %rax, %r15
movq %rax, 0x18(%rbx)
cmpl $0x0, 0x2c(%r14)
jne 0x6c80
movq (%rbp), %rcx
leaq 0x33a1(%rip), %rdi      # 0xa00b
pushq $0x14
popq %rsi
pushq $0x1
popq %rdx
callq 0x22a0
movq %r15, %rdi
movq %r13, %rsi
callq 0x5592
movq (%rbp), %rcx
leaq 0x3395(%rip), %rdi      # 0xa020
pushq $0x23
popq %rsi
pushq $0x1
popq %r13
movq %r13, %rdx
callq 0x22a0
movsd 0x70(%r14), %xmm0
movq %r15, %rdi
callq 0x55dd
movq (%rbp), %rdi
movsd 0x70(%r14), %xmm0
leaq 0x338b(%rip), %rsi      # 0xa044
movb $0x1, %al
callq 0x21b0
movsd 0x70(%r14), %xmm0
movq %r15, %rdi
movq %r12, %rsi
callq 0x5629
movq (%rbp), %rcx
leaq 0x339b(%rip), %rdi      # 0xa077
pushq $0x1e
popq %rsi
movq %r13, %rdx
callq 0x22a0
movq %r12, %rdi
movq %r15, %rsi
callq 0x56a5
movq %r15, %rdi
callq 0x5857
movq (%r15), %rdi
movq 0x40(%r14), %rsi
movl 0x18(%r14), %edx
movl 0x20(%r14), %ecx
movl 0x4c(%r14), %r8d
callq 0x74b4
movq %rax, 0x20(%rbx)
movq (%rax), %rcx
xorl %edx, %edx
xorl %esi, %esi
cmpq %rcx, %rsi
jae 0x6d3c
movq 0x8(%r15), %rdi
movq (%rdi,%rdx), %rdi
movq 0x8(%rax), %r8
movq %rdi, 0x10(%r8,%rdx)
incq %rsi
subq $-0x80, %rdx
jmp 0x6d1d
cmpl $0x2, 0x20(%r14)
jne 0x6d73
movq 0x30(%r14), %rax
movq 0x38(%r14), %rcx
movq 0x10(%rcx), %rcx
movsd -0x8(%rcx,%rax,8), %xmm0
movsd %xmm0, (%rsp)
movsd (%rcx), %xmm0
movsd 0x90(%r14), %xmm1
callq 0x2060
movsd (%rsp), %xmm1
jmp 0x6d7e
xorps %xmm0, %xmm0
movsd 0x31c2(%rip), %xmm1     # 0x9f40
movsd %xmm1, 0x30(%rbx)
movsd %xmm0, 0x28(%rbx)
movq (%rbp), %rdi
leaq 0x3303(%rip), %rsi      # 0xa096
movb $0x2, %al
callq 0x21b0
movq %rbx, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x7245(%rip), %rax      # 0xdff8
movq (%rax), %rcx
leaq 0x31c3(%rip), %rdi      # 0x9f80
pushq $0x1d
popq %rsi
pushq $0x1
popq %rdx
callq 0x22a0
pushq $0x1
popq %rdi
callq 0x2290