// File: myudelson[P]hmm-scalable/InputUtil.cpp
// Function: InputUtil::writeInputMatrix(char const*, param*, int, data**)
void InputUtil::writeInputMatrix(const char *filename, struct param* p, NCAT xndat, struct data** x_data) {
//    FILE *fid = fopen(filename,"w");
//    if(fid == NULL) {
//        fprintf(stderr,"Can't write output model file %s\n",filename);
//        exit(1);
//    }
    
    std::ofstream file;
    file.open(filename);
    
    NDAT nmax = 0;
    for(NCAT x=0; x<xndat; x++)
        if(nmax < x_data[x]->n)
            nmax = x_data[x]->n;
    
    for(NCAT x=0; x<xndat; x++) {
        std::stringstream ss;
        for(NDAT t=0; t<nmax; t++) {
            
            if(t<x_data[x]->n) {
                ss << ((t>0)?" ":"") << (int)(1+p->dat_obs[ x_data[x]->ix[t] ]);
            } else {
                ss << " " << 0;
            }
        }
        ss << "\n";
        file << ss.str();
    } // for all groups in skill
    
//    fclose(fid);
    file.close();
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x400, %rsp            # imm = 0x400
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movq %rcx, -0x20(%rbp)
leaq -0x220(%rbp), %rdi
movq %rdi, -0x3f0(%rbp)
callq 0x5510
movq -0x3f0(%rbp), %rdi
movq -0x8(%rbp), %rsi
movl $0x10, %edx
callq 0x5070
jmp 0x16354
movl $0x0, -0x230(%rbp)
movl $0x0, -0x234(%rbp)
movl -0x234(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x163cf
movl -0x230(%rbp), %eax
movq -0x20(%rbp), %rcx
movslq -0x234(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
cmpl (%rcx), %eax
jge 0x163bc
movq -0x20(%rbp), %rax
movslq -0x234(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movl (%rax), %eax
movl %eax, -0x230(%rbp)
jmp 0x163bc
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x228(%rbp)
movl %eax, -0x22c(%rbp)
jmp 0x165b2
jmp 0x163be
movl -0x234(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x234(%rbp)
jmp 0x16368
movl $0x0, -0x238(%rbp)
movl -0x238(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x1658f
leaq -0x3c0(%rbp), %rdi
callq 0x51d0
jmp 0x163f6
movl $0x0, -0x3c4(%rbp)
movl -0x3c4(%rbp), %eax
cmpl -0x230(%rbp), %eax
jge 0x164f8
movl -0x3c4(%rbp), %eax
movq -0x20(%rbp), %rcx
movslq -0x238(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
cmpl (%rcx), %eax
jge 0x164b4
leaq -0x3b0(%rbp), %rdi
movl -0x3c4(%rbp), %eax
testl %eax, %eax
leaq 0x39efe(%rip), %rax     # 0x50343
leaq 0x3a82f(%rip), %rsi     # 0x50c7b
cmovgq %rax, %rsi
callq 0x52b0
movq %rax, -0x3f8(%rbp)
jmp 0x1645e
movq -0x3f8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x870(%rax), %rax
movq -0x20(%rbp), %rcx
movslq -0x238(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
movq 0x10(%rcx), %rcx
movslq -0x3c4(%rbp), %rdx
movslq (%rcx,%rdx,4), %rcx
movsbl (%rax,%rcx), %esi
incl %esi
callq 0x5520
jmp 0x1649b
jmp 0x164e2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x228(%rbp)
movl %eax, -0x22c(%rbp)
jmp 0x16581
leaq -0x3b0(%rbp), %rdi
leaq 0x39e81(%rip), %rsi     # 0x50343
callq 0x52b0
movq %rax, -0x400(%rbp)
jmp 0x164d0
movq -0x400(%rbp), %rdi
xorl %esi, %esi
callq 0x5520
jmp 0x164e0
jmp 0x164e2
jmp 0x164e4
movl -0x3c4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c4(%rbp)
jmp 0x16400
leaq -0x3b0(%rbp), %rdi
leaq 0x3949f(%rip), %rsi     # 0x4f9a5
callq 0x52b0
jmp 0x1650d
leaq -0x3e8(%rbp), %rdi
leaq -0x3c0(%rbp), %rsi
callq 0x52a0
jmp 0x16522
leaq -0x220(%rbp), %rdi
leaq -0x3e8(%rbp), %rsi
callq 0x5280
jmp 0x16537
leaq -0x3e8(%rbp), %rdi
callq 0x5170
leaq -0x3c0(%rbp), %rdi
callq 0x5200
movl -0x238(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x238(%rbp)
jmp 0x163d9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x228(%rbp)
movl %eax, -0x22c(%rbp)
leaq -0x3e8(%rbp), %rdi
callq 0x5170
leaq -0x3c0(%rbp), %rdi
callq 0x5200
jmp 0x165b2
leaq -0x220(%rbp), %rdi
callq 0x5430
jmp 0x1659d
leaq -0x220(%rbp), %rdi
callq 0x5460
addq $0x400, %rsp            # imm = 0x400
popq %rbp
retq
leaq -0x220(%rbp), %rdi
callq 0x5460
movq -0x228(%rbp), %rdi
callq 0x5540
nopw (%rax,%rax)