// File: /workspace/github_c_makefile_star2_O2/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 $0x68, %rsp
movq %rsi, %r15
movl %edi, %r13d
leaq 0x1da3f(%rip), %rsi     # 0x23c46
pushq $0x6
popq %rdi
callq 0x60b0
movslq %r13d, %rdi
shlq $0x2, %rdi
callq 0x6090
movq %rax, 0x38(%rsp)
testq %rax, %rax
je 0x6617
decl %r13d
xorl %ebx, %ebx
pushq $0x10
popq %r14
xorl %edx, %edx
cmpl %edx, %r13d
movq %rdx, 0x8(%rsp)
je 0x633b
movq 0x8(%r15,%rdx,8), %rbp
movzbl (%rbp), %eax
cmpl $0x26, %eax
je 0x6284
cmpl $0x30, %eax
je 0x626f
cmpl $0x75, %eax
je 0x625f
cmpl $0x55, %eax
jne 0x62a3
movzbl 0x1(%rbp), %eax
cmpl $0x2d, %eax
je 0x627b
cmpl $0x2b, %eax
je 0x627b
jmp 0x62a3
movzbl 0x1(%rbp), %eax
orl $0x20, %eax
cmpl $0x78, %eax
jne 0x62a3
leaq 0x2(%rbp), %rdi
xorl %r12d, %r12d
jmp 0x62e4
cmpb $0x23, 0x1(%rbp)
jne 0x62a3
movzbl 0x2(%rbp), %eax
movb $0x1, %r12b
orl $0x20, %eax
cmpl $0x78, %eax
jne 0x6332
leaq 0x3(%rbp), %rdi
jmp 0x62e4
movq %rbp, %rdi
callq 0x6040
leaq 0x14(%rsp), %rdi
movq %rbp, %rsi
movq %rax, %rdx
callq 0x6080
movslq %eax, %r12
movq %rbp, %rdi
callq 0x6040
cmpq %r12, %rax
jne 0x62de
movl 0x14(%rsp), %eax
movq 0x38(%rsp), %rcx
movq 0x8(%rsp), %rdx
movl %eax, (%rcx,%rdx,4)
jmp 0x6326
xorl %r12d, %r12d
movq %rbp, %rdi
movl %r14d, %edx
leaq 0x48(%rsp), %rsi
callq 0x60c0
movq 0x38(%rsp), %rcx
movq 0x8(%rsp), %rdx
movl %eax, (%rcx,%rdx,4)
movq 0x48(%rsp), %rax
movb (%rax), %cl
testb %cl, %cl
je 0x6326
cmpb $0x3b, %cl
setne %cl
xorb $0x1, %r12b
orb %cl, %r12b
jne 0x6635
cmpb $0x0, 0x1(%rax)
jne 0x6635
incq %rdx
addq $-0x51, %rbx
jmp 0x6234
leaq 0x2(%rbp), %rdi
pushq $0xa
popq %rdx
jmp 0x62e7
negq %rbx
movq %rbx, 0x28(%rsp)
movl %ebx, %eax
imulq $0x18, %rax, %rdi
callq 0x6090
movq 0x8(%rsp), %rcx
movq %rax, %r12
xorl %r13d, %r13d
leaq 0x48(%rsp), %rbp
leaq 0x24(%rsp), %r15
pushq $0x14
popq %rbx
movq %rax, 0x18(%rsp)
cmpq $0x51, %r13
je 0x640e
movq %r12, 0x30(%rsp)
xorl %r14d, %r14d
cmpq %r14, %rcx
je 0x63fd
movl %r13d, %edi
callq 0x695a
testl %eax, %eax
je 0x63e6
movq 0x38(%rsp), %rax
movl (%rax,%r14,4), %eax
movl %eax, 0x14(%rsp)
leaq 0x14(%rsp), %rax
movq %rax, 0x48(%rsp)
movl $0x1, 0x24(%rsp)
andl $0x0, 0x20(%rsp)
leaq 0x20(%rsp), %rax
movq %rax, (%rsp)
movq %rbp, %rdi
movq %r15, %rsi
movq %r12, %rdx
movl %ebx, %ecx
movl %r13d, %r8d
xorl %r9d, %r9d
callq 0x666c
cmpl $0x0, 0x20(%rsp)
jne 0x63e6
cmpl $0x0, 0x24(%rsp)
jg 0x63e6
movl %eax, 0x14(%r12)
jmp 0x63ec
andl $0x0, 0x14(%r12)
movq 0x8(%rsp), %rcx
incq %r14
addq $0x798, %r12            # imm = 0x798
jmp 0x637d
incq %r13
movq 0x30(%rsp), %r12
addq $0x18, %r12
jmp 0x636b
movl 0x28(%rsp), %eax
movq %rax, 0x60(%rsp)
movq $0x0, 0x40(%rsp)
leaq 0x4c3e(%rip), %r12      # 0xb065
movq 0x18(%rsp), %rax
movq %rax, 0x30(%rsp)
xorl %eax, %eax
cmpq 0x60(%rsp), %rax
je 0x6658
movq %rax, 0x28(%rsp)
imulq $0x18, %rax, %rbp
movq 0x18(%rsp), %rax
movslq 0x14(%rax,%rbp), %r13
testq %r13, %r13
je 0x6604
movq 0x18(%rsp), %rbx
addq %rbx, %rbp
movq 0x28(%rsp), %r14
subq $0x1, %r14
jb 0x648f
cmpl %r13d, 0x14(%rbx)
jne 0x6489
movq %rbx, %rdi
movq %rbp, %rsi
movq %r13, %rdx
callq 0x60a0
testl %eax, %eax
je 0x6604
addq $0x18, %rbx
jmp 0x6467
movq 0x18(%rsp), %rbx
xorl %r14d, %r14d
movq 0x8(%rsp), %rax
cmpq %r14, %rax
je 0x64dd
xorl %r15d, %r15d
cmpq $0x798, %r15            # imm = 0x798
je 0x6604
cmpl %r13d, 0x14(%rbx,%r15)
jne 0x64cb
leaq (%rbx,%r15), %rdi
movq %rbp, %rsi
movq %r13, %rdx
callq 0x60a0
testl %eax, %eax
je 0x64d1
addq $0x18, %r15
jmp 0x64a4
incq %r14
addq $0x798, %rbx            # imm = 0x798
jmp 0x6497
testl %r13d, %r13d
movl $0x0, %r14d
cmovgl %r13d, %r14d
movq 0x18(%rsp), %rcx
movq %rcx, 0x50(%rsp)
xorl %ecx, %ecx
cmpq %rcx, %rax
leaq 0x1d746(%rip), %r15     # 0x23c46
je 0x65fc
movq %rcx, 0x58(%rsp)
xorl %ebx, %ebx
cmpq %rbx, %r14
je 0x6538
testq %rbx, %rbx
movq %r12, %rsi
cmoveq %r15, %rsi
movq 0x30(%rsp), %rax
movzbl (%rax,%rbx), %edx
leaq 0x4b15(%rip), %rdi      # 0xb041
xorl %eax, %eax
callq 0x6050
incq %rbx
jmp 0x650d
leaq 0x4b09(%rip), %rdi      # 0xb048
xorl %eax, %eax
callq 0x6050
movq 0x38(%rsp), %rax
movq 0x58(%rsp), %rcx
movl (%rax,%rcx,4), %esi
cmpl $0x10000, %esi          # imm = 0x10000
leaq 0x4af3(%rip), %rdi      # 0xb053
leaq 0x4ae5(%rip), %rax      # 0xb04c
cmovgeq %rax, %rdi
xorl %eax, %eax
callq 0x6050
leaq 0x4ae1(%rip), %rdi      # 0xb05a
xorl %eax, %eax
callq 0x6050
movq 0x50(%rsp), %rbx
xorl %r15d, %r15d
cmpq $0x51, %r15
je 0x65d2
cmpl %r13d, 0x14(%rbx)
jne 0x65c9
movq %rbx, %rdi
movq %rbp, %rsi
movq %r13, %rdx
callq 0x60a0
testl %eax, %eax
jne 0x65c9
movl %r15d, %edi
callq 0x6780
leaq 0x4aaa(%rip), %rdi      # 0xb05f
movq %r12, %rsi
movq %rax, %rdx
xorl %eax, %eax
callq 0x6050
leaq 0x4a9b(%rip), %r12      # 0xb064
incq %r15
addq $0x18, %rbx
jmp 0x6588
pushq $0xa
popq %rdi
callq 0x6030
movq 0x58(%rsp), %rcx
incq %rcx
addq $0x798, 0x50(%rsp)      # imm = 0x798
movq 0x8(%rsp), %rax
leaq 0x4a6e(%rip), %r12      # 0xb065
jmp 0x64f6
pushq $0xa
popq %rdi
callq 0x6030
movq 0x28(%rsp), %rax
incq %rax
addq $0x18, 0x30(%rsp)
jmp 0x6433
movq 0xa39da(%rip), %rax     # 0xa9ff8
movq (%rax), %rcx
leaq 0x49dc(%rip), %rdi      # 0xb004
pushq $0xe
popq %rsi
pushq $0x1
popq %rdx
callq 0x60d0
jmp 0x6650
movq 0xa39bc(%rip), %rax     # 0xa9ff8
movq (%rax), %rdi
leaq 0x49cd(%rip), %rsi      # 0xb013
movq %rbp, %rdx
xorl %eax, %eax
callq 0x6070
pushq $0x1
popq %rax
movq %rax, 0x40(%rsp)
movq 0x40(%rsp), %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq