// File: /workspace/github_c_makefile_star2_O3/Seclabs-htc[P]slip39/slip39.c
// Function: combine_mnemonics
int combine_mnemonics(int gn, char *ml[], uint8_t *pp, int ppl, uint8_t *ms, int *msl)
{
    int i, j, k, r, ret = 0;
    group_shares *gs;
    uint16_t id, ie, gt, gc, gi, sl;
    uint8_t gm[16]; // group index member
    uint16_t gmc, gmt, mic, gmic = 0;
    uint8_t *m_share[16];
    uint8_t m_share_index[16];
    uint8_t *g_share[16];
    uint8_t g_share_index[16];
    uint8_t gsv[MAX_SAHRE_VALUE_LEN];

    gs = malloc(sizeof(group_shares) * gn);
    if (gs == NULL)
        return -1;

    for (i = 0; i < gn; i++)
    {
        r = decode_mnemonic(ml[i], &gs[i]);
        if (r != 0)
        {
            ret = -2;
            goto exit;
        }
    }

    id = gs[0].id;
    ie = gs[0].ie;
    gt = gs[0].gt;
    gc = gs[0].gc;
    sl = gs[0].sl;
    for (i = 1; i < gn; i++)
    {
        if ((id != gs[i].id) || (ie != gs[i].ie) ||
            (gt != gs[i].gt) || (gc != gs[i].gc) ||
            (sl != gs[i].sl))
        {
            ret = -3;
            goto exit;
        }
    }

    if ((sl < 16) || (sl & 1))
    {
        ret = -4;
        goto exit;
    }
    if (gt > gc)
    {
        ret = -5;
        goto exit;
    }

    memset(gm, 0, sizeof(gm));
    for (i = 0; i < gn; i++)
        gm[gs[i].gi]++;

    gmc = 0;
    for (i = 0; i < 16; i++)
        if (gm[i] != 0)
            gmc++;
    if (gmc < gt)
    {
        ret = -6;
        goto exit;
    }

    for (i = 0; i < gn; i++)
    {
        gi = gs[i].gi;
        for (j = 0; j < gn; j++)
        {
            if ((i != j) && (gi == gs[j].gi))
            {
                if (gs[i].mt != gs[j].mt)
                {
                    ret = -7;
                    goto exit;
                }
            }
        }
    }

    memset(g_share, 0, sizeof(g_share));
    memset(g_share_index, 0, sizeof(g_share_index));

    for (i = 0; i < 16; i++)
    {
        if (gm[i] != 0)
        {
            mic = 0; gmt = 0;
            for (j = 0; j < gn; j++)
            {
                if (i == gs[j].gi)
                {
                    for (k = 0; k < gn; k++)
                    {
                        if ((j != k) && (i == gs[k].gi) && (gs[j].mi == gs[k].mi))
                        {
                            ret = -8;
                            goto exit;
                        }
                    }
                    gmt = gs[j].mt;
                    m_share[mic] = gs[j].s;
                    m_share_index[mic] = gs[j].mi;
                    mic++;
                }
            }
            r = _recover_secret(gmt, sl, m_share_index, (const uint8_t **)m_share, gsv);
            if (r == 0)
            {
                g_share[gmic] = (uint8_t *)malloc(MAX_SAHRE_VALUE_LEN);
                memcpy(g_share[gmic], gsv, sl);

                g_share_index[gmic] = i;
                gmic++;
            }
            else
            {
                ret = -9;
                goto exit;
            }
            if (mic < gmt)
            {
                ret = -10;
                goto exit;
            }
        }
    }

    if ((ms == NULL) || (*msl < sl))
    {
        ret = -11;
        goto exit;
    }

    r = _recover_secret(gmic, sl, g_share_index, (const uint8_t **)g_share, gsv);
    if (r == 0)
    {
        _decrypt(gsv, sl, gsv, sl, pp, ppl, ie, id);

        memcpy(ms, gsv, sl);
        *msl = sl;
    }
    else
    {
        ret = -12;
    }

exit:

    if (gs != NULL)
        free(gs);

    for (i = 0; i < gmic; i++)
        if (g_share[i] != NULL)
            free(g_share[i]);

    return ret;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1a8, %rsp            # imm = 0x1A8
movq %r9, %r15
movq %r8, %r13
movl %ecx, %r12d
movq %rdx, -0x88(%rbp)
movq %rsi, %r14
movl %edi, %ebx
movslq %edi, %rax
imulq $0x38, %rax, %rdi
callq 0x8090
testq %rax, %rax
je 0xb1bc
movl %r12d, -0x50(%rbp)
movl $0xfffffffc, %r12d      # imm = 0xFFFFFFFC
testl %ebx, %ebx
jle 0xb1ce
movq %r13, -0x80(%rbp)
movq %r15, -0x78(%rbp)
movl %ebx, -0x38(%rbp)
movl %ebx, %r13d
xorl %r15d, %r15d
movq %rax, -0x30(%rbp)
movq %rax, %r12
movq (%r14,%r15,8), %rdi
movq %r12, %rsi
callq 0xaae0
testl %eax, %eax
jne 0xb1c4
incq %r15
addq $0x38, %r12
cmpq %r15, %r13
jne 0xae6e
movq -0x30(%rbp), %rax
movzwl (%rax), %r8d
movzwl 0x2(%rax), %r14d
movzwl 0x6(%rax), %edi
movzwl 0x8(%rax), %ecx
movzwl 0x14(%rax), %r15d
movl -0x38(%rbp), %r10d
cmpl $0x1, %r10d
jne 0xb1eb
cmpw $0x10, %r15w
setae %dl
testb $0x1, %r15b
sete %sil
andb %dl, %sil
movl $0xfffffffc, %r12d      # imm = 0xFFFFFFFC
cmpb $0x1, %sil
jne 0xb1ce
movl $0xfffffffb, %r12d      # imm = 0xFFFFFFFB
cmpw %cx, %di
ja 0xb1ce
xorps %xmm0, %xmm0
movaps %xmm0, -0x60(%rbp)
testl %r10d, %r10d
jle 0xaf0c
imulq $0x38, %r13, %rcx
xorl %edx, %edx
movzwl 0x4(%rax,%rdx), %esi
incb -0x60(%rbp,%rsi)
addq $0x38, %rdx
cmpq %rdx, %rcx
jne 0xaefa
xorl %ecx, %ecx
xorl %edx, %edx
cmpb $0x1, -0x60(%rbp,%rcx)
sbbw $-0x1, %dx
incq %rcx
cmpq $0x10, %rcx
jne 0xaf10
movl $0xfffffffa, %r12d      # imm = 0xFFFFFFFA
cmpw %di, %dx
jb 0xb1ce
movw %r8w, -0x32(%rbp)
testl %r10d, %r10d
jle 0xaf86
imulq $0x38, %r13, %r11
xorl %ecx, %ecx
xorl %edx, %edx
imulq $0x38, %rdx, %rdi
leaq (%rax,%rdi), %rsi
movzwl 0x4(%rax,%rdi), %edi
xorl %r8d, %r8d
cmpq %r8, %rcx
je 0xaf71
cmpw 0x4(%rax,%r8), %di
jne 0xaf71
movzwl 0xc(%rsi), %r9d
cmpw 0xc(%rax,%r8), %r9w
jne 0xb22d
addq $0x38, %r8
cmpq %r8, %r11
jne 0xaf53
incq %rdx
addq $0x38, %rcx
cmpq %r13, %rdx
jne 0xaf43
movl %r14d, -0x4c(%rbp)
xorps %xmm0, %xmm0
movaps %xmm0, -0xe0(%rbp)
movaps %xmm0, -0xf0(%rbp)
movaps %xmm0, -0x100(%rbp)
movaps %xmm0, -0x110(%rbp)
movaps %xmm0, -0x120(%rbp)
movaps %xmm0, -0x130(%rbp)
movaps %xmm0, -0x140(%rbp)
movaps %xmm0, -0x150(%rbp)
movaps %xmm0, -0xa0(%rbp)
imulq $0x38, %r13, %r12
xorl %r14d, %r14d
leaq -0xb0(%rbp), %rdx
leaq -0x1d0(%rbp), %rcx
movq $0x0, -0x40(%rbp)
cmpb $0x0, -0x60(%rbp,%r14)
je 0xb114
testl %r10d, %r10d
jle 0xb085
xorl %r11d, %r11d
xorl %r10d, %r10d
movq $0x0, -0x48(%rbp)
xorl %ebx, %ebx
imulq $0x38, %r10, %r9
movzwl 0x4(%rax,%r9), %esi
cmpq %rsi, %r14
jne 0xb077
addq %rax, %r9
xorl %esi, %esi
cmpq %rsi, %r11
je 0xb041
movzwl 0x4(%rax,%rsi), %edi
cmpq %rdi, %r14
jne 0xb041
movzwl 0xa(%r9), %edi
cmpw 0xa(%rax,%rsi), %di
je 0xb235
addq $0x38, %rsi
cmpq %rsi, %r12
jne 0xb022
movzwl 0xc(%r9), %r8d
leaq 0x16(%r9), %rsi
movq -0x48(%rbp), %rbx
movzwl %bx, %edi
movq %rsi, -0x1d0(%rbp,%rdi,8)
movb 0xa(%r9), %sil
movb %sil, -0xb0(%rbp,%rdi)
incl %ebx
movq %rbx, -0x48(%rbp)
movl %r8d, %ebx
incq %r10
addq $0x38, %r11
cmpq %r13, %r10
jne 0xb00e
jmp 0xb08f
xorl %ebx, %ebx
movq $0x0, -0x48(%rbp)
movzbl %bl, %edi
movl %r15d, %esi
leaq -0xd0(%rbp), %r8
callq 0xb285
testl %eax, %eax
jne 0xb271
movl $0x20, %edi
callq 0x8090
movq %r15, %rdx
movq -0x40(%rbp), %r15
movzwl %r15w, %ecx
movq %rcx, -0x70(%rbp)
movq %rax, -0x150(%rbp,%rcx,8)
movq %rax, %rdi
leaq -0xd0(%rbp), %rsi
movq %rdx, -0x68(%rbp)
callq 0x8080
movq -0x70(%rbp), %rax
movb %r14b, -0xa0(%rbp,%rax)
incl %r15d
movq %r15, -0x40(%rbp)
cmpw %bx, -0x48(%rbp)
jb 0xb279
movq -0x68(%rbp), %r15
movl -0x38(%rbp), %r10d
movq -0x30(%rbp), %rax
leaq -0xb0(%rbp), %rdx
leaq -0x1d0(%rbp), %rcx
incq %r14
cmpq $0x10, %r14
jne 0xafe9
movl $0xfffffff5, %r12d      # imm = 0xFFFFFFF5
movq -0x80(%rbp), %r13
testq %r13, %r13
je 0xb23b
movq -0x78(%rbp), %r14
cmpl %r15d, (%r14)
jl 0xb23b
movzbl -0x40(%rbp), %edi
leaq -0xa0(%rbp), %rdx
leaq -0x150(%rbp), %rcx
leaq -0xd0(%rbp), %r8
movl %r15d, %esi
callq 0xb285
movl $0xfffffff4, %r12d      # imm = 0xFFFFFFF4
testl %eax, %eax
jne 0xb27f
movzwl -0x32(%rbp), %eax
movzbl -0x4c(%rbp), %r10d
leaq -0xd0(%rbp), %r12
movq %r12, %rdi
movl %r15d, %esi
movq %r12, %rdx
movl %r15d, %ecx
movq -0x88(%rbp), %r8
movl -0x50(%rbp), %r9d
pushq %rax
pushq %r10
callq 0xb387
addq $0x10, %rsp
movq %r13, %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x8080
movl %r15d, (%r14)
xorl %r12d, %r12d
jmp 0xb27f
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
jmp 0xb1d6
movl $0xfffffffe, %r12d      # imm = 0xFFFFFFFE
movq -0x30(%rbp), %rax
movq %rax, %rdi
callq 0x8030
movl %r12d, %eax
addq $0x1a8, %rsp            # imm = 0x1A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
imulq $0x38, %r13, %rdx
movl $0x38, %esi
movl $0xfffffffd, %r12d      # imm = 0xFFFFFFFD
cmpw (%rax,%rsi), %r8w
jne 0xb1ce
cmpw 0x2(%rax,%rsi), %r14w
jne 0xb1ce
cmpw 0x6(%rax,%rsi), %di
jne 0xb1ce
cmpw 0x8(%rax,%rsi), %cx
jne 0xb1ce
cmpw 0x14(%rax,%rsi), %r15w
jne 0xb1ce
addq $0x38, %rsi
cmpq %rsi, %rdx
jne 0xb1fa
jmp 0xaeb6
movl $0xfffffff9, %r12d      # imm = 0xFFFFFFF9
jmp 0xb1ce
movl $0xfffffff8, %r12d      # imm = 0xFFFFFFF8
movq %rax, %rdi
callq 0x8030
movq -0x40(%rbp), %rax
testw %ax, %ax
je 0xb1d6
movzwl %ax, %ebx
xorl %r14d, %r14d
movq -0x150(%rbp,%r14,8), %rdi
testq %rdi, %rdi
je 0xb264
callq 0x8030
incq %r14
cmpq %r14, %rbx
jne 0xb252
jmp 0xb1d6
movl $0xfffffff7, %r12d      # imm = 0xFFFFFFF7
jmp 0xb27f
movl $0xfffffff6, %r12d      # imm = 0xFFFFFFF6
movq -0x30(%rbp), %rax
jmp 0xb23b