// File: /workspace/github_c_makefile_star2_O2/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:
pushq $0x1
popq %rax
cmpq $0x36, %rsi
jb 0x1f8b
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rsi, %r15
movl (%rdi), %esi
bswapl %esi
cmpl $0x3f3, %esi            # imm = 0x3F3
jne 0x1f7d
movl 0x18(%rdi), %esi
bswapl %esi
cmpl $0x3e9, %esi            # imm = 0x3E9
jne 0x1f7d
movl 0x20(%rdi), %r9d
movl 0x32(%rdi), %r10d
bswapl %r9d
bswapl %r10d
xorl %esi, %esi
cmpl $0x41503230, %r10d      # imm = 0x41503230
sete %sil
leal (%r9,%rsi,4), %r9d
addl $0x32, %r9d
cmpq %r15, %r9
ja 0x1f7d
movq %r8, %rbp
movl %edx, %r14d
addq %rdi, %r15
addl %r9d, %ecx
addq %rdi, %rcx
shll $0x10, %r14d
addq %rdi, %r9
pushq $0x6
popq %rax
subq %rdi, %rax
movq %rax, 0x20(%rsp)
xorl %eax, %eax
movq %r9, 0x8(%rsp)
movq %rax, %r10
leaq 0x2(%rcx), %rax
cmpq %r15, %rax
jae 0x1f76
movzwl (%rcx), %eax
rolw $0x8, %ax
testw %ax, %ax
je 0x1f7b
movl %ecx, %eax
subl %r9d, %eax
movzwl %ax, %ebx
orl %r14d, %ebx
leaq 0x4(%rcx), %r13
leaq 0x5(%rcx), %rax
movq 0x20(%rsp), %rsi
leaq (%rsi,%rcx), %rdi
leaq -0x1(%rax), %rsi
cmpq %r15, %rsi
jae 0x1e5b
cmpb $0x0, (%rsi)
js 0x1e5b
incq %rax
incq %rdi
jmp 0x1e45
movq %rax, %rsi
cmpq %r15, %rsi
jae 0x1e70
cmpb $-0x4, (%rsi)
ja 0x1e70
incq %rsi
incq %rdi
jmp 0x1e5e
andl $0x1, %edi
addq %rdi, %rsi
incq %rsi
movb (%rax), %r12b
movzbl (%r13), %eax
cmpl $0x21, %eax
je 0x1ea2
cmpl $0x80, %eax
jne 0x1eac
movl $0x0, %eax
movq %rsi, %rcx
movq %r10, %r13
testq %r10, %r10
jne 0x1eac
jmp 0x1e09
addq $0x5, %rcx
movq %rcx, %r10
movq %rcx, %r13
movq %r10, 0x10(%rsp)
movq %rsi, 0x18(%rsp)
pushq $0x11
popq %rdi
cmpb $0x0, -0x11(%r13,%rdi)
leaq 0x1(%rdi), %rdi
jns 0x1eb9
callq 0x1180
testq %rax, %rax
je 0x1f76
movq %rax, %rcx
movl %ebx, %eax
shrl %eax
xorl %edx, %edx
movl $0x2069, %esi           # imm = 0x2069
divl %esi
movl %ebx, 0x8(%rcx)
movq (%rbp,%rdx,8), %rax
movq %rax, (%rcx)
movq %rcx, (%rbp,%rdx,8)
movb %r12b, 0xc(%rcx)
movq %rcx, %rax
addq $0xd, %rax
movb (%r13), %r10b
incq %r13
movl %r10d, %esi
andb $0x7f, %sil
leal -0x61(%rsi), %edi
leal -0x20(%rsi), %r9d
cmpb $0x1a, %dil
movzbl %sil, %esi
movzbl %r9b, %edi
cmovael %esi, %edi
movb %dil, (%rax)
testb %r10b, %r10b
js 0x1f43
movb (%r13), %r10b
incq %r13
movb %r10b, 0x1(%rax)
incq %rax
cmpb $0x20, %r10b
jne 0x1f25
incq %rax
jmp 0x1efe
movb (%rax), %sil
andb $0x7f, %sil
movb %sil, (%rax)
cmpb $0x20, %sil
jne 0x1f59
movb $0x49, 0xc(%rcx)
jmp 0x1f5c
incq %rax
movq 0x8(%rsp), %r9
movq 0x18(%rsp), %rcx
movq 0x10(%rsp), %rsi
movb $0x0, (%rax)
movq %rsi, %rax
jmp 0x1e09
pushq $0x1
popq %rax
jmp 0x1f7d
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq