// File: /workspace/github_c_makefile_star2_O2/Seclabs-htc[P]slip39/slip39.c
// Function: split_secret
static int _split_secret(uint8_t t, uint8_t n, uint8_t *s, int sl, uint8_t **gs)
{
    uint8_t random_share_count;
    uint8_t *base_share[t];
    uint8_t pool[sl*t];
    uint8_t bi[t];
    uint8_t hash[SHA256_DIGEST_LENGTH];
    int i;

    if (t < 1)
        return -1;
    if (t > n)
        return -2;
    if (n > MAX_SHARE_COUNT)
        return -3;
    if (sl == 0)
        return -4;

    if (t == 1)
    {
        memscpy(gs[0], sl, s, sl);
        return 0;
    }

    for (i = 0; i < t; i++)
        base_share[i] = (uint8_t *)(pool + (i * sl));

    random_share_count = t - 2;
    for (i = 0; i < random_share_count; i++)
    {
        bi[i] = i;
        random_buffer(base_share[i], sl);
    }

    // SECRET_INDEX
    memcpy(base_share[t-1], s, sl);
    bi[t-1] = SECRET_INDEX;

    // DIGEST_INDEX
    random_buffer(&base_share[t-2][DIGEST_LENGTH_BYTES], sl - DIGEST_LENGTH_BYTES);
    hmac_sha256(&base_share[t-2][DIGEST_LENGTH_BYTES], sl - DIGEST_LENGTH_BYTES, s, sl, hash);
    memcpy(base_share[t-2], hash, DIGEST_LENGTH_BYTES);
    bi[t-2] = DIGEST_INDEX;

    for (i = 0; i < n; i++)
        if (!shamir_interpolate(gs[i], i, bi, (const uint8_t **)base_share, t, sl))
        {
            printf("shamir_interpolate error\n");
            return -5;
        }

    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
leal 0xf(,%rdi,8), %eax
andl $-0x10, %eax
movq %rsp, %r14
subq %rax, %r14
movq %r14, %rsp
movl %edi, %r9d
imull %ecx, %r9d
addq $0xf, %r9
andq $-0x10, %r9
movq %rsp, %rax
subq %r9, %rax
movq %rax, %rsp
movq %rsp, %r13
leal 0xf(%rdi), %r9d
andl $-0x10, %r9d
subq %r9, %r13
movq %r13, %rsp
leal -0x1(%rdi), %r9d
cmpb %sil, %r9b
setae %r9b
cmpb $0x11, %sil
setae %r10b
orb %r9b, %r10b
je 0xa6ed
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpb $0x1, %dil
jne 0xa70f
movq (%r8), %rdi
movslq %ecx, %rcx
movq %rcx, %rsi
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xb56b
movq %rdx, -0x30(%rbp)
movslq %ecx, %r12
movzbl %dil, %edx
xorl %ecx, %ecx
cmpq %rcx, %rdx
je 0xa72d
movq %rax, (%r14,%rcx,8)
incq %rcx
addq %r12, %rax
jmp 0xa71c
movq %rdx, -0x58(%rbp)
movl %esi, -0x44(%rbp)
movq %r8, -0x50(%rbp)
movq %rdi, -0x40(%rbp)
leal -0x2(%rdi), %eax
movl %eax, -0x38(%rbp)
movzbl %al, %r15d
xorl %ebx, %ebx
cmpq %rbx, %r15
je 0xa763
movb %bl, (%r13,%rbx)
movq (%r14,%rbx,8), %rdi
movq %r12, %rsi
callq 0x9dc3
incq %rbx
jmp 0xa748
movq -0x40(%rbp), %rbx
decl %ebx
movq (%r14,%rbx,8), %rdi
movq -0x30(%rbp), %rsi
movq %r12, %rdx
callq 0x8080
movb $-0x1, (%r13,%rbx)
movl -0x38(%rbp), %eax
movq %rax, -0x40(%rbp)
movq (%r14,%rax,8), %rax
movq %rax, -0x38(%rbp)
leaq 0x4(%rax), %rbx
leal -0x4(%r12), %eax
movslq %eax, %r15
movq %rbx, %rdi
movq %r15, %rsi
callq 0x9dc3
leaq -0x80(%rbp), %r8
movq %rbx, %rdi
movl %r15d, %esi
movq -0x30(%rbp), %rdx
movl %r12d, %ecx
movq %r8, %rbx
callq 0x99d7
movl (%rbx), %eax
movq -0x38(%rbp), %rcx
movl %eax, (%rcx)
movq -0x40(%rbp), %rax
movb $-0x2, (%r13,%rax)
movzbl -0x44(%rbp), %eax
movq %rax, -0x30(%rbp)
xorl %r15d, %r15d
movq -0x50(%rbp), %rbx
cmpq %r15, -0x30(%rbp)
je 0xa6de
movq (%rbx,%r15,8), %rdi
movzbl %r15b, %esi
movq %r13, %rdx
movq %r14, %rcx
movq -0x58(%rbp), %r8
movq %r12, %r9
callq 0xb290
incq %r15
testb %al, %al
jne 0xa7df
leaq 0x3679(%rip), %rdi      # 0xde8a
callq 0x8050
jmp 0xa6de