// File: /workspace/github_c_makefile_star2_O2/termbox[P]termbox/termbox.c
// Function: tb_init_fd
int tb_init_fd(int inout_) {
    inout = inout_;
    if (inout == -1) {
        return TB_EFAILED_TO_OPEN_TTY;
    }

    if (init_term() < 0) {
        close(inout);
        return TB_EUNSUPPORTED_TERMINAL;
    }

    if (pipe(winch_fds) < 0) {
        close(inout);
        return TB_EPIPE_TRAP_ERROR;
    }

    struct sigaction sa;
    memset(&sa, 0, sizeof(sa));
    sa.sa_handler = sigwinch_handler;
    sa.sa_flags = 0;
    sigaction(SIGWINCH, &sa, 0);

    tcgetattr(inout, &orig_tios);

    struct termios tios;
    memcpy(&tios, &orig_tios, sizeof(tios));

    tios.c_iflag &=
        ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
    tios.c_oflag &= ~OPOST;
    tios.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
    tios.c_cflag &= ~(CSIZE | PARENB);
    tios.c_cflag |= CS8;
    tios.c_cc[VMIN] = 0;
    tios.c_cc[VTIME] = 0;
    tcsetattr(inout, TCSAFLUSH, &tios);

    bytebuffer_init(&input_buffer, 128);
    bytebuffer_init(&output_buffer, 32 * 1024);

    bytebuffer_puts(&output_buffer, funcs[T_ENTER_CA]);
    bytebuffer_puts(&output_buffer, funcs[T_ENTER_KEYPAD]);
    bytebuffer_puts(&output_buffer, funcs[T_HIDE_CURSOR]);
    send_clear();

    update_term_size();
    cellbuf_init(&back_buffer, termw, termh);
    cellbuf_init(&front_buffer, termw, termh);
    cellbuf_clear(&back_buffer);
    cellbuf_clear(&front_buffer);

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x1040, %rsp           # imm = 0x1040
movl %edi, 0x623b(%rip)      # 0x9890
cmpl $-0x1, %edi
je 0x368b
leaq 0x2afc(%rip), %rdi      # 0x615d
callq 0x3030
testq %rax, %rax
je 0x3804
movq %rax, %r14
leaq 0x2ae9(%rip), %rdi      # 0x6162
callq 0x3030
testq %rax, %rax
je 0x3692
movq %rax, %rdi
jmp 0x375e
pushq $-0x2
jmp 0x3b5e
leaq 0x2ad2(%rip), %rdi      # 0x616b
callq 0x3030
testq %rax, %rax
je 0x36e0
leaq 0x2ac6(%rip), %rdx      # 0x6170
leaq 0x40(%rsp), %rbx
movl $0x1000, %esi           # imm = 0x1000
movq %rbx, %rdi
movq %rax, %rcx
xorl %eax, %eax
callq 0x30d0
movb $0x0, 0xfff(%rbx)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x48ca
testq %rax, %rax
je 0x36e0
movq %rax, %rbx
jmp 0x3772
leaq 0x2a96(%rip), %rdi      # 0x617d
callq 0x3030
testq %rax, %rax
je 0x3757
leaq 0x2aaf(%rip), %rdx      # 0x61a7
leaq 0x40(%rsp), %rbx
movl $0x1000, %esi           # imm = 0x1000
movq %rbx, %rdi
movq %rax, %rcx
xorl %eax, %eax
callq 0x30d0
movb $0x0, 0xfff(%rbx)
leaq 0x2a6e(%rip), %r15      # 0x618b
movq %rbx, %rdi
movq %r15, %rsi
callq 0x3220
leaq 0x2a5e(%rip), %r12      # 0x618d
testq %rax, %rax
je 0x3757
cmpb $0x0, (%rax)
cmoveq %r12, %rax
movq %rax, %rdi
movq %r14, %rsi
callq 0x48ca
testq %rax, %rax
jne 0x36d8
xorl %edi, %edi
movq %r15, %rsi
callq 0x3220
jmp 0x372f
leaq 0x2a2f(%rip), %rdi      # 0x618d
movq %r14, %rsi
callq 0x48ca
movq %rax, %rbx
testq %rax, %rax
je 0x3804
movzwl (%rbx), %ecx
movswl 0x2(%rbx), %eax
movzwl 0x4(%rbx), %edx
movl %edx, %esi
addl %eax, %esi
testb $0x1, %sil
je 0x378d
incl %edx
movw %dx, 0x4(%rbx)
cmpw $0x21e, %cx             # imm = 0x21E
sete %cl
movswl %dx, %edx
addl %edx, %eax
movswl 0x6(%rbx), %edx
incb %cl
shll %cl, %edx
movl %eax, %ecx
addl %edx, %ecx
leal (%rdx,%rax), %r14d
addl $0xc, %r14d
movswl 0x8(%rbx), %eax
leal (%rcx,%rax,2), %ebp
addl $0xc, %ebp
movl $0xb8, %edi
callq 0x31b0
movq %rax, 0x6167(%rip)      # 0x9930
xorl %r15d, %r15d
leaq 0x28cd(%rip), %r12      # 0x60a0
cmpq $0x2c, %r15
je 0x386a
movswl (%r15,%r12), %eax
leal (%r14,%rax,2), %esi
movq %rbx, %rdi
movl %ebp, %edx
callq 0x489a
movq %rax, %rcx
movq 0x6136(%rip), %rax      # 0x9930
movq %rcx, (%rax,%r15,4)
addq $0x2, %r15
jmp 0x37d3
movb $0x0, 0x611d(%rip)      # 0x9928
leaq 0x294b(%rip), %rdi      # 0x615d
callq 0x3030
testq %rax, %rax
je 0x3b51
movq %rax, %rbx
xorl %ecx, %ecx
leaq 0x5514(%rip), %r14      # 0x8d40
leaq 0x18(%rcx), %r15
cmpq $0xa8, %r15
je 0x3a4e
movq (%rcx,%r14), %rdi
movq %rbx, %rsi
callq 0x3150
movq %r15, %rcx
testl %eax, %eax
jne 0x382c
movq -0x10(%r15,%r14), %rax
movq -0x8(%r15,%r14), %rcx
movq %rax, 0x60cf(%rip)      # 0x9930
movq %rcx, 0x6090(%rip)      # 0x98f8
jmp 0x38dd
andq $0x0, 0xb0(%rax)
pushq $0x70
popq %rdi
callq 0x31b0
movq %rax, 0x6077(%rip)      # 0x98f8
xorl %r15d, %r15d
leaq 0x2845(%rip), %r12      # 0x60d0
cmpq $0x18, %r15
je 0x38b8
movswl (%r15,%r12), %eax
leal (%r14,%rax,2), %esi
movq %rbx, %rdi
movl %ebp, %edx
callq 0x489a
movq %rax, %rcx
movq 0x604a(%rip), %rax      # 0x98f8
movq %rcx, (%rax,%r15,4)
addq $0x2, %r15
jmp 0x388b
leaq 0x285c(%rip), %rcx      # 0x611b
movq %rcx, 0x60(%rax)
leaq 0x2872(%rip), %rcx      # 0x613c
movq %rcx, 0x68(%rax)
movb $0x1, 0x6053(%rip)      # 0x9928
movq %rbx, %rdi
callq 0x3040
leaq 0x5fb0(%rip), %rdi      # 0x9894
callq 0x3130
testl %eax, %eax
js 0x3a3c
leaq 0x48(%rsp), %r14
xorl %ebx, %ebx
movl $0x90, %edx
movq %r14, %rdi
xorl %esi, %esi
callq 0x3100
leaq 0x263(%rip), %rax       # 0x3b71
movq %rax, -0x8(%r14)
pushq $0x1c
popq %rdi
leaq 0x40(%rsp), %rsi
xorl %edx, %edx
callq 0x3080
movl 0x5f69(%rip), %edi      # 0x9890
leaq 0x5f6e(%rip), %rsi      # 0x989c
callq 0x31e0
movups 0x5f62(%rip), %xmm0     # 0x989c
movq %rsp, %rdx
movaps %xmm0, (%rdx)
movups 0x5f65(%rip), %xmm0     # 0x98ac
movaps %xmm0, 0x10(%rdx)
movups 0x5f76(%rip), %xmm0     # 0x98c8
movups %xmm0, 0x2c(%rdx)
movups 0x5f5f(%rip), %xmm0     # 0x98bc
movaps %xmm0, 0x20(%rdx)
movabsq $-0x1000005ec, %rax     # imm = 0xFFFFFFFEFFFFFA14
andq %rax, (%rdx)
andl $0xffff7fb4, 0xc(%rdx)  # imm = 0xFFFF7FB4
movl $0xfffffecf, %eax       # imm = 0xFFFFFECF
andl 0x8(%rdx), %eax
orl $0x30, %eax
movl %eax, 0x8(%rdx)
andw $0x0, 0x16(%rdx)
movl 0x5f02(%rip), %edi      # 0x9890
pushq $0x2
popq %rsi
callq 0x31f0
leaq 0x5f3b(%rip), %rdi      # 0x98d8
movl $0x80, %esi
callq 0x3b8d
leaq 0x5f3a(%rip), %rdi      # 0x98e8
movl $0x8000, %esi           # imm = 0x8000
callq 0x3b8d
movq 0x5f39(%rip), %rax      # 0x98f8
movq (%rax), %rdi
callq 0x3ba5
movq 0x5f2a(%rip), %rax      # 0x98f8
movq 0x50(%rax), %rdi
callq 0x3ba5
movq 0x5f1a(%rip), %rax      # 0x98f8
movq 0x18(%rax), %rdi
callq 0x3ba5
callq 0x3bb9
callq 0x3c19
movl 0x5749(%rip), %esi      # 0x9140
movl 0x5747(%rip), %edx      # 0x9144
leaq 0x5efc(%rip), %r14      # 0x9900
movq %r14, %rdi
callq 0x3c6f
movl 0x572e(%rip), %esi      # 0x9140
movl 0x572c(%rip), %edx      # 0x9144
leaq 0x5ef1(%rip), %r15      # 0x9910
movq %r15, %rdi
callq 0x3c6f
movq %r14, %rdi
callq 0x3cc1
movq %r15, %rdi
callq 0x3cc1
jmp 0x3b5f
movl 0x5e4e(%rip), %edi      # 0x9890
callq 0x3120
pushq $-0x3
jmp 0x3b5e
leaq 0x2761(%rip), %rsi      # 0x61b6
leaq 0x5704(%rip), %rdx      # 0x9160
leaq 0x57bd(%rip), %rcx      # 0x9220
movq %rbx, %rdi
callq 0x49eb
testl %eax, %eax
je 0x38dd
leaq 0x2742(%rip), %rsi      # 0x61bc
leaq 0x580f(%rip), %rdx      # 0x9290
leaq 0x58c8(%rip), %rcx      # 0x9350
movq %rbx, %rdi
callq 0x49eb
testl %eax, %eax
je 0x38dd
leaq 0x2722(%rip), %rsi      # 0x61c1
leaq 0x591a(%rip), %rdx      # 0x93c0
leaq 0x59d3(%rip), %rcx      # 0x9480
movq %rbx, %rdi
callq 0x49eb
testl %eax, %eax
je 0x38dd
leaq 0x2703(%rip), %rsi      # 0x61c7
leaq 0x5a25(%rip), %rdx      # 0x94f0
leaq 0x5ade(%rip), %rcx      # 0x95b0
movq %rbx, %rdi
callq 0x49eb
testl %eax, %eax
je 0x38dd
leaq 0x26e4(%rip), %rsi      # 0x61cd
leaq 0x5b30(%rip), %rdx      # 0x9620
leaq 0x5be9(%rip), %rcx      # 0x96e0
movq %rbx, %rdi
callq 0x49eb
testl %eax, %eax
je 0x38dd
leaq 0x26c6(%rip), %rsi      # 0x61d4
leaq 0x5b0b(%rip), %rdx      # 0x9620
leaq 0x5bc4(%rip), %rcx      # 0x96e0
movq %rbx, %rdi
callq 0x49eb
testl %eax, %eax
je 0x38dd
leaq 0x26a6(%rip), %rsi      # 0x61d9
leaq 0x5626(%rip), %rdx      # 0x9160
leaq 0x56df(%rip), %rcx      # 0x9220
movq %rbx, %rdi
callq 0x49eb
testl %eax, %eax
je 0x38dd
movl 0x5d39(%rip), %edi      # 0x9890
callq 0x3120
pushq $-0x1
popq %rbx
movl %ebx, %eax
addq $0x1040, %rsp           # imm = 0x1040
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq