// File: /workspace/github_c_makefile_star2_O2/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 $0x38, %rsp
movq %rsi, %rbx
movq %rdi, 0x28(%rsp)
cmpl $0x1, 0x2c(%rdi)
pushq $0x6
popq %rax
sbbl $0x0, %eax
movl %eax, 0x14(%rsp)
movq 0xa658(%rip), %r14      # 0xdff8
movq (%r14), %rdi
leaq 0x5897(%rip), %rsi      # 0x9241
movq %rbx, %rdx
xorl %eax, %eax
callq 0x21a0
leaq 0x61b1(%rip), %rsi      # 0x9b6c
movq %rbx, 0x20(%rsp)
movq %rbx, %rdi
callq 0x6a74
movq %rax, %r15
movq (%r14), %rcx
leaq 0x5884(%rip), %rdi      # 0x9259
pushq $0x12
popq %rsi
pushq $0x1
popq %rbx
movq %rbx, %rdx
callq 0x2290
movq %r15, 0x8(%rsp)
movq %r15, %rdi
callq 0x6af4
movq %rax, %r12
movq (%r14), %rdi
leaq 0x5a4c(%rip), %rsi      # 0x9449
movq %rax, %rdx
xorl %eax, %eax
callq 0x21a0
movq (%r14), %rcx
leaq 0x585b(%rip), %rdi      # 0x926c
pushq $0x14
popq %rsi
movq %rbx, %rdx
callq 0x2290
movq %r12, %rdi
callq 0x38dc
movq %rax, %r13
movq (%r14), %rcx
leaq 0x5850(%rip), %rdi      # 0x9281
pushq $0x3
popq %rsi
movq %rbx, %rdx
callq 0x2290
movq (%r14), %rcx
leaq 0x583f(%rip), %rdi      # 0x9285
pushq $0x13
popq %rsi
movq %rbx, %rdx
callq 0x2290
xorl %r14d, %r14d
xorl %ebp, %ebp
cmpq %rbp, %r12
je 0x3b6b
movq 0x8(%r13), %r15
leaq (%r15,%r14), %rdx
movq 0x28(%rsp), %rax
cmpl $0x0, 0x2c(%rax)
leaq 0x8(%r15,%r14), %rcx
leaq 0x10(%r15,%r14), %r8
leaq 0x18(%r15,%r14), %r9
leaq 0x48(%r15,%r14), %r10
je 0x3aa2
leaq (%r15,%r14), %r11
addq $0x20, %r11
movq 0x8(%rsp), %rdi
leaq 0x57ff(%rip), %rsi      # 0x9299
xorl %eax, %eax
pushq %r10
pushq %r11
jmp 0x3ab6
subq $0x8, %rsp
movq 0x10(%rsp), %rdi
leaq 0x57ff(%rip), %rsi      # 0x92b1
xorl %eax, %eax
pushq %r10
callq 0x2070
popq %rcx
popq %rdx
movl %eax, %ebx
cmpl 0x14(%rsp), %eax
jne 0x3ba3
leaq (%r15,%r14), %rbx
addq $0x60, %rbx
movsd -0x58(%rbx), %xmm2
movsd 0x55ca(%rip), %xmm0     # 0x90a8
mulsd %xmm0, %xmm2
movsd %xmm2, 0x30(%rsp)
movsd -0x50(%rbx), %xmm1
mulsd %xmm0, %xmm1
movsd %xmm1, 0x18(%rsp)
movapd %xmm2, %xmm0
callq 0x2250
movsd %xmm0, -0x38(%rbx)
movsd 0x30(%rsp), %xmm0
callq 0x2140
movsd %xmm0, -0x30(%rbx)
movsd 0x18(%rsp), %xmm0
callq 0x2250
movsd %xmm0, -0x28(%rbx)
movsd 0x18(%rsp), %xmm0
callq 0x2140
movsd %xmm0, -0x20(%rbx)
movsd -0x58(%rbx), %xmm0
movsd -0x50(%rbx), %xmm1
leaq (%r15,%r14), %rsi
addq $0x68, %rsi
leaq (%r15,%r14), %rdx
addq $0x70, %rdx
leaq (%r15,%r14), %rcx
addq $0x78, %rcx
movq %rbx, %rdi
callq 0x8136
incq %rbp
subq $-0x80, %r14
jmp 0x3a56
movq 0x8(%rsp), %rdi
callq 0x20d0
movq 0xa47c(%rip), %rax      # 0xdff8
movq (%rax), %rcx
leaq 0x56fb(%rip), %rdi      # 0x9281
pushq $0x3
popq %rsi
pushq $0x1
popq %rdx
callq 0x2290
movq %r13, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0xa44e(%rip), %r12      # 0xdff8
movq (%r12), %rdi
leaq 0x5710(%rip), %rsi      # 0x92c5
movq %rbp, %rdx
movq 0x20(%rsp), %rcx
xorl %eax, %eax
callq 0x21a0
movq (%r12), %rdi
movq (%r15,%r14), %rcx
movsd 0x8(%r15,%r14), %xmm0
movsd 0x10(%r15,%r14), %xmm1
movsd 0x18(%r15,%r14), %xmm2
movq 0x48(%r15,%r14), %r8
leaq 0x56f8(%rip), %rsi      # 0x92e5
movl %ebx, %edx
movb $0x3, %al
callq 0x21a0
pushq $0x1
popq %rdi
callq 0x2280