// File: /workspace/github_c_makefile_star2_O0/kyz[P]amostools/unlockamos.c
// Function: AMOS_decrypt_procedure
void AMOS_decrypt_procedure(uint8_t *src, size_t len) {
    uint8_t *line, *next, *endline;
    uint32_t key, key2, key3, size;

    /* do not operate on compiled procedures */
    if (len < 12 || src[10] & 0x10) return;

    size = amos_leek(&src[4]);
    if (len < (size + 8 + 6)) return;
    line = next = &src[src[0] * 2]; /* the line after PROCEDURE */
    endline = &src[size + 8 + 6]; /* the start of the line after END PROC */

    /* initialise keys */
    key = (size << 8) | src[11];
    key2 = 1;
    key3 = amos_deek(&src[8]);

    while (line < endline) {
        line = next; next = &line[line[0] * 2];
        if (!line[0]) return; /* avoid infinite loop on bad data */
        for (line += 4; line < next;) {
            *line++ ^= (key >> 8) & 0xFF;
            *line++ ^=  key       & 0xFF;
            key = (key & 0xFFFF0000) | ((key + key2) & 0xFFFF);
            key2 = (key2 + key3) & 0xFFFF;
            key = (key >> 1) | (key << 31); /* rotate right one bit */
        }
    }
    src[10] ^= 0x20; /* toggle "is encrypted" bit */
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
cmpq $0xc, -0x10(%rbp)
jb 0x13b3
movq -0x8(%rbp), %rax
movzbl 0xa(%rax), %eax
andl $0x10, %eax
cmpl $0x0, %eax
je 0x13b8
jmp 0x1547
movq -0x8(%rbp), %rax
movzbl 0x4(%rax), %eax
shll $0x18, %eax
movq -0x8(%rbp), %rcx
movzbl 0x5(%rcx), %ecx
shll $0x10, %ecx
orl %ecx, %eax
movq -0x8(%rbp), %rcx
movzbl 0x6(%rcx), %ecx
shll $0x8, %ecx
orl %ecx, %eax
movq -0x8(%rbp), %rcx
movzbl 0x7(%rcx), %ecx
orl %ecx, %eax
movl %eax, -0x38(%rbp)
movq -0x10(%rbp), %rax
movl -0x38(%rbp), %ecx
addl $0x8, %ecx
addl $0x6, %ecx
movl %ecx, %ecx
cmpq %rcx, %rax
jae 0x1403
jmp 0x1547
movq -0x8(%rbp), %rax
movq -0x8(%rbp), %rcx
movzbl (%rcx), %ecx
shll %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x20(%rbp)
movq %rax, -0x18(%rbp)
movq -0x8(%rbp), %rax
movl -0x38(%rbp), %ecx
addl $0x8, %ecx
addl $0x6, %ecx
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, -0x28(%rbp)
movl -0x38(%rbp), %eax
shll $0x8, %eax
movq -0x8(%rbp), %rcx
movzbl 0xb(%rcx), %ecx
orl %ecx, %eax
movl %eax, -0x2c(%rbp)
movl $0x1, -0x30(%rbp)
movq -0x8(%rbp), %rax
movzbl 0x8(%rax), %eax
shll $0x8, %eax
movq -0x8(%rbp), %rcx
movzbl 0x9(%rcx), %ecx
orl %ecx, %eax
movl %eax, -0x34(%rbp)
movq -0x18(%rbp), %rax
cmpq -0x28(%rbp), %rax
jae 0x1539
movq -0x20(%rbp), %rax
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rax
movq -0x18(%rbp), %rcx
movzbl (%rcx), %ecx
shll %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x20(%rbp)
movq -0x18(%rbp), %rax
cmpb $0x0, (%rax)
jne 0x14a1
jmp 0x1547
movq -0x18(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rax
cmpq -0x20(%rbp), %rax
jae 0x1534
movl -0x2c(%rbp), %edx
shrl $0x8, %edx
andl $0xff, %edx
movq -0x18(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x18(%rbp)
movzbl (%rax), %ecx
xorl %edx, %ecx
movb %cl, (%rax)
movl -0x2c(%rbp), %edx
andl $0xff, %edx
movq -0x18(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x18(%rbp)
movzbl (%rax), %ecx
xorl %edx, %ecx
movb %cl, (%rax)
movl -0x2c(%rbp), %eax
andl $0xffff0000, %eax       # imm = 0xFFFF0000
movl -0x2c(%rbp), %ecx
addl -0x30(%rbp), %ecx
andl $0xffff, %ecx           # imm = 0xFFFF
orl %ecx, %eax
movl %eax, -0x2c(%rbp)
movl -0x30(%rbp), %eax
addl -0x34(%rbp), %eax
andl $0xffff, %eax           # imm = 0xFFFF
movl %eax, -0x30(%rbp)
movl -0x2c(%rbp), %eax
shrl %eax
movl -0x2c(%rbp), %ecx
shll $0x1f, %ecx
orl %ecx, %eax
movl %eax, -0x2c(%rbp)
jmp 0x14ad
jmp 0x1466
movq -0x8(%rbp), %rax
movzbl 0xa(%rax), %ecx
xorl $0x20, %ecx
movb %cl, 0xa(%rax)
popq %rbp
retq
nopl (%rax)