// File: /workspace/github_c_makefile_star2_O2/Seclabs-htc[P]slip39/slip39.c
// Function: generate_mnemonics
int generate_mnemonics(uint8_t gt, uint8_t gn, uint8_t *ms, int msl, uint8_t *pp, int ppl, uint8_t ie, char **ml)
{
    int i;
    uint8_t ems[msl];
    uint16_t identifier;
    uint8_t *gs[gn];
    uint8_t *gs_pool;

    if (msl == 0)
        return -1;

    // check passphrase
    for (i = 0; i < ppl; i++)
        if (!((pp[i] >= 32) && (pp[i] <= 126)))
            return -1;

    random_buffer((void *)&identifier, sizeof(identifier));

    identifier = 0x3761; // for test
    identifier &= ((1 << ID_LENGTH_BITS) - 1);

    _encrypt(ms, msl, ems, msl, pp, ppl, ie, identifier);

    printf("encrypt master secret\n");
    dumphex(ems, msl);

    gs_pool = (uint8_t *)malloc(msl * gn);
    if (gs_pool == NULL)
        return -1;

    for (i = 0; i < gn; i++)
        gs[i] = gs_pool + i * msl;

    _split_secret(gt, gn, ems, msl, gs);

    for (i = 0; i < gn; i++)
        encode_mnemonic(identifier, ie, 0, 1, 1,
                i, gt, gs[i], msl, ml[i], MNEMONIC_LIST_LEN);

    free(gs_pool);
    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x108, %rsp            # imm = 0x108
movq %r8, %r15
movq %rdx, %r14
movl %ecx, %eax
movq %rsp, %r13
addq $0xf, %rax
andq $-0x10, %rax
subq %rax, %r13
movq %r13, %rsp
leal 0xf(,%rsi,8), %eax
andl $-0x10, %eax
movq %rsp, %rdx
subq %rax, %rdx
movq %rdx, -0x58(%rbp)
movq %rdx, %rsp
pushq $-0x1
popq %r8
movq %rcx, -0x60(%rbp)
testl %ecx, %ecx
je 0xa3e1
movl %r9d, %r12d
movb 0x10(%rbp), %bl
xorl %eax, %eax
testl %r9d, %r9d
movl $0x0, %ecx
cmovgl %r9d, %ecx
cmpq %rax, %rcx
je 0xa079
movb (%r15,%rax), %dl
addb $-0x20, %dl
incq %rax
cmpb $0x5f, %dl
jb 0xa060
jmp 0xa3e1
movl %edi, -0x38(%rbp)
leaq -0x2a(%rbp), %rdi
movq %rsi, -0x40(%rbp)
pushq $0x2
popq %rsi
callq 0x9dc3
leaq -0x2a(%rbp), %rax
movw $0x3761, (%rax)         # imm = 0x3761
subq $0x8, %rsp
movzbl %bl, %eax
movq %r14, %rdi
movq -0x60(%rbp), %r14
movl %r14d, %esi
movq %r13, %rdx
movl %r14d, %ecx
movq %r15, %r8
movl %r12d, %r9d
pushq %rax
callq 0xa3f3
popq %rax
popq %rcx
leaq 0x3db3(%rip), %rdi      # 0xde74
callq 0x8050
movq %r13, %rdi
movl %r14d, %esi
callq 0xb47a
movzbl -0x40(%rbp), %r15d
movl %r15d, %eax
imull %r14d, %eax
movslq %eax, %rdi
callq 0x8090
pushq $-0x1
popq %r8
testq %rax, %rax
je 0xa3e1
movslq %r14d, %rsi
xorl %ecx, %ecx
movq %rax, %rdx
movq -0x58(%rbp), %r8
movl -0x38(%rbp), %edi
cmpq %rcx, %r15
je 0xa112
movq %rdx, (%r8,%rcx,8)
incq %rcx
addq %rsi, %rdx
jmp 0xa101
movq %rax, -0x70(%rbp)
movzbl %dil, %r12d
movl %r12d, %edi
movq %r15, -0x78(%rbp)
movl %r15d, %esi
movq %r13, %rdx
movl %r14d, %ecx
callq 0xa679
andb $0x1f, %bl
movzbl %bl, %eax
movl %eax, -0x50(%rbp)
decb %r12b
andb $0xf, %r12b
leal (,%r14,8), %eax
pushq $0xa
popq %rcx
cltd
idivl %ecx
subl %edx, %ecx
testl %edx, %edx
cmovel %edx, %ecx
movl %ecx, -0x4c(%rbp)
xorl %eax, %eax
testl %r14d, %r14d
movl $0x0, %ecx
cmovgl %r14d, %ecx
movzbl %r12b, %edx
movl %edx, -0x44(%rbp)
pushq $-0x1
popq %r15
movl %ecx, -0x48(%rbp)
cmpq -0x78(%rbp), %rax
je 0xa3d5
movq %rax, -0x68(%rbp)
cmpl $0x20, -0x60(%rbp)
jg 0xa358
movzwl -0x2a(%rbp), %r14d
movq -0x58(%rbp), %rax
movq -0x68(%rbp), %r12
movq (%rax,%r12,8), %rbx
movq 0x18(%rbp), %rax
movq (%rax,%r12,8), %rax
movq %rax, -0x38(%rbp)
movl $0x84, %esi
leaq -0x130(%rbp), %rdi
callq 0x81bc
movl %r14d, %eax
shrl $0x5, %eax
movl %eax, -0x130(%rbp)
andl $0x1f, %r14d
shll $0x5, %r14d
addl -0x50(%rbp), %r14d
movl %r14d, -0x12c(%rbp)
andl $0x0, -0x128(%rbp)
movl %r12d, %eax
shll $0x4, %eax
movzbl %al, %eax
addl -0x44(%rbp), %eax
movl %eax, -0x124(%rbp)
xorl %edi, %edi
xorl %esi, %esi
movl -0x4c(%rbp), %eax
pushq $0x4
popq %rcx
movl %ecx, %r12d
movl -0x48(%rbp), %r11d
movslq %r12d, %r14
cmpl %r11d, %esi
je 0xa264
shll $0x8, %edi
movzbl (%rbx), %ecx
orl %ecx, %edi
leal 0x8(%rax), %edx
leaq -0x130(,%r14,4), %r8
addq %rbp, %r8
addl $-0x2, %eax
cmpl $0xa, %edx
jl 0xa25b
addq $-0xa, %rdx
movl %edi, %r9d
movl %eax, %ecx
shrl %cl, %r9d
movq %r15, %r10
movl %edx, %ecx
shlq %cl, %r10
andl $0x3ff, %r9d            # imm = 0x3FF
movl %r9d, (%r8)
notl %r10d
andl %edi, %r10d
incl %r12d
addq $0x4, %r8
addl $-0xa, %eax
movl %r10d, %edi
jmp 0xa223
incq %rbx
incl %esi
movl %edx, %eax
jmp 0xa202
movl $0x1, -0x30(%rbp)
xorl %eax, %eax
leaq 0x21ec(%rip), %rdx      # 0xc460
cmpq $0x6, %rax
je 0xa28a
movsbl (%rax,%rdx), %ecx
movl %ecx, -0xa0(%rbp,%rax,4)
incq %rax
jmp 0xa274
leaq -0x30(%rbp), %rbx
movq %rbx, %rdi
leaq -0xa0(%rbp), %rsi
pushq $0x6
popq %rdx
callq 0xb233
movq %rbx, %rdi
leaq -0x130(%rbp), %rsi
movl %r12d, %edx
callq 0xb233
xorl %eax, %eax
cmpq $0x3, %rax
je 0xa2c7
andl $0x0, -0xa0(%rbp,%rax,4)
incq %rax
jmp 0xa2b4
leaq -0x30(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
pushq $0x3
popq %rdx
callq 0xb233
movl -0x30(%rbp), %eax
movl %eax, %ecx
shrl $0x14, %ecx
movl $0x3ff, %edx            # imm = 0x3FF
andl %edx, %ecx
movl %ecx, -0x130(%rbp,%r14,4)
movl %eax, %ecx
shrl $0xa, %ecx
andl %edx, %ecx
leal 0x2(%r12), %ebx
movl %ecx, -0x12c(%rbp,%r14,4)
andl %edx, %eax
xorl $0x1, %eax
cmpl $-0x2, %r12d
pushq $-0x3
popq %rcx
cmovll %ecx, %r12d
movl %eax, -0x128(%rbp,%r14,4)
addl $0x3, %r12d
xorl %r14d, %r14d
xorl %r13d, %r13d
cmpq %r14, %r12
je 0xa34f
movl -0x130(%rbp,%r14,4), %eax
leaq 0x9ab5(%rip), %rcx      # 0x13df0
movq (%rcx,%rax,8), %rdi
callq 0x8060
addl %eax, %r13d
incl %r13d
incq %r14
jmp 0xa327
cmpl $0x16b, %r13d           # imm = 0x16B
jbe 0xa364
movq -0x68(%rbp), %rax
incq %rax
jmp 0xa173
movq -0x38(%rbp), %rax
movb $0x0, (%rax)
movl %ebx, %eax
movq %rax, -0x80(%rbp)
xorl %ecx, %ecx
cmpq %rcx, %r12
je 0xa358
movl -0x130(%rbp,%rcx,4), %eax
movq %rcx, -0x40(%rbp)
leaq 0x9a66(%rip), %rcx      # 0x13df0
movq (%rcx,%rax,8), %rbx
movq %rbx, %rdi
callq 0x8060
movl %eax, %r14d
movq -0x38(%rbp), %r13
movq %r13, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x8080
xorl %eax, %eax
movq -0x40(%rbp), %rcx
cmpq %rcx, -0x80(%rbp)
setne %al
movl %eax, %ecx
shlb $0x5, %cl
addq %r13, %rax
addq %r14, %rax
movb %cl, (%r13,%r14)
movq -0x40(%rbp), %rcx
incq %rcx
movq %rax, -0x38(%rbp)
jmp 0xa373
movq -0x70(%rbp), %rdi
callq 0x8030
xorl %r8d, %r8d
movl %r8d, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq