#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 */
struct input_event {
  int dummy;
};
typedef size_t ssize_t;

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, size_t, size_t);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
size_t write(int, struct input_event *, size_t);
int uinput_write(int fd, struct input_event *ev, size_t size) {
  ssize_t bytes;
  if ((bytes = write(fd, ev, size)) < size) {
    if (bytes < 0) {
      perror("write");
      return -1;
    }
    fprintf(stderr,
            "uinput_write: unable to write entire struct "
            "(sent %zd of %zd bytes)\n",
            bytes, size);
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<uinput_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x20(%rbp)
cmp    -0x18(%rbp),%rax
jae    11ad <uinput_write+0x7d>
cmpq   $0x0,-0x20(%rbp)
jae    1184 <uinput_write+0x54>
lea    0xe8d(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b4 <uinput_write+0x84>
mov    0x2e55(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
mov    -0x18(%rbp),%rcx
lea    0xe6a(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b4 <uinput_write+0x84>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<uinput_write>:
push   %rbx
mov    %rdx,%rbx
call   1030 <write@plt>
xor    %ecx,%ecx
cmp    %rbx,%rax
jae    1150 <uinput_write+0x30>
mov    0x2ea9(%rip),%rcx
mov    (%rcx),%edi
lea    0xec0(%rip),%rsi
mov    %rax,%rdx
mov    %rbx,%rcx
call   1040 <fprintf@plt>
mov    $0xffffffff,%ecx
mov    %ecx,%eax
pop    %rbx
ret

LLVM IR (-O0):
%struct.input_event = type { i32 }
@.str = [6 x i8] c"write\00"
@stderr = global i32 0
@.str.1 = [69 x i8] c"uinput_write: unable to write entire struct (sent %zd of %zd bytes)\0A\00"
define i32 @uinput_write(i32 %0, %struct.input_event* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.input_event*
%7 = alloca i64
%8 = alloca i64
store i32 %0, i32* %5
store %struct.input_event* %1, %struct.input_event** %6
store i64 %2, i64* %7
%9 = load i32, i32* %5
%10 = load %struct.input_event*, %struct.input_event** %6
%11 = load i64, i64* %7
%12 = call i64 @write(i32 %9, %struct.input_event* %10, i64 %11)
store i64 %12, i64* %8
%13 = load i64, i64* %7
%14 = icmp ult i64 %12, %13
br i1 %14, label %15, label %25
15: ; preds = %3
%16 = load i64, i64* %8
%17 = icmp ult i64 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %15
%19 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %26
20: ; preds = %15
%21 = load i32, i32* @stderr
%22 = load i64, i64* %8
%23 = load i64, i64* %7
%24 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([69 x i8], [69 x i8]* @.str.1, i64 0, i64 0), i64 %22, i64 %23)
store i32 -1, i32* %4
br label %26
25: ; preds = %3
store i32 0, i32* %4
br label %26
26: ; preds = %25, %20, %18
%27 = load i32, i32* %4
ret i32 %27
}

LLVM IR (-O2):
%struct.input_event = type { i32 }
@stderr = global i32 0
@.str.1 = [69 x i8] c"uinput_write: unable to write entire struct (sent %zd of %zd bytes)\0A\00"
define i32 @uinput_write(i32 %0, %struct.input_event* nocapture %1, i64 %2) {
%4 = tail call i64 @write(i32 %0, %struct.input_event* %1, i64 %2)
%5 = icmp ult i64 %4, %2
br i1 %5, label %6, label %9
6: ; preds = %3
%7 = load i32, i32* @stderr
%8 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([69 x i8], [69 x i8]* @.str.1, i64 0, i64 0), i64 %4, i64 %2)
br label %9
9: ; preds = %3, %6
%10 = phi i32 [ -1, %6 ], [ 0, %3 ]
ret i32 %10
}