// File: /workspace/github_c_makefile_star2_O0/tstword[P]MIPSAssemblerC/src/assembler.c
// Function: alloc_segment_memory
void alloc_segment_memory(size_t size) {
    segment_t segment = cfg_assembler->segment;
    offset_t next_offset = cfg_assembler->segment_offset[segment] - SEGMENT_OFFSET_BASE[segment] + size;

    if(next_offset > cfg_assembler->segment_memory_size[segment]) {
        /* Align to the nearest 1024 */
        uint32_t remainder = (uint32_t)size & 0x03FF;
        if(remainder != 0) size += 0x0400 - remainder;

        /* Reallocate memory */
        size_t mem_size = cfg_assembler->segment_memory_size[segment];
        void *realloc_ptr = (void *)realloc(cfg_assembler->segment_memory[segment], 
                                    cfg_assembler->segment_memory_size[segment] + size);

        if(realloc_ptr == NULL) {
            perror("CRITICAL ERROR: Failed to reallocate memory for segment: ");
            cfg_assembler->status = ASSEMBLER_STATUS_FAIL;
            return;
        }

        cfg_assembler->segment_memory_size[segment] += size;
        cfg_assembler->segment_memory[segment] = realloc_ptr;

        memset((char *)cfg_assembler->segment_memory[segment] + mem_size, 0, size);
    }

    if(next_offset > cfg_assembler->segment_memory_offset[segment]) {
        cfg_assembler->segment_memory_offset[segment] = next_offset;
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x8(%rbp)
movq 0xbfb5(%rip), %rax      # 0xf5a8
movb 0x45(%rax), %al
movb %al, -0x9(%rbp)
movq 0xbfa8(%rip), %rax      # 0xf5a8
movzbl -0x9(%rbp), %ecx
movl 0x48(%rax,%rcx,4), %eax
movzbl -0x9(%rbp), %ecx
movl %ecx, %edx
leaq 0x6e4b(%rip), %rcx      # 0xa460
subl (%rcx,%rdx,4), %eax
movl %eax, %eax
addq -0x8(%rbp), %rax
movl %eax, -0x10(%rbp)
movl -0x10(%rbp), %eax
movq 0xbf7d(%rip), %rcx      # 0xf5a8
movzbl -0x9(%rbp), %edx
cmpq 0x78(%rcx,%rdx,8), %rax
jbe 0x370b
movq -0x8(%rbp), %rax
andl $0x3ff, %eax            # imm = 0x3FF
movl %eax, -0x14(%rbp)
cmpl $0x0, -0x14(%rbp)
je 0x365e
movl $0x400, %eax            # imm = 0x400
subl -0x14(%rbp), %eax
movl %eax, %eax
addq -0x8(%rbp), %rax
movq %rax, -0x8(%rbp)
movq 0xbf43(%rip), %rax      # 0xf5a8
movzbl -0x9(%rbp), %ecx
movq 0x78(%rax,%rcx,8), %rax
movq %rax, -0x20(%rbp)
movq 0xbf2f(%rip), %rax      # 0xf5a8
movzbl -0x9(%rbp), %ecx
movq 0x20(%rax,%rcx,8), %rdi
movq 0xbf1f(%rip), %rax      # 0xf5a8
movzbl -0x9(%rbp), %ecx
movq 0x78(%rax,%rcx,8), %rsi
addq -0x8(%rbp), %rsi
callq 0x3100
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0x36bf
leaq 0x6e7c(%rip), %rdi      # 0xa529
callq 0x3120
movq 0xbeef(%rip), %rax      # 0xf5a8
movb $0x2, 0x44(%rax)
jmp 0x3735
movq -0x8(%rbp), %rdx
movq 0xbede(%rip), %rax      # 0xf5a8
movzbl -0x9(%rbp), %ecx
addq 0x78(%rax,%rcx,8), %rdx
movq %rdx, 0x78(%rax,%rcx,8)
movq -0x28(%rbp), %rdx
movq 0xbec5(%rip), %rax      # 0xf5a8
movzbl -0x9(%rbp), %ecx
movq %rdx, 0x20(%rax,%rcx,8)
movq 0xbeb5(%rip), %rax      # 0xf5a8
movzbl -0x9(%rbp), %ecx
movq 0x20(%rax,%rcx,8), %rdi
addq -0x20(%rbp), %rdi
movq -0x8(%rbp), %rdx
xorl %esi, %esi
callq 0x3070
movl -0x10(%rbp), %eax
movq 0xbe93(%rip), %rcx      # 0xf5a8
movzbl -0x9(%rbp), %edx
cmpq 0x58(%rcx,%rdx,8), %rax
jbe 0x3735
movl -0x10(%rbp), %eax
movl %eax, %edx
movq 0xbe7c(%rip), %rax      # 0xf5a8
movzbl -0x9(%rbp), %ecx
movq %rdx, 0x58(%rax,%rcx,8)
addq $0x30, %rsp
popq %rbp
retq
nopl (%rax,%rax)