// File: /workspace/github_c_makefile_star2_O1/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, %r14d
movl %esi, %r15d
movq %rdi, %rbx
movl %esi, 0x4(%rdi)
movl %edx, 0x8(%rdi)
leal 0x7(%r15), %eax
testl %esi, %esi
cmovnsl %esi, %eax
sarl $0x3, %eax
movl %eax, 0xc(%rdi)
leal 0xf(%r14), %eax
testl %edx, %edx
cmovnsl %edx, %eax
sarl $0x4, %eax
movl %eax, 0x10(%rdi)
movl $0x400, 0x78(%rdi)      # imm = 0x400
movslq %eax, %rdi
movl $0x1, %esi
callq 0x2754
movq %rax, 0x28(%rbx)
movslq 0xc(%rbx), %rdi
movl $0x1, %esi
callq 0x2754
movq %rax, 0x30(%rbx)
movslq 0x78(%rbx), %rsi
movl $0x1, %edi
callq 0x2754
movq %rax, 0x68(%rbx)
imull %r15d, %r14d
movslq %r14d, %rdi
movl $0x4, %esi
callq 0x2754
movq %rax, 0xbb588(%rbx)
movslq 0x10(%rbx), %rdi
movl $0x8, %esi
callq 0x2754
movq %rax, 0x18(%rbx)
cmpl $0x0, 0x10(%rbx)
jle 0x455b
xorl %r14d, %r14d
movslq 0xc(%rbx), %rdi
movl $0x218, %esi            # imm = 0x218
callq 0x2754
movq 0x18(%rbx), %rcx
movq %rax, (%rcx,%r14,8)
incq %r14
movslq 0x10(%rbx), %rax
cmpq %rax, %r14
jl 0x4539
cmpq $0x0, 0x28(%rbx)
je 0x4618
cmpq $0x0, 0x30(%rbx)
je 0x4618
cmpq $0x0, 0x18(%rbx)
je 0x4618
cmpq $0x0, 0x68(%rbx)
je 0x4618
cmpq $0x0, 0xbb588(%rbx)
je 0x4618
xorl %eax, %eax
leaq 0x3b22(%rip), %rcx      # 0x80c0
movl (%rax,%rcx), %edx
movl %edx, 0x80(%rbx,%rax)
addq $0x4, %rax
cmpq $0x400, %rax            # imm = 0x400
jne 0x459e
movb $0x0, 0x480(%rbx)
leaq 0x488(%rbx), %rdi
xorl %r14d, %r14d
movl $0x80000, %edx          # imm = 0x80000
xorl %esi, %esi
callq 0x2130
leaq 0x3f98(%rip), %rax      # 0x8570
leaq (%rax,%r14), %rcx
movl (%r14,%rax), %edx
movq %rcx, 0x488(%rbx,%rdx,8)
addq $0x28, %r14
cmpq $0x4a290, %r14          # imm = 0x4A290
jne 0x45d8
movq 0x588(%rbx), %rcx
testq %rcx, %rcx
je 0x462f
cmpq $0x0, 0x18488(%rbx)
je 0x462f
movq %rbx, %rdi
callq 0x4347
movb $0x1, %bpl
jmp 0x4622
movq %rbx, %rdi
callq 0x4436
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
leaq 0x4e53e(%rip), %rsi     # 0x52b74
xorl %ebp, %ebp
movl $0x2, %edi
movl $0x20, %edx
movl $0x20, %r8d
movq %rcx, %r9
xorl %eax, %eax
pushq 0x18488(%rbx)
pushq $0x3000                 # imm = 0x3000
callq 0x245c
addq $0x10, %rsp
jmp 0x4622