// File: /workspace/github_cpp_makefile_star2_O2/myudelson[P]hmm-scalable/InputUtil.cpp
// Function: InputUtil::readBin(char const*, param*)
bool InputUtil::readBin(const char *fn, struct param * param) {
    char c, v/*version*/;
    NDAT i;
    NDAT nread;
    FILE *fid = fopen(fn,"rb");
    
    // version
    nread = (NDAT)fread (&v, sizeof(char), (size_t)1, fid);
    if(nread != 1) {
        fprintf(stderr,"Error reading version data from %s\n",fn);
        return false;
    }
    if( v > bin_input_file_verstion) { // we will handle earlier versions
        fprintf(stderr,"Wrong version of the data file. Expected %d, actual %d\n",(int)bin_input_file_verstion, (int)v);
        return false;
    }
    
    // N
    nread = (NDAT)fread (&i, sizeof(NDAT), (size_t)1, fid);
    if(nread != 1) {
        fprintf(stderr,"Error reading number of rows from %s\n",fn);
        return false;
    }
    param->N = (NDAT)i;
    
    // Nstacked
    nread = (NDAT)fread (&i, sizeof(NDAT), (size_t)1, fid);
    if(nread != 1) {
        fprintf(stderr,"Error reading number of rows (stacked) from %s\n",fn);
        return false;
    }
    param->Nstacked = (NDAT)i;
    
    // N_null
    nread = (NDAT)fread (&i, sizeof(NDAT), (size_t)1, fid);
    if(nread != 1) {
        fprintf(stderr,"Error reading number of rows with null skills from %s\n",fn);
        return false;
    }
    param->N_null = (NDAT)i;
    
    // nO
    nread = (NDAT)fread (&i, sizeof(NDAT), (size_t)1, fid);
    if(nread != 1) {
        fprintf(stderr,"Error reading number of observations from %s\n",fn);
        return false;
    }
    param->nO = (NPAR)i;
    
    // nG
    nread = (NDAT)fread (&i, sizeof(NDAT), (size_t)1, fid);
    if(nread != 1) {
        fprintf(stderr,"Error reading number of groups (students) from %s\n",fn);
        return false;
    }
    param->nG = (NCAT)i;
    
    // nI
    nread = (NDAT)fread (&i, sizeof(NDAT), (size_t)1, fid);
    if(nread != 1) {
        fprintf(stderr,"Error reading number of items from %s\n",fn);
        return false;
    }
    param->nI = (NCAT)i;
    
    // nK
    nread = (NDAT)fread (&i, sizeof(NDAT), (size_t)1, fid);
    if(nread != 1) {
        fprintf(stderr,"Error reading number of skills from %s\n",fn);
        return false;
    }
    param->nK = (NCAT)i;
    
    // nZ
    nread = (NDAT)fread (&i, sizeof(NDAT), (size_t)1, fid);
    if(nread != 1) {
        fprintf(stderr,"Error reading number of slices from %s\n",fn);
        return false;
    }
    param->nZ = (NPAR)i;
    if(param->nZ<1) {
        fprintf(stderr,"Number of slices should be at least 1\n");
        return true;
    }
    
    // multiskill
    nread = (NDAT)fread (&c, sizeof(char), (size_t)1, fid);
    if(nread != 1) {
        fprintf(stderr,"Error reading multiskill flag from %s\n",fn);
        return false;
    }
    param->multiskill = (NPAR)c;
    
    
    // dat_obs
    StripedArray<NPAR> *striped_dat_obs = new StripedArray<NPAR>(fid, param->N);
    param->dat_obs = striped_dat_obs->toArray();
    delete striped_dat_obs;

//    if(v==1) { // older NCAT of unsigned short
//        StripedArray<short> *dat_group_legacy = NULL;
//        StripedArray<short> *dat_skill_legacy = NULL;
//        StripedArray<short*> *dat_multiskill_legacy = NULL;
//        dat_group_legacy = new StripedArray<short>(fid, param->N);
//        for(t=0; t<param->N; t++)
//            param->dat_group[t] = (NCAT)dat_group_legacy->get(t);
//        if(param->multiskill == 0) {
//            dat_skill_legacy = new StripedArray<short>(fid, param->N);
//            for(t=0; t<param->N; t++)
//                param->dat_skill[t] = (NCAT)dat_skill_legacy->get(t);
//            delete dat_skill_legacy;
//        } else {
//            param->dat_multiskill = new StripedArray<NCAT*>(param->N,true);
//            nread = readMultiSkill(fid, param, v);
//            delete dat_multiskill_legacy;
//        }
//        delete dat_group_legacy;
//    } else {
        // dat_group
        StripedArray<NCAT> *striped_dat_group = new StripedArray<NCAT>(fid, param->N);
        param->dat_group = striped_dat_group->toArray();
        delete striped_dat_group;
//    }
    
    // dat_item
    StripedArray<NCAT> *striped_dat_item = new StripedArray<NCAT>(fid, param->N);
    param->dat_item = striped_dat_item->toArray();
    delete striped_dat_item;
    
    // dat_skill
    if(param->multiskill == 0) {
        StripedArray<NCAT> *striped_dat_skill = new StripedArray<NCAT>(fid, param->N);
        param->dat_skill = striped_dat_skill->toArray();
        delete striped_dat_skill;
    } else {
//            param->dat_multiskill = new StripedArray< NCAT* >(param->N,true);
//            nread = readMultiSkill(fid, param, v);
        StripedArray<NCAT> *striped_dat_skill_stacked = new StripedArray<NCAT>(fid, param->Nstacked);
        param->dat_skill_stacked = striped_dat_skill_stacked->toArray();
        StripedArray<NCAT> *striped_dat_skill_rcount = new StripedArray<NCAT>(fid, param->N);
        param->dat_skill_rcount = striped_dat_skill_rcount->toArray();
        StripedArray<NCAT> *striped_dat_skill_rix = new StripedArray<NCAT>(fid, param->N);
        param->dat_skill_rix = striped_dat_skill_rix->toArray();
        delete striped_dat_skill_stacked;
        delete striped_dat_skill_rcount;
        delete striped_dat_skill_rix;
    }
    // dat_slices, only of nZ > 1
    if(param->nZ > 1) {
        NDAT szZ = (param->multiskill == 0)?param->N:param->Nstacked;
        StripedArray<NPAR> *striped_dat_slice = new StripedArray<NPAR>(fid, szZ);
        param->dat_slice = striped_dat_slice->toArray();
        delete striped_dat_slice;
    }
        
    string str;
    param->map_group_fwd = new map<string,NCAT>();
    param->map_group_bwd = new map<NCAT,string>();
    param->map_skill_fwd = new map<string,NCAT>();
    param->map_skill_bwd = new map<NCAT,string>();
    param->map_step_fwd = new map<string,NCAT>();
    param->map_step_bwd = new map<NCAT,string>();
    // voc_group
    for(NCAT g=0; g<param->nG; g++) {
        str = readString(fid);
        param->map_group_fwd->insert(pair<string,NCAT>(str, g));
        param->map_group_bwd->insert(pair<NCAT,string>(g, str));
    }
    
    // voc_skill
    for(NCAT k=0; k<param->nK; k++) {
        str = readString(fid);
        param->map_skill_fwd->insert(pair<string,NCAT>(str, k));
        param->map_skill_bwd->insert(pair<NCAT,string>(k, str));
    }
    
    // voc_item
    for(NCAT i=0; i<param->nI; i++) {
        str = readString(fid);
        param->map_step_fwd->insert(pair<string,NCAT>(str, i));
        param->map_step_bwd->insert(pair<NCAT,string>(i, str));
    }
    
    fclose(fid);
    return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rsi, %rbx
movq %rdi, %r15
leaq 0x21411(%rip), %rsi     # 0x2d0ea
callq 0x4280
movq %rax, %r14
leaq 0x6(%rsp), %rdi
pushq $0x1
popq %rdx
movq %rdx, %rsi
movq %rax, %rcx
callq 0x4380
cmpl $0x1, %eax
jne 0xc35c
movsbl 0x6(%rsp), %ecx
cmpl $0x4, %ecx
jge 0xc372
movq %rsp, %rdi
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq %r14, %rcx
callq 0x4380
cmpl $0x1, %eax
jne 0xc394
movq %rsp, %rdi
movl (%rdi), %eax
movl %eax, 0x8b0(%rbx)
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq %r14, %rcx
callq 0x4380
cmpl $0x1, %eax
jne 0xc3aa
movq %rsp, %rdi
movl (%rdi), %eax
movl %eax, 0x8b4(%rbx)
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq %r14, %rcx
callq 0x4380
cmpl $0x1, %eax
jne 0xc3c0
movq %rsp, %rdi
movl (%rdi), %eax
movl %eax, 0x8cc(%rbx)
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq %r14, %rcx
callq 0x4380
cmpl $0x1, %eax
jne 0xc3d6
movq %rsp, %rdi
movb (%rdi), %al
movb %al, 0x8b9(%rbx)
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq %r14, %rcx
callq 0x4380
cmpl $0x1, %eax
jne 0xc3e9
movq %rsp, %rdi
movl (%rdi), %eax
movl %eax, 0x8bc(%rbx)
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq %r14, %rcx
callq 0x4380
cmpl $0x1, %eax
jne 0xc3fc
movq %rsp, %rdi
movl (%rdi), %eax
movl %eax, 0x8c4(%rbx)
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq %r14, %rcx
callq 0x4380
cmpl $0x1, %eax
jne 0xc40f
movq %rsp, %rdi
movl (%rdi), %eax
movl %eax, 0x8c0(%rbx)
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq %r14, %rcx
callq 0x4380
cmpl $0x1, %eax
jne 0xc422
movb (%rsp), %al
movb %al, 0x8c8(%rbx)
testb %al, %al
jle 0xc435
leaq 0x7(%rsp), %rdi
pushq $0x1
popq %rdx
movq %rdx, %rsi
movq %r14, %rcx
callq 0x4380
cmpl $0x1, %eax
jne 0xc456
movb 0x7(%rsp), %al
movb %al, 0x918(%rbx)
pushq $0x18
popq %rdi
callq 0x4220
movq %rax, %r15
movl 0x8b0(%rbx), %edx
movq %rax, %rdi
movq %r14, %rsi
callq 0xcbea
movq %r15, %rdi
callq 0xca82
movq %rax, 0x870(%rbx)
movq %r15, %rdi
callq 0xcafa
pushq $0x18
popq %r12
movq %r15, %rdi
movq %r12, %rsi
callq 0x4230
movq %r12, %rdi
callq 0x4220
movq %rax, %r15
movl 0x8b0(%rbx), %edx
movq %rax, %rdi
movq %r14, %rsi
callq 0xccce
movq %r15, %rdi
callq 0xcb2c
movq %rax, 0x878(%rbx)
movq %r15, %rdi
callq 0xcbb8
pushq $0x18
popq %r12
movq %r15, %rdi
movq %r12, %rsi
callq 0x4230
movq %r12, %rdi
callq 0x4220
movq %rax, %r15
movl 0x8b0(%rbx), %edx
movq %rax, %rdi
movq %r14, %rsi
callq 0xccce
movq %r15, %rdi
callq 0xcb2c
movq %rax, 0x8a0(%rbx)
movq %r15, %rdi
callq 0xcbb8
pushq $0x18
popq %r12
movq %r15, %rdi
movq %r12, %rsi
callq 0x4230
movb 0x918(%rbx), %bpl
movq %r12, %rdi
callq 0x4220
movq %rax, %r15
testb %bpl, %bpl
je 0xbfcf
movl 0x8b4(%rbx), %edx
movq %r15, %rdi
movq %r14, %rsi
callq 0xccce
movq %r15, %rdi
callq 0xcb2c
movq %rax, 0x888(%rbx)
pushq $0x18
popq %rdi
callq 0x4220
movq %rax, %r12
movl 0x8b0(%rbx), %edx
movq %rax, %rdi
movq %r14, %rsi
callq 0xccce
movq %r12, %rdi
callq 0xcb2c
movq %rax, 0x890(%rbx)
pushq $0x18
popq %rdi
callq 0x4220
movq %rax, %r13
movl 0x8b0(%rbx), %edx
movq %rax, %rdi
movq %r14, %rsi
callq 0xccce
movq %r13, %rdi
callq 0xcb2c
movq %rax, 0x898(%rbx)
movq %r15, %rdi
callq 0xcbb8
pushq $0x18
popq %rbp
movq %r15, %rdi
movq %rbp, %rsi
callq 0x4230
movq %r12, %rdi
callq 0xcbb8
movq %r12, %rdi
movq %rbp, %rsi
callq 0x4230
movq %r13, %r15
jmp 0xbfef
movl 0x8b0(%rbx), %edx
movq %r15, %rdi
movq %r14, %rsi
callq 0xccce
movq %r15, %rdi
callq 0xcb2c
movq %rax, 0x880(%rbx)
movq %r15, %rdi
callq 0xcbb8
pushq $0x18
popq %rsi
movq %r15, %rdi
callq 0x4230
cmpb $0x2, 0x8c8(%rbx)
jl 0xc058
xorl %eax, %eax
cmpb $0x0, 0x918(%rbx)
setne %al
movl 0x8b0(%rbx,%rax,4), %ebp
pushq $0x18
popq %rdi
callq 0x4220
movq %rax, %r15
movq %rax, %rdi
movq %r14, %rsi
movl %ebp, %edx
callq 0xcbea
movq %r15, %rdi
callq 0xca82
movq %rax, 0x8a8(%rbx)
movq %r15, %rdi
callq 0xcafa
pushq $0x18
popq %rsi
movq %r15, %rdi
callq 0x4230
movq %r14, 0x8(%rsp)
leaq 0x48(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
pushq $0x30
popq %rdi
callq 0x4220
movq %rax, %rcx
addq $0x8, %rcx
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rax)
movaps %xmm0, (%rax)
movq %rcx, 0x18(%rax)
movq %rcx, 0x20(%rax)
andq $0x0, 0x28(%rax)
movq %rax, 0x920(%rbx)
pushq $0x30
popq %rdi
callq 0x4220
movq %rax, %rcx
addq $0x8, %rcx
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rax)
movaps %xmm0, (%rax)
movq %rcx, 0x18(%rax)
movq %rcx, 0x20(%rax)
andq $0x0, 0x28(%rax)
movq %rax, 0x928(%rbx)
pushq $0x30
popq %rdi
callq 0x4220
movq %rax, %rcx
addq $0x8, %rcx
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rax)
movaps %xmm0, (%rax)
movq %rcx, 0x18(%rax)
movq %rcx, 0x20(%rax)
andq $0x0, 0x28(%rax)
movq %rax, 0x940(%rbx)
pushq $0x30
popq %rdi
callq 0x4220
movq %rax, %rcx
addq $0x8, %rcx
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rax)
movaps %xmm0, (%rax)
movq %rcx, 0x18(%rax)
movq %rcx, 0x20(%rax)
andq $0x0, 0x28(%rax)
movq %rax, 0x948(%rbx)
pushq $0x30
popq %rdi
callq 0x4220
movq %rax, %rcx
addq $0x8, %rcx
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rax)
movaps %xmm0, (%rax)
movq %rcx, 0x18(%rax)
movq %rcx, 0x20(%rax)
andq $0x0, 0x28(%rax)
movq %rax, 0x930(%rbx)
pushq $0x30
popq %rdi
callq 0x4220
movq %rax, %rcx
addq $0x8, %rcx
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rax)
movaps %xmm0, (%rax)
movq %rcx, 0x18(%rax)
movq %rcx, 0x20(%rax)
andq $0x0, 0x28(%rax)
movq %rax, 0x938(%rbx)
xorl %r14d, %r14d
leaq 0x18(%rsp), %r15
leaq 0x10(%rsp), %r12
leaq 0x38(%rsp), %r13
cmpl 0x8bc(%rbx), %r14d
jge 0xc213
movq %r12, %rdi
movq 0x8(%rsp), %rsi
callq 0xaf6e
movq %r13, %rdi
movq %r12, %rsi
callq 0x42b0
movq %r12, %rdi
callq 0x40f0
movq 0x920(%rbx), %rbp
movq %r12, %rdi
movq %r13, %rsi
callq 0x4320
movl %r14d, 0x30(%rsp)
movq %rbp, %rdi
movq %r12, %rsi
callq 0xcf7e
movq %r12, %rdi
callq 0x40f0
movq 0x928(%rbx), %rbp
movl %r14d, 0x10(%rsp)
movq %r15, %rdi
movq %r13, %rsi
callq 0x4320
movq %rbp, %rdi
movq %r12, %rsi
callq 0xd16a
movq %r15, %rdi
callq 0x40f0
incl %r14d
jmp 0xc18e
xorl %r14d, %r14d
leaq 0x18(%rsp), %r15
leaq 0x10(%rsp), %r12
leaq 0x38(%rsp), %r13
cmpl 0x8c0(%rbx), %r14d
jge 0xc2aa
movq %r12, %rdi
movq 0x8(%rsp), %rsi
callq 0xaf6e
movq %r13, %rdi
movq %r12, %rsi
callq 0x42b0
movq %r12, %rdi
callq 0x40f0
movq 0x940(%rbx), %rbp
movq %r12, %rdi
movq %r13, %rsi
callq 0x4320
movl %r14d, 0x30(%rsp)
movq %rbp, %rdi
movq %r12, %rsi
callq 0xcf7e
movq %r12, %rdi
callq 0x40f0
movq 0x948(%rbx), %rbp
movl %r14d, 0x10(%rsp)
movq %r15, %rdi
movq %r13, %rsi
callq 0x4320
movq %rbp, %rdi
movq %r12, %rsi
callq 0xd16a
movq %r15, %rdi
callq 0x40f0
incl %r14d
jmp 0xc225
xorl %r14d, %r14d
leaq 0x18(%rsp), %r15
leaq 0x10(%rsp), %r12
leaq 0x38(%rsp), %r13
cmpl 0x8c4(%rbx), %r14d
jge 0xc341
movq %r12, %rdi
movq 0x8(%rsp), %rsi
callq 0xaf6e
movq %r13, %rdi
movq %r12, %rsi
callq 0x42b0
movq %r12, %rdi
callq 0x40f0
movq 0x930(%rbx), %rbp
movq %r12, %rdi
movq %r13, %rsi
callq 0x4320
movl %r14d, 0x30(%rsp)
movq %rbp, %rdi
movq %r12, %rsi
callq 0xcf7e
movq %r12, %rdi
callq 0x40f0
movq 0x938(%rbx), %rbp
movl %r14d, 0x10(%rsp)
movq %r15, %rdi
movq %r13, %rsi
callq 0x4320
movq %rbp, %rdi
movq %r12, %rsi
callq 0xd16a
movq %r15, %rdi
callq 0x40f0
incl %r14d
jmp 0xc2bc
movq 0x8(%rsp), %rdi
callq 0x4210
leaq 0x38(%rsp), %rdi
callq 0x40f0
movb $0x1, %bl
jmp 0xc473
movq 0x2cc75(%rip), %rax     # 0x38fd8
movq (%rax), %rdi
leaq 0x20d80(%rip), %rsi     # 0x2d0ed
jmp 0xc467
movq 0x2cc5f(%rip), %rax     # 0x38fd8
movq (%rax), %rdi
leaq 0x20d8e(%rip), %rsi     # 0x2d111
xorl %ebx, %ebx
pushq $0x3
popq %rdx
xorl %eax, %eax
callq 0x4350
jmp 0xc473
movq 0x2cc3d(%rip), %rax     # 0x38fd8
movq (%rax), %rdi
leaq 0x20da4(%rip), %rsi     # 0x2d149
jmp 0xc467
movq 0x2cc27(%rip), %rax     # 0x38fd8
movq (%rax), %rdi
leaq 0x20db4(%rip), %rsi     # 0x2d16f
jmp 0xc467
movq 0x2cc11(%rip), %rax     # 0x38fd8
movq (%rax), %rdi
leaq 0x20dce(%rip), %rsi     # 0x2d19f
jmp 0xc467
movq 0x2cbfb(%rip), %rax     # 0x38fd8
movq (%rax), %rdi
leaq 0x20def(%rip), %rsi     # 0x2d1d6
jmp 0xc467
movq 0x2cbe8(%rip), %rax     # 0x38fd8
movq (%rax), %rdi
leaq 0x20e0a(%rip), %rsi     # 0x2d204
jmp 0xc467
movq 0x2cbd5(%rip), %rax     # 0x38fd8
movq (%rax), %rdi
leaq 0x20e2a(%rip), %rsi     # 0x2d237
jmp 0xc467
movq 0x2cbc2(%rip), %rax     # 0x38fd8
movq (%rax), %rdi
leaq 0x20e3e(%rip), %rsi     # 0x2d25e
jmp 0xc467
movq 0x2cbaf(%rip), %rax     # 0x38fd8
movq (%rax), %rdi
leaq 0x20e53(%rip), %rsi     # 0x2d286
jmp 0xc467
movq 0x2cb9c(%rip), %rax     # 0x38fd8
movq (%rax), %rcx
leaq 0x20e68(%rip), %rdi     # 0x2d2ae
pushq $0x26
popq %rsi
pushq $0x1
popq %rdx
callq 0x44b0
jmp 0xc355
movq 0x2cb7b(%rip), %rax     # 0x38fd8
movq (%rax), %rdi
leaq 0x20e6e(%rip), %rsi     # 0x2d2d5
xorl %ebx, %ebx
movq %r15, %rdx
xorl %eax, %eax
callq 0x4350
movl %ebx, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0xc4a4
jmp 0xc4a4
movq %rax, %rbx
pushq $0x18
popq %rsi
movq %r13, %rdi
jmp 0xc4ad
movq %rax, %rbx
pushq $0x18
popq %rsi
movq %r12, %rdi
jmp 0xc4ad
jmp 0xc4a4
jmp 0xc4a4
jmp 0xc4a4
movq %rax, %rbx
pushq $0x18
popq %rsi
movq %r15, %rdi
callq 0x4230
jmp 0xc4f2
jmp 0xc4e5
jmp 0xc4ca
jmp 0xc4e5
jmp 0xc4e5
jmp 0xc4d6
jmp 0xc4e5
jmp 0xc4ca
jmp 0xc4e5
jmp 0xc4e5
jmp 0xc4d6
jmp 0xc4e5
movq %rax, %rbx
movq %r15, %rdi
jmp 0xc4de
jmp 0xc4e5
jmp 0xc4e5
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x40f0
jmp 0xc4e8
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
callq 0x40f0
movq %rbx, %rdi
callq 0x4450