// File: /workspace/github_c_makefile_star2_O1/mloar[P]charset/confuse.c
// Function: main
int main(int argc, char **argv)
{
    wchar_t *chars;
    struct enc { char string[MAXENCLEN]; int len; } *encodings;
    int nchars;
    int i, j, k, cs;
    const char *sep;

    setlocale(LC_ALL, "");

    chars = malloc(argc * sizeof(wchar_t));
    if (!chars) {
        fprintf(stderr, "out of memory\n");
        return 1;
    }

    nchars = 0;

    while (--argc) {
        char *p = *++argv;
        char *orig = p;
        char *end;
        int base = 16, semi_ok = 0;
        wchar_t ch;

        if ((p[0] == 'U' || p[0] == 'u') &&
            (p[1] == '-' || p[1] == '+')) {
            p += 2;
        } else if (p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) {
            p += 2;
        } else if (p[0] == '&' && p[1] == '#') {
            p += 2;
            if (p[0] == 'x' || p[0] == 'X')
                p++;
            else
                base = 10;
            semi_ok = 1;
        } else if (mbtowc(&ch, p, strlen(p)) == strlen(p)) {
            chars[nchars++] = ch;
            continue;
        }

        chars[nchars++] = strtoul(p, &end, base);
        if (!*end || (semi_ok && !strcmp(end, ";")))
            continue;
        else {
            fprintf(stderr, "unable to parse '%s' as a Unicode code point\n",
                    orig);
            return 1;
        }
    }

    encodings = malloc(nchars * CS_LIMIT * sizeof(struct enc));
    for (cs = 0; cs < CS_LIMIT; cs++) {
        for (i = 0; i < nchars; i++) {
            wchar_t inbuf[1];
            const wchar_t *inptr;
            int inlen, error, ret;

            if (!charset_exists(cs)) {
                encodings[i*CS_LIMIT+cs].len = 0;
                continue;
            }

            inbuf[0] = chars[i];
            inptr = inbuf;
            inlen = 1;
            error = 0;
            ret = charset_from_unicode(&inptr, &inlen,
                                       encodings[i*CS_LIMIT+cs].string,
                                       MAXENCLEN, cs, NULL, &error);
            if (error || inlen > 0)
                encodings[i*CS_LIMIT+cs].len = 0;
            else
                encodings[i*CS_LIMIT+cs].len = ret;
        }
    }

    /*
     * Really simple and slow approach to finding each distinct string
     * and outputting it.
     */
    for (i = 0; i < nchars*CS_LIMIT; i++) {
        const char *thisstr = encodings[i].string;
        int thislen = encodings[i].len;

        if (thislen == 0)
            continue;
        for (j = 0; j < i; j++)
            if (encodings[j].len == thislen &&
                !memcmp(encodings[j].string, thisstr, thislen))
                break;
        if (j < i)
            continue;        /* not the first instance of this encoding */

        /*
         * See if every character is encoded like this somewhere.
         */
        for (j = 0; j < nchars; j++) {
            for (cs = 0; cs < CS_LIMIT; cs++) {
                if (encodings[j*CS_LIMIT+cs].len == thislen &&
                    !memcmp(encodings[j*CS_LIMIT+cs].string, thisstr, thislen))
                    break;
            }
            if (cs == CS_LIMIT)
                break;                 /* this char not in any cs */
        }
        if (j < nchars)
            continue;                  /* some char not in any cs */

        /*
         * Match! Print the encoding, then all charsets.
         */
        for (j = 0; j < nchars; j++) {
            for (k = 0; k < thislen; k++)
                printf("%s%02X", k>0?" ":"", (unsigned)(thisstr[k] & 0xFF));
            printf(" = ");
            if (chars[j] >= 0x10000)
                printf("U-%08X", (unsigned)chars[j]);
            else
                printf("U+%04X", (unsigned)chars[j]);
            printf(" in:");
            sep = " ";
            for (cs = 0; cs < CS_LIMIT; cs++)
                if (encodings[j*CS_LIMIT+cs].len == thislen &&
                    !memcmp(encodings[j*CS_LIMIT+cs].string, thisstr, thislen))
                {
                    printf("%s%s", sep, charset_to_localenc(cs));
                    sep = ", ";
                }
            printf("\n");
        }
        printf("\n");
    }

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rsi, %r15
movl %edi, %r13d
leaq 0x1da2b(%rip), %rsi     # 0x23c62
movl $0x6, %edi
callq 0x60c0
movslq %r13d, %rdi
shlq $0x2, %rdi
callq 0x60a0
movq %rax, 0x38(%rsp)
testq %rax, %rax
je 0x672b
decl %r13d
movq $0x0, 0x10(%rsp)
movq 0xa2d8a(%rip), %r14     # 0xa8ff8
xorl %eax, %eax
movl $0x0, 0x8(%rsp)
cmpl %eax, %r13d
movq %rax, 0x18(%rsp)
je 0x63c6
movq 0x8(%r15,%rax,8), %rbp
movzbl (%rbp), %eax
cmpl $0x54, %eax
jle 0x62ac
cmpl $0x75, %eax
je 0x629e
cmpl $0x55, %eax
jne 0x62f1
movzbl 0x1(%rbp), %ecx
cmpl $0x2d, %ecx
je 0x62c2
cmpl $0x2b, %ecx
je 0x62c2
cmpl $0x26, %eax
je 0x62d4
cmpl $0x30, %eax
jne 0x62f1
movzbl 0x1(%rbp), %ecx
orl $0x20, %ecx
cmpl $0x78, %ecx
jne 0x62cf
leaq 0x2(%rbp), %rdi
movb $0x1, %bl
movl $0x10, %edx
jmp 0x6349
cmpl $0x26, %eax
jne 0x62f1
cmpb $0x23, 0x1(%rbp)
jne 0x62f1
movzbl 0x2(%rbp), %eax
orl $0x20, %eax
cmpl $0x78, %eax
jne 0x633e
leaq 0x3(%rbp), %rdi
movl $0x10, %edx
jmp 0x6347
movq %rbp, %rdi
callq 0x6040
leaq 0x24(%rsp), %rdi
movq %rbp, %rsi
movq %rax, %rdx
callq 0x6090
movslq %eax, %rbx
movq %rbp, %rdi
callq 0x6040
cmpq %rbx, %rax
jne 0x6332
movl 0x24(%rsp), %eax
movq 0x38(%rsp), %rcx
movq 0x18(%rsp), %rdx
movl %eax, (%rcx,%rdx,4)
movq %rdx, %rax
movb $0x1, %r12b
jmp 0x63af
movb $0x1, %bl
movl $0x10, %edx
movq %rbp, %rdi
jmp 0x6349
leaq 0x2(%rbp), %rdi
movl $0xa, %edx
xorl %ebx, %ebx
leaq 0x40(%rsp), %rsi
callq 0x60d0
movq 0x38(%rsp), %rcx
movq 0x18(%rsp), %rdx
movl %eax, (%rcx,%rdx,4)
movq 0x40(%rsp), %rdi
movb $0x1, %r12b
cmpb $0x0, (%rdi)
je 0x63ac
testb %bl, %bl
jne 0x6386
leaq 0x4c9b(%rip), %rsi      # 0xb013
callq 0x6070
testl %eax, %eax
movq 0x18(%rsp), %rax
je 0x63af
movq (%r14), %rdi
xorl %r12d, %r12d
leaq 0x4c82(%rip), %rsi      # 0xb015
movq %rbp, %rdx
xorl %eax, %eax
callq 0x6080
movq 0x18(%rsp), %rax
movl $0x1, 0x8(%rsp)
jmp 0x63af
movq %rdx, %rax
incq %rax
addq $-0x51, 0x10(%rsp)
testb %r12b, %r12b
jne 0x6278
jmp 0x6718
movq 0x10(%rsp), %rax
negq %rax
movq %rax, 0x8(%rsp)
movl %eax, %eax
shlq $0x3, %rax
leaq (%rax,%rax,2), %rdi
callq 0x60a0
movq %rax, %rbx
xorl %r15d, %r15d
leaq 0x40(%rsp), %r13
leaq 0x34(%rsp), %rbp
movq %rax, 0x28(%rsp)
cmpq $0x0, 0x10(%rsp)
je 0x6491
movq %rbx, %r12
xorl %r14d, %r14d
movl %r15d, %edi
callq 0x6a6d
testl %eax, %eax
je 0x6473
movq 0x38(%rsp), %rax
movl (%rax,%r14,4), %eax
movl %eax, 0x24(%rsp)
leaq 0x24(%rsp), %rax
movq %rax, 0x40(%rsp)
movl $0x1, 0x34(%rsp)
movl $0x0, 0x30(%rsp)
leaq 0x30(%rsp), %rax
movq %rax, (%rsp)
movq %r13, %rdi
movq %rbp, %rsi
movq %r12, %rdx
movl $0x14, %ecx
movl %r15d, %r8d
xorl %r9d, %r9d
callq 0x673c
cmpl $0x0, 0x30(%rsp)
jne 0x6473
cmpl $0x0, 0x34(%rsp)
jg 0x6473
movl %eax, 0x14(%r12)
jmp 0x647c
movl $0x0, 0x14(%r12)
incq %r14
addq $0x798, %r12            # imm = 0x798
cmpq %r14, 0x18(%rsp)
jne 0x6409
incq %r15
addq $0x18, %rbx
cmpq $0x51, %r15
jne 0x63f7
cmpq $0x0, 0x10(%rsp)
je 0x6710
movq 0x8(%rsp), %rcx
cmpl $0x2, %ecx
movl $0x1, %eax
cmovael %ecx, %eax
movq %rax, 0x60(%rsp)
movq 0x28(%rsp), %rbp
xorl %eax, %eax
movq %rax, 0x48(%rsp)
leaq (%rax,%rax,2), %rax
movq 0x28(%rsp), %rcx
movslq 0x14(%rcx,%rax,8), %r13
testq %r13, %r13
je 0x66f9
movq 0x28(%rsp), %rbx
leaq (%rbx,%rax,8), %rax
movq %rax, 0x10(%rsp)
movq 0x48(%rsp), %rax
movq %rax, %r14
testq %rax, %rax
je 0x6528
cmpl %r13d, 0x14(%rbx)
jne 0x651f
movq %rbx, %rdi
movq 0x10(%rsp), %rsi
movq %r13, %rdx
callq 0x60b0
testl %eax, %eax
je 0x66f9
addq $0x18, %rbx
decq %r14
jne 0x6501
movb $0x1, 0x8(%rsp)
movq 0x28(%rsp), %r14
xorl %r12d, %r12d
movq %r14, %rbx
xorl %r15d, %r15d
cmpl %r13d, 0x14(%rbx)
jne 0x6555
movq %rbx, %rdi
movq 0x10(%rsp), %rsi
movq %r13, %rdx
callq 0x60b0
testl %eax, %eax
je 0x6568
incq %r15
addq $0x18, %rbx
cmpq $0x51, %r15
jne 0x653b
movl $0x51, %r15d
cmpl $0x51, %r15d
movq 0x18(%rsp), %rax
je 0x6593
incq %r12
cmpq %rax, %r12
setb 0x8(%rsp)
addq $0x798, %r14            # imm = 0x798
cmpq %r12, %rax
leaq 0x4ad8(%rip), %r15      # 0xb067
jne 0x6535
jmp 0x65a5
testb $0x1, 0x8(%rsp)
leaq 0x4ac8(%rip), %r15      # 0xb067
jne 0x66f9
movq %rbp, 0x50(%rsp)
movl %r13d, %eax
movq %rax, 0x70(%rsp)
movq 0x28(%rsp), %rax
movq %rax, 0x58(%rsp)
movq $0x0, 0x8(%rsp)
movq %r13, 0x68(%rsp)
testl %r13d, %r13d
leaq 0x1d68e(%rip), %rax     # 0x23c62
leaq 0x4a68(%rip), %r13      # 0xb043
movq 0x50(%rsp), %r12
movq 0x70(%rsp), %r14
jle 0x6610
xorl %ebx, %ebx
testq %rbx, %rbx
movq %r15, %rsi
cmoveq %rax, %rsi
movzbl (%r12,%rbx), %edx
movq %r13, %rdi
movq %rax, %rbp
xorl %eax, %eax
callq 0x6050
movq %rbp, %rax
incq %rbx
cmpq %rbx, %r14
jne 0x65e9
leaq 0x4a33(%rip), %rdi      # 0xb04a
xorl %eax, %eax
callq 0x6050
movq 0x38(%rsp), %rax
movq 0x8(%rsp), %rcx
movl (%rax,%rcx,4), %esi
cmpl $0x10000, %esi          # imm = 0x10000
leaq 0x4a1d(%rip), %rdi      # 0xb055
leaq 0x4a0f(%rip), %rax      # 0xb04e
cmovgeq %rax, %rdi
xorl %eax, %eax
callq 0x6050
leaq 0x4a0b(%rip), %rdi      # 0xb05c
xorl %eax, %eax
callq 0x6050
movq 0x58(%rsp), %r14
movq %r15, %rbx
xorl %r15d, %r15d
leaq 0x49f7(%rip), %rbp      # 0xb061
movq 0x10(%rsp), %r12
movq 0x68(%rsp), %r13
cmpl %r13d, 0x14(%r14)
jne 0x66ab
movq %r14, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x60b0
testl %eax, %eax
jne 0x66ab
movl %r15d, %edi
callq 0x6880
movq %rbp, %rdi
movq %rbx, %rsi
movq %rax, %rdx
xorl %eax, %eax
callq 0x6050
leaq 0x49bb(%rip), %rbx      # 0xb066
incq %r15
addq $0x18, %r14
cmpq $0x51, %r15
jne 0x6674
movl $0xa, %edi
callq 0x6030
movq 0x8(%rsp), %rcx
incq %rcx
addq $0x798, 0x58(%rsp)      # imm = 0x798
movq %rcx, 0x8(%rsp)
cmpq %rcx, 0x18(%rsp)
leaq 0x4983(%rip), %r15      # 0xb067
jne 0x65ca
movl $0xa, %edi
callq 0x6030
movq 0x50(%rsp), %rbp
movq 0x48(%rsp), %rax
incq %rax
addq $0x18, %rbp
cmpq 0x60(%rsp), %rax
jne 0x64ca
movl $0x0, 0x8(%rsp)
movl 0x8(%rsp), %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x6110
movl $0x1, 0x8(%rsp)
jmp 0x6718
nop