// File: /workspace/github_c_makefile_star2_O0/kyz[P]amostools/unlockamos.c
// Function: unlock_source
int unlock_source(uint8_t *src, size_t len) {
    int locked_procs_found = 0;
    uint32_t x;

    /* go through the lines of source code */
    len = amos_leek(&src[16]);
    for (src += 20; len > 0; src += x, len -= x) {
        x = src[0] * 2;
        if (x > len) {
            printf("line length error\n");
            return 0;
        }

        /* does this a line begin with "PROCEDURE" ? */
        if (amos_deek(&src[2]) == 0x0376) {
            /* decrypt and remove the "is encrypted" flag */
            if (src[10] & 0x20) {
                if ((size_t)(amos_leek(&src[4]) + 8 + 6) < len) {
                    AMOS_decrypt_procedure(src, len);
                    locked_procs_found++;
                }
                else {
                    printf("WARNING: locked procedure with bad length found\n");
                }
            }
            /* always remove "is locked" flag */
            if (src[10] & 0x40) {
                src[10] ^= 0x040;
                locked_procs_found++;
            }
        }
    }
    return locked_procs_found;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x20, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl $0x0, -0x1c(%rbp)
movq -0x10(%rbp), %rax
movzbl 0x10(%rax), %eax
shll $0x18, %eax
movq -0x10(%rbp), %rcx
movzbl 0x11(%rcx), %ecx
shll $0x10, %ecx
orl %ecx, %eax
movq -0x10(%rbp), %rcx
movzbl 0x12(%rcx), %ecx
shll $0x8, %ecx
orl %ecx, %eax
movq -0x10(%rbp), %rcx
movzbl 0x13(%rcx), %ecx
orl %ecx, %eax
cltq
movq %rax, -0x18(%rbp)
movq -0x10(%rbp), %rax
addq $0x14, %rax
movq %rax, -0x10(%rbp)
cmpq $0x0, -0x18(%rbp)
jbe 0x16c7
movq -0x10(%rbp), %rax
movzbl (%rax), %eax
shll %eax
movl %eax, -0x20(%rbp)
movl -0x20(%rbp), %eax
cmpq -0x18(%rbp), %rax
jbe 0x15e2
leaq 0xa3b(%rip), %rdi       # 0x200a
movb $0x0, %al
callq 0x1070
movl $0x0, -0x4(%rbp)
jmp 0x16cd
movq -0x10(%rbp), %rax
movzbl 0x2(%rax), %eax
shll $0x8, %eax
movq -0x10(%rbp), %rcx
movzbl 0x3(%rcx), %ecx
orl %ecx, %eax
cmpl $0x376, %eax            # imm = 0x376
jne 0x16a0
movq -0x10(%rbp), %rax
movzbl 0xa(%rax), %eax
andl $0x20, %eax
cmpl $0x0, %eax
je 0x1677
movq -0x10(%rbp), %rax
movzbl 0x4(%rax), %eax
shll $0x18, %eax
movq -0x10(%rbp), %rcx
movzbl 0x5(%rcx), %ecx
shll $0x10, %ecx
orl %ecx, %eax
movq -0x10(%rbp), %rcx
movzbl 0x6(%rcx), %ecx
shll $0x8, %ecx
orl %ecx, %eax
movq -0x10(%rbp), %rcx
movzbl 0x7(%rcx), %ecx
orl %ecx, %eax
addl $0x8, %eax
addl $0x6, %eax
cltq
cmpq -0x18(%rbp), %rax
jae 0x1667
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x1390
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x1675
leaq 0x9af(%rip), %rdi       # 0x201d
movb $0x0, %al
callq 0x1070
jmp 0x1677
movq -0x10(%rbp), %rax
movzbl 0xa(%rax), %eax
andl $0x40, %eax
cmpl $0x0, %eax
je 0x169e
movq -0x10(%rbp), %rax
movzbl 0xa(%rax), %ecx
xorl $0x40, %ecx
movb %cl, 0xa(%rax)
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x16a0
jmp 0x16a2
movl -0x20(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, -0x10(%rbp)
movl -0x20(%rbp), %eax
movl %eax, %ecx
movq -0x18(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x18(%rbp)
jmp 0x15a8
movl -0x1c(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x20, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)