// File: /workspace/github_c_makefile_star2_O0/uobikiemukot[P]yaft/./terminal.h
// Function: term_init
bool term_init(struct terminal_t *term, int width, int height)
{
	extern const uint32_t color_list[COLORS]; /* global */

	term->width  = width;
	term->height = height;

	term->cols  = term->width / CELL_WIDTH;
	term->lines = term->height / CELL_HEIGHT;

	term->esc.size = ESCSEQ_SIZE;

	logging(DEBUG, "terminal cols:%d lines:%d\n", term->cols, term->lines);

	/* allocate memory */
	term->line_dirty   = (bool *) ecalloc(term->lines, sizeof(bool));
	term->tabstop      = (bool *) ecalloc(term->cols, sizeof(bool));
	term->esc.buf      = (char *) ecalloc(1, term->esc.size);
	term->sixel.pixmap = (uint8_t *) ecalloc(width * height, BYTES_PER_PIXEL);

	term->cells        = (struct cell_t **) ecalloc(term->lines, sizeof(struct cell_t *));
	for (int i = 0; i < term->lines; i++)
		term->cells[i] = (struct cell_t *) ecalloc(term->cols, sizeof(struct cell_t));

	if (!term->line_dirty || !term->tabstop || !term->cells
		|| !term->esc.buf || !term->sixel.pixmap) {
		term_die(term);
		return false;
	}

	/* initialize palette */
	for (int i = 0; i < COLORS; i++)
		term->virtual_palette[i] = color_list[i];
	term->palette_modified = false;

	/* initialize glyph map */
	for (uint32_t code = 0; code < UCS2_CHARS; code++)
		term->glyph[code] = NULL;

	for (uint32_t gi = 0; gi < sizeof(glyphs) / sizeof(struct glyph_t); gi++)
		term->glyph[glyphs[gi].code] = &glyphs[gi];

	if (!term->glyph[DEFAULT_CHAR]
		|| !term->glyph[SUBSTITUTE_HALF]
		|| !term->glyph[SUBSTITUTE_WIDE]) {
		logging(ERROR, "couldn't find essential glyph:\
			DEFAULT_CHAR(U+%.4X):%p SUBSTITUTE_HALF(U+%.4X):%p SUBSTITUTE_WIDE(U+%.4X):%p\n",
			DEFAULT_CHAR, term->glyph[DEFAULT_CHAR],
			SUBSTITUTE_HALF, term->glyph[SUBSTITUTE_HALF],
			SUBSTITUTE_WIDE, term->glyph[SUBSTITUTE_WIDE]);
		return false;
	}

	/* reset terminal */
	reset(term);

	return true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movl %edx, -0x18(%rbp)
movl -0x14(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x4(%rax)
movl -0x18(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x8(%rax)
movq -0x10(%rbp), %rax
movl 0x4(%rax), %eax
movl $0x8, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0xc(%rax)
movq -0x10(%rbp), %rax
movl 0x8(%rax), %eax
movl $0x10, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x10(%rax)
movq -0x10(%rbp), %rax
movl $0x400, 0x78(%rax)      # imm = 0x400
movq -0x10(%rbp), %rax
movl 0xc(%rax), %edx
movq -0x10(%rbp), %rax
movl 0x10(%rax), %ecx
xorl %edi, %edi
leaq 0x506b8(%rip), %rsi     # 0x55d10
movb $0x0, %al
callq 0x2450
movq -0x10(%rbp), %rax
movslq 0x10(%rax), %rdi
movl $0x1, %esi
callq 0x27d0
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x10(%rbp), %rax
movslq 0xc(%rax), %rdi
movl $0x1, %esi
callq 0x27d0
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x30(%rax)
movq -0x10(%rbp), %rax
movslq 0x78(%rax), %rsi
movl $0x1, %edi
callq 0x27d0
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x68(%rax)
movl -0x14(%rbp), %eax
imull -0x18(%rbp), %eax
movslq %eax, %rdi
movl $0x4, %esi
callq 0x27d0
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0xbb588(%rax)
movq -0x10(%rbp), %rax
movslq 0x10(%rax), %rdi
movl $0x8, %esi
callq 0x27d0
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x18(%rax)
movl $0x0, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x10(%rcx), %eax
jge 0x5738
movq -0x10(%rbp), %rax
movslq 0xc(%rax), %rdi
movl $0x218, %esi            # imm = 0x218
callq 0x27d0
movq %rax, %rdx
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x1c(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x56fc
movq -0x10(%rbp), %rax
cmpq $0x0, 0x28(%rax)
je 0x5772
movq -0x10(%rbp), %rax
cmpq $0x0, 0x30(%rax)
je 0x5772
movq -0x10(%rbp), %rax
cmpq $0x0, 0x18(%rax)
je 0x5772
movq -0x10(%rbp), %rax
cmpq $0x0, 0x68(%rax)
je 0x5772
movq -0x10(%rbp), %rax
cmpq $0x0, 0xbb588(%rax)
jne 0x5784
movq -0x10(%rbp), %rdi
callq 0x5550
movb $0x0, -0x1(%rbp)
jmp 0x58ca
movl $0x0, -0x20(%rbp)
cmpl $0x100, -0x20(%rbp)     # imm = 0x100
jge 0x57bc
movslq -0x20(%rbp), %rcx
leaq 0x5871(%rip), %rax      # 0xb010
movl (%rax,%rcx,4), %edx
movq -0x10(%rbp), %rax
movslq -0x20(%rbp), %rcx
movl %edx, 0x80(%rax,%rcx,4)
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x578b
movq -0x10(%rbp), %rax
movb $0x0, 0x480(%rax)
movl $0x0, -0x24(%rbp)
cmpl $0x10000, -0x24(%rbp)   # imm = 0x10000
jae 0x57f5
movq -0x10(%rbp), %rax
movl -0x24(%rbp), %ecx
movq $0x0, 0x488(%rax,%rcx,8)
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x57ce
movl $0x0, -0x28(%rbp)
movl -0x28(%rbp), %eax
cmpq $0x1daa, %rax           # imm = 0x1DAA
jae 0x5844
movl -0x28(%rbp), %eax
leaq 0x5c9f(%rip), %rdx      # 0xb4b0
imulq $0x28, %rax, %rax
addq %rax, %rdx
movq -0x10(%rbp), %rax
movl -0x28(%rbp), %ecx
movl %ecx, %esi
leaq 0x5c88(%rip), %rcx      # 0xb4b0
imulq $0x28, %rsi, %rsi
addq %rsi, %rcx
movl (%rcx), %ecx
movq %rdx, 0x488(%rax,%rcx,8)
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x57fc
movq -0x10(%rbp), %rax
cmpq $0x0, 0x588(%rax)
je 0x586e
movq -0x10(%rbp), %rax
cmpq $0x0, 0x588(%rax)
je 0x586e
movq -0x10(%rbp), %rax
cmpq $0x0, 0x18488(%rax)
jne 0x58bd
movq -0x10(%rbp), %rax
movq 0x588(%rax), %rcx
movq -0x10(%rbp), %rax
movq 0x588(%rax), %r9
movq -0x10(%rbp), %rax
movq 0x18488(%rax), %rax
movl $0x2, %edi
leaq 0x50490(%rip), %rsi     # 0x55d2b
movl $0x20, %r8d
movl %r8d, %edx
movl $0x3000, (%rsp)         # imm = 0x3000
movq %rax, 0x8(%rsp)
movb $0x0, %al
callq 0x2450
movb $0x0, -0x1(%rbp)
jmp 0x58ca
movq -0x10(%rbp), %rdi
callq 0x53c0
movb $0x1, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)