// File: /workspace/github_cpp_makefile_star2_O2/qmmlib[P]QmmLibrary/linear.cpp
// Function: save_model
int save_model(const char *model_file_name, const struct model *model_)
{
	int i;
	int nr_feature=model_->nr_feature;
	int n;
	const parameter& param = model_->param;

	if(model_->bias>=0)
		n=nr_feature+1;
	else
		n=nr_feature;
	int w_size = n;
	FILE *fp = fopen(model_file_name,"w");
	if(fp==NULL) return -1;

	char *old_locale = strdup(setlocale(LC_ALL, NULL));
	setlocale(LC_ALL, "C");

	int nr_w;
	if(model_->nr_class==2 && model_->param.solver_type != MCSVM_CS)
		nr_w=1;
	else
		nr_w=model_->nr_class;

	fprintf(fp, "solver_type %s\n", solver_type_table[param.solver_type]);
	fprintf(fp, "nr_class %d\n", model_->nr_class);
	
	if(model_->label)
	{
		fprintf(fp, "label");
		for(i=0; i<model_->nr_class; i++)
			fprintf(fp, " %d", model_->label[i]);
		fprintf(fp, "\n");
	}

	fprintf(fp, "nr_feature %d\n", param.real_dim);

	fprintf(fp, "bias %.16g\n", model_->bias);

	fprintf(fp, "w\n");
	if(param.structured_w=='f')
	{
		double ** outputw = new double*[param.tmx_c];
		for(i=0; i<param.tmx_c; i++)
			outputw[i] = new double[nr_w];
		
		for(i=0; i<nr_w; i++) //classes
		{
			for(int j=0; j<param.tmx_c; j++)
			{
				double wtoput = 0;
				for(int k=0; k<w_size;k++)
					wtoput += param.transform_matrix[k][j]*model_->w[k*nr_w+i];
				outputw[j][i] = wtoput;
			}
		}
		
		for(int k=0; k<param.tmx_c;k++)
		{
			for(i=0; i<nr_w; i++) 
				fprintf(fp, "%.16g ", outputw[k][i]);
			fprintf(fp, "\n");
		}
		
		for(i=0; i<param.tmx_c; i++)
			delete[] outputw[i];
		delete[] outputw;
	}
	else
	{
		for(i=0; i<w_size; i++)
		{
			int j;
			for(j=0; j<nr_w; j++)
				fprintf(fp, "%.16g ", model_->w[i*nr_w+j]);
			fprintf(fp, "\n");
			if(i==nr_feature-1 && (param.structured_w=='s' || param.structured_w=='a'))
			{
				int k;
				for(k=nr_feature-(param.real_dim%2==0?1:2); k>=0; k--)
				{
					int j;
					for(j=0; j<nr_w; j++) 
						fprintf(fp, "%.16g ", (param.structured_w=='a'?-1:1) * model_->w[k*nr_w+j]);
					fprintf(fp, "\n");
				}
			}
		}
	}
	setlocale(LC_ALL, old_locale);
	free(old_locale);

	if (ferror(fp) != 0 || fclose(fp) != 0) return -1;
	else return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rsi, %r15
movsd 0xc8(%rsi), %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
movl 0xb4(%rsi), %ebx
movq %rbx, 0x28(%rsp)
sbbl $-0x1, %ebx
leaq 0x4c2a(%rip), %rsi      # 0x165bc
callq 0x32a0
pushq $-0x1
popq %rcx
testq %rax, %rax
je 0x11d9f
movq %rax, %r14
pushq $0x6
popq %rbp
movl %ebp, %edi
xorl %esi, %esi
callq 0x31e0
movq %rax, %rdi
callq 0x3460
movq %rax, 0x40(%rsp)
leaq 0x4eeb(%rip), %rsi      # 0x168b1
movl %ebp, %edi
callq 0x31e0
movl 0xb0(%r15), %eax
movslq (%r15), %rcx
cmpq $0x4, %rcx
pushq $0x1
popq %rdx
cmovel %eax, %edx
cmpl $0x2, %eax
cmovnel %eax, %edx
movq %rdx, 0x8(%rsp)
leaq 0x819d(%rip), %rax      # 0x19b90
movq (%rax,%rcx,8), %rdx
leaq 0x4bc0(%rip), %rsi      # 0x165be
movq %r14, %rdi
xorl %eax, %eax
callq 0x3370
movl 0xb0(%r15), %edx
leaq 0x4bb8(%rip), %rsi      # 0x165ce
movq %r14, %rdi
xorl %eax, %eax
callq 0x3370
cmpq $0x0, 0xc0(%r15)
je 0x11a7d
leaq 0x4baa(%rip), %rdi      # 0x165db
pushq $0x5
popq %rsi
pushq $0x1
popq %rdx
movq %r14, %rcx
callq 0x34a0
leaq 0x4b9b(%rip), %r12      # 0x165e1
xorl %r13d, %r13d
movslq 0xb0(%r15), %rax
cmpq %rax, %r13
jge 0x11a72
movq 0xc0(%r15), %rax
movl (%rax,%r13,4), %edx
movq %r14, %rdi
movq %r12, %rsi
xorl %eax, %eax
callq 0x3370
incq %r13
jmp 0x11a49
pushq $0xa
popq %rdi
movq %r14, %rsi
callq 0x32e0
movl 0x68(%r15), %edx
leaq 0x4b5d(%rip), %rsi      # 0x165e5
xorl %r12d, %r12d
movq %r14, %rdi
xorl %eax, %eax
callq 0x3370
movsd 0xc8(%r15), %xmm0
leaq 0x4b4f(%rip), %rsi      # 0x165f4
movq %r14, %rdi
movb $0x1, %al
callq 0x3370
leaq 0x4b4a(%rip), %rdi      # 0x16600
pushq $0x2
popq %rsi
pushq $0x1
popq %rdx
movq %r14, %rcx
callq 0x34a0
cmpb $0x66, 0x6c(%r15)
jne 0x11b47
movslq 0x7c(%r15), %rbp
leaq (,%rbp,8), %rdi
testq %rbp, %rbp
pushq $-0x1
popq %rax
cmovsq %rax, %rdi
callq 0x3030
movq %rax, %r13
movq 0x8(%rsp), %rax
movslq %eax, %r12
leaq (,%r12,8), %rcx
testl %eax, %eax
pushq $-0x1
popq %rax
cmovsq %rax, %rcx
movq %rcx, 0x20(%rsp)
movq %rbp, %rax
testl %eax, %eax
movl $0x0, %ebp
movq %rax, 0x18(%rsp)
cmovgl %eax, %ebp
xorl %eax, %eax
cmpq %rax, %rbp
je 0x11c89
movq 0x20(%rsp), %rdi
movq %rax, 0x10(%rsp)
callq 0x3030
movq 0x10(%rsp), %rcx
movq %rax, (%r13,%rcx,8)
movq 0x10(%rsp), %rax
incq %rax
jmp 0x11b1b
movq 0x28(%rsp), %rax
decl %eax
movq %rax, 0x50(%rsp)
movq 0x8(%rsp), %rax
testl %eax, %eax
movl $0x0, %ecx
cmovgl %eax, %ecx
movq %rcx, 0x18(%rsp)
cltq
testl %ebx, %ebx
cmovlel %r12d, %ebx
movq %rax, 0x38(%rsp)
leaq (,%rax,8), %rax
movq %rax, 0x48(%rsp)
leaq 0x4a7b(%rip), %r13      # 0x16603
xorl %eax, %eax
xorl %ebp, %ebp
cmpq %rbx, %rax
je 0x11d6a
movq %rax, 0x30(%rsp)
movq 0x18(%rsp), %r12
movq %rbp, 0x10(%rsp)
subq $0x1, %r12
jb 0x11bc9
movq 0xb8(%r15), %rax
movsd (%rax,%rbp), %xmm0
movq %r14, %rdi
movq %r13, %rsi
movb $0x1, %al
callq 0x3370
addq $0x8, %rbp
jmp 0x11ba4
pushq $0xa
popq %rdi
movq %r14, %rsi
callq 0x32e0
movq 0x30(%rsp), %rax
cmpq 0x50(%rsp), %rax
jne 0x11c72
movzbl 0x6c(%r15), %eax
cmpl $0x73, %eax
je 0x11bf3
cmpl $0x61, %eax
jne 0x11c72
movl 0x68(%r15), %eax
notl %eax
orl $-0x2, %eax
addl 0x28(%rsp), %eax
cltq
movq 0x38(%rsp), %r12
imulq %rax, %r12
testq %rax, %rax
js 0x11c72
movq %rax, 0x8(%rsp)
movq 0x18(%rsp), %rbp
movq %r12, 0x20(%rsp)
subq $0x1, %rbp
jb 0x11c53
cmpb $0x61, 0x6c(%r15)
movq 0xb8(%r15), %rax
movsd (%rax,%r12,8), %xmm0
jne 0x11c41
xorpd 0x44ef(%rip), %xmm0     # 0x16130
movq %r14, %rdi
movq %r13, %rsi
movb $0x1, %al
callq 0x3370
incq %r12
jmp 0x11c1f
pushq $0xa
popq %rdi
movq %r14, %rsi
callq 0x32e0
movq 0x8(%rsp), %rax
decq %rax
movq 0x20(%rsp), %r12
subq 0x38(%rsp), %r12
jmp 0x11c0b
movq 0x30(%rsp), %rax
incq %rax
movq 0x10(%rsp), %rbp
addq 0x48(%rsp), %rbp
jmp 0x11b8c
xorl %eax, %eax
testl %ebx, %ebx
cmovlel %eax, %ebx
movq 0x8(%rsp), %rcx
testl %ecx, %ecx
cmovlel %eax, %ecx
movq %rcx, 0x8(%rsp)
cmpq 0x8(%rsp), %rax
je 0x11cf5
xorl %ecx, %ecx
cmpq %rbp, %rcx
je 0x11cf0
xorpd %xmm0, %xmm0
movq %rax, %rdx
xorl %esi, %esi
cmpq %rsi, %rbx
je 0x11ce1
movq 0x70(%r15), %rdi
movq 0xb8(%r15), %r8
movq (%rdi,%rsi,8), %rdi
movsd (%rdi,%rcx,8), %xmm1
mulsd (%r8,%rdx,8), %xmm1
addsd %xmm1, %xmm0
incq %rsi
addq %r12, %rdx
jmp 0x11cb6
movq (%r13,%rcx,8), %rdx
movsd %xmm0, (%rdx,%rax,8)
incq %rcx
jmp 0x11ca8
incq %rax
jmp 0x11c9f
xorl %ebp, %ebp
leaq 0x4905(%rip), %r12      # 0x16603
movq 0x18(%rsp), %rcx
movslq %ecx, %rax
xorl %ebx, %ebx
cmpq %rax, %rbp
jge 0x11d5a
cmpq %rbx, 0x8(%rsp)
je 0x11d30
movq (%r13,%rbp,8), %rax
movsd (%rax,%rbx,8), %xmm0
movq %r14, %rdi
movq %r12, %rsi
movb $0x1, %al
callq 0x3370
incq %rbx
jmp 0x11d0d
pushq $0xa
popq %rdi
movq %r14, %rsi
callq 0x32e0
incq %rbp
movl 0x7c(%r15), %ecx
jmp 0x11d03
movq (%r13,%rbx,8), %rdi
testq %rdi, %rdi
je 0x11d57
callq 0x32f0
movl 0x7c(%r15), %ecx
incq %rbx
movslq %ecx, %rax
cmpq %rax, %rbx
jl 0x11d44
movq %r13, %rdi
callq 0x32f0
pushq $0x6
popq %rdi
movq 0x40(%rsp), %rbx
movq %rbx, %rsi
callq 0x31e0
movq %rbx, %rdi
callq 0x32b0
movq %r14, %rdi
callq 0x3480
testl %eax, %eax
pushq $-0x1
popq %rcx
jne 0x11d9f
movq %r14, %rdi
callq 0x3220
xorl %ecx, %ecx
negl %eax
sbbl %ecx, %ecx
movl %ecx, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq