// File: /workspace/github_c_makefile_star2_O2/neovim[P]libvterm/src/pen.c
// Function: vterm_state_setpen
INTERNAL void vterm_state_setpen(VTermState *state, const long args[], int argcount)
{
  // SGR - ECMA-48 8.3.117

  int argi = 0;
  int value;

  while(argi < argcount) {
    // This logic is easier to do 'done' backwards; set it true, and make it
    // false again in the 'default' case
    int done = 1;

    long arg;
    switch(arg = CSI_ARG(args[argi])) {
    case CSI_ARG_MISSING:
    case 0: // Reset
      vterm_state_resetpen(state);
      break;

    case 1: { // Bold on
      const VTermColor *fg = &state->pen.fg;
      state->pen.bold = 1;
      setpenattr_bool(state, VTERM_ATTR_BOLD, 1);
      if(!VTERM_COLOR_IS_DEFAULT_FG(fg) && VTERM_COLOR_IS_INDEXED(fg) && fg->indexed.idx < 8 && state->bold_is_highbright)
        set_pen_col_ansi(state, VTERM_ATTR_FOREGROUND, fg->indexed.idx + (state->pen.bold ? 8 : 0));
      break;
    }

    case 3: // Italic on
      state->pen.italic = 1;
      setpenattr_bool(state, VTERM_ATTR_ITALIC, 1);
      break;

    case 4: // Underline
      state->pen.underline = VTERM_UNDERLINE_SINGLE;
      if(CSI_ARG_HAS_MORE(args[argi])) {
        argi++;
        switch(CSI_ARG(args[argi])) {
          case 0:
            state->pen.underline = 0;
            break;
          case 1:
            state->pen.underline = VTERM_UNDERLINE_SINGLE;
            break;
          case 2:
            state->pen.underline = VTERM_UNDERLINE_DOUBLE;
            break;
          case 3:
            state->pen.underline = VTERM_UNDERLINE_CURLY;
            break;
        }
      }
      setpenattr_int(state, VTERM_ATTR_UNDERLINE, state->pen.underline);
      break;

    case 5: // Blink
      state->pen.blink = 1;
      setpenattr_bool(state, VTERM_ATTR_BLINK, 1);
      break;

    case 7: // Reverse on
      state->pen.reverse = 1;
      setpenattr_bool(state, VTERM_ATTR_REVERSE, 1);
      break;

    case 8: // Conceal on
      state->pen.conceal = 1;
      setpenattr_bool(state, VTERM_ATTR_CONCEAL, 1);
      break;

    case 9: // Strikethrough on
      state->pen.strike = 1;
      setpenattr_bool(state, VTERM_ATTR_STRIKE, 1);
      break;

    case 10: case 11: case 12: case 13: case 14:
    case 15: case 16: case 17: case 18: case 19: // Select font
      state->pen.font = CSI_ARG(args[argi]) - 10;
      setpenattr_int(state, VTERM_ATTR_FONT, state->pen.font);
      break;

    case 21: // Underline double
      state->pen.underline = VTERM_UNDERLINE_DOUBLE;
      setpenattr_int(state, VTERM_ATTR_UNDERLINE, state->pen.underline);
      break;

    case 22: // Bold off
      state->pen.bold = 0;
      setpenattr_bool(state, VTERM_ATTR_BOLD, 0);
      break;

    case 23: // Italic and Gothic (currently unsupported) off
      state->pen.italic = 0;
      setpenattr_bool(state, VTERM_ATTR_ITALIC, 0);
      break;

    case 24: // Underline off
      state->pen.underline = 0;
      setpenattr_int(state, VTERM_ATTR_UNDERLINE, 0);
      break;

    case 25: // Blink off
      state->pen.blink = 0;
      setpenattr_bool(state, VTERM_ATTR_BLINK, 0);
      break;

    case 27: // Reverse off
      state->pen.reverse = 0;
      setpenattr_bool(state, VTERM_ATTR_REVERSE, 0);
      break;

    case 28: // Conceal off (Reveal)
      state->pen.conceal = 0;
      setpenattr_bool(state, VTERM_ATTR_CONCEAL, 0);
      break;

    case 29: // Strikethrough off
      state->pen.strike = 0;
      setpenattr_bool(state, VTERM_ATTR_STRIKE, 0);
      break;

    case 30: case 31: case 32: case 33:
    case 34: case 35: case 36: case 37: // Foreground colour palette
      value = CSI_ARG(args[argi]) - 30;
      if(state->pen.bold && state->bold_is_highbright)
        value += 8;
      set_pen_col_ansi(state, VTERM_ATTR_FOREGROUND, value);
      break;

    case 38: // Foreground colour alternative palette
      if(argcount - argi < 1)
        return;
      argi += 1 + lookup_colour(state, CSI_ARG(args[argi+1]), args+argi+2, argcount-argi-2, &state->pen.fg);
      setpenattr_col(state, VTERM_ATTR_FOREGROUND, state->pen.fg);
      break;

    case 39: // Foreground colour default
      state->pen.fg = state->default_fg;
      setpenattr_col(state, VTERM_ATTR_FOREGROUND, state->pen.fg);
      break;

    case 40: case 41: case 42: case 43:
    case 44: case 45: case 46: case 47: // Background colour palette
      value = CSI_ARG(args[argi]) - 40;
      set_pen_col_ansi(state, VTERM_ATTR_BACKGROUND, value);
      break;

    case 48: // Background colour alternative palette
      if(argcount - argi < 1)
        return;
      argi += 1 + lookup_colour(state, CSI_ARG(args[argi+1]), args+argi+2, argcount-argi-2, &state->pen.bg);
      setpenattr_col(state, VTERM_ATTR_BACKGROUND, state->pen.bg);
      break;

    case 49: // Default background
      state->pen.bg = state->default_bg;
      setpenattr_col(state, VTERM_ATTR_BACKGROUND, state->pen.bg);
      break;

    case 73: // Superscript
    case 74: // Subscript
    case 75: // Superscript/subscript off
      state->pen.small = (arg != 75);
      state->pen.baseline =
        (arg == 73) ? VTERM_BASELINE_RAISE :
        (arg == 74) ? VTERM_BASELINE_LOWER :
                      VTERM_BASELINE_NORMAL;
      setpenattr_bool(state, VTERM_ATTR_SMALL,    state->pen.small);
      setpenattr_int (state, VTERM_ATTR_BASELINE, state->pen.baseline);
      break;

    case 90: case 91: case 92: case 93:
    case 94: case 95: case 96: case 97: // Foreground colour high-intensity palette
      value = CSI_ARG(args[argi]) - 90 + 8;
      set_pen_col_ansi(state, VTERM_ATTR_FOREGROUND, value);
      break;

    case 100: case 101: case 102: case 103:
    case 104: case 105: case 106: case 107: // Background colour high-intensity palette
      value = CSI_ARG(args[argi]) - 100 + 8;
      set_pen_col_ansi(state, VTERM_ATTR_BACKGROUND, value);
      break;

    default:
      done = 0;
      break;
    }

    if(!done)
      DEBUG_LOG("libvterm: Unhandled CSI SGR %ld\n", arg);

    while(CSI_ARG_HAS_MORE(args[argi++]));
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %edx, %ebx
movq %rsi, %r14
movq %rdi, %r15
movabsq $0x7fffffff00000000, %rbp # imm = 0x7FFFFFFF00000000
leaq 0x138(%rdi), %rax
movq %rax, 0x10(%rsp)
leaq 0x134(%rdi), %rax
movq %rax, 0x8(%rsp)
xorl %r12d, %r12d
leaq 0x6bf4(%rip), %r13      # 0xa370
cmpl %r12d, %ebx
jle 0x3b33
movslq %r12d, %rcx
movq (%r14,%rcx,8), %rdx
movl %edx, %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpq $0x6b, %rax
ja 0x38c8
leaq (%r14,%rcx,8), %rsi
movslq (%r13,%rax,4), %rdi
addq %r13, %rdi
jmpq *%rdi
shll $0x8, %edx
addl $0x600, %edx            # imm = 0x600
movl %edx, %eax
andl $0xf00, %eax            # imm = 0xF00
movl $0xfffff0ff, %ecx       # imm = 0xFFFFF0FF
andl 0x13c(%r15), %ecx
orl %eax, %ecx
movw %cx, 0x13c(%r15)
shrl $0x8, %edx
andl $0xf, %edx
pushq $0x8
popq %rsi
movq %r15, %rdi
jmp 0x3b15
shlq $0x20, %rdx
andq %rbp, %rdx
movabsq $-0x5c00000000, %rax    # imm = 0xFFFFFFA400000000
jmp 0x3806
shlq $0x20, %rdx
andq %rbp, %rdx
movabsq $-0x2800000000, %rax    # imm = 0xFFFFFFD800000000
addq %rdx, %rax
sarq $0x20, %rax
pushq $0xa
jmp 0x3859
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
leal -0x1e(%rdx), %eax
testb $0x1, 0x13c(%r15)
je 0x3834
addl $-0x16, %edx
cmpl $0x0, 0x188(%r15)
cmovel %eax, %edx
movl %edx, %eax
movslq %eax, %rdx
pushq $0x9
popq %rsi
movq %r15, %rdi
jmp 0x3860
shlq $0x20, %rdx
andq %rbp, %rdx
movabsq $-0x5200000000, %rax    # imm = 0xFFFFFFAE00000000
addq %rdx, %rax
sarq $0x20, %rax
pushq $0x9
popq %rsi
movq %r15, %rdi
movq %rax, %rdx
callq 0x3b42
jmp 0x3b1a
xorl %edx, %edx
cmpq $0x4b, %rax
setne %dl
movl %edx, %ecx
shll $0xc, %ecx
movl $0xffff8fff, %esi       # imm = 0xFFFF8FFF
andl 0x13c(%r15), %esi
xorl %edi, %edi
cmpq $0x4a, %rax
sete %dil
shll $0xe, %edi
cmpq $0x49, %rax
movl $0x2000, %eax           # imm = 0x2000
cmovnel %edi, %eax
orl %ecx, %esi
orl %eax, %esi
movw %si, 0x13c(%r15)
pushq $0xb
popq %rsi
movq %r15, %rdi
callq 0x34ac
movl 0x13c(%r15), %edx
shrl $0xd, %edx
andl $0x3, %edx
pushq $0xc
jmp 0x3b11
cmpl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
jne 0x3b1a
movq %r15, %rdi
callq 0x33b7
jmp 0x3b1a
movl %ebx, %edx
subl %r12d, %edx
jle 0x3b33
movl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
andl 0x8(%rsi), %edi
addq $0x10, %rsi
addl $-0x2, %edx
movq 0x8(%rsp), %rcx
callq 0x3b65
addl %eax, %r12d
incl %r12d
movl 0x134(%r15), %edx
jmp 0x3abd
andb $-0x2, 0x13c(%r15)
pushq $0x1
jmp 0x3aa2
andb $-0x7, 0x13c(%r15)
pushq $0x2
popq %rsi
movq %r15, %rdi
xorl %edx, %edx
jmp 0x3b15
andb $-0x11, 0x13c(%r15)
pushq $0x4
jmp 0x3aa2
movzwl 0x13c(%r15), %edi
movl %edi, %eax
andl $-0x7, %eax
leal 0x2(%rax), %edx
movw %dx, 0x13c(%r15)
testb $-0x80, 0x3(%rsi)
je 0x3b0a
incl %r12d
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
andq 0x8(%r14,%rcx,8), %rsi
je 0x3b00
cmpl $0x3, %esi
je 0x3afb
cmpl $0x2, %esi
jne 0x3b0a
orl $0x4, %eax
jmp 0x3b00
orb $0x20, 0x13c(%r15)
pushq $0x5
jmp 0x3a67
orb $0x40, 0x13c(%r15)
pushq $0x6
jmp 0x3a67
orb $0x8, 0x13c(%r15)
pushq $0x3
jmp 0x3a67
orb $0x10, 0x13c(%r15)
pushq $0x4
jmp 0x3a67
orb $0x1, 0x13c(%r15)
pushq $0x1
popq %rdx
movq %r15, %rdi
movl %edx, %esi
callq 0x34ac
movb 0x134(%r15), %al
andb $0x3, %al
cmpb $0x1, %al
jne 0x3b1a
movzbl 0x135(%r15), %eax
cmpq $0x7, %rax
ja 0x3b1a
cmpl $0x0, 0x188(%r15)
je 0x3b1a
movl 0x13c(%r15), %edx
shll $0x3, %edx
andl $0x8, %edx
orq %rax, %rdx
jmp 0x3837
andb $-0x21, 0x13c(%r15)
pushq $0x5
jmp 0x3aa2
movl 0x13c(%r15), %eax
andl $-0x7, %eax
orl $0x4, %eax
movw %ax, 0x13c(%r15)
pushq $0x2
popq %rdx
movq %r15, %rdi
movl %edx, %esi
jmp 0x3b15
orb $-0x80, 0x13c(%r15)
pushq $0x7
popq %rsi
pushq $0x1
popq %rdx
movq %r15, %rdi
jmp 0x3aa8
andb $-0x9, 0x13c(%r15)
pushq $0x3
jmp 0x3aa2
movl 0x144(%r15), %edx
movl %edx, 0x138(%r15)
jmp 0x3aee
andb $-0x41, 0x13c(%r15)
pushq $0x6
jmp 0x3aa2
andb $0x7f, 0x13c(%r15)
pushq $0x7
popq %rsi
movq %r15, %rdi
xorl %edx, %edx
callq 0x34ac
jmp 0x3b1a
movl 0x140(%r15), %edx
movl %edx, 0x134(%r15)
pushq $0x9
jmp 0x3af0
movl %ebx, %edx
subl %r12d, %edx
jle 0x3b33
movl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
andl 0x8(%rsi), %edi
addq $0x10, %rsi
addl $-0x2, %edx
movq 0x10(%rsp), %rcx
callq 0x3b65
addl %eax, %r12d
incl %r12d
movl 0x138(%r15), %edx
pushq $0xa
popq %rsi
movq %r15, %rdi
callq 0x34dc
jmp 0x3b1a
orl $0x6, %edi
movl %edi, %eax
movw %ax, 0x13c(%r15)
movl %eax, %edx
shrl %edx
andl $0x3, %edx
pushq $0x2
popq %rsi
movq %r15, %rdi
callq 0x34c4
movslq %r12d, %rax
leaq (%r14,%rax,8), %rax
incl %r12d
testb $-0x80, 0x3(%rax)
leaq 0x8(%rax), %rax
jne 0x3b21
jmp 0x377c
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq