// File: /workspace/github_c_makefile_star2_O1/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 $0x2018, %rsp           # imm = 0x2018
movq %rdi, %r15
cmpb $0x40, (%rdi)
jne 0x1549
incq %r15
leaq 0x428a(%rip), %rsi      # 0x5790
movq %r15, %rdi
callq 0x1050
testl %eax, %eax
je 0x16c0
xorl %ecx, %ecx
leaq 0x6851(%rip), %rbx      # 0x7d70
leaq 0x10(%rcx), %r14
cmpq $0x50, %r14
je 0x16e4
movq 0x10(%rcx,%rbx), %rsi
movq %r15, %rdi
callq 0x1050
movq %r14, %rcx
testl %eax, %eax
jne 0x151f
addq %r14, %rbx
jmp 0x16c7
movl $0x30, %edi
callq 0x1150
movq %rax, %rbx
movq 0x6cb3(%rip), %rax      # 0x8210
movups (%rax), %xmm0
movups 0x10(%rax), %xmm1
movups 0x20(%rax), %xmm2
movups %xmm0, (%rbx)
movups %xmm1, 0x10(%rbx)
movups %xmm2, 0x20(%rbx)
movq %rbx, 0x6c96(%rip)      # 0x8210
leaq 0x4262(%rip), %rsi      # 0x57e3
movq %r15, %rdi
callq 0x1190
testq %rax, %rax
je 0x16f3
movq %rax, %r14
leaq 0x10(%rsp), %r13
movq %r13, %rdi
movl $0x2000, %esi           # imm = 0x2000
movq %rax, %rdx
callq 0x1100
testq %rax, %rax
je 0x16b6
movabsq $0x2000000900000200, %r12 # imm = 0x2000000900000200
movq %r13, %r15
movzbl (%r15), %eax
cmpl $0x9, %eax
je 0x15ce
cmpl $0x20, %eax
jne 0x15d3
incq %r15
jmp 0x15c0
cmpl $0x23, %eax
je 0x169d
movl $0x5, %ebp
movl $0x5, %edx
movq %r15, %rdi
leaq 0x41ef(%rip), %rsi      # 0x57df
callq 0x1160
testl %eax, %eax
je 0x161a
movl $0x6, %ebp
movl $0x6, %edx
movq %r15, %rdi
leaq 0x41d8(%rip), %rsi      # 0x57e5
callq 0x1160
testl %eax, %eax
jne 0x169d
movb (%r15,%rbp), %al
addb $-0x3a, %al
cmpb $-0xa, %al
jb 0x169d
addq %rbp, %r15
movq %r15, %rdi
leaq 0x8(%rsp), %rsi
xorl %edx, %edx
callq 0x1140
movq 0x8(%rsp), %rdi
cmpq %rdi, %r15
je 0x16e9
movq %rax, %rbp
cmpq $0x10, %rax
ja 0x16e9
leaq 0x6(%rdi), %r15
movzbl (%rdi), %eax
cmpq $0x3d, %rax
ja 0x166c
btq %rax, %r12
jae 0x166c
incq %rdi
incq %r15
jmp 0x1655
leaq 0x8(%rsp), %rsi
movl $0x10, %edx
callq 0x1140
cmpq %r15, 0x8(%rsp)
jne 0x16e9
movl %eax, %ecx
shrl $0x10, %ecx
leaq (,%rbp,2), %rdx
addq %rbp, %rdx
movb %cl, (%rbx,%rdx)
movb %ah, 0x1(%rbx,%rdx)
movb %al, 0x2(%rbx,%rdx)
movq %r13, %rdi
movl $0x2000, %esi           # imm = 0x2000
movq %r14, %rdx
callq 0x1100
testq %rax, %rax
jne 0x15bd
movq %r14, %rdi
callq 0x1090
jmp 0x16d2
leaq 0x66a9(%rip), %rbx      # 0x7d70
movq 0x8(%rbx), %rax
movq %rax, 0x6b3e(%rip)      # 0x8210
addq $0x2018, %rsp           # imm = 0x2018
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x1262
leaq 0x10(%rsp), %rdi
callq 0x1230
movq %r15, %rdi
callq 0x1254