// File: /workspace/github_c_makefile_star2_O3/kyz[P]amostools/./amoslib.c
// Function: AMOS_parse_extension
int AMOS_parse_extension(uint8_t *src, size_t len, int slot, int start,
                         struct AMOS_token *table[AMOS_TOKEN_TABLE_SIZE])
{
    uint8_t *p, *end = &src[len], *pname, *ptype, *last_name = NULL;
    uint32_t tkoff;

    /* Extension format is an Amiga hunk file with a single code hunk */
    if (len < 54 || amos_leek(src) != 0x3f3 || amos_leek(&src[24]) != 0x3e9) {
        return 1;
    }

    /* the first bytes in the code hunk are a header: 4 longwords and 1 word
     * The four longwords are the sizes of the 4 sections that follow.
     * AMOSPro 2.0 appends 4 bytes to the header, the text "AP20" */
    tkoff = amos_leek(&src[32]) + 32 + 18;
    if (amos_leek(&src[32 + 18]) == 0x41503230) tkoff += 4;
    if (tkoff > len) return 1;

    /* each token has this format:
     * 2 byte "instruction" code pointer (0 marks end of table)
     * 2 byte "function" code pointer
     * instruction name (ASCII chars, final char has high bit set)
     * instruction parameters (ASCII ended by 0xFF, 0xFE or 0xFD)
     * 0/1 byte: realign to word boundary if needed
     */
    for (p = &src[tkoff + start]; (p+2) < end;) {
        /* unique key is slot and 16-bit offset within table */
        uint32_t key = (slot << 16) | (((p - src) - tkoff) & 0xFFFF);
        if (!amos_deek(p)) return 0; /* success: reached end of list */
        p += 4;
        pname = p; while (p < end && *p < 0x80) p++; p++;
        ptype = p; while (p < end && *p < 0xFD) p++; p++;
        if ((p - src) & 1) p++; /* re-align to word boundary */

        /* add entry to token table */
        if (add_token(key, pname, *ptype, table, &last_name)) return 1;
    }
    return 1; /* failure: ran out of list before end */
}

// Assembly:
movl $0x1, %eax
cmpq $0x36, %rsi
jb 0x2108
movl %edx, %r10d
movl (%rdi), %edx
bswapl %edx
cmpl $0x3f3, %edx            # imm = 0x3F3
jne 0x2108
movl 0x18(%rdi), %edx
bswapl %edx
cmpl $0x3e9, %edx            # imm = 0x3E9
jne 0x2108
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rsi, %r15
movl 0x20(%rdi), %r9d
movl 0x32(%rdi), %edx
bswapl %r9d
bswapl %edx
xorl %esi, %esi
cmpl $0x41503230, %edx       # imm = 0x41503230
sete %sil
leal (%r9,%rsi,4), %ebx
addl $0x32, %ebx
cmpq %r15, %rbx
ja 0x20fa
addq %rdi, %r15
addl %ebx, %ecx
leaq (%rdi,%rcx), %rdx
addq $0x2, %rdx
cmpq %r15, %rdx
jae 0x20fa
addq %rdi, %rcx
shll $0x10, %r10d
addq %rdi, %rbx
movq $0x0, 0x10(%rsp)
movq %rdi, 0x20(%rsp)
movq %rbx, 0x18(%rsp)
movzwl (%rcx), %edx
rolw $0x8, %dx
testw %dx, %dx
je 0x20f1
leaq 0x4(%rcx), %rbp
movq %rbp, %r9
cmpq %r15, %rbp
jae 0x1fa4
movq %rbp, %r9
cmpb $0x0, (%r9)
js 0x1fa4
incq %r9
cmpq %r15, %r9
jb 0x1f96
incq %r9
movq %r9, %r13
cmpq %r15, %r9
jae 0x1fc1
movq %r9, %r13
cmpb $-0x4, (%r13)
ja 0x1fc1
incq %r13
cmpq %r15, %r13
jb 0x1fb2
leaq 0x1(%r13), %rdx
movl %edx, %esi
subl %edi, %esi
addq $0x2, %r13
testb $0x1, %sil
cmoveq %rdx, %r13
movb (%r9), %sil
movzbl (%rbp), %edx
cmpl $0x21, %edx
je 0x2004
cmpl $0x80, %edx
jne 0x200d
movq 0x10(%rsp), %rdx
movq %rdx, %rbp
testq %rdx, %rdx
jne 0x200d
movq $0x0, 0x10(%rsp)
jmp 0x20df
leaq 0x5(%rcx), %rbp
movq %rbp, 0x10(%rsp)
movb %sil, 0xf(%rsp)
movq %r8, %r14
subl %ebx, %ecx
movzwl %cx, %ebx
movl %r10d, %r12d
orl %r10d, %ebx
movl $0x11, %edi
cmpb $0x0, -0x11(%rbp,%rdi)
leaq 0x1(%rdi), %rdi
jns 0x2025
callq 0x1190
testq %rax, %rax
je 0x20f5
movl %ebx, %ecx
shrl %ecx
imulq $0x7e6150b, %rcx, %rdx  # imm = 0x7E6150B
shrq $0x28, %rdx
imull $0x2069, %edx, %edx     # imm = 0x2069
subl %edx, %ecx
movl %ebx, 0x8(%rax)
movq %r14, %r8
movq (%r14,%rcx,8), %rdx
movq %rdx, (%rax)
movq %rax, (%r14,%rcx,8)
movb 0xf(%rsp), %cl
movb %cl, 0xc(%rax)
movq %rax, %rcx
addq $0xd, %rcx
movl %r12d, %r10d
movq 0x20(%rsp), %rdi
movq 0x18(%rsp), %rbx
movb (%rbp), %dl
incq %rbp
movl %edx, %esi
andb $0x7f, %sil
leal -0x61(%rsi), %r11d
leal -0x20(%rsi), %r9d
cmpb $0x1a, %r11b
movzbl %sil, %esi
movzbl %r9b, %r9d
cmovael %esi, %r9d
movb %r9b, (%rcx)
testb %dl, %dl
js 0x20c2
movb (%rbp), %dl
incq %rbp
movb %dl, 0x1(%rcx)
incq %rcx
cmpb $0x20, %dl
jne 0x20a8
incq %rcx
jmp 0x2081
movb (%rcx), %dl
andb $0x7f, %dl
movb %dl, (%rcx)
cmpb $0x20, %dl
jne 0x20d4
movb $0x49, 0xc(%rax)
jmp 0x20d7
incq %rcx
movl $0x1, %eax
movb $0x0, (%rcx)
leaq 0x2(%r13), %rdx
movq %r13, %rcx
cmpq %r15, %rdx
jb 0x1f77
jmp 0x20fa
xorl %eax, %eax
jmp 0x20fa
movl $0x1, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq