// File: corecodec[P]coremake/corerc.c
// Function: main
int main(int argc, char** argv)
{
    FILE *f = NULL;
    FILE *e = stdout;
    char filename[256];
    char varname[256];
    char name[256];
    char align[32];
    int writeable=0;
    int corec=0;
    int n;
    
    if (argc<=1)
    {
        printf("usage: %s [-a <num>] [-w] [-corec] [-n <name>] <infile.bin> [<outfile.c>]\n",argv[0]);
        return 1;
    }

    align[0]=0;
    varname[0]=0;

    for (n=1;n<argc;++n)
    {
        if (argv[n][0]=='-')
        {
            if (argv[n][1]=='c')
                corec=1;
            else
            if (argv[n][1]=='w')
                writeable = 1;
            else
            if (argv[n][1]=='a')
                sprintf(align,"__attribute__((aligned(%d)))",atoi(argv[++n]));
            else
            if (argv[n][1]=='n')
                strcpy(varname,argv[++n]);
        }
        else
        {
            if (!f)
            {
                strcpy(name,argv[n]);
                f = fopen(argv[n],"rb");
                if (!f)
                    return 1;
            }
            else
            {
                e = fopen(argv[n],"w+");
            }
        }
    }

    pathunix(name);
    strcpy(filename,getfilename(name));
    truncfilename(name);

    if (!varname[0])
        strcpy(varname,name);

    fprintf(f,"/* DO NOT EDIT, FILE GENERATED BY CORERC */\n\n");

    if (corec)
    {
        fprintf(e,"#include \"resource/resource.h\"\n\n");
        fprintf(e,"static %s uint8_t Data[] %s = {",writeable?"":"const",align);
    }
    else
    {
        fseek(f,0,SEEK_END);
        fprintf(e,"%s int %s_size = %d;\n",writeable?"":"const",varname,(int)ftell(f));
        fseek(f,0,SEEK_SET);
        fprintf(e,"%s char %s[] %s = {",writeable?"":"const",varname,align);
    }

    for (n=0;;++n)
    {
        int ch = fgetc(f);
        if (feof(f))
            break;

        if (n)
            fprintf(e,",");
        if ((n%32)==0)
            fprintf(e,"\n");
        fprintf(e,"0x%02X",(unsigned char)ch);
    }

    fprintf(e,"};\n\n");

    if (corec)
    {
        fprintf(e,"META_START(Resource_%s_Class,0)\n"
                  "META_PARAM(STRING,NODE_NAME,T(\"%s\"))\n"
                  "META_PARAM(CUSTOM,RESOURCEDATA_SIZE,sizeof(Data))\n"
                  "META_PARAM(CUSTOM,RESOURCEDATA_PTR,Data)\n"
                  "META_END(RESOURCEDATA_ID)\n",name,filename);
    }

    fclose(f);
    if (e != stdout)
        fclose(e);
	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x380, %rsp            # imm = 0x380
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq $0x0, -0x18(%rbp)
movq 0x2c98(%rip), %rax      # 0x3fe0
movq (%rax), %rax
movq %rax, -0x20(%rbp)
movl $0x0, -0x344(%rbp)
movl $0x0, -0x348(%rbp)
cmpl $0x1, -0x8(%rbp)
jg 0x138a
movq -0x10(%rbp), %rax
movq (%rax), %rsi
leaq 0xc8d(%rip), %rdi       # 0x2004
movb $0x0, %al
callq 0x1060
movl $0x1, -0x4(%rbp)
jmp 0x17d0
movb $0x0, -0x340(%rbp)
movb $0x0, -0x220(%rbp)
movl $0x1, -0x34c(%rbp)
movl -0x34c(%rbp), %eax
cmpl -0x8(%rbp), %eax
jge 0x154b
movq -0x10(%rbp), %rax
movslq -0x34c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsbl (%rax), %eax
cmpl $0x2d, %eax
jne 0x14be
movq -0x10(%rbp), %rax
movslq -0x34c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsbl 0x1(%rax), %eax
cmpl $0x63, %eax
jne 0x13f3
movl $0x1, -0x348(%rbp)
jmp 0x14bc
movq -0x10(%rbp), %rax
movslq -0x34c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsbl 0x1(%rax), %eax
cmpl $0x77, %eax
jne 0x141a
movl $0x1, -0x344(%rbp)
jmp 0x14ba
movq -0x10(%rbp), %rax
movslq -0x34c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsbl 0x1(%rax), %eax
cmpl $0x61, %eax
jne 0x1478
leaq -0x340(%rbp), %rax
movq %rax, -0x358(%rbp)
movq -0x10(%rbp), %rax
movl -0x34c(%rbp), %ecx
addl $0x1, %ecx
movl %ecx, -0x34c(%rbp)
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rdi
callq 0x10f0
movq -0x358(%rbp), %rdi
movl %eax, %edx
leaq 0xbe0(%rip), %rsi       # 0x204f
movb $0x0, %al
callq 0x1100
jmp 0x14b8
movq -0x10(%rbp), %rax
movslq -0x34c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsbl 0x1(%rax), %eax
cmpl $0x6e, %eax
jne 0x14b6
leaq -0x220(%rbp), %rdi
movq -0x10(%rbp), %rax
movl -0x34c(%rbp), %ecx
addl $0x1, %ecx
movl %ecx, -0x34c(%rbp)
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rsi
callq 0x1030
jmp 0x14b8
jmp 0x14ba
jmp 0x14bc
jmp 0x1535
cmpq $0x0, -0x18(%rbp)
jne 0x1514
leaq -0x320(%rbp), %rdi
movq -0x10(%rbp), %rax
movslq -0x34c(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
callq 0x1030
movq -0x10(%rbp), %rax
movslq -0x34c(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
leaq 0xb76(%rip), %rsi       # 0x206c
callq 0x10e0
movq %rax, -0x18(%rbp)
cmpq $0x0, -0x18(%rbp)
jne 0x1512
movl $0x1, -0x4(%rbp)
jmp 0x17d0
jmp 0x1533
movq -0x10(%rbp), %rax
movslq -0x34c(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
leaq 0xb45(%rip), %rsi       # 0x206f
callq 0x10e0
movq %rax, -0x20(%rbp)
jmp 0x1535
jmp 0x1537
movl -0x34c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34c(%rbp)
jmp 0x13a2
leaq -0x320(%rbp), %rdi
callq 0x1210
leaq -0x120(%rbp), %rax
movq %rax, -0x360(%rbp)
leaq -0x320(%rbp), %rdi
callq 0x1260
movq -0x360(%rbp), %rdi
movq %rax, %rsi
callq 0x1030
leaq -0x320(%rbp), %rdi
callq 0x12b0
cmpb $0x0, -0x220(%rbp)
jne 0x15a8
leaq -0x220(%rbp), %rdi
leaq -0x320(%rbp), %rsi
callq 0x1030
movq -0x18(%rbp), %rdi
leaq 0xabf(%rip), %rsi       # 0x2072
movb $0x0, %al
callq 0x1090
cmpl $0x0, -0x348(%rbp)
je 0x160e
movq -0x20(%rbp), %rdi
leaq 0xad2(%rip), %rsi       # 0x20a0
movb $0x0, %al
callq 0x1090
movq -0x20(%rbp), %rdi
movl -0x344(%rbp), %ecx
leaq 0xafb(%rip), %rdx       # 0x20e1
leaq 0xad3(%rip), %rax       # 0x20c0
cmpl $0x0, %ecx
cmovneq %rax, %rdx
leaq -0x340(%rbp), %rcx
leaq 0xabf(%rip), %rsi       # 0x20c1
movb $0x0, %al
callq 0x1090
jmp 0x16d4
movq -0x18(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0x2, %edx
callq 0x10c0
movq -0x20(%rbp), %rax
movq %rax, -0x378(%rbp)
movl -0x344(%rbp), %edx
leaq 0xaa9(%rip), %rax       # 0x20e1
leaq 0xa81(%rip), %rcx       # 0x20c0
cmpl $0x0, %edx
cmovneq %rcx, %rax
movq %rax, -0x370(%rbp)
leaq -0x220(%rbp), %rax
movq %rax, -0x368(%rbp)
movq -0x18(%rbp), %rdi
callq 0x10a0
movq -0x378(%rbp), %rdi
movq -0x370(%rbp), %rdx
movq -0x368(%rbp), %rcx
movl %eax, %r8d
leaq 0xa64(%rip), %rsi       # 0x20e7
movb $0x0, %al
callq 0x1090
movq -0x18(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x10c0
movq -0x20(%rbp), %rdi
movl -0x344(%rbp), %ecx
leaq 0xa37(%rip), %rdx       # 0x20e1
leaq 0xa0f(%rip), %rax       # 0x20c0
cmpl $0x0, %ecx
cmovneq %rax, %rdx
leaq -0x220(%rbp), %rcx
leaq -0x340(%rbp), %r8
leaq 0xa30(%rip), %rsi       # 0x20fd
movb $0x0, %al
callq 0x1090
movl $0x0, -0x34c(%rbp)
movq -0x18(%rbp), %rdi
callq 0x1080
movl %eax, -0x350(%rbp)
movq -0x18(%rbp), %rdi
callq 0x10b0
cmpl $0x0, %eax
je 0x16fd
jmp 0x176c
cmpl $0x0, -0x34c(%rbp)
je 0x1718
movq -0x20(%rbp), %rdi
leaq 0xa00(%rip), %rsi       # 0x2111
movb $0x0, %al
callq 0x1090
movl -0x34c(%rbp), %eax
movl $0x20, %ecx
cltd
idivl %ecx
cmpl $0x0, %edx
jne 0x173d
movq -0x20(%rbp), %rdi
leaq 0x989(%rip), %rsi       # 0x20bf
movb $0x0, %al
callq 0x1090
movq -0x20(%rbp), %rdi
movl -0x350(%rbp), %eax
movzbl %al, %edx
leaq 0x9c2(%rip), %rsi       # 0x2113
movb $0x0, %al
callq 0x1090
movl -0x34c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34c(%rbp)
jmp 0x16de
movq -0x20(%rbp), %rdi
leaq 0x9a3(%rip), %rsi       # 0x211a
movb $0x0, %al
callq 0x1090
cmpl $0x0, -0x348(%rbp)
je 0x17a7
movq -0x20(%rbp), %rdi
leaq -0x320(%rbp), %rdx
leaq -0x120(%rbp), %rcx
leaq 0x97f(%rip), %rsi       # 0x211f
movb $0x0, %al
callq 0x1090
movq -0x18(%rbp), %rdi
callq 0x1040
movq -0x20(%rbp), %rax
movq 0x2825(%rip), %rcx      # 0x3fe0
cmpq (%rcx), %rax
je 0x17c9
movq -0x20(%rbp), %rdi
callq 0x1040
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x380, %rsp            # imm = 0x380
popq %rbp
retq