#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 uhid_event {
  int dummy;
};
typedef int ssize_t;
typedef int /*<<< orphan*/ ev;

/* Variables and functions */
int EFAULT;
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ stderr;
int write(int, struct uhid_event const *, int);
int uhid_write(int fd, const struct uhid_event *ev) {
  ssize_t ret;
  ret = write(fd, ev, sizeof(*ev));
  if (ret < 0) {
    fprintf(stderr, "Cannot write to uhid: %m\n");
    return -errno;
  } else if (ret != sizeof(*ev)) {
    fprintf(stderr, "Wrong size written to uhid: %ld != %lu\n", ret,
            sizeof(ev));
    return -EFAULT;
  } else {
    return 0;
  }
}


x86 assembly (clang, -O0):
<uhid_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1177 <uhid_write+0x57>
mov    0x2e84(%rip),%rax
mov    (%rax),%edi
lea    0xea3(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
xor    %eax,%eax
mov    0x2e63(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11be <uhid_write+0x9e>
movslq -0x14(%rbp),%rax
cmp    $0x4,%rax
je     11b7 <uhid_write+0x97>
mov    0x2e4c(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%edx
lea    0xe82(%rip),%rsi
mov    $0x8,%ecx
mov    $0x0,%al
call   1040 <fprintf@plt>
xor    %eax,%eax
mov    0x2e4b(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11be <uhid_write+0x9e>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<uhid_write>:
push   %rbx
mov    $0x4,%edx
call   1030 <write@plt>
test   %eax,%eax
js     115f <uhid_write+0x3f>
xor    %ebx,%ebx
cmp    $0x4,%eax
je     1181 <uhid_write+0x61>
mov    0x2e9b(%rip),%rcx
mov    (%rcx),%edi
lea    0xed4(%rip),%rsi
xor    %ebx,%ebx
mov    $0x8,%ecx
mov    %eax,%edx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    0x2e9b(%rip),%rax
jmp    117f <uhid_write+0x5f>
mov    0x2e72(%rip),%rax
mov    (%rax),%edi
lea    0xe91(%rip),%rsi
xor    %ebx,%ebx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    0x2e51(%rip),%rax
sub    (%rax),%ebx
mov    %ebx,%eax
pop    %rbx
ret

LLVM IR (-O0):
%struct.uhid_event = type { i32 }
@stderr = global i32 0
@.str = [26 x i8] c"Cannot write to uhid: %m\0A\00"
@errno = global i32 0
@.str.1 = [40 x i8] c"Wrong size written to uhid: %ld != %lu\0A\00"
@EFAULT = global i32 0
define i32 @uhid_write(i32 %0, %struct.uhid_event* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.uhid_event*
%6 = alloca i32
store i32 %0, i32* %4
store %struct.uhid_event* %1, %struct.uhid_event** %5
%7 = load i32, i32* %4
%8 = load %struct.uhid_event*, %struct.uhid_event** %5
%9 = call i32 @write(i32 %7, %struct.uhid_event* %8, i32 4)
store i32 %9, i32* %6
%10 = load i32, i32* %6
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %17
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%15 = load i32, i32* @errno
%16 = sub nsw i32 0, %15
store i32 %16, i32* %3
br label %28
17: ; preds = %2
%18 = load i32, i32* %6
%19 = sext i32 %18 to i64
%20 = icmp ne i64 %19, 4
br i1 %20, label %21, label %27
21: ; preds = %17
%22 = load i32, i32* @stderr
%23 = load i32, i32* %6
%24 = call i32 (i32, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), i32 %23, i64 8)
%25 = load i32, i32* @EFAULT
%26 = sub nsw i32 0, %25
store i32 %26, i32* %3
br label %28
27: ; preds = %17
store i32 0, i32* %3
br label %28
28: ; preds = %27, %21, %12
%29 = load i32, i32* %3
ret i32 %29
}

LLVM IR (-O2):
%struct.uhid_event = type { i32 }
@stderr = global i32 0
@.str = [26 x i8] c"Cannot write to uhid: %m\0A\00"
@errno = global i32 0
@.str.1 = [40 x i8] c"Wrong size written to uhid: %ld != %lu\0A\00"
@EFAULT = global i32 0
define i32 @uhid_write(i32 %0, %struct.uhid_event* nocapture %1) {
%3 = tail call i32 @write(i32 %0, %struct.uhid_event* %1, i32 4)
%4 = icmp slt i32 %3, 0
br i1 %4, label %5, label %10
5: ; preds = %2
%6 = load i32, i32* @stderr
%7 = tail call i32 (i32, i8*, ...) @fprintf(i32 %6, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%8 = load i32, i32* @errno
%9 = sub nsw i32 0, %8
br label %17
10: ; preds = %2
%11 = icmp eq i32 %3, 4
br i1 %11, label %17, label %12
12: ; preds = %10
%13 = load i32, i32* @stderr
%14 = tail call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), i32 %3, i64 8)
%15 = load i32, i32* @EFAULT
%16 = sub nsw i32 0, %15
br label %17
17: ; preds = %10, %12, %5
%18 = phi i32 [ %9, %5 ], [ %16, %12 ], [ 0, %10 ]
ret i32 %18
}