// File: /workspace/github_c_makefile_star2_O3/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, %r15
leal 0xf(%rdi), %r9d
andl $-0x10, %r9d
subq %r9, %r15
movq %r15, %rsp
leal -0x1(%rdi), %r9d
cmpb %sil, %r9b
setae %r9b
cmpb $0x11, %sil
setae %r11b
orb %r9b, %r11b
jne 0xaad1
cmpb $0x1, %dil
jne 0xa9c7
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 0xb962
movq %rdx, -0x30(%rbp)
movslq %ecx, %r12
movzbl %dil, %edx
xorl %ecx, %ecx
movq %rax, (%r14,%rcx,8)
incq %rcx
addq %r12, %rax
cmpq %rcx, %rdx
jne 0xa9d4
movq %rdx, -0x50(%rbp)
movl %esi, -0x44(%rbp)
movq %r8, -0x58(%rbp)
movq %rdi, -0x40(%rbp)
leal -0x2(%rdi), %eax
movl %eax, -0x38(%rbp)
andl $0xff, %eax
je 0xaa1c
movl %eax, %ebx
xorl %r13d, %r13d
movb %r13b, (%r15,%r13)
movq (%r14,%r13,8), %rdi
movq %r12, %rsi
callq 0x9fbb
incq %r13
cmpq %r13, %rbx
jne 0xaa04
movq -0x40(%rbp), %rbx
decl %ebx
movq (%r14,%rbx,8), %rdi
movq -0x30(%rbp), %rsi
movq %r12, %rdx
callq 0x8080
movb $-0x1, (%r15,%rbx)
movl -0x38(%rbp), %eax
movq %rax, -0x40(%rbp)
movq (%r14,%rax,8), %rax
movq %rax, -0x38(%rbp)
leaq 0x4(%rax), %r13
leal -0x4(%r12), %eax
movslq %eax, %rbx
movq %r13, %rdi
movq %rbx, %rsi
callq 0x9fbb
leaq -0x80(%rbp), %r8
movq %r13, %rdi
movl %ebx, %esi
movq -0x30(%rbp), %rdx
movl %r12d, %ecx
movq %r8, %rbx
callq 0x9bd3
movl (%rbx), %eax
movq -0x38(%rbp), %rcx
movl %eax, (%rcx)
movq -0x40(%rbp), %rax
movb $-0x2, (%r15,%rax)
movzbl -0x44(%rbp), %eax
movq %rax, -0x30(%rbp)
xorl %ebx, %ebx
movq %r15, %rdx
movq -0x50(%rbp), %r15
movq -0x58(%rbp), %rax
movq (%rax,%rbx,8), %rdi
movzbl %bl, %esi
movq %rdx, %r13
movq %r14, %rcx
movl %r15d, %r8d
movq %r12, %r9
callq 0xb610
testb %al, %al
je 0xaac5
incq %rbx
cmpq %rbx, -0x30(%rbp)
movq %r13, %rdx
jne 0xaa97
jmp 0xaad1
leaq 0x33be(%rip), %rdi      # 0xde8a
callq 0x8050
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq