// File: /workspace/github_c_makefile_star2_O2/kyz[P]amostools/./amoslib.c
// Function: AMOS_find_slot
int AMOS_find_slot(uint8_t *src, size_t len) {
    uint8_t *p, *end;
    uint32_t tkoff, codeoff, titleoff;
    int b = -1, w = -1, l = -1;

    if (len < 50) return -1;

    tkoff    = amos_leek(&src[32]) + 32 + 18;
    codeoff  = amos_leek(&src[36]) + tkoff;
    titleoff = amos_leek(&src[40]) + codeoff;
    if (amos_leek(&src[32 + 18]) == 0x41503230) {
        tkoff += 4;
        codeoff += 4;
        titleoff += 4;
    }
    if (codeoff > len || titleoff > len) return -1;

    /* scan init code for MOVE #slot-1,D0 before RTS.
     * This works on all extensions I can find, except:
     * - Dump.Lib V1.1 (has an RTS just before the desired MOVEQ #n,D0 / RTS)
     * - AMOSPro_TURBO_Plus.Lib V2.15 (complex startup code)
     * - Intuition.Lib / AMOSPro_Intuition.Lib V1.3/a/b (complex startup code)
     */
    for (p = &src[codeoff], end = &src[titleoff]; p+2 < end; p += 2) {
        uint32_t c = amos_deek(p);
        if (c == 0x4E75) { /* stop at first RTS */
            break;
        }
        else if (c >= 0x7000 && c <= (0x7000 | AMOS_EXTENSION_SLOTS)) {
            b = (c & 0xFF) + 1; /* MOVEQ #slot-1,D0 */
        }
        else if (c == 0x303C && p+4 < end) {
            c = amos_deek(&p[2]); /* MOVE.W #slot-1,D0 */
            if (c <= AMOS_EXTENSION_SLOTS) w = c + 1;
        }
        else if (c == 0x203C && p+6 < end) {
            c = amos_leek(&p[2]); /* MOVE.L #slot-1,D0 */
            if (c <= AMOS_EXTENSION_SLOTS) l = c + 1;
        }
    }

    /* Prefer MOVEQ (most extensions) over MOVE.L (needed only for TOME
     * and CTEXT) and MOVE.W (needed only for SLN) */
    return (b != -1) ? b : (l != -1) ? l : w;
}

// Assembly:
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpq $0x32, %rsi
jb 0x20b4
movl 0x20(%rdi), %ecx
movl 0x24(%rdi), %r9d
bswapl %ecx
bswapl %r9d
leal (%rcx,%r9), %edx
addl $0x32, %edx
movl 0x32(%rdi), %r8d
bswapl %r8d
cmpl $0x41503230, %r8d       # imm = 0x41503230
leal 0x36(%rcx,%r9), %ecx
cmovnel %edx, %ecx
cmpq %rsi, %rcx
ja 0x20b4
movl 0x28(%rdi), %r9d
bswapl %r9d
leal (%r9,%rdx), %r10d
cmpl $0x41503230, %r8d       # imm = 0x41503230
leal 0x4(%r9,%rdx), %edx
cmovnel %r10d, %edx
cmpq %rsi, %rdx
ja 0x20b4
pushq %rbx
addq %rdi, %rcx
addq %rdi, %rdx
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
movl %esi, %eax
movl %esi, %edi
movq %rcx, %r8
addq $0x2, %rcx
cmpq %rdx, %rcx
jae 0x20a7
movzbl (%r8), %r11d
movl %r11d, %r9d
shll $0x8, %r9d
movzbl 0x1(%r8), %r10d
orl %r10d, %r9d
cmpl $0x4e75, %r9d           # imm = 0x4E75
je 0x20a7
cmpb $0x70, %r11b
setb %r11b
cmpl $0x701a, %r9d           # imm = 0x701A
setae %bl
orb %r11b, %bl
je 0x207c
cmpl $0x303c, %r9d           # imm = 0x303C
jne 0x2056
leaq 0x4(%r8), %r10
cmpq %rdx, %r10
jb 0x2087
cmpl $0x203c, %r9d           # imm = 0x203C
jne 0x2001
addq $0x6, %r8
cmpq %rdx, %r8
jae 0x2001
movl (%rcx), %r8d
bswapl %r8d
leal 0x1(%r8), %r9d
cmpl $0x1a, %r8d
cmovbl %r9d, %edi
jmp 0x2001
incl %r10d
movl %r10d, %esi
jmp 0x2001
movzwl 0x2(%r8), %r8d
rolw $0x8, %r8w
movzwl %r8w, %r8d
leal 0x1(%r8), %r9d
cmpw $0x1a, %r8w
cmovbl %r9d, %eax
jmp 0x2001
cmpl $-0x1, %edi
cmovnel %edi, %eax
cmpl $-0x1, %esi
cmovnel %esi, %eax
popq %rbx
retq