// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x150, %rsp            # imm = 0x150
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
je 0x2309f
movq -0x20(%rbp), %rax
movq %rax, -0x128(%rbp)
jmp 0x230ce
leaq -0x120(%rbp), %rdi
movq -0x10(%rbp), %rax
movl 0x10(%rax), %ecx
movl $0x100, %esi            # imm = 0x100
leaq 0x1b6d95(%rip), %rdx    # 0x1d9e4e
movb $0x0, %al
callq 0x3170
leaq -0x120(%rbp), %rax
movq %rax, -0x128(%rbp)
movq -0x10(%rbp), %rdi
movq -0x128(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x22ee0
cmpq $0x0, %rax
je 0x23111
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq (%rax), %rcx
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x20(%rax), %rdi
movq -0x128(%rbp), %rdx
leaq 0x1b5ca9(%rip), %rsi    # 0x1d8db6
movb $0x0, %al
callq *%rcx
movq -0x18(%rbp), %rax
cmpl $0x1, (%rax)
jne 0x23143
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq (%rax), %rcx
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x20(%rax), %rdi
movq -0x128(%rbp), %rdx
leaq 0x1b6d16(%rip), %rsi    # 0x1d9e55
movb $0x0, %al
callq *%rcx
movq -0x18(%rbp), %rax
cmpl $0x2, (%rax)
jne 0x23175
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq (%rax), %rcx
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x20(%rax), %rdi
movq -0x128(%rbp), %rdx
leaq 0x1b6d15(%rip), %rsi    # 0x1d9e86
movb $0x0, %al
callq *%rcx
movl $0x0, -0x12c(%rbp)
movq -0x10(%rbp), %rax
cmpl $0x0, 0x10(%rax)
jle 0x231d0
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movq -0x10(%rbp), %rcx
movl 0x10(%rcx), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x138(%rbp)
movq -0x138(%rbp), %rax
movl 0x110(%rax), %eax
movq -0x138(%rbp), %rcx
movq 0x108(%rcx), %rcx
addl 0xc0(%rcx), %eax
movl %eax, -0x12c(%rbp)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x21fe0
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
subl -0x12c(%rbp), %ecx
cmpl %ecx, %eax
jle 0x2320e
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq (%rax), %rcx
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x20(%rax), %rdi
leaq 0x1b6c99(%rip), %rsi    # 0x1d9ea3
movb $0x0, %al
callq *%rcx
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rdi
movl $0x118, %esi            # imm = 0x118
callq 0x4b20
movq %rax, -0x140(%rbp)
movq -0x140(%rbp), %rdi
movq -0x128(%rbp), %rsi
movl $0xff, %edx
callq 0x3080
movq -0x140(%rbp), %rax
movb $0x0, 0xff(%rax)
movq -0x128(%rbp), %rdi
callq 0x22fc0
movl %eax, %ecx
movq -0x140(%rbp), %rax
movl %ecx, 0x100(%rax)
movq -0x18(%rbp), %rcx
movq -0x140(%rbp), %rax
movq %rcx, 0x108(%rax)
movslq -0x12c(%rbp), %rax
movq %rax, -0x148(%rbp)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x220a0
movq -0x148(%rbp), %rdi
movslq %eax, %rsi
callq 0x233b0
movl %eax, %ecx
movq -0x140(%rbp), %rax
movl %ecx, 0x110(%rax)
movq -0x10(%rbp), %rax
cmpl $0x0, 0x10(%rax)
jle 0x232f0
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rsi
movq -0x10(%rbp), %rax
movl 0x10(%rax), %eax
addl $0x1, %eax
movslq %eax, %rdx
shlq $0x3, %rdx
callq 0x4e10
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x28(%rax)
jmp 0x2330d
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rdi
movl $0x8, %esi
callq 0x4b20
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x10(%rbp), %rax
movl 0x10(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x10(%rax)
movq -0x140(%rbp), %rdx
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movq -0x10(%rbp), %rcx
movl 0x10(%rcx), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x10(%rbp), %rax
movl 0xc4(%rax), %eax
movq -0x18(%rbp), %rcx
cmpl 0xc4(%rcx), %eax
jge 0x23364
movq -0x18(%rbp), %rax
movl 0xc4(%rax), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0xc4(%rax)
movq -0x140(%rbp), %rax
movl 0x110(%rax), %eax
movq -0x18(%rbp), %rcx
addl 0xc0(%rcx), %eax
movslq %eax, %rdi
movq -0x10(%rbp), %rax
movslq 0xc4(%rax), %rsi
callq 0x233b0
movl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0xc0(%rax)
movq -0x140(%rbp), %rax
addq $0x150, %rsp            # imm = 0x150
popq %rbp
retq
nopw (%rax,%rax)