// File: /workspace/github_c_makefile_star2_O3/bokunimowakaru[P]petit15term/lpc21isp/lpcterm.c
// Function: Terminal
void Terminal(ISP_ENVIRONMENT *IspEnvironment)
{
    if (IspEnvironment->TerminalAfterUpload || IspEnvironment->TerminalOnly)
    {
        int           ch = 0;
        char buffer[128];
        int           fdlogfile = -1;
        unsigned long realsize;

        // When logging is switched on, output terminal output to lpc21isp.log
        if (IspEnvironment->LogFile)
        {
            fdlogfile = open("lpc21isp.log", O_RDWR | O_BINARY | O_CREAT | O_TRUNC, 0777);
        }

        PrepareKeyboardTtySettings();

        DebugPrintf(1, "Terminal started (press Escape to abort)\n\n");
        fflush(stdout);

        do
        {
            ReceiveComPort(IspEnvironment, buffer, sizeof(buffer) - 1, &realsize, 0,200);          // Check for received characters

            if (realsize)
            {
                write(1, buffer, realsize);
                fflush(stdout);
                if (IspEnvironment->LogFile)     // When logging is turned on, then copy output to logfile
                {
                    write(fdlogfile, buffer, realsize);
                }
            }

            // check for keypress, and write any out the port.
            if (kbhit())
            {
                ch = getch();
                if (ch == 0x1b)
                {
                    break;
                }
                buffer[0] = (unsigned char)ch;
                buffer[1] = 0;

                if (IspEnvironment->LocalEcho)
                {
                    write(1, buffer, 1);
                }

                SendComPort(IspEnvironment, buffer);
            }
        }
        while (ch != 0x1b);

        DebugPrintf(1, "\n\nTerminal stopped\n\n");
        fflush(stdout);

        ResetKeyboardTtySettings();

        if (IspEnvironment->LogFile)
        {
            close(fdlogfile);
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x90, %rsp
movq %rdi, %rbx
cmpb $0x0, 0x31(%rdi)
jne 0x86b3
cmpb $0x0, 0x30(%rbx)
je 0x87cf
cmpb $0x0, 0xc(%rbx)
je 0x86d5
leaq 0x338e(%rip), %rdi      # 0xba4e
movl $0x242, %esi            # imm = 0x242
movl $0x1ff, %edx            # imm = 0x1FF
xorl %eax, %eax
callq 0x4200
movl %eax, %ebp
jmp 0x86da
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
callq 0x4645
leaq 0x3375(%rip), %rsi      # 0xba5b
movl $0x1, %edi
xorl %eax, %eax
callq 0x43fd
movq 0x58df(%rip), %r12      # 0xdfd8
movq (%r12), %rdi
callq 0x4180
leaq 0x10(%rsp), %r14
leaq 0x8(%rsp), %r15
movl $0x7f, %edx
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rcx
xorl %r8d, %r8d
movl $0xc8, %r9d
callq 0x4724
movq 0x8(%rsp), %rdx
testq %rdx, %rdx
je 0x875d
movl $0x1, %edi
movq %r14, %rsi
callq 0x4090
movq (%r12), %rdi
callq 0x4180
cmpb $0x0, 0xc(%rbx)
je 0x875d
movq 0x8(%rsp), %rdx
movl %ebp, %edi
movq %r14, %rsi
callq 0x4090
callq 0x45de
testl %eax, %eax
je 0x870c
callq 0x45bc
cmpl $0x1b, %eax
je 0x87a1
movb %al, 0x10(%rsp)
movb $0x0, 0x11(%rsp)
cmpb $0x0, 0x32(%rbx)
je 0x8791
movl $0x1, %edx
movl $0x1, %edi
movq %r14, %rsi
callq 0x4090
movq %rbx, %rdi
movq %r14, %rsi
callq 0x4595
jmp 0x870c
leaq 0x32de(%rip), %rsi      # 0xba86
movl $0x1, %edi
xorl %eax, %eax
callq 0x43fd
movq (%r12), %rdi
callq 0x4180
callq 0x46a4
cmpb $0x0, 0xc(%rbx)
je 0x87cf
movl %ebp, %edi
callq 0x4100
addq $0x90, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq