// File: /workspace/github_c_makefile_star2_O1/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 0x6bcf
testq %rdi, %rdi
je 0x6bcf
testq %rsi, %rsi
je 0x6bcf
testq %r8, %r8
je 0x6bcf
movq %r9, %r14
testq %r9, %r9
je 0x6bcf
movl %edx, %r15d
movq %rdi, -0x88(%rbp)
movq %rsi, -0x80(%rbp)
movq %rsi, %rdi
movl %ecx, -0x34(%rbp)
movq %r8, -0x68(%rbp)
callq 0x2130
movq %rax, -0x70(%rbp)
leaq 0xe9e8(%rip), %rsi      # 0x153a8
leaq -0xa0(%rbp), %rdi
movl %r15d, %edx
xorl %eax, %eax
callq 0x2370
movq %rax, -0x60(%rbp)
movq %r15, -0x78(%rbp)
movl %r15d, %ebx
movl $0x4, %esi
movq %rbx, %rdi
callq 0x21e0
movq %rax, -0x48(%rbp)
movl $0x4, %esi
movq %rbx, -0x50(%rbp)
movq %rbx, %rdi
callq 0x21e0
movq %rax, -0x30(%rbp)
xorl %ebx, %ebx
movq %r14, -0x40(%rbp)
xorl %r14d, %r14d
xorl %r13d, %r13d
movq -0x68(%rbp), %rax
movq (%rax,%rbx,8), %r12
movq %r12, %rdi
callq 0x2130
movq -0x48(%rbp), %rcx
movl %eax, (%rcx,%rbx,4)
movq %r12, %rdi
movl %eax, %esi
callq 0x6be0
addl %eax, %r14d
movq -0x40(%rbp), %rax
movq (%rax,%rbx,8), %r12
movq %r12, %rdi
callq 0x2130
movq %rax, %r15
movq -0x30(%rbp), %rax
movl %r15d, (%rax,%rbx,4)
movq %r12, %rdi
movl %r15d, %esi
callq 0x6be0
addl %r13d, %eax
movl -0x34(%rbp), %r13d
subl %r15d, %r13d
movl $0x0, %ecx
cmovbl %ecx, %r13d
addl %eax, %r13d
incq %rbx
cmpq %rbx, -0x50(%rbp)
jne 0x6a0e
movq -0x70(%rbp), %rbx
movq -0x60(%rbp), %rdx
leal (%rbx,%rdx), %eax
leal (%rbx,%rdx), %ecx
incl %ecx
movl %ecx, -0x54(%rbp)
leal (%rbx,%rdx), %r15d
addl $0x2, %r15d
movq -0x78(%rbp), %rcx
leal (%rax,%rcx,2), %eax
addl $0x2, %eax
addl %r14d, %eax
addl %r13d, %eax
movslq %eax, %rdi
callq 0x2270
movq %rax, %r12
movslq %ebx, %rbx
movq %rax, %rdi
movq -0x80(%rbp), %rsi
movq %rbx, %rdx
callq 0x2240
movb $0x20, %r14b
leaq (%r12,%rbx), %rdi
incq %rdi
movb %r14b, -0x1(%rdi)
movslq -0x60(%rbp), %rdx
leaq -0xa0(%rbp), %rsi
callq 0x2240
movslq -0x54(%rbp), %rax
movb %r14b, (%r12,%rax)
xorl %ebx, %ebx
movq -0x48(%rbp), %r13
movslq %r15d, %r14
leaq (%r12,%r14), %rdi
movq -0x68(%rbp), %rax
movq (%rax,%rbx,8), %rsi
movl (%r13,%rbx,4), %edx
callq 0x6c49
leal (%rax,%r14), %r15d
incl %r15d
addl %r14d, %eax
cltq
movb $0x20, (%r12,%rax)
incq %rbx
cmpq %rbx, -0x50(%rbp)
jne 0x6aeb
xorl %r13d, %r13d
movq -0x40(%rbp), %r14
movl -0x34(%rbp), %eax
testl %eax, %eax
je 0x6b5a
movl %eax, %r14d
movq -0x30(%rbp), %rax
subl (%rax,%r13,4), %r14d
jbe 0x6b54
movslq %r15d, %r15
leaq (%r12,%r15), %rdi
movl $0x30, %esi
movq %r14, %rdx
callq 0x2190
movq -0x30(%rbp), %rax
addl %r14d, %r15d
movq -0x40(%rbp), %r14
jmp 0x6b5e
movq -0x30(%rbp), %rax
movslq %r15d, %rbx
leaq (%r12,%rbx), %rdi
movq (%r14,%r13,8), %rsi
movl (%rax,%r13,4), %edx
callq 0x6c49
addl %eax, %ebx
movslq %ebx, %r15
movb $0x20, (%r12,%r15)
incq %r15
incq %r13
cmpq %r13, -0x50(%rbp)
jne 0x6b25
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 0x6856
movq %r12, %rdi
callq 0x2050
movq -0x30(%rbp), %rdi
callq 0x2050
movq -0x48(%rbp), %rdi
callq 0x2050
movl %ebx, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq