// File: /workspace/github_c_makefile_star2_O3/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:
testl %edx, %edx
jle 0x449b
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), %rax
movq %rax, 0x20(%rsp)
leaq 0x134(%rdi), %rax
movq %rax, 0x18(%rsp)
xorl %r12d, %r12d
leaq 0x8517(%rip), %r13      # 0xc370
movslq %r12d, %rdx
movq (%r14,%rdx,8), %rax
movl %eax, %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpq $0x6b, %rcx
ja 0x4043
leaq (%r14,%rdx,8), %rsi
movslq (%r13,%rcx,4), %rdi
addq %r13, %rdi
jmpq *%rdi
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 0x3f92
movq 0x30(%rax), %rax
testq %rax, %rax
je 0x3f92
movq 0x10(%r15), %rdx
leaq 0x8(%rsp), %rsi
movl $0x8, %edi
jmp 0x3f90
addb $-0x5c, %al
jmp 0x3ee3
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 0x3f92
movq 0x30(%rax), %rax
testq %rax, %rax
je 0x3f92
movq 0x10(%r15), %rdx
leaq 0x8(%rsp), %rsi
movl $0xa, %edi
jmp 0x3f90
leal -0x1e(%rax), %ecx
testb $0x1, 0x13c(%r15)
je 0x3f43
addb $-0x16, %al
cmpl $0x0, 0x188(%r15)
movzbl %cl, %edx
movzbl %al, %ecx
cmovel %edx, %ecx
movb $0x1, 0x134(%r15)
movb %cl, 0x135(%r15)
jmp 0x3f65
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 0x3f92
movq 0x30(%rax), %rax
testq %rax, %rax
je 0x3f92
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 0x3f99
cmpl %ebx, %r12d
jl 0x3e59
jmp 0x448d
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 0x3f92
movq 0x30(%rax), %r8
testq %r8, %r8
je 0x441e
movq 0x10(%r15), %rdx
leaq 0x8(%rsp), %rbp
movl $0xb, %edi
movq %rbp, %rsi
callq *%r8
movl 0x13c(%r15), %ecx
movq 0x8(%r15), %rax
shrl $0xd, %ecx
andl $0x3, %ecx
movl %ecx, (%rbp)
testq %rax, %rax
jne 0x4428
jmp 0x3f92
cmpl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
jne 0x3f92
movq %r15, %rdi
callq 0x3523
jmp 0x3f92
movl %ebx, %edx
subl %r12d, %edx
jle 0x448d
movl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
andl 0x8(%rsi), %edi
addq $0x10, %rsi
addl $-0x2, %edx
movq 0x18(%rsp), %rcx
callq 0x449c
movl %eax, %ebp
movl 0x134(%r15), %eax
movl %eax, 0x8(%rsp)
movq 0x8(%r15), %rax
testq %rax, %rax
je 0x4413
movq 0x30(%rax), %rax
testq %rax, %rax
je 0x4413
movq 0x10(%r15), %rdx
leaq 0x8(%rsp), %rsi
movl $0x9, %edi
jmp 0x4411
andb $-0x2, 0x13c(%r15)
movl $0x0, 0x8(%rsp)
movq 0x8(%r15), %rax
testq %rax, %rax
je 0x3f92
movq 0x30(%rax), %rax
testq %rax, %rax
je 0x3f92
movq 0x10(%r15), %rdx
leaq 0x8(%rsp), %rsi
movl $0x1, %edi
jmp 0x3f90
andb $-0x7, 0x13c(%r15)
movl $0x0, 0x8(%rsp)
jmp 0x4460
andb $-0x11, 0x13c(%r15)
movl $0x0, 0x8(%rsp)
jmp 0x41bf
movzwl 0x13c(%r15), %edi
movl %edi, %ecx
andl $-0x7, %ecx
leal 0x2(%rcx), %eax
movw %ax, 0x13c(%r15)
testb $-0x80, 0x3(%rsi)
je 0x4457
incl %r12d
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
andq 0x8(%r14,%rdx,8), %rsi
je 0x444d
cmpl $0x3, %esi
je 0x4448
cmpl $0x2, %esi
jne 0x4457
orl $0x4, %ecx
jmp 0x444d
orb $0x20, 0x13c(%r15)
movl $0x1, 0x8(%rsp)
jmp 0x427f
orb $0x40, 0x13c(%r15)
movl $0x1, 0x8(%rsp)
jmp 0x4343
orb $0x8, 0x13c(%r15)
movl $0x1, 0x8(%rsp)
jmp 0x42f3
orb $0x10, 0x13c(%r15)
movl $0x1, 0x8(%rsp)
movq 0x8(%r15), %rax
testq %rax, %rax
je 0x3f92
movq 0x30(%rax), %rax
testq %rax, %rax
je 0x3f92
movq 0x10(%r15), %rdx
leaq 0x8(%rsp), %rsi
movl $0x4, %edi
jmp 0x3f90
orb $0x1, 0x13c(%r15)
movl $0x1, 0x8(%rsp)
movq 0x8(%r15), %rax
testq %rax, %rax
je 0x421e
movq 0x30(%rax), %rax
testq %rax, %rax
je 0x421e
movq 0x10(%r15), %rdx
leaq 0x8(%rsp), %rsi
movl $0x1, %edi
callq *%rax
movq 0x18(%rsp), %rax
movb (%rax), %al
andb $0x3, %al
cmpb $0x1, %al
jne 0x3f92
movb 0x135(%r15), %al
cmpb $0x7, %al
ja 0x3f92
cmpl $0x0, 0x188(%r15)
je 0x3f92
movb 0x13c(%r15), %cl
shlb $0x3, %cl
andb $0x8, %cl
orb %al, %cl
movb $0x1, 0x134(%r15)
movb %cl, 0x135(%r15)
jmp 0x3f65
andb $-0x21, 0x13c(%r15)
movl $0x0, 0x8(%rsp)
movq 0x8(%r15), %rax
testq %rax, %rax
je 0x3f92
movq 0x30(%rax), %rax
testq %rax, %rax
je 0x3f92
movq 0x10(%r15), %rdx
leaq 0x8(%rsp), %rsi
movl $0x5, %edi
jmp 0x3f90
movl 0x13c(%r15), %eax
andl $-0x7, %eax
orl $0x4, %eax
movw %ax, 0x13c(%r15)
movl $0x2, 0x8(%rsp)
jmp 0x4460
orb $-0x80, 0x13c(%r15)
movl $0x1, 0x8(%rsp)
jmp 0x4380
andb $-0x9, 0x13c(%r15)
movl $0x0, 0x8(%rsp)
movq 0x8(%r15), %rax
testq %rax, %rax
je 0x3f92
movq 0x30(%rax), %rax
testq %rax, %rax
je 0x3f92
movq 0x10(%r15), %rdx
leaq 0x8(%rsp), %rsi
movl $0x3, %edi
jmp 0x3f90
movl 0x144(%r15), %eax
movl %eax, 0x138(%r15)
jmp 0x3ef9
andb $-0x41, 0x13c(%r15)
movl $0x0, 0x8(%rsp)
movq 0x8(%r15), %rax
testq %rax, %rax
je 0x3f92
movq 0x30(%rax), %rax
testq %rax, %rax
je 0x3f92
movq 0x10(%r15), %rdx
leaq 0x8(%rsp), %rsi
movl $0x6, %edi
jmp 0x3f90
andb $0x7f, 0x13c(%r15)
movl $0x0, 0x8(%rsp)
movq 0x8(%r15), %rax
testq %rax, %rax
je 0x3f92
movq 0x30(%rax), %rax
testq %rax, %rax
je 0x3f92
movq 0x10(%r15), %rdx
leaq 0x8(%rsp), %rsi
movl $0x7, %edi
jmp 0x3f90
movl 0x140(%r15), %eax
movl %eax, 0x134(%r15)
jmp 0x3f6c
movl %ebx, %edx
subl %r12d, %edx
jle 0x448d
movl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
andl 0x8(%rsi), %edi
addq $0x10, %rsi
addl $-0x2, %edx
movq 0x20(%rsp), %rcx
callq 0x449c
movl %eax, %ebp
movl 0x138(%r15), %eax
movl %eax, 0x8(%rsp)
movq 0x8(%r15), %rax
testq %rax, %rax
je 0x4413
movq 0x30(%rax), %rax
testq %rax, %rax
je 0x4413
movq 0x10(%r15), %rdx
leaq 0x8(%rsp), %rsi
movl $0xa, %edi
callq *%rax
addl %ebp, %r12d
incl %r12d
jmp 0x3f92
movzwl %cx, %ecx
shrl $0xd, %ecx
movl %ecx, 0x8(%rsp)
movq 0x30(%rax), %rax
testq %rax, %rax
je 0x3f92
movq 0x10(%r15), %rdx
leaq 0x8(%rsp), %rsi
movl $0xc, %edi
jmp 0x3f90
orl $0x6, %edi
movl %edi, %ecx
movw %cx, 0x13c(%r15)
movl %ecx, %eax
shrl %eax
andl $0x3, %eax
movl %eax, 0x8(%rsp)
movq 0x8(%r15), %rax
testq %rax, %rax
je 0x3f92
movq 0x30(%rax), %rax
testq %rax, %rax
je 0x3f92
movq 0x10(%r15), %rdx
leaq 0x8(%rsp), %rsi
movl $0x2, %edi
jmp 0x3f90
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq