#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */

/* Variables and functions */
int /*<<< orphan*/ TCIFLUSH;
int /*<<< orphan*/ read(int, unsigned char *, int);
int /*<<< orphan*/ tcflush(int, int /*<<< orphan*/);
int /*<<< orphan*/ usleep(int);
int /*<<< orphan*/ write(int, unsigned char *, int);
int write_to_mouse(int fd, unsigned char *data, size_t len) {
  int i;
  int error = 0;
  for (i = 0; i < len; i++) {
    unsigned char c;
    write(fd, &data[i], 1);
    read(fd, &c, 1);
    if (c != 0xFA)
      error++;
  }
  usleep(30000);
  tcflush(fd, TCIFLUSH);
  return (error);
}


x86 assembly (clang, -O0):
<write_to_mouse>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x20(%rbp)
movl   $0x0,-0x1c(%rbp)
movslq -0x1c(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    11c3 <write_to_mouse+0x83>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x1c(%rbp),%rax
add    %rax,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    -0x4(%rbp),%edi
lea    -0x21(%rbp),%rsi
mov    $0x1,%edx
call   1050 <read@plt>
movzbl -0x21(%rbp),%eax
cmp    $0xfa,%eax
je     11b0 <write_to_mouse+0x70>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    11b5 <write_to_mouse+0x75>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1161 <write_to_mouse+0x21>
mov    $0x7530,%edi
call   1060 <usleep@plt>
mov    -0x4(%rbp),%edi
mov    0x2e09(%rip),%rax
mov    (%rax),%esi
call   1040 <tcflush@plt>
mov    -0x20(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_to_mouse>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %edi,%r14d
test   %rdx,%rdx
je     11a9 <write_to_mouse+0x69>
mov    %rdx,%rbx
mov    %rsi,%rbp
xor    %r15d,%r15d
lea    0xf(%rsp),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r14d,%edi
mov    %rbp,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    %r14d,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1050 <read@plt>
xor    %eax,%eax
cmpb   $0xfa,0xf(%rsp)
setne  %al
add    %eax,%r15d
add    $0x1,%rbp
add    $0xffffffffffffffff,%rbx
jne    1170 <write_to_mouse+0x30>
jmp    11ac <write_to_mouse+0x6c>
xor    %r15d,%r15d
mov    $0x7530,%edi
call   1060 <usleep@plt>
mov    0x2e23(%rip),%rax
mov    (%rax),%esi
mov    %r14d,%edi
call   1040 <tcflush@plt>
mov    %r15d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@TCIFLUSH = global i32 0
define i32 @write_to_mouse(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8
store i32 %0, i32* %4
store i8* %1, i8** %5
store i64 %2, i64* %6
store i32 0, i32* %8
store i32 0, i32* %7
br label %10
10: ; preds = %31, %3
%11 = load i32, i32* %7
%12 = sext i32 %11 to i64
%13 = load i64, i64* %6
%14 = icmp ult i64 %12, %13
br i1 %14, label %15, label %34
15: ; preds = %10
%16 = load i32, i32* %4
%17 = load i8*, i8** %5
%18 = load i32, i32* %7
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %17, i64 %19
%21 = call i32 @write(i32 %16, i8* %20, i32 1)
%22 = load i32, i32* %4
%23 = call i32 @read(i32 %22, i8* %9, i32 1)
%24 = load i8, i8* %9
%25 = zext i8 %24 to i32
%26 = icmp ne i32 %25, 250
br i1 %26, label %27, label %30
27: ; preds = %15
%28 = load i32, i32* %8
%29 = add nsw i32 %28, 1
store i32 %29, i32* %8
br label %30
30: ; preds = %27, %15
br label %31
31: ; preds = %30
%32 = load i32, i32* %7
%33 = add nsw i32 %32, 1
store i32 %33, i32* %7
br label %10
34: ; preds = %10
%35 = call i32 @usleep(i32 30000)
%36 = load i32, i32* %4
%37 = load i32, i32* @TCIFLUSH
%38 = call i32 @tcflush(i32 %36, i32 %37)
%39 = load i32, i32* %8
ret i32 %39
}

LLVM IR (-O2):
@TCIFLUSH = global i32 0
define i32 @write_to_mouse(i32 %0, i8* nocapture %1, i64 %2) {
%4 = alloca i8
%5 = icmp eq i64 %2, 0
br i1 %5, label %18, label %6
6: ; preds = %3, %6
%7 = phi i64 [ %16, %6 ], [ 0, %3 ]
%8 = phi i32 [ %15, %6 ], [ 0, %3 ]
%9 = getelementptr inbounds i8, i8* %1, i64 %7
%10 = tail call i32 @write(i32 %0, i8* %9, i32 1)
%11 = call i32 @read(i32 %0, i8* %4, i32 1)
%12 = load i8, i8* %4
%13 = icmp ne i8 %12, -6
%14 = zext i1 %13 to i32
%15 = add nuw nsw i32 %8, %14
%16 = add nuw i64 %7, 1
%17 = icmp eq i64 %16, %2
br i1 %17, label %18, label %6
18: ; preds = %6, %3
%19 = phi i32 [ 0, %3 ], [ %15, %6 ]
%20 = tail call i32 @usleep(i32 30000)
%21 = load i32, i32* @TCIFLUSH
%22 = tail call i32 @tcflush(i32 %0, i32 %21)
ret i32 %19
}