// File: /workspace/github_c_makefile_star2_O0/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:
pushq %rbp
movq %rsp, %rbp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl $0xffffffff, -0x38(%rbp) # imm = 0xFFFFFFFF
movl $0xffffffff, -0x3c(%rbp) # imm = 0xFFFFFFFF
movl $0xffffffff, -0x40(%rbp) # imm = 0xFFFFFFFF
cmpq $0x32, -0x18(%rbp)
jae 0x2c04
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x2e6e
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, -0x2c(%rbp)
movq -0x10(%rbp), %rax
movzbl 0x24(%rax), %eax
shll $0x18, %eax
movq -0x10(%rbp), %rcx
movzbl 0x25(%rcx), %ecx
shll $0x10, %ecx
orl %ecx, %eax
movq -0x10(%rbp), %rcx
movzbl 0x26(%rcx), %ecx
shll $0x8, %ecx
orl %ecx, %eax
movq -0x10(%rbp), %rcx
movzbl 0x27(%rcx), %ecx
orl %ecx, %eax
addl -0x2c(%rbp), %eax
movl %eax, -0x30(%rbp)
movq -0x10(%rbp), %rax
movzbl 0x28(%rax), %eax
shll $0x18, %eax
movq -0x10(%rbp), %rcx
movzbl 0x29(%rcx), %ecx
shll $0x10, %ecx
orl %ecx, %eax
movq -0x10(%rbp), %rcx
movzbl 0x2a(%rcx), %ecx
shll $0x8, %ecx
orl %ecx, %eax
movq -0x10(%rbp), %rcx
movzbl 0x2b(%rcx), %ecx
orl %ecx, %eax
addl -0x30(%rbp), %eax
movl %eax, -0x34(%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 0x2cf7
movl -0x2c(%rbp), %eax
addl $0x4, %eax
movl %eax, -0x2c(%rbp)
movl -0x30(%rbp), %eax
addl $0x4, %eax
movl %eax, -0x30(%rbp)
movl -0x34(%rbp), %eax
addl $0x4, %eax
movl %eax, -0x34(%rbp)
movl -0x30(%rbp), %eax
cmpq -0x18(%rbp), %rax
ja 0x2d09
movl -0x34(%rbp), %eax
cmpq -0x18(%rbp), %rax
jbe 0x2d15
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x2e6e
movq -0x10(%rbp), %rax
movl -0x30(%rbp), %ecx
addq %rcx, %rax
movq %rax, -0x20(%rbp)
movq -0x10(%rbp), %rax
movl -0x34(%rbp), %ecx
addq %rcx, %rax
movq %rax, -0x28(%rbp)
movq -0x20(%rbp), %rax
addq $0x2, %rax
cmpq -0x28(%rbp), %rax
jae 0x2e40
movq -0x20(%rbp), %rax
movzbl (%rax), %eax
shll $0x8, %eax
movq -0x20(%rbp), %rcx
movzbl 0x1(%rcx), %ecx
orl %ecx, %eax
movl %eax, -0x44(%rbp)
cmpl $0x4e75, -0x44(%rbp)    # imm = 0x4E75
jne 0x2d68
jmp 0x2e40
cmpl $0x7000, -0x44(%rbp)    # imm = 0x7000
jb 0x2d8d
cmpl $0x7019, -0x44(%rbp)    # imm = 0x7019
ja 0x2d8d
movl -0x44(%rbp), %eax
andl $0xff, %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x2e2b
cmpl $0x303c, -0x44(%rbp)    # imm = 0x303C
jne 0x2dcd
movq -0x20(%rbp), %rax
addq $0x4, %rax
cmpq -0x28(%rbp), %rax
jae 0x2dcd
movq -0x20(%rbp), %rax
movzbl 0x2(%rax), %eax
shll $0x8, %eax
movq -0x20(%rbp), %rcx
movzbl 0x3(%rcx), %ecx
orl %ecx, %eax
movl %eax, -0x44(%rbp)
cmpl $0x19, -0x44(%rbp)
ja 0x2dcb
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x2e29
cmpl $0x203c, -0x44(%rbp)    # imm = 0x203C
jne 0x2e27
movq -0x20(%rbp), %rax
addq $0x6, %rax
cmpq -0x28(%rbp), %rax
jae 0x2e27
movq -0x20(%rbp), %rax
movzbl 0x2(%rax), %eax
shll $0x18, %eax
movq -0x20(%rbp), %rcx
movzbl 0x3(%rcx), %ecx
shll $0x10, %ecx
orl %ecx, %eax
movq -0x20(%rbp), %rcx
movzbl 0x4(%rcx), %ecx
shll $0x8, %ecx
orl %ecx, %eax
movq -0x20(%rbp), %rcx
movzbl 0x5(%rcx), %ecx
orl %ecx, %eax
movl %eax, -0x44(%rbp)
cmpl $0x19, -0x44(%rbp)
ja 0x2e25
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0x2e27
jmp 0x2e29
jmp 0x2e2b
jmp 0x2e2d
jmp 0x2e2f
movq -0x20(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x20(%rbp)
jmp 0x2d31
cmpl $-0x1, -0x38(%rbp)
je 0x2e4e
movl -0x38(%rbp), %eax
movl %eax, -0x48(%rbp)
jmp 0x2e68
cmpl $-0x1, -0x40(%rbp)
je 0x2e5c
movl -0x40(%rbp), %eax
movl %eax, -0x4c(%rbp)
jmp 0x2e62
movl -0x3c(%rbp), %eax
movl %eax, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
movl %eax, -0x48(%rbp)
movl -0x48(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
popq %rbp
retq
nopw %cs:(%rax,%rax)