// File: /workspace/github_c_makefile_star2_O2/lecram[P]congif/term.c
// Function: set_default_palette
void
set_default_palette(char * pname)
{
    static struct {
        char * name;
        uint8_t * plt;
    } pal[] = {
        { "xterm", plt_xterm },
        { "vga", plt_vga_ansi },
        { "sol_ansi", plt_solarized_like_ansi },
        { "solarized", plt_solarized },
        { "putty", plt_putty },
        { 0, 0}
    };

    if (pname[0] == '@') {
        int i;
        for(i=0; pal[i].name; i++) {
            if (strcasecmp(pname+1, pal[i].name) == 0) {
                def_plt = pal[i].plt;
                return;
            }
        }
        fprintf(stderr, "Known standard palette names are:\n");
        for(i=0; pal[i].name; i++)
            fprintf(stderr, "    @%s\n", pal[i].name);
        exit(2);
    } else {
        FILE * fd;
        char buf[BUFSIZ];
        Term * term = 0;
        uint8_t * plt = malloc(sizeof(term->plt));
        memcpy(plt, def_plt, sizeof(term->plt));
        def_plt = plt;

        if ((fd = fopen(pname, "r")) == 0) {
            perror(pname); exit(1);
        }
        while (fgets(buf, sizeof(buf), fd) != 0) {
            char * s = buf, *e;
            long cno = 0, colour = 0;

            while (*s == ' ' || *s == '\t') s++;
            if (*s == '#') continue;

            if (strncasecmp(s, "color", 5) == 0) {
                s += 5;
            } else if (strncasecmp(s, "colour", 6) == 0) {
                s += 6;
            } else continue;

            // Only lines that match /^ *colou?r[0-9]+/i
            if (*s < '0' || *s > '9') continue;

            cno = strtol(s, &e, 0);
            if (s == e) goto bad_line;
            if (cno < 0 || cno > 16) goto bad_line;

            s = e;
            while (*s == ' ' || *s == '\t' || *s == '#' || *s == '=') s++;
            colour = strtol(s, &e, 16);
            if (e-s != 6) goto bad_line;

            plt[cno*3+0] = ((colour>>16) & 0xFF);
            plt[cno*3+1] = ((colour>> 8) & 0xFF);
            plt[cno*3+2] = ((colour    ) & 0xFF);
            continue;
bad_line:
            fprintf(stderr, "Bad line in colour file: %s", buf);
            exit(2);
        }

        fclose(fd);
    }
    return;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2028, %rsp           # imm = 0x2028
movq %rdi, %r15
cmpb $0x40, (%rdi)
jne 0x137a
incq %r15
xorl %ecx, %ecx
leaq 0x5a28(%rip), %r14      # 0x6d70
leaq 0x10(%rcx), %rbx
cmpq $0x60, %rbx
je 0x1518
movq (%rcx,%r14), %rsi
movq %r15, %rdi
callq 0x1050
movq %rbx, %rcx
testl %eax, %eax
jne 0x1348
movq -0x8(%rbx,%r14), %rax
movq %rax, 0x5e9b(%rip)      # 0x7210
jmp 0x1506
pushq $0x30
popq %rdi
callq 0x1150
movq 0x5e87(%rip), %rcx      # 0x7210
movups (%rcx), %xmm0
movups 0x10(%rcx), %xmm1
movups 0x20(%rcx), %xmm2
movups %xmm0, (%rax)
movups %xmm1, 0x10(%rax)
movups %xmm2, 0x20(%rax)
movq %rax, 0x18(%rsp)
movq %rax, 0x5e65(%rip)      # 0x7210
leaq 0x3431(%rip), %rsi      # 0x47e3
movq %r15, %rdi
callq 0x1190
testq %rax, %rax
je 0x1580
movq %rax, %r14
leaq 0x20(%rsp), %r12
leaq 0x340d(%rip), %rbp      # 0x47df
pushq $0x5
popq %rbx
movabsq $0x2000000900000200, %r13 # imm = 0x2000000900000200
movq %rax, 0x10(%rsp)
movq %r12, %rdi
movl $0x2000, %esi           # imm = 0x2000
movq %r14, %rdx
callq 0x1100
testq %rax, %rax
je 0x14fe
movq %r12, %r15
movzbl (%r15), %eax
cmpl $0x9, %eax
je 0x140e
cmpl $0x20, %eax
jne 0x1413
incq %r15
jmp 0x1400
cmpl $0x23, %eax
je 0x13e4
movq %r15, %rdi
movq %rbp, %rsi
movq %rbx, %rdx
callq 0x1160
movq %rbx, %rcx
testl %eax, %eax
je 0x1458
movq %r15, %rdi
leaq 0x33ae(%rip), %rsi      # 0x47e5
movq %r14, %rbx
movq %rbp, %r14
pushq $0x6
popq %rbp
movq %rbp, %rdx
callq 0x1160
movq %rbp, %rcx
movq %r14, %rbp
movq %rbx, %r14
pushq $0x5
popq %rbx
testl %eax, %eax
jne 0x13e4
movb (%r15,%rcx), %al
addb $-0x3a, %al
cmpb $-0xa, %al
jb 0x13e4
movq %r12, %r14
movq %rbx, %r12
movq %rbp, %rbx
addq %rcx, %r15
movq %r15, %rdi
leaq 0x8(%rsp), %rsi
xorl %edx, %edx
callq 0x1140
movq 0x8(%rsp), %rdi
cmpq %rdi, %r15
je 0x155b
movq %rax, %rbp
cmpq $0x10, %rax
ja 0x155b
leaq 0x6(%rdi), %r15
movzbl (%rdi), %eax
cmpq $0x3d, %rax
ja 0x14b3
btq %rax, %r13
jae 0x14b3
incq %rdi
incq %r15
jmp 0x149c
leaq 0x8(%rsp), %rsi
pushq $0x10
popq %rdx
callq 0x1140
cmpq %r15, 0x8(%rsp)
jne 0x155b
movl %eax, %ecx
shrl $0x10, %ecx
leaq (,%rbp,2), %rdx
addq %rbp, %rdx
movq 0x18(%rsp), %rsi
movb %cl, (%rsi,%rdx)
movb %ah, 0x1(%rsi,%rdx)
movb %al, 0x2(%rsi,%rdx)
movq %rbx, %rbp
movq %r12, %rbx
movq %r14, %r12
movq 0x10(%rsp), %r14
jmp 0x13e4
movq %r14, %rdi
callq 0x1090
addq $0x2028, %rsp           # imm = 0x2028
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x5ad1(%rip), %r15      # 0x6ff0
movq (%r15), %rcx
leaq 0x328a(%rip), %rdi      # 0x47b3
pushq $0x22
popq %rsi
pushq $0x1
popq %rdx
callq 0x1200
leaq 0x329b(%rip), %rbx      # 0x47d6
xorl %r12d, %r12d
cmpq $0x50, %r12
je 0x1578
movq (%r12,%r14), %rdx
movq (%r15), %rdi
movq %rbx, %rsi
xorl %eax, %eax
callq 0x1130
addq $0x10, %r12
jmp 0x153e
movq 0x5a8e(%rip), %rax      # 0x6ff0
movq (%rax), %rdi
leaq 0x3280(%rip), %rsi      # 0x47ec
leaq 0x20(%rsp), %rdx
xorl %eax, %eax
callq 0x1130
pushq $0x2
popq %rdi
callq 0x11f0
movq %r15, %rdi
callq 0x11a0
pushq $0x1
jmp 0x157a