// File: /workspace/github_c_makefile_star2_O1/Seclabs-htc[P]slip39/slip39.c
// Function: decrypt
static int _decrypt(uint8_t *ems, int emsl, uint8_t *ms, int msl,
        uint8_t *pp, int ppl, uint8_t ie, uint16_t id)
{
    int j, hl = emsl / 2;
    uint8_t l[hl], r[hl];
    uint8_t _r[hl], f[hl];
    int csl = strlen(CUSTOMIZATION_STRING);
    uint8_t salt[hl+csl+2];
    uint8_t pass[ppl+1];
    int i;
    uint32_t it;

    if (msl != emsl)
        return -1;
    if (emsl & 1)
        return -2;

    memscpy(l, sizeof(l), ems, hl);
    memscpy(r, sizeof(r), ems + hl, hl);

    // salt
    memscpy(salt, sizeof(salt), CUSTOMIZATION_STRING, csl);
    salt[csl] = id >> 8;
    salt[csl+1] = id & 0xff;

    // pass
    memscpy(pass+1, sizeof(pass)-1 , pp, ppl);

    // iterations
    it = (BASE_ITERATION_COUNT << ie) / ROUND_COUNT;

    for (i = ROUND_COUNT - 1; i >= 0; i--)
    {
        // salt
        memscpy(salt+8, sizeof(salt)-8, r, hl);
        // pass
        pass[0] = i;
        // PBKDF2
        pbkdf2_hmac_sha256(pass, sizeof(pass), salt, sizeof(salt), it, f, sizeof(f));

        for (j = 0; j < hl; j++)
            _r[j] = l[j] ^ f[j];

        memscpy(l, sizeof(l), r, hl);
        memscpy(r, sizeof(r), _r, hl);
    }

    memscpy(ms, msl, r, hl);
    memscpy(ms + hl, msl - hl, l, hl);

    memzero(pass, sizeof(pass));
    memzero(salt, sizeof(salt));
    memzero(l, sizeof(l));
    memzero(r, sizeof(r));
    memzero(_r, sizeof(_r));

    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movl %r9d, %r15d
movq %r8, -0x40(%rbp)
movl %esi, %ebx
shrl %ebx
movq %rsp, %r14
leal 0xf(%rbx), %eax
andl $-0x10, %eax
subq %rax, %r14
movq %r14, %rsp
movq %rsp, %r8
subq %rax, %r8
movq %r8, %rsp
movq %rsp, %r12
subq %rax, %r12
movq %r12, %rsp
movq %rsp, %r13
subq %rax, %r13
movq %r13, %rsp
movq %rsp, %r11
leal 0x17(%rbx), %eax
andl $-0x10, %eax
subq %rax, %r11
movq %r11, %rsp
leal 0x1(%r15), %r9d
movq %rsp, %r10
leaq 0xf(%r9), %rax
andq $-0x10, %rax
subq %rax, %r10
movq %r10, %rsp
movl %ecx, -0x64(%rbp)
cmpl %esi, %ecx
setne %al
movl %esi, -0x68(%rbp)
movl %esi, %ecx
orb %al, %cl
testb $0x1, %cl
jne 0xb58d
movq %rdx, -0x70(%rbp)
movq %r11, -0x60(%rbp)
movq %r15, -0x38(%rbp)
movq %rdi, %r15
movl %ebx, %eax
addl $0x8, %eax
movq %rax, -0x48(%rbp)
movq %r8, -0x30(%rbp)
movq %r14, %rdi
movq %rbx, %rsi
movq %r15, %rdx
movq %rbx, %rcx
movq %r9, -0x58(%rbp)
movq %r10, -0x50(%rbp)
callq 0xb916
addq %rbx, %r15
movq -0x30(%rbp), %rdi
movq %rbx, %rsi
movq %r15, %rdx
movq %rbx, %rcx
callq 0xb916
leaq 0x102d(%rip), %rdx      # 0xc460
movl $0x6, %ecx
movq -0x60(%rbp), %r15
movq %r15, %rdi
movq -0x48(%rbp), %rsi
callq 0xb916
movzwl 0x18(%rbp), %eax
rolw $0x8, %ax
movw %ax, 0x6(%r15)
movq -0x50(%rbp), %rax
leaq 0x1(%rax), %rdi
movq -0x58(%rbp), %rax
leaq -0x1(%rax), %rsi
movslq -0x38(%rbp), %rcx
movq -0x40(%rbp), %rdx
callq 0xb916
movl $0x2710, %eax           # imm = 0x2710
movb 0x10(%rbp), %cl
shll %cl, %eax
sarl $0x2, %eax
movl %eax, -0x40(%rbp)
leaq 0x8(%r15), %rax
movq %rax, -0x38(%rbp)
movl $0x3, %r15d
movq %r14, -0x78(%rbp)
movq -0x38(%rbp), %rdi
movq %rbx, %rsi
movq -0x30(%rbp), %rdx
movq %rbx, %rcx
callq 0xb916
movq -0x50(%rbp), %rdi
movb %r15b, (%rdi)
subq $0x8, %rsp
movq -0x58(%rbp), %rsi
movq -0x60(%rbp), %rdx
movq -0x48(%rbp), %rcx
movl -0x40(%rbp), %r8d
movq %r13, %r9
pushq %rbx
callq 0x8366
addq $0x10, %rsp
cmpl $0x2, -0x68(%rbp)
jb 0xb4ec
xorl %eax, %eax
movb (%r13,%rax), %cl
xorb (%r14,%rax), %cl
movb %cl, (%r12,%rax)
incq %rax
cmpq %rax, %rbx
jne 0xb4d7
movq %r14, %rdi
movq %rbx, %rsi
movq -0x30(%rbp), %r14
movq %r14, %rdx
movq %rbx, %rcx
callq 0xb916
movq %r14, %rdi
movq -0x78(%rbp), %r14
movq %rbx, %rsi
movq %r12, %rdx
movq %rbx, %rcx
callq 0xb916
subl $0x1, %r15d
jae 0xb494
movl -0x64(%rbp), %r13d
movl %r13d, %esi
movq -0x70(%rbp), %r15
movq %r15, %rdi
movq -0x30(%rbp), %rdx
movq %rbx, %rcx
callq 0xb916
addq %rbx, %r15
subl %ebx, %r13d
movslq %r13d, %rsi
movq %r15, %rdi
movq %r14, %rdx
movq %rbx, %rcx
callq 0xb916
movq -0x50(%rbp), %rdi
movq -0x58(%rbp), %rsi
callq 0x81bc
movq -0x60(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0x81bc
movq %r14, %rdi
movq %rbx, %rsi
callq 0x81bc
movq -0x30(%rbp), %rdi
movq %rbx, %rsi
callq 0x81bc
movq %r12, %rdi
movq %rbx, %rsi
callq 0x81bc
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq