// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movl %ecx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq -0x10(%rbp), %rax
addq -0x18(%rbp), %rax
movq %rax, -0x38(%rbp)
movq $0x0, -0x50(%rbp)
cmpq $0x36, -0x18(%rbp)
jb 0x27c0
movq -0x10(%rbp), %rax
movzbl (%rax), %eax
shll $0x18, %eax
movq -0x10(%rbp), %rcx
movzbl 0x1(%rcx), %ecx
shll $0x10, %ecx
orl %ecx, %eax
movq -0x10(%rbp), %rcx
movzbl 0x2(%rcx), %ecx
shll $0x8, %ecx
orl %ecx, %eax
movq -0x10(%rbp), %rcx
movzbl 0x3(%rcx), %ecx
orl %ecx, %eax
cmpl $0x3f3, %eax            # imm = 0x3F3
jne 0x27c0
movq -0x10(%rbp), %rax
movzbl 0x18(%rax), %eax
shll $0x18, %eax
movq -0x10(%rbp), %rcx
movzbl 0x19(%rcx), %ecx
shll $0x10, %ecx
orl %ecx, %eax
movq -0x10(%rbp), %rcx
movzbl 0x1a(%rcx), %ecx
shll $0x8, %ecx
orl %ecx, %eax
movq -0x10(%rbp), %rcx
movzbl 0x1b(%rcx), %ecx
orl %ecx, %eax
cmpl $0x3e9, %eax            # imm = 0x3E9
je 0x27cc
movl $0x1, -0x4(%rbp)
jmp 0x29c4
movq -0x10(%rbp), %rax
movzbl 0x20(%rax), %eax
shll $0x18, %eax
movq -0x10(%rbp), %rcx
movzbl 0x21(%rcx), %ecx
shll $0x10, %ecx
orl %ecx, %eax
movq -0x10(%rbp), %rcx
movzbl 0x22(%rcx), %ecx
shll $0x8, %ecx
orl %ecx, %eax
movq -0x10(%rbp), %rcx
movzbl 0x23(%rcx), %ecx
orl %ecx, %eax
addl $0x20, %eax
addl $0x12, %eax
movl %eax, -0x54(%rbp)
movq -0x10(%rbp), %rax
movzbl 0x32(%rax), %eax
shll $0x18, %eax
movq -0x10(%rbp), %rcx
movzbl 0x33(%rcx), %ecx
shll $0x10, %ecx
orl %ecx, %eax
movq -0x10(%rbp), %rcx
movzbl 0x34(%rcx), %ecx
shll $0x8, %ecx
orl %ecx, %eax
movq -0x10(%rbp), %rcx
movzbl 0x35(%rcx), %ecx
orl %ecx, %eax
cmpl $0x41503230, %eax       # imm = 0x41503230
jne 0x2843
movl -0x54(%rbp), %eax
addl $0x4, %eax
movl %eax, -0x54(%rbp)
movl -0x54(%rbp), %eax
cmpq -0x18(%rbp), %rax
jbe 0x2858
movl $0x1, -0x4(%rbp)
jmp 0x29c4
movq -0x10(%rbp), %rax
movl -0x54(%rbp), %ecx
addl -0x20(%rbp), %ecx
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
addq $0x2, %rax
cmpq -0x38(%rbp), %rax
jae 0x29bd
movl -0x1c(%rbp), %eax
shll $0x10, %eax
cltq
movq -0x30(%rbp), %rcx
movq -0x10(%rbp), %rdx
subq %rdx, %rcx
movl -0x54(%rbp), %edx
subq %rdx, %rcx
andq $0xffff, %rcx           # imm = 0xFFFF
orq %rcx, %rax
movl %eax, -0x58(%rbp)
movq -0x30(%rbp), %rax
movzbl (%rax), %eax
shll $0x8, %eax
movq -0x30(%rbp), %rcx
movzbl 0x1(%rcx), %ecx
orl %ecx, %eax
cmpl $0x0, %eax
jne 0x28c8
movl $0x0, -0x4(%rbp)
jmp 0x29c4
movq -0x30(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x30(%rbp), %rcx
xorl %eax, %eax
cmpq -0x38(%rbp), %rcx
movb %al, -0x59(%rbp)
jae 0x28fd
movq -0x30(%rbp), %rax
movzbl (%rax), %eax
cmpl $0x80, %eax
setl %al
movb %al, -0x59(%rbp)
movb -0x59(%rbp), %al
testb $0x1, %al
jne 0x2906
jmp 0x2914
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
jmp 0x28dc
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x30(%rbp), %rcx
xorl %eax, %eax
cmpq -0x38(%rbp), %rcx
movb %al, -0x5a(%rbp)
jae 0x2949
movq -0x30(%rbp), %rax
movzbl (%rax), %eax
cmpl $0xfd, %eax
setl %al
movb %al, -0x5a(%rbp)
movb -0x5a(%rbp), %al
testb $0x1, %al
jne 0x2952
jmp 0x2960
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
jmp 0x2928
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movq -0x10(%rbp), %rcx
subq %rcx, %rax
andq $0x1, %rax
cmpq $0x0, %rax
je 0x298d
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
movl -0x58(%rbp), %edi
movq -0x40(%rbp), %rsi
movq -0x48(%rbp), %rax
movb (%rax), %al
movq -0x28(%rbp), %rcx
leaq -0x50(%rbp), %r8
movsbl %al, %edx
callq 0x29d0
cmpl $0x0, %eax
je 0x29b8
movl $0x1, -0x4(%rbp)
jmp 0x29c4
jmp 0x286b
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nopl (%rax)