// File: /workspace/github_c_makefile_star2_O0/lecram[P]congif/term.c
// Function: sgr
static void
sgr(Term *term, int n, int *params)
{
    int i, number;
    for(i=0; i<n; i++) {
        number = params[i];

        switch (number) {
        case 0:
            term->attr = def_attr;
            term->pair = def_pair;
            break;
        case 1:
            term->attr |= A_BOLD;
            break;
        case 2:
            term->attr |= A_DIM;
            break;
        case 3:
            term->attr |= A_ITALIC;
            break;
        case 4:
            term->attr |= A_UNDERLINE;
            break;
        case 5:
            term->attr |= A_BLINK;
            break;
        case 7:
            term->attr |= A_INVERSE;
            break;
        case 8:
            term->attr |= A_INVISIBLE;
            break;
        case 10:
            /* TODO: reset toggle meta flag */
            term->cs_array[term->cs_index = 0] = (term->mode&M_ISOLAT1)?CS_ISO:CS_BMP;
            term->mode &= ~M_DISPCTRL;
            break;
        case 11:
            /* TODO: reset toggle meta flag */
            term->cs_array[term->cs_index] = CS_437;
            term->mode |= M_DISPCTRL;
            break;
        case 12:
            /* TODO: set toggle meta flag */
            term->cs_array[term->cs_index] = CS_437;
            term->mode |= M_DISPCTRL;
            break;
        case 21:
#ifndef OLDLINUX
            term->attr |= A_UNDERLINE; /* Linux say should be: DOUBLE_ULINE */
            break;
#endif
        case 22:
            term->attr &= ~A_DIM;
            term->attr &= ~A_BOLD;
            break;
        case 23:
            term->attr &= ~A_ITALIC;
            break;
        case 24:
            term->attr &= ~A_UNDERLINE;
            break;
        case 25:
            term->attr &= ~A_BLINK;
            break;
        case 27:
            term->attr &= ~A_INVERSE;
            break;
        case 30: case 31: case 32: case 33: case 34: case 35: case 36: case 37:
            term->pair = ((number - 30) << 4) | (term->pair & 0x0F);
            break;
#ifdef OLDLINUX
        case 38:
            term->attr |= A_UNDERLINE;
            term->pair = (DEF_FORE << 4) | (term->pair & 0x0F);
            break;
        case 39:
            term->attr &= ~A_UNDERLINE;
            term->pair = (DEF_FORE << 4) | (term->pair & 0x0F);
            break;
#endif
#ifndef OLDLINUX
        case 38:
            i++ ;
            if (i>n) break;
            if (params[i] == 5) {
                /* 256 colours */
                term->pair = (fake_colour_256(params[i+1]) << 4) | (term->pair & 0x0F);
                i++;
                break;
            } else if (params[i] == 2) {
                /* 16M colours, note I'm using common form not strict ITU T.416 */
                term->pair = (fake_colour_16m(params[i+1],params[i+2],params[i+3]) << 4) | (term->pair & 0x0F);
                i+=3;
                break;
            }
            /* Could be CMYK; probably broken */
            i = n;
            break;
        case 39:
            term->pair = (DEF_FORE << 4) | (term->pair & 0x0F);
            break;
#endif
        case 40: case 41: case 42: case 43: case 44: case 45: case 46: case 47:
            term->pair = (term->pair & 0xF0) | (number - 40);
            break;
#ifndef OLDLINUX
        case 48:
            i++ ;
            if (i>n) break;
            if (params[i] == 5) {
                /* 256 colours */
                term->pair = (term->pair & 0xF0) | fake_colour_256(params[i+1]);
                i++;
                break;
            } else if (params[i] == 2) {
                /* 16M colours, note I'm using common form not strict ITU T.416 */
                term->pair = (term->pair & 0xF0) | fake_colour_16m(params[i+1],params[i+2],params[i+3]);
                i+=3;
                break;
            }
            /* Could be CMYK; probably broken */
            i = n;
            break;
#endif
        case 49:
            term->pair = (term->pair & 0xF0) | DEF_BACK;
            break;
        case 90: case 91: case 92: case 93: case 94: case 95: case 96: case 97:
            term->pair = ((number - 90 + 8) << 4) | (term->pair & 0x0F);
            break;
        case 100: case 101: case 102: case 103: case 104: case 105: case 106: case 107:
            term->pair = (term->pair & 0xF0) | (number - 100 + 8);
            break;
        default:
            logfmt("UNS: SGR %d\n", number);
        }
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq %rdx, -0x18(%rbp)
movl $0x0, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
cmpl -0xc(%rbp), %eax
jge 0x435a
movq -0x18(%rbp), %rax
movslq -0x1c(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x20(%rbp)
movl -0x20(%rbp), %eax
movq %rax, -0x28(%rbp)
subq $0x6b, %rax
ja 0x4339
movq -0x28(%rbp), %rax
leaq 0x3460(%rip), %rcx      # 0x7360
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movb 0xa8e9(%rip), %cl       # 0xe7f8
movq -0x8(%rbp), %rax
movb %cl, 0x1a(%rax)
movb 0x6776(%rip), %cl       # 0xa692
movq -0x8(%rbp), %rax
movb %cl, 0x1b(%rax)
jmp 0x434a
movq -0x8(%rbp), %rax
movzbl 0x1a(%rax), %ecx
orl $0x1, %ecx
movb %cl, 0x1a(%rax)
jmp 0x434a
movq -0x8(%rbp), %rax
movzbl 0x1a(%rax), %ecx
orl $0x2, %ecx
movb %cl, 0x1a(%rax)
jmp 0x434a
movq -0x8(%rbp), %rax
movzbl 0x1a(%rax), %ecx
orl $0x4, %ecx
movb %cl, 0x1a(%rax)
jmp 0x434a
movq -0x8(%rbp), %rax
movzbl 0x1a(%rax), %ecx
orl $0x8, %ecx
movb %cl, 0x1a(%rax)
jmp 0x434a
movq -0x8(%rbp), %rax
movzbl 0x1a(%rax), %ecx
orl $0x10, %ecx
movb %cl, 0x1a(%rax)
jmp 0x434a
movq -0x8(%rbp), %rax
movzbl 0x1a(%rax), %ecx
orl $0x20, %ecx
movb %cl, 0x1a(%rax)
jmp 0x434a
movq -0x8(%rbp), %rax
movzbl 0x1a(%rax), %ecx
orl $0x40, %ecx
movb %cl, 0x1a(%rax)
jmp 0x434a
movq -0x8(%rbp), %rax
movzwl 0x18(%rax), %edx
andl $0x1000, %edx           # imm = 0x1000
xorl %ecx, %ecx
movl $0x1, %eax
cmpl $0x0, %edx
cmovnel %eax, %ecx
movq -0x8(%rbp), %rax
movq -0x8(%rbp), %rdx
movl $0x0, 0x38(%rdx)
movl %ecx, 0x30(%rax)
movq -0x8(%rbp), %rax
movzwl 0x18(%rax), %ecx
andl $-0x2, %ecx
movw %cx, 0x18(%rax)
jmp 0x434a
movq -0x8(%rbp), %rax
movq -0x8(%rbp), %rcx
movslq 0x38(%rcx), %rcx
movl $0x3, 0x30(%rax,%rcx,4)
movq -0x8(%rbp), %rax
movzwl 0x18(%rax), %ecx
orl $0x1, %ecx
movw %cx, 0x18(%rax)
jmp 0x434a
movq -0x8(%rbp), %rax
movq -0x8(%rbp), %rcx
movslq 0x38(%rcx), %rcx
movl $0x3, 0x30(%rax,%rcx,4)
movq -0x8(%rbp), %rax
movzwl 0x18(%rax), %ecx
orl $0x1, %ecx
movw %cx, 0x18(%rax)
jmp 0x434a
movq -0x8(%rbp), %rax
movzbl 0x1a(%rax), %ecx
orl $0x8, %ecx
movb %cl, 0x1a(%rax)
jmp 0x434a
movq -0x8(%rbp), %rax
movzbl 0x1a(%rax), %ecx
andl $-0x3, %ecx
movb %cl, 0x1a(%rax)
movq -0x8(%rbp), %rax
movzbl 0x1a(%rax), %ecx
andl $-0x2, %ecx
movb %cl, 0x1a(%rax)
jmp 0x434a
movq -0x8(%rbp), %rax
movzbl 0x1a(%rax), %ecx
andl $-0x5, %ecx
movb %cl, 0x1a(%rax)
jmp 0x434a
movq -0x8(%rbp), %rax
movzbl 0x1a(%rax), %ecx
andl $-0x9, %ecx
movb %cl, 0x1a(%rax)
jmp 0x434a
movq -0x8(%rbp), %rax
movzbl 0x1a(%rax), %ecx
andl $-0x11, %ecx
movb %cl, 0x1a(%rax)
jmp 0x434a
movq -0x8(%rbp), %rax
movzbl 0x1a(%rax), %ecx
andl $-0x21, %ecx
movb %cl, 0x1a(%rax)
jmp 0x434a
movl -0x20(%rbp), %eax
subl $0x1e, %eax
shll $0x4, %eax
movq -0x8(%rbp), %rcx
movzbl 0x1b(%rcx), %ecx
andl $0xf, %ecx
orl %ecx, %eax
movb %al, %cl
movq -0x8(%rbp), %rax
movb %cl, 0x1b(%rax)
jmp 0x434a
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
cmpl -0xc(%rbp), %eax
jle 0x40f8
jmp 0x434a
movq -0x18(%rbp), %rax
movslq -0x1c(%rbp), %rcx
cmpl $0x5, (%rax,%rcx,4)
jne 0x4142
movq -0x18(%rbp), %rax
movl -0x1c(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %edi
callq 0x4390
shll $0x4, %eax
movq -0x8(%rbp), %rcx
movzbl 0x1b(%rcx), %ecx
andl $0xf, %ecx
orl %ecx, %eax
movb %al, %cl
movq -0x8(%rbp), %rax
movb %cl, 0x1b(%rax)
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x434a
movq -0x18(%rbp), %rax
movslq -0x1c(%rbp), %rcx
cmpl $0x2, (%rax,%rcx,4)
jne 0x41ac
movq -0x18(%rbp), %rax
movl -0x1c(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %edi
movq -0x18(%rbp), %rax
movl -0x1c(%rbp), %ecx
addl $0x2, %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %esi
movq -0x18(%rbp), %rax
movl -0x1c(%rbp), %ecx
addl $0x3, %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %edx
callq 0x4520
shll $0x4, %eax
movq -0x8(%rbp), %rcx
movzbl 0x1b(%rcx), %ecx
andl $0xf, %ecx
orl %ecx, %eax
movb %al, %cl
movq -0x8(%rbp), %rax
movb %cl, 0x1b(%rax)
movl -0x1c(%rbp), %eax
addl $0x3, %eax
movl %eax, -0x1c(%rbp)
jmp 0x434a
jmp 0x41ae
movl -0xc(%rbp), %eax
movl %eax, -0x1c(%rbp)
jmp 0x434a
movq -0x8(%rbp), %rax
movzbl 0x1b(%rax), %eax
andl $0xf, %eax
orl $0x70, %eax
movb %al, %cl
movq -0x8(%rbp), %rax
movb %cl, 0x1b(%rax)
jmp 0x434a
movq -0x8(%rbp), %rax
movzbl 0x1b(%rax), %eax
andl $0xf0, %eax
movl -0x20(%rbp), %ecx
subl $0x28, %ecx
orl %ecx, %eax
movb %al, %cl
movq -0x8(%rbp), %rax
movb %cl, 0x1b(%rax)
jmp 0x434a
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
cmpl -0xc(%rbp), %eax
jle 0x420e
jmp 0x434a
movq -0x18(%rbp), %rax
movslq -0x1c(%rbp), %rcx
cmpl $0x5, (%rax,%rcx,4)
jne 0x425f
movq -0x8(%rbp), %rax
movzbl 0x1b(%rax), %eax
andl $0xf0, %eax
movl %eax, -0x2c(%rbp)
movq -0x18(%rbp), %rax
movl -0x1c(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %edi
callq 0x4390
movl %eax, %ecx
movl -0x2c(%rbp), %eax
orl %ecx, %eax
movb %al, %cl
movq -0x8(%rbp), %rax
movb %cl, 0x1b(%rax)
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x434a
movq -0x18(%rbp), %rax
movslq -0x1c(%rbp), %rcx
cmpl $0x2, (%rax,%rcx,4)
jne 0x42cd
movq -0x8(%rbp), %rax
movzbl 0x1b(%rax), %eax
andl $0xf0, %eax
movl %eax, -0x30(%rbp)
movq -0x18(%rbp), %rax
movl -0x1c(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %edi
movq -0x18(%rbp), %rax
movl -0x1c(%rbp), %ecx
addl $0x2, %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %esi
movq -0x18(%rbp), %rax
movl -0x1c(%rbp), %ecx
addl $0x3, %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %edx
callq 0x4520
movl %eax, %ecx
movl -0x30(%rbp), %eax
orl %ecx, %eax
movb %al, %cl
movq -0x8(%rbp), %rax
movb %cl, 0x1b(%rax)
movl -0x1c(%rbp), %eax
addl $0x3, %eax
movl %eax, -0x1c(%rbp)
jmp 0x434a
jmp 0x42cf
movl -0xc(%rbp), %eax
movl %eax, -0x1c(%rbp)
jmp 0x434a
movq -0x8(%rbp), %rax
movzbl 0x1b(%rax), %eax
andl $0xf0, %eax
orl $0x0, %eax
movb %al, %cl
movq -0x8(%rbp), %rax
movb %cl, 0x1b(%rax)
jmp 0x434a
movl -0x20(%rbp), %eax
subl $0x5a, %eax
addl $0x8, %eax
shll $0x4, %eax
movq -0x8(%rbp), %rcx
movzbl 0x1b(%rcx), %ecx
andl $0xf, %ecx
orl %ecx, %eax
movb %al, %cl
movq -0x8(%rbp), %rax
movb %cl, 0x1b(%rax)
jmp 0x434a
movq -0x8(%rbp), %rax
movzbl 0x1b(%rax), %eax
andl $0xf0, %eax
movl -0x20(%rbp), %ecx
subl $0x64, %ecx
addl $0x8, %ecx
orl %ecx, %eax
movb %al, %cl
movq -0x8(%rbp), %rax
movb %cl, 0x1b(%rax)
jmp 0x434a
movl -0x20(%rbp), %esi
leaq 0x3418(%rip), %rdi      # 0x775b
movb $0x0, %al
callq 0x2220
jmp 0x434c
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x3eca
addq $0x30, %rsp
popq %rbp
retq