// File: /workspace/github_c_makefile_star2_O1/esheldon[P]xshear/src/lens.c
// Function: lcat_read
LensCatalog* lcat_read(const ShearConfig* config, const char* lens_url) {

    int nread=0, expected=0;
    if (config->Dlens_input) {
        expected=6;
    } else {
        expected=5;
    }

    wlog("Reading lenses from %s\n", lens_url);
    FILE* stream=open_url(lens_url, "r");

    wlog("counting lines....");
    size_t nlens = count_lines_and_rewind(stream);

    wlog("%lu\n", nlens);

    wlog("    creating lcat...");

    LensCatalog* lcat=lcat_new(nlens);
    wlog("OK\n");

    wlog("    reading data...");
    double ra_rad,dec_rad;
    for (size_t i=0; i<nlens; i++) {

        Lens* lens = &lcat->data[i];

        if (config->Dlens_input) {
            nread=fscanf(stream,"%ld %lf %lf %lf %lf %ld",
                    &lens->index,&lens->ra,&lens->dec,&lens->z,&lens->da,&lens->maskflags);
        } else {
            nread=fscanf(stream,"%ld %lf %lf %lf %ld",
                    &lens->index,&lens->ra,&lens->dec,&lens->z,&lens->maskflags);
        }
        
        if (nread != expected) {
            wlog("Failed to read row %lu from %s\n", i, lens_url);
            wlog("nread=%d index=%d ra=%lf dec=%lf z=%lf maskflags=%d\n", nread, lens->index, lens->ra, lens->dec, lens->z, lens->maskflags);
            exit(EXIT_FAILURE);
        }

        ra_rad = lens->ra*D2R;
        dec_rad = lens->dec*D2R;

        lens->sinra = sin(ra_rad);
        lens->cosra = cos(ra_rad);
        lens->sindec = sin(dec_rad);
        lens->cosdec = cos(dec_rad);

        // add sin(lam),cos(lam),sin(eta),cos(eta)
        eq2sdss_sincos(lens->ra,lens->dec,
                       &lens->sinlam, &lens->coslam,
                       &lens->sineta, &lens->coseta);

    }
    fclose(stream);
    wlog("OK\n");

    return lcat;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rsi, %rbx
movq %rdi, 0x18(%rsp)
cmpl $0x1, 0x2c(%rdi)
pushq $0x6
popq %r14
sbbl $0x0, %r14d
movq 0xbb2d(%rip), %r13      # 0xdff8
movq (%r13), %rdi
leaq 0x6d6b(%rip), %rsi      # 0x9241
movq %rbx, %rdx
xorl %eax, %eax
callq 0x21a0
leaq 0x7685(%rip), %rsi      # 0x9b6c
movq %rbx, 0x10(%rsp)
movq %rbx, %rdi
callq 0x75d4
movq %rax, %r15
movq (%r13), %rcx
leaq 0x6d57(%rip), %rdi      # 0x9259
pushq $0x12
popq %rsi
pushq $0x1
popq %rbx
movq %rbx, %rdx
callq 0x2290
movq %r15, %rdi
callq 0x763d
movq %rax, %r12
movq (%r13), %rdi
leaq 0x6f23(%rip), %rsi      # 0x9449
movq %rax, %rdx
xorl %eax, %eax
callq 0x21a0
movq (%r13), %rcx
leaq 0x6d31(%rip), %rdi      # 0x926c
pushq $0x14
popq %rsi
movq %rbx, %rdx
callq 0x2290
movq %r12, %rdi
callq 0x51f0
movq %rax, (%rsp)
movq (%r13), %rcx
leaq 0x6d24(%rip), %rdi      # 0x9281
pushq $0x3
popq %rsi
movq %rbx, %rdx
callq 0x2290
movq (%r13), %rcx
leaq 0x6d12(%rip), %rdi      # 0x9285
pushq $0x13
popq %rsi
movq %rbx, %rdx
callq 0x2290
testq %r12, %r12
je 0x2682
movq (%rsp), %rax
movq 0x8(%rax), %rbp
xorl %ebx, %ebx
leaq 0x8(%rbp), %rcx
leaq 0x10(%rbp), %r8
leaq 0x18(%rbp), %r9
movq 0x18(%rsp), %rax
cmpl $0x0, 0x2c(%rax)
je 0x25c3
leaq 0x20(%rbp), %r10
leaq 0x48(%rbp), %r11
movq %r15, %rdi
leaq 0x6cdf(%rip), %rsi      # 0x9299
movq %rbp, %rdx
xorl %eax, %eax
pushq %r11
jmp 0x25da
leaq 0x48(%rbp), %r10
subq $0x8, %rsp
movq %r15, %rdi
leaq 0x6cdc(%rip), %rsi      # 0x92b1
movq %rbp, %rdx
xorl %eax, %eax
pushq %r10
callq 0x2070
popq %rcx
popq %rdx
movl %eax, %r13d
cmpl %r14d, %eax
jne 0x26b9
movsd 0x8(%rbp), %xmm2
movsd 0x6aac(%rip), %xmm0     # 0x90a8
mulsd %xmm0, %xmm2
movsd %xmm2, 0x20(%rsp)
movsd 0x10(%rbp), %xmm1
mulsd %xmm0, %xmm1
movsd %xmm1, 0x8(%rsp)
movapd %xmm2, %xmm0
callq 0x2250
movsd %xmm0, 0x28(%rbp)
movsd 0x20(%rsp), %xmm0
callq 0x2140
movsd %xmm0, 0x30(%rbp)
movsd 0x8(%rsp), %xmm0
callq 0x2250
movsd %xmm0, 0x38(%rbp)
movsd 0x8(%rsp), %xmm0
callq 0x2140
movsd %xmm0, 0x40(%rbp)
movsd 0x8(%rbp), %xmm0
movsd 0x10(%rbp), %xmm1
leaq 0x60(%rbp), %rdi
leaq 0x68(%rbp), %rsi
leaq 0x70(%rbp), %rdx
leaq 0x78(%rbp), %rcx
callq 0x8ac1
incq %rbx
subq $-0x80, %rbp
cmpq %rbx, %r12
jne 0x2591
movq %r15, %rdi
callq 0x20d0
movq 0xb967(%rip), %rax      # 0xdff8
movq (%rax), %rcx
leaq 0x6be6(%rip), %rdi      # 0x9281
pushq $0x3
popq %rsi
pushq $0x1
popq %rdx
callq 0x2290
movq (%rsp), %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0xb938(%rip), %r14      # 0xdff8
movq (%r14), %rdi
leaq 0x6bfb(%rip), %rsi      # 0x92c5
movq %rbx, %rdx
movq 0x10(%rsp), %rcx
xorl %eax, %eax
callq 0x21a0
movq (%r14), %rdi
movq (%rbp), %rcx
movsd 0x8(%rbp), %xmm0
movsd 0x10(%rbp), %xmm1
movsd 0x18(%rbp), %xmm2
movq 0x48(%rbp), %r8
leaq 0x6beb(%rip), %rsi      # 0x92e5
movl %r13d, %edx
movb $0x3, %al
callq 0x21a0
pushq $0x1
popq %rdi
callq 0x2280