// File: /workspace/github_c_makefile_star2_O3/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 %r15
pushq %r14
pushq %rbx
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 0x4582
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 0x4560
cmpq $0x0, 0x28(%rbx)
je 0x463f
cmpq $0x0, 0x30(%rbx)
je 0x463f
cmpq $0x0, 0x18(%rbx)
je 0x463f
cmpq $0x0, 0x68(%rbx)
je 0x463f
cmpq $0x0, 0xbb588(%rbx)
je 0x463f
xorl %eax, %eax
leaq 0x3b1b(%rip), %rcx      # 0x80e0
movl (%rax,%rcx), %edx
movl %edx, 0x80(%rbx,%rax)
addq $0x4, %rax
cmpq $0x400, %rax            # imm = 0x400
jne 0x45c5
movb $0x0, 0x480(%rbx)
leaq 0x488(%rbx), %r14
xorl %r15d, %r15d
movl $0x80000, %edx          # imm = 0x80000
movq %r14, %rdi
xorl %esi, %esi
callq 0x2130
leaq 0x3f8e(%rip), %rax      # 0x8590
leaq (%rax,%r15), %rcx
movl (%r15,%rax), %edx
movq %rcx, (%r14,%rdx,8)
addq $0x28, %r15
cmpq $0x4a290, %r15          # imm = 0x4A290
jne 0x4602
movq 0x588(%rbx), %rcx
movq 0x18488(%rbx), %r10
testq %rcx, %rcx
je 0x4654
testq %r10, %r10
je 0x4651
movq %rbx, %rdi
callq 0x4368
movb $0x1, %bl
jmp 0x4649
movq %rbx, %rdi
callq 0x445f
xorl %ebx, %ebx
movl %ebx, %eax
popq %rbx
popq %r14
popq %r15
retq
xorl %r10d, %r10d
leaq 0x4e539(%rip), %rsi     # 0x52b94
xorl %ebx, %ebx
movl $0x2, %edi
movl $0x20, %edx
movl $0x20, %r8d
movq %rcx, %r9
xorl %eax, %eax
pushq %r10
pushq $0x3000                 # imm = 0x3000
callq 0x245c
addq $0x10, %rsp
jmp 0x4649