// File: /workspace/github_c_makefile_star2_O0/orempel[P]multiboot_tool/mpmboot.c
// Function: mpm_send
static int mpm_send(struct mpm_privdata *mpm, uint8_t command,
                    uint8_t *data, uint16_t length)
{
    struct termios tio;

    if (tcgetattr(mpm->fd, &tio) < 0)
    {
        perror("tcgetattr(tio)");
        return -1;
    }

    tio.c_cflag |= PARODD;

    if (tcsetattr(mpm->fd, TCSAFLUSH, &tio) < 0)
    {
        perror("tcsetattr(tio)");
        return -1;
    }

//     usleep(5000);

    uint8_t address = mpm->address;
    if (write(mpm->fd, &address, sizeof(address)) != sizeof(address))
    {
        perror("write(address)");
        return -1;
    }

    usleep(500);

    tio.c_cflag &= ~(PARODD);

    if (tcsetattr(mpm->fd, TCSAFLUSH, &tio) < 0)
    {
        perror("tcsetattr(tio)");
        return -1;
    }

    uint8_t header[3];
    header[0] = command;
    header[1] = (length >> 8) & 0xFF;
    header[2] = length & 0xFF;

    if (write(mpm->fd, header, sizeof(header)) != sizeof(header))
    {
        perror("write(header)");
        return -1;
    }

    if (data != NULL && length != 0)
    {
        if (write(mpm->fd, data, length) != length)
        {
            perror("write(data)");
            return -1;
        }
    }

    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movw %cx, %ax
movb %sil, %cl
movq %rdi, -0x10(%rbp)
movb %cl, -0x11(%rbp)
movq %rdx, -0x20(%rbp)
movw %ax, -0x22(%rbp)
movq -0x10(%rbp), %rax
movl 0x8(%rax), %edi
leaq -0x60(%rbp), %rsi
callq 0x21c0
cmpl $0x0, %eax
jge 0x836a
leaq 0x4054(%rip), %rdi      # 0xc3ad
callq 0x2210
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x84c9
movl -0x58(%rbp), %eax
orl $0x200, %eax            # imm = 0x200
movl %eax, -0x58(%rbp)
movq -0x10(%rbp), %rax
movl 0x8(%rax), %edi
movl $0x2, %esi
leaq -0x60(%rbp), %rdx
callq 0x21d0
cmpl $0x0, %eax
jge 0x83a7
leaq 0x4026(%rip), %rdi      # 0xc3bc
callq 0x2210
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x84c9
movq -0x10(%rbp), %rax
movb 0x10(%rax), %al
movb %al, -0x61(%rbp)
movq -0x10(%rbp), %rax
movl 0x8(%rax), %edi
leaq -0x61(%rbp), %rsi
movl $0x1, %edx
callq 0x2070
cmpq $0x1, %rax
je 0x83e4
leaq 0x3ff8(%rip), %rdi      # 0xc3cb
callq 0x2210
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x84c9
movl $0x1f4, %edi            # imm = 0x1F4
callq 0x2280
movl -0x58(%rbp), %eax
andl $0xfffffdff, %eax       # imm = 0xFFFFFDFF
movl %eax, -0x58(%rbp)
movq -0x10(%rbp), %rax
movl 0x8(%rax), %edi
movl $0x2, %esi
leaq -0x60(%rbp), %rdx
callq 0x21d0
cmpl $0x0, %eax
jge 0x842b
leaq 0x3fa2(%rip), %rdi      # 0xc3bc
callq 0x2210
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x84c9
movb -0x11(%rbp), %al
movb %al, -0x64(%rbp)
movzwl -0x22(%rbp), %eax
sarl $0x8, %eax
andl $0xff, %eax
movb %al, -0x63(%rbp)
movzwl -0x22(%rbp), %eax
andl $0xff, %eax
movb %al, -0x62(%rbp)
movq -0x10(%rbp), %rax
movl 0x8(%rax), %edi
leaq -0x64(%rbp), %rsi
movl $0x3, %edx
callq 0x2070
cmpq $0x3, %rax
je 0x847c
leaq 0x3f6c(%rip), %rdi      # 0xc3da
callq 0x2210
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x84c9
cmpq $0x0, -0x20(%rbp)
je 0x84c2
movzwl -0x22(%rbp), %eax
cmpl $0x0, %eax
je 0x84c2
movq -0x10(%rbp), %rax
movl 0x8(%rax), %edi
movq -0x20(%rbp), %rsi
movzwl -0x22(%rbp), %eax
movl %eax, %edx
callq 0x2070
movzwl -0x22(%rbp), %ecx
cmpq %rcx, %rax
je 0x84c0
leaq 0x3f36(%rip), %rdi      # 0xc3e8
callq 0x2210
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x84c9
jmp 0x84c2
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x70, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)