// File: /workspace/github_cpp_makefile_star2_O3/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), %eax
movq %rax, 0x28(%rsp)
movl %eax, %ebx
sbbl $-0x1, %ebx
leaq 0x4e50(%rip), %rsi      # 0x195ec
callq 0x3280
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
testq %rax, %rax
je 0x14c04
movq %rax, %r12
movl $0x6, %edi
xorl %esi, %esi
callq 0x31d0
movq %rax, %rdi
callq 0x3420
movq %rax, 0x30(%rsp)
leaq 0x510f(%rip), %rsi      # 0x198e1
movl $0x6, %edi
callq 0x31d0
movl 0xb0(%r15), %eax
movslq (%r15), %rcx
cmpq $0x4, %rcx
movl $0x1, %edx
cmovel %eax, %edx
cmpl $0x2, %eax
cmovnel %eax, %edx
movl %edx, 0x4(%rsp)
leaq 0x937d(%rip), %rax      # 0x1db80
movq (%rax,%rcx,8), %rdx
leaq 0x4de0(%rip), %rsi      # 0x195ee
movq %r12, %rdi
xorl %eax, %eax
callq 0x3330
movl 0xb0(%r15), %edx
leaq 0x4dd8(%rip), %rsi      # 0x195fe
movq %r12, %rdi
xorl %eax, %eax
callq 0x3330
cmpq $0x0, 0xc0(%r15)
je 0x1489b
leaq 0x4dca(%rip), %rdi      # 0x1960b
movl $0x5, %esi
movl $0x1, %edx
movq %r12, %rcx
callq 0x3460
cmpl $0x0, 0xb0(%r15)
jle 0x1488e
leaq 0x4dad(%rip), %r13      # 0x19611
xorl %r14d, %r14d
movq 0xc0(%r15), %rax
movl (%rax,%r14,4), %edx
movq %r12, %rdi
movq %r13, %rsi
xorl %eax, %eax
callq 0x3330
incq %r14
movslq 0xb0(%r15), %rax
cmpq %rax, %r14
jl 0x14867
movl $0xa, %edi
movq %r12, %rsi
callq 0x32c0
movl 0x68(%r15), %edx
leaq 0x4d6f(%rip), %rsi      # 0x19615
movq %r12, %rdi
xorl %eax, %eax
callq 0x3330
movsd 0xc8(%r15), %xmm0
leaq 0x4d64(%rip), %rsi      # 0x19624
movq %r12, %rdi
movb $0x1, %al
callq 0x3330
leaq 0x4d5f(%rip), %rdi      # 0x19630
movl $0x2, %esi
movl $0x1, %edx
movq %r12, %rcx
callq 0x3460
cmpb $0x66, 0x6c(%r15)
movq %r12, 0x18(%rsp)
jne 0x14a7d
movslq 0x7c(%r15), %r12
leaq (,%r12,8), %rdi
testq %r12, %r12
movq $-0x1, %r14
cmovsq %r14, %rdi
callq 0x3030
movq %rax, %r13
testq %r12, %r12
jle 0x14943
movl 0x4(%rsp), %eax
movslq %eax, %rbp
shlq $0x3, %rbp
testl %eax, %eax
cmovsq %r14, %rbp
xorl %r14d, %r14d
movq %rbp, %rdi
callq 0x3030
movq %rax, (%r13,%r14,8)
incq %r14
cmpq %r14, %r12
jne 0x1492e
cmpl $0x0, 0x4(%rsp)
jle 0x149c0
movl 0x4(%rsp), %eax
movl %ebx, %ecx
leaq (,%rax,8), %rdx
xorl %esi, %esi
xorl %edi, %edi
testl %r12d, %r12d
jle 0x149b4
xorl %r8d, %r8d
testl %ebx, %ebx
jle 0x1499d
movq 0x70(%r15), %r9
movq 0xb8(%r15), %r10
addq %rsi, %r10
xorpd %xmm0, %xmm0
xorl %r11d, %r11d
movq (%r9,%r11,8), %r14
movsd (%r14,%r8,8), %xmm1
mulsd (%r10), %xmm1
addsd %xmm1, %xmm0
incq %r11
addq %rdx, %r10
cmpq %r11, %rcx
jne 0x1497d
jmp 0x149a1
xorpd %xmm0, %xmm0
movq (%r13,%r8,8), %r9
movsd %xmm0, (%r9,%rdi,8)
incq %r8
cmpq %r12, %r8
jne 0x14964
incq %rdi
addq $0x8, %rsi
cmpq %rax, %rdi
jne 0x1495c
testl %r12d, %r12d
movq 0x18(%rsp), %r12
jle 0x14a70
movq %r13, 0x10(%rsp)
movl 0x4(%rsp), %ebx
movq $0x0, 0x8(%rsp)
leaq 0x4c4c(%rip), %rbp      # 0x19633
movq 0x18(%rsp), %r12
cmpl $0x0, 0x4(%rsp)
jle 0x14a1f
movq 0x10(%rsp), %rax
movq 0x8(%rsp), %rcx
movq (%rax,%rcx,8), %r14
xorl %r13d, %r13d
movsd (%r14,%r13,8), %xmm0
movq %r12, %rdi
movq %rbp, %rsi
movb $0x1, %al
callq 0x3330
incq %r13
cmpq %r13, %rbx
jne 0x14a04
movl $0xa, %edi
movq %r12, %rsi
callq 0x32c0
movq 0x8(%rsp), %rdx
incq %rdx
movslq 0x7c(%r15), %rax
movq %rdx, 0x8(%rsp)
cmpq %rax, %rdx
jl 0x149ec
testl %eax, %eax
movq 0x18(%rsp), %r12
movq 0x10(%rsp), %r13
jle 0x14a70
xorl %ebx, %ebx
movq (%r13,%rbx,8), %rdi
testq %rdi, %rdi
je 0x14a65
callq 0x32d0
movl 0x7c(%r15), %eax
incq %rbx
movslq %eax, %rcx
cmpq %rcx, %rbx
jl 0x14a52
movq %r13, %rdi
callq 0x32d0
jmp 0x14bcb
testl %ebx, %ebx
jle 0x14bcb
movq 0x28(%rsp), %rax
decl %eax
movq %rax, 0x48(%rsp)
movslq 0x4(%rsp), %rax
movq %rax, 0x8(%rsp)
movl %eax, %eax
movl %ebx, %ecx
movq %rcx, 0x40(%rsp)
movq %rax, 0x10(%rsp)
leaq (,%rax,8), %rax
movq %rax, 0x38(%rsp)
xorl %ebp, %ebp
leaq 0x4b74(%rip), %r13      # 0x19633
movq $0x0, 0x20(%rsp)
cmpl $0x0, 0x4(%rsp)
jle 0x14afa
movq 0x10(%rsp), %rbx
movq %rbp, %r14
movq 0xb8(%r15), %rax
movsd (%rax,%r14), %xmm0
movq %r12, %rdi
movq %r13, %rsi
movb $0x1, %al
callq 0x3330
addq $0x8, %r14
decq %rbx
jne 0x14ad7
movq %rbp, 0x50(%rsp)
movl $0xa, %edi
movq %r12, %rsi
callq 0x32c0
movq 0x20(%rsp), %rax
cmpq 0x48(%rsp), %rax
jne 0x14ba9
movzbl 0x6c(%r15), %eax
cmpl $0x73, %eax
je 0x14b2b
cmpl $0x61, %eax
jne 0x14ba9
movl 0x68(%r15), %eax
notl %eax
orl $-0x2, %eax
addl 0x28(%rsp), %eax
js 0x14ba9
movl %eax, %ebx
movq 0x8(%rsp), %r14
imulq %rbx, %r14
cmpl $0x0, 0x4(%rsp)
jle 0x14b86
movq 0x10(%rsp), %r12
movq %r14, %rbp
cmpb $0x61, 0x6c(%r15)
movq 0xb8(%r15), %rax
movsd (%rax,%rbp,8), %xmm0
jne 0x14b6f
xorpd 0x45c1(%rip), %xmm0     # 0x19130
movq 0x18(%rsp), %rdi
movq %r13, %rsi
movb $0x1, %al
callq 0x3330
incq %rbp
decq %r12
jne 0x14b54
movl $0xa, %edi
movq 0x18(%rsp), %r12
movq %r12, %rsi
callq 0x32c0
leaq -0x1(%rbx), %rax
subq 0x8(%rsp), %r14
testq %rbx, %rbx
movq %rax, %rbx
jg 0x14b45
movq 0x20(%rsp), %rcx
incq %rcx
movq 0x50(%rsp), %rbp
addq 0x38(%rsp), %rbp
movq %rcx, 0x20(%rsp)
cmpq 0x40(%rsp), %rcx
jne 0x14ac8
movl $0x6, %edi
movq 0x30(%rsp), %rbx
movq %rbx, %rsi
callq 0x31d0
movq %rbx, %rdi
callq 0x3290
movq %r12, %rdi
callq 0x3440
testl %eax, %eax
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
jne 0x14c04
movq %r12, %rdi
callq 0x3200
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