// File: /workspace/github_c_makefile_star2_O3/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 0x2240
movl 0x20(%rdi), %edx
movl 0x24(%rdi), %r9d
bswapl %edx
bswapl %r9d
leal (%rdx,%r9), %ecx
addl $0x32, %ecx
movl 0x32(%rdi), %r8d
bswapl %r8d
cmpl $0x41503230, %r8d       # imm = 0x41503230
leal 0x36(%rdx,%r9), %edx
cmovnel %ecx, %edx
cmpq %rsi, %rdx
ja 0x2240
movl 0x28(%rdi), %r9d
bswapl %r9d
leal (%r9,%rcx), %r10d
cmpl $0x41503230, %r8d       # imm = 0x41503230
leal 0x4(%r9,%rcx), %ecx
cmovnel %r10d, %ecx
cmpq %rsi, %rcx
ja 0x2240
addq %rdi, %rcx
leaq (%rdi,%rdx), %rsi
addq $0x2, %rsi
cmpq %rcx, %rsi
jae 0x2240
addq %rdx, %rdi
addq $0x4, %rdi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
movzbl -0x4(%rdi), %r10d
movl %r10d, %r8d
shll $0x8, %r8d
movzbl -0x3(%rdi), %r9d
orl %r9d, %r8d
cmpl $0x4e75, %r8d           # imm = 0x4E75
je 0x2234
cmpb $0x70, %r10b
setb %r10b
cmpl $0x701a, %r8d           # imm = 0x701A
setae %r11b
orb %r10b, %r11b
jne 0x21d4
incl %r9d
movl %r9d, %esi
jmp 0x2224
cmpl $0x303c, %r8d           # imm = 0x303C
jne 0x21ff
cmpq %rcx, %rdi
jae 0x21ff
movzwl -0x2(%rdi), %r8d
rolw $0x8, %r8w
movzwl %r8w, %r8d
leal 0x1(%r8), %r9d
cmpw $0x1a, %r8w
cmovbl %r9d, %eax
jmp 0x2224
cmpl $0x203c, %r8d           # imm = 0x203C
jne 0x2224
leaq 0x2(%rdi), %r8
cmpq %rcx, %r8
jae 0x2224
movl -0x2(%rdi), %r8d
bswapl %r8d
leal 0x1(%r8), %r9d
cmpl $0x1a, %r8d
cmovbl %r9d, %edx
leaq 0x2(%rdi), %r8
cmpq %rcx, %rdi
movq %r8, %rdi
jb 0x2193
cmpl $-0x1, %edx
cmovnel %edx, %eax
cmpl $-0x1, %esi
cmovnel %esi, %eax
retq