// File: /workspace/github_c_makefile_star2_O2/uobikiemukot[P]yaft/./ctrlseq/csi.h
// Function: set_attr
void set_attr(struct terminal_t *term, struct parm_t *parm)
{
	/* SGR: Set Graphic Rendition
	 * 	ESC [ Pm m
	 * 	Pm:
	 *		0: reset all attribute
	 *
	 *		1: brighten foreground color (only work for color index 0-7)
	 *		4: underline
	 *		5: brighten background color (only work for color index 0-7)
	 *		7: reverse (swap foreground/background color)
	 *
	 *		21: reset brighten foreground color
	 *		24: reset underline
	 *		25: reset brighten background color
	 *		27: reset reverse
	 *
	 *		30-37: select foreground color (color index 0-7)
	 *		38: special foreground color selection: implemented in select_color_value()
	 *
	 *		40-47: select background color (color index 0-7)
	 *		48: special foreground color selection: implemented in select_color_value()
	 */
	int i, num;

	if (parm->argc <= 0) {
		term->attribute     = ATTR_RESET;
		term->color_pair.fg = DEFAULT_FG;
		term->color_pair.bg = DEFAULT_BG;
		return;
	}

	for (i = 0; i < parm->argc; i++) {
		num = dec2num(parm->argv[i]);
		//logging(DEBUG, "argc:%d num:%d\n", parm->argc, num);

		if (num == 0) {                        /* reset all attribute and color */
			term->attribute = ATTR_RESET;
			term->color_pair.fg = DEFAULT_FG;
			term->color_pair.bg = DEFAULT_BG;
		} else if (1 <= num && num <= 7) {     /* set attribute */
			term->attribute |= attr_mask[num];
		} else if (21 <= num && num <= 27) {   /* reset attribute */
			term->attribute &= ~attr_mask[num - 20];
		} else if (30 <= num && num <= 37) {   /* set foreground */
			term->color_pair.fg = (num - 30);
		} else if (num == 38) {                /* special foreground color selection */
			/* select foreground color from 256 color index */
			if ((i + 2) < parm->argc && dec2num(parm->argv[i + 1]) == 5) {
				term->color_pair.fg = dec2num(parm->argv[i + 2]);
				i += 2;
			/* select foreground color from specified rgb color */
			} else if ((i + 4) < parm->argc && dec2num(parm->argv[i + 1]) == 2) {
				term->color_pair.fg = rgb2index(dec2num(parm->argv[i + 2]),
					dec2num(parm->argv[i + 3]), dec2num(parm->argv[i + 4]));
				i += 4;
			}
		} else if (num == 39) {                /* reset foreground */
			term->color_pair.fg = DEFAULT_FG;
		} else if (40 <= num && num <= 47) {   /* set background */
			term->color_pair.bg = (num - 40);
		} else if (num == 48) {                /* special background  color selection */
			/* select background color from 256 color index */
			if ((i + 2) < parm->argc && dec2num(parm->argv[i + 1]) == 5) {
				term->color_pair.bg = dec2num(parm->argv[i + 2]);
				i += 2;
			/* select background color from specified rgb color */
			} else if ((i + 4) < parm->argc && dec2num(parm->argv[i + 1]) == 2) {
				term->color_pair.bg = rgb2index(dec2num(parm->argv[i + 2]),
					dec2num(parm->argv[i + 3]), dec2num(parm->argv[i + 4]));
				i += 4;
			}
		} else if (num == 49) {                /* reset background */
			term->color_pair.bg = DEFAULT_BG;
		} else if (90 <= num && num <= 97) {   /* set bright foreground */
			term->color_pair.fg = (num - 90) + BRIGHT_INC;
		} else if (100 <= num && num <= 107) { /* set bright background */
			term->color_pair.bg = (num - 100) + BRIGHT_INC;
		}
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %rbx
movl (%rsi), %eax
testl %eax, %eax
jle 0x4b60
movq %rsi, %r14
xorl %r15d, %r15d
leaq 0x2bac(%rip), %r12      # 0x74e0
cmpl %eax, %r15d
jge 0x4b6a
movslq %r15d, %r13
movq 0x8(%r14,%r13,8), %rdi
callq 0x2dd7
testl %eax, %eax
je 0x4962
cmpl $0x7, %eax
ja 0x4971
movl %eax, %eax
movzbl (%rax,%r12), %eax
orl %eax, 0x50(%rbx)
jmp 0x4b55
andl $0x0, 0x50(%rbx)
movw $0x7, 0x4c(%rbx)
jmp 0x4b55
leal -0x15(%rax), %ecx
cmpl $0x6, %ecx
ja 0x498b
addl $-0x14, %eax
movzbl (%rax,%r12), %eax
notl %eax
andl %eax, 0x50(%rbx)
jmp 0x4b55
leal -0x1e(%rax), %ecx
cmpl $0x7, %ecx
ja 0x499e
addl $-0x1e, %eax
movb %al, 0x4c(%rbx)
jmp 0x4b55
cmpl $0x27, %eax
je 0x49e7
cmpl $0x26, %eax
jne 0x49f0
leal 0x2(%r15), %ebp
movl (%r14), %eax
movslq %ebp, %rcx
movq %rcx, (%rsp)
cmpl %eax, %ebp
jge 0x4a56
movq 0x10(%r14,%r13,8), %rdi
callq 0x2dd7
cmpl $0x5, %eax
jne 0x4a53
movq (%rsp), %rax
movq 0x8(%r14,%rax,8), %rdi
callq 0x2dd7
movb %al, 0x4c(%rbx)
jmp 0x4abc
movb $0x7, 0x4c(%rbx)
jmp 0x4b55
movl %eax, %ecx
andl $-0x8, %ecx
cmpl $0x28, %ecx
jne 0x4a05
addl $-0x28, %eax
movb %al, 0x4d(%rbx)
jmp 0x4b55
cmpl $0x31, %eax
je 0x4ac4
cmpl $0x30, %eax
jne 0x4acd
leal 0x2(%r15), %ebp
movl (%r14), %eax
movslq %ebp, %rcx
movq %rcx, (%rsp)
cmpl %eax, %ebp
jge 0x4af2
movq 0x10(%r14,%r13,8), %rdi
callq 0x2dd7
cmpl $0x5, %eax
jne 0x4aef
movq (%rsp), %rax
movq 0x8(%r14,%rax,8), %rdi
callq 0x2dd7
movb %al, 0x4d(%rbx)
jmp 0x4abc
movl (%r14), %eax
leal 0x4(%r15), %ebp
cmpl %eax, %ebp
jge 0x4b55
movq 0x10(%r14,%r13,8), %rdi
callq 0x2dd7
cmpl $0x2, %eax
jne 0x4b55
movq (%rsp), %rax
movq 0x8(%r14,%rax,8), %rdi
callq 0x2dd7
movl %eax, (%rsp)
movq 0x20(%r14,%r13,8), %rdi
callq 0x2dd7
movl %eax, %r15d
movslq %ebp, %rax
movq 0x8(%r14,%rax,8), %rdi
callq 0x2dd7
movl $0xff, %ecx
movl (%rsp), %edi
andl %ecx, %edi
andl %ecx, %r15d
andl %ecx, %eax
movl %r15d, %esi
movl %eax, %edx
callq 0x483a
movb %al, 0x4c(%rbx)
movl %ebp, %r15d
jmp 0x4b55
movb $0x0, 0x4d(%rbx)
jmp 0x4b55
leal -0x5a(%rax), %ecx
cmpl $0x7, %ecx
ja 0x4add
addl $-0x52, %eax
jmp 0x4996
movl %eax, %ecx
addl $-0x64, %ecx
cmpl $0x7, %ecx
ja 0x4b55
addl $-0x5c, %eax
jmp 0x49fd
movl (%r14), %eax
leal 0x4(%r15), %ebp
cmpl %eax, %ebp
jge 0x4b55
movq 0x10(%r14,%r13,8), %rdi
callq 0x2dd7
cmpl $0x2, %eax
jne 0x4b55
movq (%rsp), %rax
movq 0x8(%r14,%rax,8), %rdi
callq 0x2dd7
movl %eax, (%rsp)
movq 0x20(%r14,%r13,8), %rdi
callq 0x2dd7
movl %eax, %r15d
movslq %ebp, %rax
movq 0x8(%r14,%rax,8), %rdi
callq 0x2dd7
movl $0xff, %ecx
movl (%rsp), %edi
andl %ecx, %edi
andl %ecx, %r15d
andl %ecx, %eax
movl %r15d, %esi
movl %eax, %edx
callq 0x483a
movb %al, 0x4d(%rbx)
jmp 0x4abc
incl %r15d
movl (%r14), %eax
jmp 0x4934
andl $0x0, 0x50(%rbx)
movw $0x7, 0x4c(%rbx)
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq