// File: /workspace/github_c_makefile_star2_O2/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
movq %r8, -0x60(%rbp)
movq %rdi, %r8
movl %esi, %ebx
shrl %ebx
movq %rsp, %r14
leal 0xf(%rbx), %eax
andl $-0x10, %eax
subq %rax, %r14
movq %r14, %rsp
movq %rsp, %r15
subq %rax, %r15
movq %r15, %rsp
movq %rsp, %r12
subq %rax, %r12
movq %r12, %rsp
movq %rsp, %r13
subq %rax, %r13
movq %r13, %rsp
movq %rsp, %rdi
leal 0x17(%rbx), %eax
andl $-0x10, %eax
subq %rax, %rdi
movq %rdi, %rsp
leal 0x1(%r9), %r10d
movq %rsp, %r11
leaq 0xf(%r10), %rax
andq $-0x10, %rax
subq %rax, %r11
movq %r11, %rsp
cmpl %esi, %ecx
setne %al
movl %esi, %ecx
orb %al, %cl
testb $0x1, %cl
jne 0xb224
movl %esi, -0x64(%rbp)
movq %rdx, -0x70(%rbp)
movl %ebx, %eax
addl $0x8, %eax
movq %rax, -0x40(%rbp)
movq %r15, -0x30(%rbp)
movq %rdi, -0x58(%rbp)
movq %r14, %rdi
movq %rbx, %rsi
movq %r8, %r15
movq %r8, %rdx
movq %rbx, %rcx
movq %r10, -0x50(%rbp)
movq %r11, -0x48(%rbp)
movq %r9, -0x38(%rbp)
callq 0xb56b
addq %rbx, %r15
movq -0x30(%rbp), %rdi
movq %rbx, %rsi
movq %r15, %rdx
movq %rbx, %rcx
callq 0xb56b
leaq 0x1395(%rip), %rdx      # 0xc460
pushq $0x6
popq %rcx
movq -0x58(%rbp), %r15
movq %r15, %rdi
movq -0x40(%rbp), %rsi
callq 0xb56b
movzwl 0x18(%rbp), %eax
rolw $0x8, %ax
movw %ax, 0x6(%r15)
movq -0x48(%rbp), %rax
leaq 0x1(%rax), %rdi
movq -0x50(%rbp), %rax
leaq -0x1(%rax), %rsi
movslq -0x38(%rbp), %rcx
movq -0x60(%rbp), %rdx
callq 0xb56b
movl $0x2710, %eax           # imm = 0x2710
movb 0x10(%rbp), %cl
shll %cl, %eax
sarl $0x2, %eax
movl %eax, -0x38(%rbp)
leaq 0x8(%r15), %rax
movq %rax, -0x78(%rbp)
pushq $0x3
popq %r15
testl %r15d, %r15d
js 0xb1b7
movq -0x78(%rbp), %rdi
movq %rbx, %rsi
movq -0x30(%rbp), %rdx
movq %rbx, %rcx
callq 0xb56b
movq -0x48(%rbp), %rdi
movb %r15b, (%rdi)
subq $0x8, %rsp
movq -0x50(%rbp), %rsi
movq -0x58(%rbp), %rdx
movq -0x40(%rbp), %rcx
movl -0x38(%rbp), %r8d
movq %r13, %r9
pushq %rbx
callq 0x8368
addq $0x10, %rsp
xorl %eax, %eax
cmpq %rax, %rbx
je 0xb181
movb (%r13,%rax), %cl
xorb (%r14,%rax), %cl
movb %cl, (%r12,%rax)
incq %rax
jmp 0xb16a
movq %r14, %rdi
movq %rbx, %rsi
movl %r15d, -0x60(%rbp)
movq -0x30(%rbp), %r15
movq %r15, %rdx
movq %rbx, %rcx
callq 0xb56b
movq %r15, %rdi
movl -0x60(%rbp), %r15d
movq %rbx, %rsi
movq %r12, %rdx
movq %rbx, %rcx
callq 0xb56b
decl %r15d
jmp 0xb124
movl -0x64(%rbp), %r15d
movl %r15d, %esi
movq -0x70(%rbp), %r13
movq %r13, %rdi
movq -0x30(%rbp), %rdx
movq %rbx, %rcx
callq 0xb56b
addq %rbx, %r13
subl %ebx, %r15d
movslq %r15d, %rsi
movq %r13, %rdi
movq %r14, %rdx
movq %rbx, %rcx
callq 0xb56b
movq -0x48(%rbp), %rdi
movq -0x50(%rbp), %rsi
callq 0x81bc
movq -0x58(%rbp), %rdi
movq -0x40(%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