// File: /workspace/github_cpp_makefile_star2_O1/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 $0x68, %rsp
movq %rsi, %r15
movsd 0xc8(%rsi), %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
movl 0xb4(%rsi), %eax
movq %rax, 0x30(%rsp)
movl %eax, %ebx
sbbl $-0x1, %ebx
leaq 0x49dd(%rip), %rsi      # 0x1757c
callq 0x3270
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
testq %rax, %rax
je 0x1300e
movq %rax, %rbp
movl $0x6, %edi
xorl %esi, %esi
callq 0x31c0
movq %rax, %rdi
callq 0x3410
movq %rax, 0x38(%rsp)
leaq 0x4c9c(%rip), %rsi      # 0x17871
movl $0x6, %edi
callq 0x31c0
movl 0xb0(%r15), %eax
cmpl $0x2, %eax
jne 0x12bf9
movl $0x1, 0xc(%rsp)
cmpl $0x4, (%r15)
jne 0x12bfd
movl %eax, 0xc(%rsp)
movslq (%r15), %rax
leaq 0x7f79(%rip), %rcx      # 0x1ab80
movq (%rcx,%rax,8), %rdx
leaq 0x496c(%rip), %rsi      # 0x1757e
movq %rbp, %rdi
xorl %eax, %eax
callq 0x3320
movl 0xb0(%r15), %edx
leaq 0x4964(%rip), %rsi      # 0x1758e
movq %rbp, %rdi
xorl %eax, %eax
callq 0x3320
cmpq $0x0, 0xc0(%r15)
je 0x12c9f
leaq 0x4956(%rip), %rdi      # 0x1759b
movl $0x5, %esi
movl $0x1, %edx
movq %rbp, %rcx
callq 0x3450
cmpl $0x0, 0xb0(%r15)
jle 0x12c92
leaq 0x4939(%rip), %r13      # 0x175a1
xorl %r14d, %r14d
movq 0xc0(%r15), %rax
movl (%rax,%r14,4), %edx
movq %rbp, %rdi
movq %r13, %rsi
xorl %eax, %eax
callq 0x3320
incq %r14
movslq 0xb0(%r15), %rax
cmpq %rax, %r14
jl 0x12c6b
movl $0xa, %edi
movq %rbp, %rsi
callq 0x32b0
movl 0x68(%r15), %edx
leaq 0x48fb(%rip), %rsi      # 0x175a5
movq %rbp, %rdi
xorl %eax, %eax
callq 0x3320
movsd 0xc8(%r15), %xmm0
leaq 0x48f0(%rip), %rsi      # 0x175b4
movq %rbp, %rdi
movb $0x1, %al
callq 0x3320
leaq 0x48eb(%rip), %rdi      # 0x175c0
movl $0x2, %esi
movl $0x1, %edx
movq %rbp, %rcx
callq 0x3450
cmpb $0x66, 0x6c(%r15)
movq %rbp, 0x20(%rsp)
jne 0x12e80
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 0x12d47
movl 0xc(%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 0x12d32
cmpl $0x0, 0xc(%rsp)
jle 0x12dc4
movl 0xc(%rsp), %eax
movl %ebx, %ecx
leaq (,%rax,8), %rdx
xorl %esi, %esi
xorl %edi, %edi
testl %r12d, %r12d
jle 0x12db8
xorl %r8d, %r8d
testl %ebx, %ebx
jle 0x12da1
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 0x12d81
jmp 0x12da5
xorpd %xmm0, %xmm0
movq (%r13,%r8,8), %r9
movsd %xmm0, (%r9,%rdi,8)
incq %r8
cmpq %r12, %r8
jne 0x12d68
incq %rdi
addq $0x8, %rsi
cmpq %rax, %rdi
jne 0x12d60
movq %r13, 0x10(%rsp)
cmpl $0x0, 0x7c(%r15)
jle 0x12e46
movl 0xc(%rsp), %eax
movq %rax, 0x18(%rsp)
xorl %ebp, %ebp
movq %r15, 0x40(%rsp)
cmpl $0x0, 0xc(%rsp)
movq 0x20(%rsp), %rsi
leaq 0x47d2(%rip), %rbx      # 0x175c3
movq 0x10(%rsp), %rcx
movq 0x18(%rsp), %r15
jle 0x12e2b
xorl %r14d, %r14d
movq (%rcx,%rbp,8), %rax
movsd (%rax,%r14,8), %xmm0
movq %rsi, %rdi
movq %rsi, %r13
movq %rbx, %rsi
movb $0x1, %al
movq %rcx, %r12
callq 0x3320
movq %r12, %rcx
movq %r13, %rsi
incq %r14
cmpq %r14, %r15
jne 0x12e00
movl $0xa, %edi
callq 0x32b0
incq %rbp
movq 0x40(%rsp), %r15
movslq 0x7c(%r15), %rax
cmpq %rax, %rbp
jl 0x12de0
cmpl $0x0, 0x7c(%r15)
movq 0x20(%rsp), %rbp
movq 0x10(%rsp), %r14
jle 0x12e73
xorl %ebx, %ebx
movq (%r14,%rbx,8), %rdi
testq %rdi, %rdi
je 0x12e67
callq 0x32c0
incq %rbx
movslq 0x7c(%r15), %rax
cmpq %rax, %rbx
jl 0x12e59
movq %r14, %rdi
callq 0x32c0
jmp 0x12fd5
testl %ebx, %ebx
jle 0x12fd5
movq 0x30(%rsp), %rax
decl %eax
movq %rax, 0x58(%rsp)
movslq 0xc(%rsp), %rax
movl %ebx, %ecx
movq %rcx, 0x50(%rsp)
movq %rax, 0x10(%rsp)
movl %eax, %eax
movq %rax, 0x18(%rsp)
leaq (,%rax,8), %rax
movq %rax, 0x48(%rsp)
xorl %r12d, %r12d
leaq 0x4700(%rip), %r13      # 0x175c3
movq $0x0, 0x28(%rsp)
cmpl $0x0, 0xc(%rsp)
jle 0x12efe
movq 0x18(%rsp), %rbx
movq %r12, %r14
movq 0xb8(%r15), %rax
movsd (%rax,%r14), %xmm0
movq %rbp, %rdi
movq %r13, %rsi
movb $0x1, %al
callq 0x3320
addq $0x8, %r14
decq %rbx
jne 0x12edb
movq %r12, 0x60(%rsp)
movl $0xa, %edi
movq %rbp, %rsi
callq 0x32b0
movq 0x28(%rsp), %rax
cmpq 0x58(%rsp), %rax
jne 0x12fb3
movzbl 0x6c(%r15), %eax
cmpl $0x73, %eax
je 0x12f33
cmpl $0x61, %eax
jne 0x12fb3
movl 0x68(%r15), %eax
notl %eax
orl $-0x2, %eax
addl 0x30(%rsp), %eax
js 0x12fb3
movl %eax, %r12d
movq 0x10(%rsp), %r14
imulq %r12, %r14
cmpl $0x0, 0xc(%rsp)
jle 0x12f8f
movq 0x18(%rsp), %rbx
movq %r14, %rbp
cmpb $0x61, 0x6c(%r15)
movq 0xb8(%r15), %rax
movsd (%rax,%rbp,8), %xmm0
jne 0x12f78
xorpd 0x4188(%rip), %xmm0     # 0x17100
movq 0x20(%rsp), %rdi
movq %r13, %rsi
movb $0x1, %al
callq 0x3320
incq %rbp
decq %rbx
jne 0x12f5d
movl $0xa, %edi
movq 0x20(%rsp), %rbp
movq %rbp, %rsi
callq 0x32b0
leaq -0x1(%r12), %rax
subq 0x10(%rsp), %r14
testq %r12, %r12
movq %rax, %r12
jg 0x12f4e
movq 0x28(%rsp), %rcx
incq %rcx
movq 0x60(%rsp), %r12
addq 0x48(%rsp), %r12
movq %rcx, 0x28(%rsp)
cmpq 0x50(%rsp), %rcx
jne 0x12ecc
movl $0x6, %edi
movq 0x38(%rsp), %rbx
movq %rbx, %rsi
callq 0x31c0
movq %rbx, %rdi
callq 0x3280
movq %rbp, %rdi
callq 0x3430
testl %eax, %eax
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
jne 0x1300e
movq %rbp, %rdi
callq 0x31f0
xorl %ecx, %ecx
negl %eax
sbbl %ecx, %ecx
movl %ecx, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq