// File: /workspace/github_c_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %rbx
pushq %rax
movl %edx, %ebp
movl %esi, %r14d
movq %rdi, %rbx
movl %esi, 0x4(%rdi)
movl %edx, 0x8(%rdi)
pushq $0x8
popq %rcx
movl %esi, %eax
cltd
idivl %ecx
movl %eax, 0xc(%rdi)
pushq $0x10
popq %rcx
movl %ebp, %eax
cltd
idivl %ecx
movl %eax, 0x10(%rdi)
movl $0x400, 0x78(%rdi)      # imm = 0x400
movslq %eax, %rdi
pushq $0x1
popq %r15
movq %r15, %rsi
callq 0x2732
movq %rax, 0x28(%rbx)
movslq 0xc(%rbx), %rdi
movq %r15, %rsi
callq 0x2732
movq %rax, 0x30(%rbx)
movslq 0x78(%rbx), %rsi
movq %r15, %rdi
callq 0x2732
movq %rax, 0x68(%rbx)
imull %r14d, %ebp
movslq %ebp, %rdi
pushq $0x4
popq %rsi
callq 0x2732
movq %rax, 0xbb588(%rbx)
movslq 0x10(%rbx), %rdi
pushq $0x8
popq %rsi
callq 0x2732
movq %rax, 0x18(%rbx)
xorl %r14d, %r14d
movslq 0x10(%rbx), %rax
cmpq %rax, %r14
jge 0x41cd
movslq 0xc(%rbx), %rdi
movl $0x218, %esi            # imm = 0x218
callq 0x2732
movq 0x18(%rbx), %rcx
movq %rax, (%rcx,%r14,8)
incq %r14
jmp 0x41a9
cmpq $0x0, 0x28(%rbx)
je 0x4214
cmpq $0x0, 0x30(%rbx)
je 0x4214
cmpq $0x0, 0x18(%rbx)
je 0x4214
cmpq $0x0, 0x68(%rbx)
je 0x4214
cmpq $0x0, 0xbb588(%rbx)
je 0x4214
xorl %eax, %eax
leaq 0x2ee4(%rip), %rcx      # 0x70e0
cmpq $0x400, %rax            # imm = 0x400
je 0x422b
movl (%rax,%rcx), %edx
movl %edx, 0x80(%rbx,%rax)
addq $0x4, %rax
jmp 0x41fc
movq %rbx, %rdi
callq 0x40c3
xorl %ebx, %ebx
movl %ebx, %eax
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
movb $0x0, 0x480(%rbx)
movl $0x91, %eax
cmpq $0x10091, %rax          # imm = 0x10091
je 0x4249
andq $0x0, (%rbx,%rax,8)
incq %rax
jmp 0x4237
xorl %eax, %eax
leaq 0x333e(%rip), %rcx      # 0x7590
cmpq $0x4a290, %rax          # imm = 0x4A290
je 0x426f
leaq (%rcx,%rax), %rdx
movl (%rax,%rcx), %esi
movq %rdx, 0x488(%rbx,%rsi,8)
addq $0x28, %rax
jmp 0x4252
movq 0x588(%rbx), %rcx
movq 0x18488(%rbx), %r10
testq %rcx, %rcx
je 0x4296
testq %r10, %r10
je 0x4293
movq %rbx, %rdi
callq 0x4008
movb $0x1, %bl
jmp 0x421e
xorl %r10d, %r10d
leaq 0x4d8f7(%rip), %rsi     # 0x51b94
xorl %ebx, %ebx
pushq $0x2
popq %rdi
pushq $0x20
popq %rdx
movl %edx, %r8d
movq %rcx, %r9
xorl %eax, %eax
pushq %r10
pushq $0x3000                 # imm = 0x3000
callq 0x244c
popq %rax
popq %rcx
jmp 0x421e