// File: /workspace/github_c_makefile_star2_O1/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 $0x28, %rsp
movl %edx, %ebx
movq %rsi, %r14
movq %rdi, %r15
leaq 0x138(%rdi), %rbp
leaq 0x134(%rdi), %rax
movq %rax, 0x20(%rsp)
xorl %r12d, %r12d
leaq 0x782c(%rip), %r13      # 0xb370
cmpl %r12d, %ebx
jle 0x4183
movslq %r12d, %rdi
movq (%r14,%rdi,8), %rax
movl %eax, %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpq $0x6b, %rcx
ja 0x3d49
leaq (%r14,%rdi,8), %rsi
movslq (%r13,%rcx,4), %rdx
addq %r13, %rdx
jmpq *%rdx
shll $0x8, %eax
addl $0x600, %eax            # imm = 0x600
movl %eax, %ecx
movl $0xfffff0ff, %edx       # imm = 0xFFFFF0FF
andl 0x13c(%r15), %edx
andl $0xf00, %ecx            # imm = 0xF00
orl %ecx, %edx
movw %dx, 0x13c(%r15)
shrl $0x8, %eax
andl $0xf, %eax
movl %eax, 0x8(%rsp)
movq 0x8(%r15), %rax
testq %rax, %rax
je 0x3c83
movq 0x30(%rax), %rax
testq %rax, %rax
je 0x3c83
movq 0x10(%r15), %rdx
leaq 0x8(%rsp), %rsi
movl $0x8, %edi
jmp 0x3c81
addb $-0x5c, %al
jmp 0x3bd7
addb $-0x28, %al
movb $0x1, 0x138(%r15)
movb %al, 0x139(%r15)
movl 0x138(%r15), %eax
movl %eax, 0x8(%rsp)
movq 0x8(%r15), %rax
testq %rax, %rax
je 0x3c83
movq 0x30(%rax), %rax
testq %rax, %rax
je 0x3c83
movq 0x10(%r15), %rdx
leaq 0x8(%rsp), %rsi
movl $0xa, %edi
jmp 0x3c81
leal -0x1e(%rax), %ecx
testb $0x1, 0x13c(%r15)
je 0x3c34
addl $-0x16, %eax
cmpl $0x0, 0x188(%r15)
cmovel %ecx, %eax
movl %eax, %ecx
movb $0x1, 0x134(%r15)
movb %cl, 0x135(%r15)
jmp 0x3c56
addb $-0x52, %al
movb $0x1, 0x134(%r15)
movb %al, 0x135(%r15)
movl 0x134(%r15), %eax
movl %eax, 0x8(%rsp)
movq 0x8(%r15), %rax
testq %rax, %rax
je 0x3c83
movq 0x30(%rax), %rax
testq %rax, %rax
je 0x3c83
movq 0x10(%r15), %rdx
leaq 0x8(%rsp), %rsi
movl $0x9, %edi
callq *%rax
movslq %r12d, %rax
leaq (%r14,%rax,8), %rax
incl %r12d
testb $-0x80, 0x3(%rax)
leaq 0x8(%rax), %rax
jne 0x3c8a
movb $0x1, %al
testb %al, %al
jne 0x3b44
jmp 0x4183
xorl %eax, %eax
cmpq $0x4b, %rcx
setne %al
movl %eax, %edx
shll $0xc, %edx
movl $0xffff8fff, %esi       # imm = 0xFFFF8FFF
andl 0x13c(%r15), %esi
xorl %edi, %edi
cmpq $0x4a, %rcx
sete %dil
shll $0xe, %edi
cmpq $0x49, %rcx
movl $0x2000, %ecx           # imm = 0x2000
cmovnel %edi, %ecx
orl %edx, %esi
orl %ecx, %esi
movw %si, 0x13c(%r15)
movl %eax, 0x8(%rsp)
movq 0x8(%r15), %rax
testq %rax, %rax
je 0x3d0b
movq 0x30(%rax), %rax
testq %rax, %rax
je 0x3d0b
movq 0x10(%r15), %rdx
leaq 0x8(%rsp), %rsi
movl $0xb, %edi
callq *%rax
movl 0x13c(%r15), %eax
shrl $0xd, %eax
andl $0x3, %eax
movl %eax, 0x8(%rsp)
movq 0x8(%r15), %rax
testq %rax, %rax
je 0x3c83
movq 0x30(%rax), %rax
testq %rax, %rax
je 0x3c83
movq 0x10(%r15), %rdx
leaq 0x8(%rsp), %rsi
movl $0xc, %edi
jmp 0x3c81
cmpl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
jne 0x3c83
movq %r15, %rdi
callq 0x3524
jmp 0x3c83
movl %ebx, %edx
subl %r12d, %edx
jle 0x4132
movq %rbp, 0x18(%rsp)
movl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
andl 0x8(%rsi), %edi
addq $0x10, %rsi
addl $-0x2, %edx
movq 0x20(%rsp), %rcx
callq 0x4192
movl %eax, %ebp
movl 0x134(%r15), %eax
movl %eax, 0x8(%rsp)
movq 0x8(%r15), %rax
testq %rax, %rax
je 0x4122
movq 0x30(%rax), %rax
testq %rax, %rax
je 0x4122
movq 0x10(%r15), %rdx
leaq 0x8(%rsp), %rsi
movl $0x9, %edi
jmp 0x4120
andb $-0x2, 0x13c(%r15)
movl $0x0, 0x8(%rsp)
movq 0x8(%r15), %rax
testq %rax, %rax
je 0x3c83
movq 0x30(%rax), %rax
testq %rax, %rax
je 0x3c83
movq 0x10(%r15), %rdx
leaq 0x8(%rsp), %rsi
movl $0x1, %edi
jmp 0x3c81
andb $-0x7, 0x13c(%r15)
movl $0x0, 0x8(%rsp)
jmp 0x4156
andb $-0x11, 0x13c(%r15)
movl $0x0, 0x8(%rsp)
jmp 0x3ecf
movzwl 0x13c(%r15), %eax
movl %eax, %edx
andl $-0x7, %edx
leal 0x2(%rdx), %ecx
movw %cx, 0x13c(%r15)
testb $-0x80, 0x3(%rsi)
je 0x4146
incl %r12d
movq 0x8(%r14,%rdi,8), %rsi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
cmpq $0x3, %rsi
ja 0x4146
leaq 0x76b3(%rip), %rdi      # 0xb520
movslq (%rdi,%rsi,4), %rsi
addq %rdi, %rsi
jmpq *%rsi
orl $0x4, %edx
movl %edx, %ecx
jmp 0x413e
orb $0x20, 0x13c(%r15)
movl $0x1, 0x8(%rsp)
jmp 0x3f8f
orb $0x40, 0x13c(%r15)
movl $0x1, 0x8(%rsp)
jmp 0x4053
orb $0x8, 0x13c(%r15)
movl $0x1, 0x8(%rsp)
jmp 0x4003
orb $0x10, 0x13c(%r15)
movl $0x1, 0x8(%rsp)
movq 0x8(%r15), %rax
testq %rax, %rax
je 0x3c83
movq 0x30(%rax), %rax
testq %rax, %rax
je 0x3c83
movq 0x10(%r15), %rdx
leaq 0x8(%rsp), %rsi
movl $0x4, %edi
jmp 0x3c81
orb $0x1, 0x13c(%r15)
movl $0x1, 0x8(%rsp)
movq 0x8(%r15), %rax
testq %rax, %rax
je 0x3f2e
movq 0x30(%rax), %rax
testq %rax, %rax
je 0x3f2e
movq 0x10(%r15), %rdx
leaq 0x8(%rsp), %rsi
movl $0x1, %edi
callq *%rax
movb 0x134(%r15), %al
andb $0x3, %al
cmpb $0x1, %al
jne 0x3c83
movb 0x135(%r15), %al
cmpb $0x7, %al
ja 0x3c83
cmpl $0x0, 0x188(%r15)
je 0x3c83
movb 0x13c(%r15), %cl
shlb $0x3, %cl
andb $0x8, %cl
orb %al, %cl
movb $0x1, 0x134(%r15)
movb %cl, 0x135(%r15)
jmp 0x3c56
andb $-0x21, 0x13c(%r15)
movl $0x0, 0x8(%rsp)
movq 0x8(%r15), %rax
testq %rax, %rax
je 0x3c83
movq 0x30(%rax), %rax
testq %rax, %rax
je 0x3c83
movq 0x10(%r15), %rdx
leaq 0x8(%rsp), %rsi
movl $0x5, %edi
jmp 0x3c81
movl 0x13c(%r15), %eax
andl $-0x7, %eax
orl $0x4, %eax
movw %ax, 0x13c(%r15)
movl $0x2, 0x8(%rsp)
jmp 0x4156
orb $-0x80, 0x13c(%r15)
movl $0x1, 0x8(%rsp)
jmp 0x4090
andb $-0x9, 0x13c(%r15)
movl $0x0, 0x8(%rsp)
movq 0x8(%r15), %rax
testq %rax, %rax
je 0x3c83
movq 0x30(%rax), %rax
testq %rax, %rax
je 0x3c83
movq 0x10(%r15), %rdx
leaq 0x8(%rsp), %rsi
movl $0x3, %edi
jmp 0x3c81
movl 0x144(%r15), %eax
movl %eax, 0x138(%r15)
jmp 0x3bed
andb $-0x41, 0x13c(%r15)
movl $0x0, 0x8(%rsp)
movq 0x8(%r15), %rax
testq %rax, %rax
je 0x3c83
movq 0x30(%rax), %rax
testq %rax, %rax
je 0x3c83
movq 0x10(%r15), %rdx
leaq 0x8(%rsp), %rsi
movl $0x6, %edi
jmp 0x3c81
andb $0x7f, 0x13c(%r15)
movl $0x0, 0x8(%rsp)
movq 0x8(%r15), %rax
testq %rax, %rax
je 0x3c83
movq 0x30(%rax), %rax
testq %rax, %rax
je 0x3c83
movq 0x10(%r15), %rdx
leaq 0x8(%rsp), %rsi
movl $0x7, %edi
jmp 0x3c81
movl 0x140(%r15), %eax
movl %eax, 0x134(%r15)
jmp 0x3c5d
movl %ebx, %edx
subl %r12d, %edx
jle 0x4132
movl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
andl 0x8(%rsi), %edi
addq $0x10, %rsi
addl $-0x2, %edx
movq %rbp, 0x18(%rsp)
movq %rbp, %rcx
callq 0x4192
movl %eax, %ebp
movl 0x138(%r15), %eax
movl %eax, 0x8(%rsp)
movq 0x8(%r15), %rax
testq %rax, %rax
je 0x4122
movq 0x30(%rax), %rax
testq %rax, %rax
je 0x4122
movq 0x10(%r15), %rdx
leaq 0x8(%rsp), %rsi
movl $0xa, %edi
callq *%rax
addl %ebp, %r12d
incl %r12d
movq 0x18(%rsp), %rbp
jmp 0x3c83
xorl %eax, %eax
jmp 0x3c99
orl $0x6, %eax
movl %eax, %ecx
movw %cx, 0x13c(%r15)
movl 0x13c(%r15), %eax
shrl %eax
andl $0x3, %eax
movl %eax, 0x8(%rsp)
movq 0x8(%r15), %rax
testq %rax, %rax
je 0x3c83
movq 0x30(%rax), %rax
testq %rax, %rax
je 0x3c83
movq 0x10(%r15), %rdx
leaq 0x8(%rsp), %rsi
movl $0x2, %edi
jmp 0x3c81
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq