// File: /workspace/github_c_makefile_star2_O1/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 %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl $0x1, %ebx
cmpq $0x36, %rsi
jb 0x2155
movl (%rdi), %eax
bswapl %eax
cmpl $0x3f3, %eax            # imm = 0x3F3
jne 0x2155
movl 0x18(%rdi), %eax
bswapl %eax
cmpl $0x3e9, %eax            # imm = 0x3E9
jne 0x2155
movq %rsi, %r14
movl 0x20(%rdi), %eax
movl 0x32(%rdi), %r9d
bswapl %eax
bswapl %r9d
xorl %esi, %esi
cmpl $0x41503230, %r9d       # imm = 0x41503230
sete %sil
leal (%rax,%rsi,4), %esi
addl $0x32, %esi
cmpq %r14, %rsi
ja 0x2155
movq %rsi, %rax
addq %rdi, %r14
addl %eax, %ecx
addq %rdi, %rcx
shll $0x10, %edx
addq %rdi, %rax
movq %rax, 0x20(%rsp)
xorl %r11d, %r11d
movq %r8, 0x18(%rsp)
movq %rdi, 0x10(%rsp)
leaq 0x2(%rcx), %rax
cmpq %r14, %rax
jae 0x2150
movzwl (%rcx), %eax
rolw $0x8, %ax
testw %ax, %ax
je 0x2017
leaq 0x4(%rcx), %rbp
movq %rbp, %rax
cmpq %r14, %rbp
jae 0x1fb8
movq %rbp, %rax
cmpb $0x0, (%rax)
js 0x1fb8
incq %rax
cmpq %r14, %rax
jb 0x1fab
incq %rax
movq %rax, %r13
cmpq %r14, %rax
jae 0x1fd5
movq %rax, %r13
cmpb $-0x4, (%r13)
ja 0x1fd5
incq %r13
cmpq %r14, %r13
jb 0x1fc6
leaq 0x1(%r13), %r8
movl %r8d, %esi
subl %edi, %esi
addq $0x2, %r13
testb $0x1, %sil
cmoveq %r8, %r13
movb (%rax), %sil
movzbl (%rbp), %eax
cmpl $0x21, %eax
je 0x2020
cmpl $0x80, %eax
jne 0x2032
testq %r11, %r11
je 0x2123
movb %sil, 0xb(%rsp)
movl %ebx, 0xc(%rsp)
movq %r11, %r12
movq %r11, %rbp
jmp 0x203e
xorl %eax, %eax
xorl %ebx, %ebx
jmp 0x2146
movb %sil, 0xb(%rsp)
movl %ebx, 0xc(%rsp)
leaq 0x5(%rcx), %rbp
movq %rbp, %r12
jmp 0x203e
movb %sil, 0xb(%rsp)
movl %ebx, 0xc(%rsp)
movq %r11, %r12
subl 0x20(%rsp), %ecx
movzwl %cx, %r15d
movl %edx, %ebx
orl %edx, %r15d
movl $0x11, %edi
cmpb $0x0, -0x11(%rbp,%rdi)
leaq 0x1(%rdi), %rdi
jns 0x2050
callq 0x11a0
testq %rax, %rax
je 0x2127
movl %r15d, %ecx
shrl %ecx
imulq $0x7e6150b, %rcx, %rdx  # imm = 0x7E6150B
shrq $0x28, %rdx
imull $0x2069, %edx, %edx     # imm = 0x2069
subl %edx, %ecx
movl %r15d, 0x8(%rax)
movq 0x18(%rsp), %rsi
movq (%rsi,%rcx,8), %rdx
movq %rdx, (%rax)
movq %rax, (%rsi,%rcx,8)
movb 0xb(%rsp), %cl
movb %cl, 0xc(%rax)
movq %rax, %rcx
addq $0xd, %rcx
movl %ebx, %edx
movq 0x10(%rsp), %rdi
movq %r12, %r11
movl 0xc(%rsp), %ebx
movb (%rbp), %r9b
incq %rbp
movl %r9d, %esi
andb $0x7f, %sil
leal -0x61(%rsi), %r10d
leal -0x20(%rsi), %r8d
cmpb $0x1a, %r10b
movzbl %sil, %esi
movzbl %r8b, %r8d
cmovael %esi, %r8d
movb %r8b, (%rcx)
incq %rcx
testb %r9b, %r9b
js 0x20f9
movb (%rbp), %r9b
incq %rbp
movb %r9b, (%rcx)
incq %rcx
cmpb $0x20, %r9b
jne 0x20dd
xorl %esi, %esi
jmp 0x20fe
movl $0x9, %esi
testl %esi, %esi
je 0x20b1
cmpl $0x9, %esi
jne 0x2139
movb -0x1(%rcx), %sil
andb $0x7f, %sil
movb %sil, -0x1(%rcx)
cmpb $0x20, %sil
jne 0x2120
decq %rcx
movb $0x49, 0xc(%rax)
movb $0x0, (%rcx)
movb $0x1, %al
jmp 0x2139
xorl %eax, %eax
movl %ebx, %edx
movq 0x10(%rsp), %rdi
movq %r12, %r11
movl 0xc(%rsp), %ebx
jmp 0x2139
testb $0x1, %al
movl $0x1, %ecx
cmovel %ecx, %ebx
movq %r13, %rcx
testb $0x1, %al
jne 0x1f83
jmp 0x2155
movl $0x1, %ebx
movl %ebx, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq