// File: /workspace/github_c_makefile_star2_O3/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
cmpl $0x0, (%rsi)
jle 0x534a
movq %rsi, %r14
xorl %r15d, %r15d
leaq 0x347f(%rip), %r13      # 0x84e0
movslq %r15d, %r12
movq 0x8(%r14,%r12,8), %rdi
testq %rdi, %rdi
je 0x5090
xorl %esi, %esi
movl $0xa, %edx
callq 0x2d12
testl %eax, %eax
je 0x5090
cmpl $0x7, %eax
ja 0x50aa
andl $0x7, %eax
movzbl (%rax,%r13), %eax
orl %eax, 0x50(%rbx)
jmp 0x509d
movl $0x0, 0x50(%rbx)
movw $0x7, 0x4c(%rbx)
incl %r15d
cmpl (%r14), %r15d
jl 0x5061
jmp 0x5357
leal -0x15(%rax), %ecx
cmpl $0x6, %ecx
ja 0x50c1
addl $-0x14, %eax
movzbl (%rax,%r13), %eax
notl %eax
andl %eax, 0x50(%rbx)
jmp 0x509d
leal -0x1e(%rax), %ecx
cmpl $0x7, %ecx
ja 0x50d0
addb $-0x1e, %al
movb %al, 0x4c(%rbx)
jmp 0x509d
cmpl $0x27, %eax
je 0x512e
cmpl $0x26, %eax
jne 0x5137
leal 0x2(%r15), %ebp
movl (%r14), %eax
cmpl %eax, %ebp
jge 0x51c4
movq 0x10(%r14,%r12,8), %rdi
testq %rdi, %rdi
je 0x51c4
xorl %esi, %esi
movl $0xa, %edx
callq 0x2d12
cmpl $0x5, %eax
jne 0x51c1
movslq %ebp, %rax
movq 0x8(%r14,%rax,8), %rdi
testq %rdi, %rdi
je 0x5228
xorl %esi, %esi
movl $0xa, %edx
callq 0x2d12
jmp 0x522a
movb $0x7, 0x4c(%rbx)
jmp 0x509d
movl %eax, %ecx
andl $-0x8, %ecx
cmpl $0x28, %ecx
jne 0x514b
addb $-0x28, %al
movb %al, 0x4d(%rbx)
jmp 0x509d
cmpl $0x31, %eax
je 0x51a9
cmpl $0x30, %eax
jne 0x51b2
leal 0x2(%r15), %ebp
movl (%r14), %eax
cmpl %eax, %ebp
jge 0x5297
movq 0x10(%r14,%r12,8), %rdi
testq %rdi, %rdi
je 0x5297
xorl %esi, %esi
movl $0xa, %edx
callq 0x2d12
cmpl $0x5, %eax
jne 0x5294
movslq %ebp, %rax
movq 0x8(%r14,%rax,8), %rdi
testq %rdi, %rdi
je 0x52e8
xorl %esi, %esi
movl $0xa, %edx
callq 0x2d12
jmp 0x52ea
movb $0x0, 0x4d(%rbx)
jmp 0x509d
leal -0x5a(%rax), %ecx
cmpl $0x7, %ecx
ja 0x5215
addb $-0x52, %al
jmp 0x50cb
movl (%r14), %eax
leal 0x4(%r15), %ecx
cmpl %eax, %ecx
jge 0x509d
movq 0x10(%r14,%r12,8), %rdi
testq %rdi, %rdi
je 0x509d
movl %ecx, 0x4(%rsp)
xorl %esi, %esi
movl $0xa, %edx
callq 0x2d12
cmpl $0x2, %eax
jne 0x509d
movslq %ebp, %rax
movq 0x8(%r14,%rax,8), %rdi
testq %rdi, %rdi
je 0x5235
xorl %esi, %esi
movl $0xa, %edx
callq 0x2d12
movq %rax, %r15
jmp 0x5238
leal -0x64(%rax), %ecx
cmpl $0x7, %ecx
ja 0x509d
addb $-0x5c, %al
jmp 0x5143
xorl %eax, %eax
movb %al, 0x4c(%rbx)
movl %ebp, %r15d
jmp 0x509d
xorl %r15d, %r15d
movq 0x20(%r14,%r12,8), %rdi
testq %rdi, %rdi
je 0x5253
xorl %esi, %esi
movl $0xa, %edx
callq 0x2d12
movq %rax, %r12
jmp 0x5256
xorl %r12d, %r12d
movslq 0x4(%rsp), %rax
movq 0x8(%r14,%rax,8), %rdi
testq %rdi, %rdi
je 0x5273
xorl %esi, %esi
movl $0xa, %edx
callq 0x2d12
jmp 0x5275
xorl %eax, %eax
movzbl %r15b, %edi
movzbl %r12b, %esi
movzbl %al, %edx
callq 0x4f6e
movb %al, 0x4c(%rbx)
movl 0x4(%rsp), %eax
movl %eax, %r15d
jmp 0x509d
movl (%r14), %eax
leal 0x4(%r15), %ecx
cmpl %eax, %ecx
jge 0x509d
movq 0x10(%r14,%r12,8), %rdi
testq %rdi, %rdi
je 0x509d
movl %ecx, 0x4(%rsp)
xorl %esi, %esi
movl $0xa, %edx
callq 0x2d12
cmpl $0x2, %eax
jne 0x509d
movslq %ebp, %rax
movq 0x8(%r14,%rax,8), %rdi
testq %rdi, %rdi
je 0x52f2
xorl %esi, %esi
movl $0xa, %edx
callq 0x2d12
movq %rax, %r15
jmp 0x52f5
xorl %eax, %eax
movb %al, 0x4d(%rbx)
jmp 0x522d
xorl %r15d, %r15d
movq 0x20(%r14,%r12,8), %rdi
testq %rdi, %rdi
je 0x5310
xorl %esi, %esi
movl $0xa, %edx
callq 0x2d12
movq %rax, %r12
jmp 0x5313
xorl %r12d, %r12d
movslq 0x4(%rsp), %rax
movq 0x8(%r14,%rax,8), %rdi
testq %rdi, %rdi
je 0x5330
xorl %esi, %esi
movl $0xa, %edx
callq 0x2d12
jmp 0x5332
xorl %eax, %eax
movzbl %r15b, %edi
movzbl %r12b, %esi
movzbl %al, %edx
callq 0x4f6e
movb %al, 0x4d(%rbx)
jmp 0x5288
movl $0x0, 0x50(%rbx)
movw $0x7, 0x4c(%rbx)
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq