// File: /workspace/github_c_makefile_star2_O2/Seclabs-htc[P]slip39/slip39.c
// Function: decode_mnemonic
int decode_mnemonic(char *ml, group_shares *gs)
{
    uint32_t words[33];
    int i, j, wc, r, vc;
    int padding, msl;
    uint32_t p, bits;
    uint8_t *v = gs->s;

    wc = 33;
    mnemonic_to_indices(ml, words, &wc);

    if (wc <= 6)
        return -1;

    r = rs1024_verify_checksum(words, wc);
    if (r != 0)
        return -2;

    padding = (wc - 7) * 10 % 8;
    msl = (wc - 7) * 10 / 8;

    if (msl > 32)
        return -3;

    gs->id = words[0] << 5 | words[1] >> 5;
    gs->ie = words[1] & 0x1f;
    gs->gi = words[2] >> 6;
    gs->gt = ((words[2] >> 2) & 0xf) + 1;
    gs->gc = ((words[2] & 0x3) << 8 | (words[3] >> 8) & 0x3) + 1;
    gs->mi = (words[3] >> 4) & 0xf;
    gs->mt = (words[3] & 0xf) + 1;

    j = 4;
    if (padding)
    {
        bits = 10 - padding;
        // padding should be zero
        if( (words[j] >> bits) != 0)
            return -5;
        p = words[j++] & ((1<<(bits+1))-1);
    }
    else
    {
        p = 0;
        bits = 0;
    }

    vc = 0;
    while(j < wc - 3)
    {
        p = p << 10 | words[j++];
        bits += 10;

        while(bits >= 8)
        {
            *v++ = (p >> (bits - 8)) & 0xFF;
            vc++;
            if (vc > 32)
                return -4;
            bits -= 8;
            p = p & ((0x1UL << bits) - 1);
        }
    }
    gs->sl = msl;

    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc8, %rsp
movq %rsi, -0x40(%rbp)
testq %rdi, %rdi
je 0xa85e
movq %rdi, %r12
xorl %eax, %eax
movq %rdi, %rcx
movzbl (%rcx), %edx
cmpl $0x20, %edx
je 0xa84e
testl %edx, %edx
jne 0xa850
jmp 0xa855
incl %eax
incq %rcx
jmp 0xa840
cmpl $0x20, %eax
jle 0xa8ea
pushq $0x21
popq %rbx
movq -0x40(%rbp), %r14
leaq 0x16(%r14), %r12
movl $0x1, -0x34(%rbp)
xorl %eax, %eax
leaq 0x1be7(%rip), %rcx      # 0xc460
cmpq $0x6, %rax
je 0xa88c
movsbl (%rax,%rcx), %edx
movl %edx, -0x60(%rbp,%rax,4)
incq %rax
jmp 0xa879
leaq -0x34(%rbp), %r15
leaq -0x60(%rbp), %rsi
pushq $0x6
popq %rdx
movq %r15, %rdi
callq 0xb233
leaq -0xf0(%rbp), %rsi
movq %r15, %rdi
movl %ebx, %edx
callq 0xb233
cmpl $0x1, (%r15)
jne 0xa8d1
imull $0xa, %ebx, %esi
addl $-0x46, %esi
pushq $0x8
popq %rcx
movl %esi, %eax
cltd
idivl %ecx
cmpl $0x21, %ebx
jle 0xa98d
pushq $-0x3
jmp 0xa8d3
pushq $-0x2
popq %r15
movl %r15d, %eax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq $0x0, -0x30(%rbp)
pushq $0x1
popq %rcx
leaq -0x60(%rbp), %r13
xorl %ebx, %ebx
movslq %ebx, %rbx
movb (%r12,%rbx), %al
testb %al, %al
je 0xaab1
leaq (%r12,%rbx), %rdx
negq %rbx
testb $-0x21, %al
je 0xa92e
cmpq $0xc, %rcx
je 0xa85e
movb %al, -0x61(%rbp,%rcx)
movb (%rdx,%rcx), %al
incq %rcx
decq %rbx
jmp 0xa911
cmpb $0x1, %al
adcq $-0x1, %rbx
movb $0x0, -0x61(%rbp,%rcx)
negq %rbx
xorl %r15d, %r15d
leaq 0x94aa(%rip), %r14      # 0x13df0
movq (%r14), %rsi
testq %rsi, %rsi
je 0xa85e
movq %r13, %rdi
callq 0x8070
testl %eax, %eax
je 0xa967
incl %r15d
addq $0x8, %r14
jmp 0xa946
cmpq $0x21, -0x30(%rbp)
je 0xa85e
movq -0x30(%rbp), %rax
movl %r15d, -0xf0(%rbp,%rax,4)
incq %rax
movq %rax, -0x30(%rbp)
pushq $0x1
popq %rcx
jmp 0xa8fb
leal (,%rax,8), %ecx
subl %ecx, %esi
movl -0xf0(%rbp), %ecx
movl -0xec(%rbp), %edx
shll $0x5, %ecx
movl %edx, %edi
shrl $0x5, %edi
orl %ecx, %edi
movw %di, (%r14)
andl $0x1f, %edx
movw %dx, 0x2(%r14)
movl -0xe8(%rbp), %ecx
movl %ecx, %edx
shrl $0x6, %edx
movw %dx, 0x4(%r14)
movl %ecx, %edx
shrl $0x2, %edx
andl $0xf, %edx
incl %edx
movw %dx, 0x6(%r14)
andl $0x3, %ecx
shll $0x8, %ecx
movl -0xe4(%rbp), %edx
movl %edx, %edi
shrl $0x8, %edi
andl $0x3, %edi
addl %edi, %ecx
incl %ecx
movw %cx, 0x8(%r14)
movl %edx, %ecx
shrl $0x4, %ecx
andl $0xf, %ecx
movw %cx, 0xa(%r14)
andl $0xf, %edx
incl %edx
movw %dx, 0xc(%r14)
xorl %edi, %edi
testl %esi, %esi
je 0xaa31
pushq $0xa
popq %rdx
subl %esi, %edx
movl -0xe0(%rbp), %r8d
movl %r8d, %r9d
movl %edx, %ecx
shrl %cl, %r9d
testl %r9d, %r9d
je 0xaa3c
pushq $-0x5
jmp 0xa8d3
pushq $0x4
popq %r9
xorl %r8d, %r8d
xorl %edx, %edx
jmp 0xaa4f
movb $0xb, %cl
subb %sil, %cl
pushq $-0x1
popq %rsi
shll %cl, %esi
notl %esi
andl %esi, %r8d
pushq $0x5
popq %r9
addl $-0x3, %ebx
movslq %ebx, %r10
pushq $-0x4
popq %r15
pushq $-0x1
popq %r11
cmpq %r10, %r9
jge 0xaac8
shll $0xa, %r8d
orl -0xf0(%rbp,%r9,4), %r8d
incq %r9
leal 0xa(%rdx), %ecx
addl $0x2, %edx
movl %edx, %esi
movq %rcx, %rdx
cmpq $0x8, %rdx
jb 0xaa5d
movl %r8d, %ebx
movl %esi, %ecx
shrl %cl, %ebx
movb %bl, (%r12)
cmpl $0x1f, %edi
jg 0xa8d5
addq $-0x8, %rdx
incl %edi
movq %r11, %rbx
movl %edx, %ecx
shlq %cl, %rbx
incq %r12
notl %ebx
andl %ebx, %r8d
addl $-0x8, %esi
jmp 0xaa7c
movq -0x30(%rbp), %rbx
cmpq $0x7, %rbx
pushq $-0x1
popq %r15
jae 0xa861
jmp 0xa8d5
movw %ax, 0x14(%r14)
xorl %r15d, %r15d
jmp 0xa8d5