// File: /workspace/github_c_makefile_star2_O0/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
subq $0xf0, %rsp
movw 0x18(%rbp), %ax
movb 0x10(%rbp), %al
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
movl %ecx, -0x24(%rbp)
movq %r8, -0x30(%rbp)
movl %r9d, -0x34(%rbp)
movl -0x14(%rbp), %eax
movl %eax, %ecx
shrl $0x1f, %ecx
addl %ecx, %eax
sarl %eax
movl %eax, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
movq %rax, -0xf0(%rbp)
movq %rsp, %rcx
movq %rcx, -0x48(%rbp)
movq %rax, %rdx
addq $0xf, %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0xe8(%rbp)
movq %rcx, %rsp
movq %rax, -0x50(%rbp)
movl -0x3c(%rbp), %eax
movq %rax, -0xe0(%rbp)
movq %rax, %rdx
addq $0xf, %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0xd8(%rbp)
movq %rcx, %rsp
movq %rax, -0x58(%rbp)
movl -0x3c(%rbp), %eax
movq %rax, -0xd0(%rbp)
movq %rax, %rdx
addq $0xf, %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0xc8(%rbp)
movq %rcx, %rsp
movq %rax, -0x60(%rbp)
movl -0x3c(%rbp), %eax
movq %rax, -0xc0(%rbp)
movq %rax, %rdx
addq $0xf, %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0xb8(%rbp)
movq %rcx, %rsp
movq %rax, -0x68(%rbp)
movl $0x6, -0x6c(%rbp)
movl -0x3c(%rbp), %edx
movl -0x6c(%rbp), %eax
movl %eax, %ecx
movl %edx, %eax
leal 0x2(%rax,%rcx), %eax
movq %rax, -0xb0(%rbp)
movq %rax, %rdx
addq $0xf, %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0xa8(%rbp)
movq %rcx, %rsp
movq %rax, -0x78(%rbp)
movl -0x34(%rbp), %eax
incl %eax
movq %rax, -0xa0(%rbp)
movq %rax, %rdx
addq $0xf, %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x98(%rbp)
movq %rcx, %rsp
movq %rax, -0x80(%rbp)
movl -0x24(%rbp), %eax
cmpl -0x14(%rbp), %eax
je 0xd886
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0x8c(%rbp)
jmp 0xdb3b
movl -0x14(%rbp), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0xd8a7
movl $0xfffffffe, -0x4(%rbp) # imm = 0xFFFFFFFE
movl $0x1, -0x8c(%rbp)
jmp 0xdb3b
movq -0xf0(%rbp), %rsi
movq -0xe8(%rbp), %rdi
movq -0x10(%rbp), %rdx
movslq -0x3c(%rbp), %rcx
callq 0xe330
movq -0xd8(%rbp), %rdi
movq -0xe0(%rbp), %rsi
movq -0x10(%rbp), %rdx
movslq -0x3c(%rbp), %rax
addq %rax, %rdx
movslq -0x3c(%rbp), %rcx
callq 0xe330
movq -0xb0(%rbp), %rsi
movq -0xa8(%rbp), %rdi
movslq -0x6c(%rbp), %rcx
leaq 0x1b80(%rip), %rdx      # 0xf47d
callq 0xe330
movq -0x98(%rbp), %rdi
movq -0xa0(%rbp), %rsi
movq -0xa8(%rbp), %rax
movzwl 0x18(%rbp), %ecx
sarl $0x8, %ecx
movb %cl, %dl
movslq -0x6c(%rbp), %rcx
movb %dl, (%rax,%rcx)
movzwl 0x18(%rbp), %ecx
andl $0xff, %ecx
movb %cl, %dl
movl -0x6c(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
addq $0x1, %rdi
subq $0x1, %rsi
movq -0x30(%rbp), %rdx
movslq -0x34(%rbp), %rcx
callq 0xe330
movzbl 0x10(%rbp), %ecx
movl $0x2710, %eax           # imm = 0x2710
shll %cl, %eax
movl $0x4, %ecx
cltd
idivl %ecx
movl %eax, -0x88(%rbp)
movl $0x3, -0x84(%rbp)
cmpl $0x0, -0x84(%rbp)
jl 0xda8f
movq -0xa8(%rbp), %rdi
movq -0xb0(%rbp), %rsi
movq -0xd8(%rbp), %rdx
addq $0x8, %rdi
subq $0x8, %rsi
movslq -0x3c(%rbp), %rcx
callq 0xe330
movq -0xa0(%rbp), %rsi
movq -0xb0(%rbp), %rcx
movq -0x98(%rbp), %rdi
movq -0xa8(%rbp), %rdx
movq -0xb8(%rbp), %r9
movq -0xc0(%rbp), %rax
movl -0x84(%rbp), %r8d
movb %r8b, (%rdi)
movl -0x88(%rbp), %r8d
subq $0x10, %rsp
movl %eax, (%rsp)
callq 0x8570
addq $0x10, %rsp
movl $0x0, -0x38(%rbp)
movl -0x38(%rbp), %eax
cmpl -0x3c(%rbp), %eax
jge 0xda3f
movq -0xc8(%rbp), %rax
movq -0xb8(%rbp), %rdx
movq -0xe8(%rbp), %rcx
movslq -0x38(%rbp), %rsi
movzbl (%rcx,%rsi), %ecx
movslq -0x38(%rbp), %rsi
movzbl (%rdx,%rsi), %edx
xorl %edx, %ecx
movb %cl, %dl
movslq -0x38(%rbp), %rcx
movb %dl, (%rax,%rcx)
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0xd9fc
movq -0xd8(%rbp), %rdx
movq -0xf0(%rbp), %rsi
movq -0xe8(%rbp), %rdi
movslq -0x3c(%rbp), %rcx
callq 0xe330
movq -0xd8(%rbp), %rdi
movq -0xe0(%rbp), %rsi
movq -0xc8(%rbp), %rdx
movslq -0x3c(%rbp), %rcx
callq 0xe330
movl -0x84(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x84(%rbp)
jmp 0xd977
movq -0xd8(%rbp), %rdx
movq -0x20(%rbp), %rdi
movslq -0x24(%rbp), %rsi
movslq -0x3c(%rbp), %rcx
callq 0xe330
movq -0xe8(%rbp), %rdx
movq -0x20(%rbp), %rdi
movslq -0x3c(%rbp), %rax
addq %rax, %rdi
movl -0x24(%rbp), %eax
subl -0x3c(%rbp), %eax
movslq %eax, %rsi
movslq -0x3c(%rbp), %rcx
callq 0xe330
movq -0x98(%rbp), %rdi
movq -0xa0(%rbp), %rsi
callq 0x81c0
movq -0xa8(%rbp), %rdi
movq -0xb0(%rbp), %rsi
callq 0x81c0
movq -0xe8(%rbp), %rdi
movq -0xf0(%rbp), %rsi
callq 0x81c0
movq -0xd8(%rbp), %rdi
movq -0xe0(%rbp), %rsi
callq 0x81c0
movq -0xc8(%rbp), %rdi
movq -0xd0(%rbp), %rsi
callq 0x81c0
movl $0x0, -0x4(%rbp)
movl $0x1, -0x8c(%rbp)
movq -0x48(%rbp), %rax
movq %rax, %rsp
movl -0x4(%rbp), %eax
movq %rbp, %rsp
popq %rbp
retq
nopw (%rax,%rax)