// File: /workspace/github_c_makefile_star2_O3/bluecmd[P]fst-example/gtkwave/fstapi.c
// Function: fstWriterCreateEnumTable
fstEnumHandle fstWriterCreateEnumTable(void *ctx, const char *name, uint32_t elem_count, unsigned int min_valbits, const char **literal_arr, const char **val_arr)
{
fstEnumHandle handle = 0;
unsigned int *literal_lens = NULL;
unsigned int *val_lens = NULL;
int lit_len_tot = 0;
int val_len_tot = 0;
int name_len;
char elem_count_buf[16];
int elem_count_len;
int total_len;
int pos = 0;
char *attr_str = NULL;

if(ctx && name && literal_arr && val_arr && (elem_count != 0))
	{
	struct fstWriterContext *xc = (struct fstWriterContext *)ctx;

	uint32_t i;

	name_len = strlen(name);
	elem_count_len = sprintf(elem_count_buf, "%" PRIu32, elem_count);

	literal_lens = (unsigned int *)calloc(elem_count, sizeof(unsigned int));
	val_lens = (unsigned int *)calloc(elem_count, sizeof(unsigned int));

	for(i=0;i<elem_count;i++)
		{
		literal_lens[i] = strlen(literal_arr[i]);
		lit_len_tot += fstUtilityBinToEscConvertedLen((unsigned char*)literal_arr[i], literal_lens[i]);

		val_lens[i] =  strlen(val_arr[i]);
		val_len_tot += fstUtilityBinToEscConvertedLen((unsigned char*)val_arr[i], val_lens[i]);

		if(min_valbits > 0)
			{
			if(val_lens[i] < min_valbits)
				{
				val_len_tot += (min_valbits - val_lens[i]); /* additional converted len is same for '0' character */
				}
			}
		}

	total_len = name_len + 1 + elem_count_len + 1 + lit_len_tot + elem_count + val_len_tot + elem_count;

	attr_str = (char*)malloc(total_len);
	pos = 0;

	memcpy(attr_str+pos, name, name_len);
	pos += name_len;
	attr_str[pos++] = ' ';

	memcpy(attr_str+pos, elem_count_buf, elem_count_len);
	pos += elem_count_len;
	attr_str[pos++] = ' ';

	for(i=0;i<elem_count;i++)
		{
		pos += fstUtilityBinToEsc((unsigned char*)attr_str+pos, (unsigned char*)literal_arr[i], literal_lens[i]);
		attr_str[pos++] = ' ';
		}

	for(i=0;i<elem_count;i++)
		{
		if(min_valbits > 0)
			{
			if(val_lens[i] < min_valbits)
				{
				memset(attr_str+pos, '0', min_valbits - val_lens[i]);
				pos += (min_valbits - val_lens[i]);
				}
			}

		pos += fstUtilityBinToEsc((unsigned char*)attr_str+pos, (unsigned char*)val_arr[i], val_lens[i]);
		attr_str[pos++] = ' ';
		}

	attr_str[pos-1] = 0;

#ifdef FST_DEBUG
	fprintf(stderr, FST_APIMESS "fstWriterCreateEnumTable() total_len: %d, pos: %d\n", total_len, pos);
	fprintf(stderr, FST_APIMESS "*%s*\n", attr_str);
#endif

	fstWriterSetAttrBegin(xc, FST_AT_MISC, FST_MT_ENUMTABLE, attr_str, handle = ++xc->max_enumhandle);

	free(attr_str);
	free(val_lens);
	free(literal_lens);
	}

return(handle);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
xorl %ebx, %ebx
testl %edx, %edx
je 0x85df
testq %rdi, %rdi
je 0x85df
testq %rsi, %rsi
je 0x85df
testq %r8, %r8
je 0x85df
movq %r9, %r12
testq %r9, %r9
je 0x85df
movl %edx, %r14d
movq %rdi, -0x88(%rbp)
movq %rsi, -0x80(%rbp)
movq %rsi, %rdi
movl %ecx, -0x2c(%rbp)
movq %r8, -0x68(%rbp)
callq 0x2130
movq %rax, -0x70(%rbp)
leaq 0xbfcd(%rip), %rsi      # 0x143a8
leaq -0xa0(%rbp), %rdi
movl %r14d, %edx
xorl %eax, %eax
callq 0x2370
movq %rax, -0x60(%rbp)
movq %r14, -0x78(%rbp)
movl %r14d, %ebx
movl $0x4, %esi
movq %rbx, %rdi
callq 0x21e0
movq %rax, -0x40(%rbp)
movl $0x4, %esi
movq %rbx, -0x50(%rbp)
movq %rbx, %rdi
callq 0x21e0
movq %rax, -0x48(%rbp)
xorl %r15d, %r15d
xorl %r14d, %r14d
xorl %ebx, %ebx
movq %r12, -0x38(%rbp)
movq -0x68(%rbp), %rax
movq (%rax,%r15,8), %r13
movq %r13, %rdi
callq 0x2130
movq -0x40(%rbp), %rcx
movl %eax, (%rcx,%r15,4)
movq %r13, %rdi
movl %eax, %esi
callq 0x85f0
addl %eax, %r14d
movq -0x38(%rbp), %rax
movq (%rax,%r15,8), %r13
movq %r13, %rdi
callq 0x2130
movq %rax, %r12
movq -0x48(%rbp), %rax
movl %r12d, (%rax,%r15,4)
movq %r13, %rdi
movl %r12d, %esi
callq 0x85f0
addl %ebx, %eax
movl -0x2c(%rbp), %ebx
subl %r12d, %ebx
movl $0x0, %ecx
cmovbl %ecx, %ebx
addl %eax, %ebx
incq %r15
cmpq %r15, -0x50(%rbp)
jne 0x8429
movq -0x70(%rbp), %r13
movq -0x60(%rbp), %rdx
leal (%rdx,%r13), %eax
leal (%rdx,%r13), %ecx
incl %ecx
movl %ecx, -0x54(%rbp)
leal (%rdx,%r13), %r15d
addl $0x2, %r15d
movq -0x78(%rbp), %rcx
leal (%rax,%rcx,2), %eax
addl $0x2, %eax
addl %r14d, %eax
addl %ebx, %eax
movslq %eax, %rdi
callq 0x2270
movq %rax, %r12
movslq %r13d, %r13
movq %rax, %rdi
movq -0x80(%rbp), %rsi
movq %r13, %rdx
callq 0x2240
movb $0x20, %bl
leaq (%r12,%r13), %rdi
incq %rdi
movb %bl, -0x1(%rdi)
movslq -0x60(%rbp), %rdx
leaq -0xa0(%rbp), %rsi
callq 0x2240
movslq -0x54(%rbp), %rax
movb %bl, (%r12,%rax)
xorl %ebx, %ebx
movq -0x40(%rbp), %r13
movslq %r15d, %r14
leaq (%r12,%r14), %rdi
movq -0x68(%rbp), %rax
movq (%rax,%rbx,8), %rsi
movl (%r13,%rbx,4), %edx
callq 0x8659
leal (%rax,%r14), %r15d
incl %r15d
addl %r14d, %eax
cltq
movb $0x20, (%r12,%rax)
incq %rbx
cmpq %rbx, -0x50(%rbp)
jne 0x8502
xorl %ebx, %ebx
movq -0x38(%rbp), %r13
movq -0x48(%rbp), %rax
movl (%rax,%rbx,4), %r14d
movl -0x2c(%rbp), %eax
testl %eax, %eax
je 0x856d
movl %eax, %r13d
subl %r14d, %r13d
jbe 0x8569
movslq %r15d, %r15
leaq (%r12,%r15), %rdi
movl $0x30, %esi
movq %r13, %rdx
callq 0x2190
addl %r13d, %r15d
movq -0x38(%rbp), %r13
movslq %r15d, %r15
leaq (%r12,%r15), %rdi
movq (%r13,%rbx,8), %rsi
movl %r14d, %edx
callq 0x8659
addl %eax, %r15d
movslq %r15d, %r15
movb $0x20, (%r12,%r15)
incq %r15
incq %rbx
cmpq %rbx, -0x50(%rbp)
jne 0x853b
movb $0x0, -0x1(%r12,%r15)
movq -0x88(%rbp), %rdi
movl 0x150(%rdi), %ebx
incl %ebx
movl %ebx, 0x150(%rdi)
xorl %esi, %esi
movl $0x7, %edx
movq %r12, %rcx
movq %rbx, %r8
callq 0x8268
movq %r12, %rdi
callq 0x2050
movq -0x48(%rbp), %rdi
callq 0x2050
movq -0x40(%rbp), %rdi
callq 0x2050
movl %ebx, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq