// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xa0, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movzbl 0x31(%rax), %eax
cmpl $0x0, %eax
jne 0xac5d
movq -0x8(%rbp), %rax
movzbl 0x30(%rax), %eax
cmpl $0x0, %eax
je 0xadde
movl $0x0, -0xc(%rbp)
movl $0xffffffff, -0x94(%rbp) # imm = 0xFFFFFFFF
movq -0x8(%rbp), %rax
cmpb $0x0, 0xc(%rax)
je 0xac96
leaq 0x2c5d(%rip), %rdi      # 0xd8dc
movl $0x242, %esi            # imm = 0x242
movl $0x1ff, %edx            # imm = 0x1FF
movb $0x0, %al
callq 0x4210
movl %eax, -0x94(%rbp)
callq 0x4860
movl $0x1, %edi
leaq 0x2c42(%rip), %rsi      # 0xd8e9
movb $0x0, %al
callq 0x44e0
movq 0x5323(%rip), %rax      # 0xffd8
movq (%rax), %rdi
callq 0x41a0
movq -0x8(%rbp), %rdi
leaq -0x90(%rbp), %rsi
movl $0x7f, %edx
leaq -0xa0(%rbp), %rcx
xorl %eax, %eax
movl %eax, %r8d
movl $0xc8, %r9d
callq 0x4960
cmpq $0x0, -0xa0(%rbp)
je 0xad3a
leaq -0x90(%rbp), %rsi
movq -0xa0(%rbp), %rdx
movl $0x1, %edi
callq 0x4090
movq 0x52cb(%rip), %rax      # 0xffd8
movq (%rax), %rdi
callq 0x41a0
movq -0x8(%rbp), %rax
cmpb $0x0, 0xc(%rax)
je 0xad38
movl -0x94(%rbp), %edi
leaq -0x90(%rbp), %rsi
movq -0xa0(%rbp), %rdx
callq 0x4090
jmp 0xad3a
callq 0x47a0
cmpl $0x0, %eax
je 0xad94
callq 0x4770
movl %eax, -0xc(%rbp)
cmpl $0x1b, -0xc(%rbp)
jne 0xad54
jmp 0xada0
movl -0xc(%rbp), %eax
movb %al, -0x90(%rbp)
movb $0x0, -0x8f(%rbp)
movq -0x8(%rbp), %rax
cmpb $0x0, 0x32(%rax)
je 0xad84
leaq -0x90(%rbp), %rsi
movl $0x1, %edi
movl $0x1, %edx
callq 0x4090
movq -0x8(%rbp), %rdi
leaq -0x90(%rbp), %rsi
callq 0x4730
jmp 0xad96
cmpl $0x1b, -0xc(%rbp)
jne 0xacbd
movl $0x1, %edi
leaq 0x2b68(%rip), %rsi      # 0xd914
movb $0x0, %al
callq 0x44e0
movq 0x521e(%rip), %rax      # 0xffd8
movq (%rax), %rdi
callq 0x41a0
callq 0x48d0
movq -0x8(%rbp), %rax
cmpb $0x0, 0xc(%rax)
je 0xaddc
movl -0x94(%rbp), %edi
callq 0x4100
jmp 0xadde
addq $0xa0, %rsp
popq %rbp
retq