// File: /workspace/github_c_makefile_star2_O2/Seclabs-htc[P]slip39/slip39.c
// Function: encrypt
static int _encrypt(uint8_t *ms, int msl, uint8_t *ems, int emsl,
        uint8_t *pp, int ppl, uint8_t ie, uint16_t id)
{
    int j, hl = msl / 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];
    uint8_t i;
    uint32_t it;

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

    memscpy(l, sizeof(l), ms, hl);
    memscpy(r, sizeof(r), ms + 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 = 0; i < ROUND_COUNT; 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(ems, emsl, r, hl);
    memscpy(ems + hl, emsl - 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 $0x68, %rsp
movq %r8, -0x70(%rbp)
movq %rdx, %r8
movl %esi, %r11d
movq %rdi, -0x30(%rbp)
pushq $0x2
popq %rsi
movl %r11d, %eax
cltd
idivl %esi
movl %eax, %ebx
leaq 0xf(%rbx), %rax
andq $-0x10, %rax
movq %rsp, %r14
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
movl %ebx, %edx
addl $0x8, %edx
leaq 0xf(%rdx), %rax
andq $-0x10, %rax
movq %rsp, %rsi
subq %rax, %rsi
movq %rsi, %rsp
movq %r9, -0x48(%rbp)
leal 0x1(%r9), %edi
movq %rsp, %r10
leaq 0xf(%rdi), %rax
andq $-0x10, %rax
subq %rax, %r10
movq %r10, %rsp
cmpl %r11d, %ecx
setne %al
movl %r11d, %ecx
orb %al, %cl
testb $0x1, %cl
jne 0xa66a
movl %r11d, -0x74(%rbp)
movq %r8, -0x80(%rbp)
movslq %ebx, %rcx
movq %rcx, -0x38(%rbp)
movq %rsi, -0x60(%rbp)
movq %rdi, -0x58(%rbp)
movq %r14, %rdi
movq %rdx, -0x68(%rbp)
movq %rbx, %rsi
movq %rbx, -0x40(%rbp)
movq -0x30(%rbp), %rbx
movq %rbx, %rdx
movq %r10, -0x50(%rbp)
callq 0xb56b
movq -0x38(%rbp), %rcx
addq %rcx, %rbx
movq %r15, -0x30(%rbp)
movq %r15, %rdi
movq -0x40(%rbp), %rsi
movq %rbx, %rdx
callq 0xb56b
leaq 0x1f7e(%rip), %rdx      # 0xc460
pushq $0x6
popq %rcx
movq -0x60(%rbp), %rbx
movq %rbx, %rdi
movq -0x68(%rbp), %r15
movq %r15, %rsi
callq 0xb56b
movw $0x6137, 0x6(%rbx)      # imm = 0x6137
movq -0x50(%rbp), %rax
leaq 0x1(%rax), %rdi
movq -0x58(%rbp), %rax
leaq -0x1(%rax), %rsi
movslq -0x48(%rbp), %rcx
movq -0x70(%rbp), %rdx
callq 0xb56b
movl $0x2710, %eax           # imm = 0x2710
movb 0x10(%rbp), %cl
shll %cl, %eax
sarl $0x2, %eax
movl %eax, -0x48(%rbp)
leaq 0x8(%rbx), %rax
movq %rax, -0x90(%rbp)
leaq -0x8(%r15), %rax
movq %rax, -0x88(%rbp)
xorl %r15d, %r15d
movq -0x38(%rbp), %rcx
testl %ecx, %ecx
movl $0x0, %ebx
cmovgl %ecx, %ebx
cmpb $0x4, %r15b
je 0xa5f4
movq -0x90(%rbp), %rdi
movq -0x88(%rbp), %rsi
movq -0x30(%rbp), %rdx
movq -0x38(%rbp), %rcx
callq 0xb56b
movq -0x50(%rbp), %rdi
movq %r15, -0x70(%rbp)
movb %r15b, (%rdi)
subq $0x8, %rsp
movq -0x58(%rbp), %rsi
movq -0x60(%rbp), %rdx
movq -0x68(%rbp), %rcx
movl -0x48(%rbp), %r8d
movq %r13, %r9
pushq -0x40(%rbp)
callq 0x8368
addq $0x10, %rsp
xorl %eax, %eax
cmpq %rax, %rbx
je 0xa5be
movb (%r13,%rax), %cl
xorb (%r14,%rax), %cl
movb %cl, (%r12,%rax)
incq %rax
jmp 0xa5a7
movq %r14, %rdi
movq -0x30(%rbp), %r15
movq -0x40(%rbp), %rsi
movq %r15, %rdx
movq -0x38(%rbp), %rcx
callq 0xb56b
movq %r15, %rdi
movq -0x40(%rbp), %rsi
movq %r12, %rdx
movq -0x38(%rbp), %rcx
callq 0xb56b
movq -0x70(%rbp), %r15
incb %r15b
jmp 0xa552
movl -0x74(%rbp), %r15d
movslq %r15d, %rsi
movq -0x80(%rbp), %r13
movq %r13, %rdi
movq -0x30(%rbp), %rdx
movq -0x38(%rbp), %rbx
movq %rbx, %rcx
callq 0xb56b
addq %rbx, %r13
subl -0x40(%rbp), %r15d
movslq %r15d, %rsi
movq %r13, %rdi
movq %r14, %rdx
movq %rbx, %rcx
callq 0xb56b
movq -0x50(%rbp), %rdi
movq -0x58(%rbp), %rsi
callq 0x81bc
movq -0x60(%rbp), %rdi
movq -0x68(%rbp), %rsi
callq 0x81bc
movq %r14, %rdi
movq -0x40(%rbp), %rbx
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