// File: /workspace/github_c_makefile_star2_O1/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 0x5339
movq %rsi, %r14
cmpl $0x0, (%rsi)
jle 0x5346
xorl %r15d, %r15d
leaq 0x345f(%rip), %r13      # 0x84c0
movslq %r15d, %r12
movq 0x8(%r14,%r12,8), %rdi
movl $0x0, %eax
testq %rdi, %rdi
je 0x507f
xorl %esi, %esi
movl $0xa, %edx
callq 0x2d03
testl %eax, %eax
je 0x5097
cmpl $0x7, %eax
ja 0x50a9
movl %eax, %eax
movzbl (%rax,%r13), %eax
orl %eax, 0x50(%rbx)
jmp 0x52cf
movl $0x0, 0x50(%rbx)
movw $0x7, 0x4c(%rbx)
jmp 0x52cf
leal -0x15(%rax), %ecx
cmpl $0x6, %ecx
ja 0x50c3
addl $-0x14, %eax
movzbl (%rax,%r13), %eax
notl %eax
andl %eax, 0x50(%rbx)
jmp 0x52cf
leal -0x1e(%rax), %ecx
cmpl $0x7, %ecx
ja 0x50d5
addb $-0x1e, %al
movb %al, 0x4c(%rbx)
jmp 0x52cf
cmpl $0x27, %eax
je 0x5125
cmpl $0x26, %eax
jne 0x512e
leal 0x2(%r15), %ebp
cmpl (%r14), %ebp
jge 0x5142
movq 0x10(%r14,%r12,8), %rdi
testq %rdi, %rdi
je 0x5142
xorl %esi, %esi
movl $0xa, %edx
callq 0x2d03
cmpl $0x5, %eax
jne 0x5142
movslq %ebp, %rax
movq 0x8(%r14,%rax,8), %rdi
testq %rdi, %rdi
je 0x526b
xorl %esi, %esi
movl $0xa, %edx
callq 0x2d03
jmp 0x526d
movb $0x7, 0x4c(%rbx)
jmp 0x52cf
movl %eax, %ecx
andl $-0x8, %ecx
cmpl $0x28, %ecx
jne 0x519b
addb $-0x28, %al
movb %al, 0x4d(%rbx)
jmp 0x52cf
leal 0x4(%r15), %eax
cmpl (%r14), %eax
jge 0x52cf
movq 0x10(%r14,%r12,8), %rdi
testq %rdi, %rdi
je 0x52cf
movl %eax, 0x4(%rsp)
xorl %esi, %esi
movl $0xa, %edx
callq 0x2d03
cmpl $0x2, %eax
jne 0x52cf
movslq %ebp, %rax
movq 0x8(%r14,%rax,8), %rdi
testq %rdi, %rdi
je 0x5275
xorl %esi, %esi
movl $0xa, %edx
callq 0x2d03
movq %rax, %r15
jmp 0x5278
cmpl $0x31, %eax
je 0x51eb
cmpl $0x30, %eax
jne 0x51f4
leal 0x2(%r15), %ebp
cmpl (%r14), %ebp
jge 0x5203
movq 0x10(%r14,%r12,8), %rdi
testq %rdi, %rdi
je 0x5203
xorl %esi, %esi
movl $0xa, %edx
callq 0x2d03
cmpl $0x5, %eax
jne 0x5203
movslq %ebp, %rax
movq 0x8(%r14,%rax,8), %rdi
testq %rdi, %rdi
je 0x52dd
xorl %esi, %esi
movl $0xa, %edx
callq 0x2d03
jmp 0x52df
movb $0x0, 0x4d(%rbx)
jmp 0x52cf
leal -0x5a(%rax), %ecx
cmpl $0x7, %ecx
ja 0x525c
addb $-0x52, %al
jmp 0x50cd
leal 0x4(%r15), %eax
cmpl (%r14), %eax
jge 0x52cf
movq 0x10(%r14,%r12,8), %rdi
testq %rdi, %rdi
je 0x52cf
movl %eax, 0x4(%rsp)
xorl %esi, %esi
movl $0xa, %edx
callq 0x2d03
cmpl $0x2, %eax
jne 0x52cf
movslq %ebp, %rax
movq 0x8(%r14,%rax,8), %rdi
testq %rdi, %rdi
je 0x52e4
xorl %esi, %esi
movl $0xa, %edx
callq 0x2d03
movq %rax, %r15
jmp 0x52e7
leal -0x64(%rax), %ecx
cmpl $0x7, %ecx
ja 0x52cf
addb $-0x5c, %al
jmp 0x513a
xorl %eax, %eax
movb %al, 0x4c(%rbx)
movl %ebp, %r15d
jmp 0x52cf
xorl %r15d, %r15d
movq 0x20(%r14,%r12,8), %rdi
testq %rdi, %rdi
je 0x5293
xorl %esi, %esi
movl $0xa, %edx
callq 0x2d03
movq %rax, %r12
jmp 0x5296
xorl %r12d, %r12d
movslq 0x4(%rsp), %rax
movq 0x8(%r14,%rax,8), %rdi
testq %rdi, %rdi
je 0x52b3
xorl %esi, %esi
movl $0xa, %edx
callq 0x2d03
jmp 0x52b5
xorl %eax, %eax
movzbl %r15b, %edi
movzbl %r12b, %esi
movzbl %al, %edx
callq 0x4f61
movb %al, 0x4c(%rbx)
movl 0x4(%rsp), %eax
movl %eax, %r15d
incl %r15d
cmpl (%r14), %r15d
jl 0x5061
jmp 0x5346
xorl %eax, %eax
movb %al, 0x4d(%rbx)
jmp 0x5270
xorl %r15d, %r15d
movq 0x20(%r14,%r12,8), %rdi
testq %rdi, %rdi
je 0x5302
xorl %esi, %esi
movl $0xa, %edx
callq 0x2d03
movq %rax, %r12
jmp 0x5305
xorl %r12d, %r12d
movslq 0x4(%rsp), %rax
movq 0x8(%r14,%rax,8), %rdi
testq %rdi, %rdi
je 0x5322
xorl %esi, %esi
movl $0xa, %edx
callq 0x2d03
jmp 0x5324
xorl %eax, %eax
movzbl %r15b, %edi
movzbl %r12b, %esi
movzbl %al, %edx
callq 0x4f61
movb %al, 0x4d(%rbx)
jmp 0x52c8
movl $0x0, 0x50(%rbx)
movw $0x7, 0x4c(%rbx)
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq