// File: /workspace/github_c_makefile_star2_O3/vtereshkov[P]umka-lang/src/umka_types.c
// Function: typeAddField
const Field *typeAddField(const Types *types, Type *structType, const Type *fieldType, const char *fieldName)
{
    IdentName fieldNameBuf;
    const char *name;

    if (fieldName)
        name = fieldName;
    else
    {
        // Automatic field naming
        snprintf(fieldNameBuf, DEFAULT_STR_LEN + 1, "item%d", structType->numItems);
        name = fieldNameBuf;
    }

    if (typeFindField(structType, name, NULL))
        types->error->handler(types->error->context, "Duplicate field %s", name);

    if (fieldType->kind == TYPE_FORWARD)
        types->error->handler(types->error->context, "Unresolved forward type declaration for field %s", name);

    if (fieldType->kind == TYPE_VOID)
        types->error->handler(types->error->context, "Void field %s is not allowed", name);

    int minNextFieldOffset = 0;
    if (structType->numItems > 0)
    {
        const Field *lastField = structType->field[structType->numItems - 1];
        minNextFieldOffset = lastField->offset + lastField->type->size;
    }

    if (typeSize(types, fieldType) > INT_MAX - minNextFieldOffset)
        types->error->handler(types->error->context, "Structure is too large");

    Field *field = storageAdd(types->storage, sizeof(Field));

    strncpy(field->name, name, MAX_IDENT_LEN);
    field->name[MAX_IDENT_LEN] = 0;

    field->hash = hash(name);
    field->type = fieldType;
    field->offset = align(minNextFieldOffset, typeAlignment(types, fieldType));

    if (structType->numItems > 0)
        structType->field = storageRealloc(types->storage, structType->field, (structType->numItems + 1) * sizeof(Field *));
    else
        structType->field = storageAdd(types->storage, sizeof(Field *));

    structType->numItems++;
    structType->field[structType->numItems - 1] = field;

    if (structType->alignment < fieldType->alignment)
        structType->alignment = fieldType->alignment;

    structType->size = align(field->offset + fieldType->size, structType->alignment);

    return field;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x108, %rsp            # imm = 0x108
movq %rcx, %r12
movq %rdx, %r14
movq %rsi, %rbx
movq %rdi, %r15
testq %rcx, %rcx
jne 0x1a6d5
movl 0x10(%rbx), %ecx
leaq 0x5d4ff(%rip), %rdx     # 0x77bc2
movq %rsp, %r12
movl $0x100, %esi            # imm = 0x100
movq %r12, %rdi
xorl %eax, %eax
callq 0x3170
movq %rbx, %rdi
movq %r12, %rsi
xorl %edx, %edx
callq 0x1a5a1
testq %rax, %rax
je 0x1a6fd
movq 0x18(%r15), %rcx
movq 0x20(%rcx), %rdi
leaq 0x5c5d8(%rip), %rsi     # 0x76cce
movq %r12, %rdx
xorl %eax, %eax
callq *(%rcx)
movl (%r14), %eax
cmpl $0x1, %eax
jne 0x1a71e
movq 0x18(%r15), %rcx
movq 0x20(%rcx), %rdi
leaq 0x5d4b5(%rip), %rsi     # 0x77bc9
movq %r12, %rdx
xorl %eax, %eax
callq *(%rcx)
movl (%r14), %eax
cmpl $0x2, %eax
jne 0x1a739
movq 0x18(%r15), %rcx
movq 0x20(%rcx), %rdi
leaq 0x5d4c8(%rip), %rsi     # 0x77bfa
movq %r12, %rdx
xorl %eax, %eax
callq *(%rcx)
movslq 0x10(%rbx), %rax
testq %rax, %rax
jle 0x1a760
movq 0x28(%rbx), %rcx
movq -0x8(%rcx,%rax,8), %rax
movq 0x108(%rax), %rcx
movl 0xc0(%rcx), %ebp
addl 0x110(%rax), %ebp
jmp 0x1a762
xorl %ebp, %ebp
movq %r15, %rdi
movq %r14, %rsi
callq 0x19e29
movl %ebp, %ecx
xorl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpl %ecx, %eax
jle 0x1a78c
movq 0x18(%r15), %rcx
movq 0x20(%rcx), %rdi
leaq 0x5d48f(%rip), %rsi     # 0x77c17
xorl %eax, %eax
callq *(%rcx)
movq 0x10(%r15), %rdi
movl $0x118, %esi            # imm = 0x118
callq 0x7a70
movq %rax, %r13
movl $0xff, %edx
movq %rax, %rdi
movq %r12, %rsi
callq 0x3080
movb $0x0, 0xff(%r13)
movb (%r12), %cl
testb %cl, %cl
je 0x1a7de
incq %r12
movl $0x1505, %eax           # imm = 0x1505
movl %eax, %edx
shll $0x5, %edx
addl %eax, %edx
movsbl %cl, %eax
addl %edx, %eax
movb (%r12), %cl
incq %r12
testb %cl, %cl
jne 0x1a7c5
jmp 0x1a7e3
movl $0x1505, %eax           # imm = 0x1505
movl %eax, 0x100(%r13)
movq %r14, 0x108(%r13)
movslq %ebp, %r12
movq %r15, %rdi
movq %r14, %rsi
callq 0x19e94
movslq %eax, %rsi
leaq (%r12,%rsi), %rcx
decq %rcx
movq %rcx, %rax
cqto
idivq %rsi
subl %edx, %ecx
movl %ecx, 0x110(%r13)
movl 0x10(%rbx), %eax
movq 0x10(%r15), %rdi
testl %eax, %eax
jle 0x1a838
movq 0x28(%rbx), %rsi
leaq 0x8(,%rax,8), %rdx
callq 0x7be8
jmp 0x1a842
movl $0x8, %esi
callq 0x7a70
movq %rax, 0x28(%rbx)
movslq 0x10(%rbx), %rcx
leal 0x1(%rcx), %edx
movl %edx, 0x10(%rbx)
movq %r13, (%rax,%rcx,8)
movl 0xc4(%rbx), %eax
movl 0xc4(%r14), %ecx
cmpl %ecx, %eax
jge 0x1a86d
movl %ecx, 0xc4(%rbx)
movl %ecx, %eax
movslq 0x110(%r13), %rcx
movslq 0xc0(%r14), %rdx
addq %rcx, %rdx
movslq %eax, %rsi
leaq (%rsi,%rdx), %rcx
decq %rcx
movq %rcx, %rax
cqto
idivq %rsi
subl %edx, %ecx
movl %ecx, 0xc0(%rbx)
movq %r13, %rax
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq