// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/ansiart.c
// Function: do_main
static void do_main(deark *c, lctx *d)
{
	i64 pos, nextpos;
	i64 params_start_pos = 0;
#define STATE_NORMAL 0
#define STATE_GOT_ESC 1
#define STATE_READING_PARAM 2
#define STATE_GOT_1_CHAR 3
	int state;
	u8 first_ch = 0;
	u8 ch;

	d->xpos = 0; d->ypos = 0;
	d->curr_bgcol = DEFAULT_BGCOL;
	d->curr_fgcol = DEFAULT_FGCOL;
	state = STATE_NORMAL;

	nextpos = 0;
	while(nextpos<d->effective_file_size) {
		pos = nextpos;
		ch = de_getbyte(pos);
		nextpos = pos+1;

		if(pos==0 && ch==0x9b) {
			// 0x9b can sometimes mean the same thing as Esc [, but it could
			// also be a printable character. I don't know how to tell the
			// difference, but I'll assume that if the file starts with 0x9b,
			// it is a control character.
			d->support_9b_csi = 1;
		}

		if(state==STATE_NORMAL) {
			if(ch==0x1b) { // ESC
				state=STATE_GOT_ESC;
				continue;
			}
			else if(ch==0x9b && d->support_9b_csi) {
				state=STATE_READING_PARAM;
				params_start_pos = pos+1;
				continue;
			}
			else { // A byte that's not part of an escape sequence
				do_normal_char(c, d, pos, ch);
			}
		}
		else if(state==STATE_GOT_ESC) {
			if(ch=='[') {
				state=STATE_READING_PARAM;
				params_start_pos = pos+1;
				continue;
			}
			else if(ch>=64 && ch<=95) {
				// A 2-character escape sequence
				i64 extra_bytes_to_skip;
				extra_bytes_to_skip = 0;
				do_escape_code(c, d, ch, pos, &extra_bytes_to_skip);
				nextpos += extra_bytes_to_skip;
				state=STATE_NORMAL;
				continue;
			}
			else if(ch=='(' || ch==')' || ch=='#') {
				first_ch = ch;
				state=STATE_GOT_1_CHAR;
				continue;
			}
		}
		else if(state==STATE_READING_PARAM) {
			// Control sequences end with a byte from 64-126
			if(ch>=64 && ch<=126) {
				do_control_sequence(c, d, ch, params_start_pos, pos-params_start_pos);
				state=STATE_NORMAL;
				continue;
			}
		}
		else if(state==STATE_GOT_1_CHAR) {
			do_2char_code(c, d, first_ch, ch, pos-1);
			state=STATE_NORMAL;
			continue;
		}
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq $0x0, -0x28(%rbp)
movb $0x0, -0x2d(%rbp)
movq -0x10(%rbp), %rax
movq $0x0, 0x28(%rax)
movq -0x10(%rbp), %rax
movq $0x0, 0x30(%rax)
movq -0x10(%rbp), %rax
movl $0x0, 0x4c(%rax)
movq -0x10(%rbp), %rax
movl $0x7, 0x48(%rax)
movl $0x0, -0x2c(%rbp)
movq $0x0, -0x20(%rbp)
movq -0x20(%rbp), %rax
movq -0x10(%rbp), %rcx
cmpq 0x20(%rcx), %rax
jge 0x2037f
movq -0x20(%rbp), %rax
movq %rax, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x18(%rbp), %rsi
callq 0x165060
movb %al, -0x2e(%rbp)
movq -0x18(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x18(%rbp)
jne 0x201ed
movzbl -0x2e(%rbp), %eax
cmpl $0x9b, %eax
jne 0x201ed
movq -0x10(%rbp), %rax
movb $0x1, 0x71(%rax)
cmpl $0x0, -0x2c(%rbp)
jne 0x20251
movzbl -0x2e(%rbp), %eax
cmpl $0x1b, %eax
jne 0x20205
movl $0x1, -0x2c(%rbp)
jmp 0x20199
movzbl -0x2e(%rbp), %eax
cmpl $0x9b, %eax
jne 0x20235
movq -0x10(%rbp), %rax
movzbl 0x71(%rax), %eax
cmpl $0x0, %eax
je 0x20235
movl $0x2, -0x2c(%rbp)
movq -0x18(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
jmp 0x20199
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
movzbl -0x2e(%rbp), %ecx
callq 0x20540
jmp 0x2024c
jmp 0x2037a
cmpl $0x1, -0x2c(%rbp)
jne 0x202fc
movzbl -0x2e(%rbp), %eax
cmpl $0x5b, %eax
jne 0x2027c
movl $0x2, -0x2c(%rbp)
movq -0x18(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
jmp 0x20199
movzbl -0x2e(%rbp), %eax
cmpl $0x40, %eax
jl 0x202c9
movzbl -0x2e(%rbp), %eax
cmpl $0x5f, %eax
jg 0x202c9
movq $0x0, -0x38(%rbp)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movb -0x2e(%rbp), %al
movq -0x18(%rbp), %rcx
leaq -0x38(%rbp), %r8
movzbl %al, %edx
callq 0x20800
movq -0x38(%rbp), %rax
addq -0x20(%rbp), %rax
movq %rax, -0x20(%rbp)
movl $0x0, -0x2c(%rbp)
jmp 0x20199
movzbl -0x2e(%rbp), %eax
cmpl $0x28, %eax
je 0x202e4
movzbl -0x2e(%rbp), %eax
cmpl $0x29, %eax
je 0x202e4
movzbl -0x2e(%rbp), %eax
cmpl $0x23, %eax
jne 0x202f6
movb -0x2e(%rbp), %al
movb %al, -0x2d(%rbp)
movl $0x3, -0x2c(%rbp)
jmp 0x20199
jmp 0x202f8
jmp 0x202fa
jmp 0x20378
cmpl $0x2, -0x2c(%rbp)
jne 0x20341
movzbl -0x2e(%rbp), %eax
cmpl $0x40, %eax
jl 0x2033f
movzbl -0x2e(%rbp), %eax
cmpl $0x7e, %eax
jg 0x2033f
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movb -0x2e(%rbp), %al
movq -0x28(%rbp), %rcx
movq -0x18(%rbp), %r8
subq -0x28(%rbp), %r8
movzbl %al, %edx
callq 0x20950
movl $0x0, -0x2c(%rbp)
jmp 0x20199
jmp 0x20376
cmpl $0x3, -0x2c(%rbp)
jne 0x20374
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movb -0x2d(%rbp), %cl
movb -0x2e(%rbp), %al
movq -0x18(%rbp), %r8
subq $0x1, %r8
movzbl %cl, %edx
movzbl %al, %ecx
callq 0x20c40
movl $0x0, -0x2c(%rbp)
jmp 0x20199
jmp 0x20376
jmp 0x20378
jmp 0x2037a
jmp 0x20199
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)