// File: /workspace/github_c_makefile_star2_O1/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, -0x40(%rbp)
movq %rdx, -0x88(%rbp)
movq %rdi, %r8
movl %esi, %ebx
shrl $0x1f, %ebx
addl %esi, %ebx
sarl %ebx
leaq 0xf(%rbx), %rax
andq $-0x10, %rax
movq %rsp, %rdi
subq %rax, %rdi
movq %rdi, %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, %r14
subq %rax, %r14
movq %r14, %rsp
leal 0x1(%r9), %r10d
movq %rsp, %r11
leaq 0xf(%r10), %rax
andq $-0x10, %rax
subq %rax, %r11
movq %r11, %rsp
movl %ecx, -0x74(%rbp)
cmpl %esi, %ecx
setne %al
movl %esi, -0x78(%rbp)
movl %esi, %ecx
orb %al, %cl
testb $0x1, %cl
jne 0xa91e
movq %r15, -0x68(%rbp)
movslq %ebx, %r15
movq %r14, -0x58(%rbp)
movq %rdi, -0x80(%rbp)
movq %rbx, %rsi
movq %rdx, -0x60(%rbp)
movq %r8, %r14
movq %r8, %rdx
movq %r15, %rcx
movq %r10, -0x50(%rbp)
movq %r11, -0x48(%rbp)
movq %r9, -0x38(%rbp)
callq 0xb916
addq %r15, %r14
movq -0x68(%rbp), %rdi
movq %rbx, -0x30(%rbp)
movq %rbx, %rsi
movq %r14, %rdx
movq %r15, -0x70(%rbp)
movq %r15, %rcx
callq 0xb916
leaq 0x1cba(%rip), %rdx      # 0xc460
movl $0x6, %ecx
movq -0x58(%rbp), %rbx
movq %rbx, %rdi
movq -0x60(%rbp), %r14
movq %r14, %rsi
callq 0xb916
movw $0x6137, 0x6(%rbx)      # imm = 0x6137
movq -0x48(%rbp), %rax
leaq 0x1(%rax), %rdi
movq -0x50(%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(%rbx), %rax
movq %rax, -0x38(%rbp)
leaq -0x8(%r14), %rax
movq %rax, -0x90(%rbp)
movq -0x68(%rbp), %rbx
xorl %r15d, %r15d
movq -0x70(%rbp), %r14
movq -0x38(%rbp), %rdi
movq -0x90(%rbp), %rsi
movq %rbx, %rdx
movq -0x70(%rbp), %rbx
movq %rbx, %rcx
callq 0xb916
movq -0x48(%rbp), %rdi
movb %r15b, (%rdi)
subq $0x8, %rsp
movq -0x50(%rbp), %rsi
movq -0x58(%rbp), %rdx
movq -0x60(%rbp), %rcx
movl -0x40(%rbp), %r8d
movq %r13, %r9
pushq %rbx
callq 0x8366
addq $0x10, %rsp
cmpl $0x2, -0x78(%rbp)
movq -0x30(%rbp), %rsi
movq -0x80(%rbp), %rdi
jl 0xa875
xorl %eax, %eax
movb (%r13,%rax), %cl
xorb (%rdi,%rax), %cl
movb %cl, (%r12,%rax)
incq %rax
cmpq %rax, %rsi
jne 0xa861
movq -0x68(%rbp), %rbx
movq %rbx, %rdx
movq %r14, %rcx
callq 0xb916
movq %rbx, %rdi
movq -0x30(%rbp), %rsi
movq %r12, %rdx
movq %r14, %rcx
callq 0xb916
incb %r15b
cmpb $0x4, %r15b
jne 0xa80f
movl -0x74(%rbp), %r15d
movslq %r15d, %rsi
movq -0x88(%rbp), %r13
movq %r13, %rdi
movq %rbx, %rdx
movq -0x70(%rbp), %r14
movq %r14, %rcx
callq 0xb916
addq %r14, %r13
subl -0x30(%rbp), %r15d
movslq %r15d, %rsi
movq %r13, %rdi
movq -0x80(%rbp), %r15
movq %r15, %rdx
movq %r14, %rcx
callq 0xb916
movq -0x48(%rbp), %rdi
movq -0x50(%rbp), %rsi
callq 0x81bc
movq -0x58(%rbp), %rdi
movq -0x60(%rbp), %rsi
callq 0x81bc
movq %r15, %rdi
movq -0x30(%rbp), %r14
movq %r14, %rsi
callq 0x81bc
movq %rbx, %rdi
movq %r14, %rsi
callq 0x81bc
movq %r12, %rdi
movq %r14, %rsi
callq 0x81bc
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq