#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 hiddev_event {
  unsigned char value;
};
typedef int /*<<< orphan*/ readBuffer;

/* Variables and functions */
int read(int, struct hiddev_event *, int);
int hiddev_read(unsigned char *data, int bufsize, int fd) {
  struct hiddev_event readBuffer[64];
  int ret, n, i;
  ret = read(fd, readBuffer, sizeof(readBuffer));
  if (ret < 0)
    return ret;
  n = ret / sizeof(readBuffer[0]);
  for (i = 0; i < n && i < bufsize; i++)
    data[i] = readBuffer[i].value;
  return n;
}


x86 assembly (clang, -O0):
<hiddev_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    -0x18(%rbp),%edi
lea    -0x60(%rbp),%rsi
mov    $0x40,%edx
call   1030 <read@plt>
mov    %eax,-0x64(%rbp)
cmpl   $0x0,-0x64(%rbp)
jge    114b <hiddev_read+0x3b>
mov    -0x64(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11b1 <hiddev_read+0xa1>
movslq -0x64(%rbp),%rax
shr    $0x0,%rax
mov    %eax,-0x68(%rbp)
movl   $0x0,-0x6c(%rbp)
mov    -0x6c(%rbp),%ecx
xor    %eax,%eax
cmp    -0x68(%rbp),%ecx
mov    %al,-0x6d(%rbp)
jge    117a <hiddev_read+0x6a>
mov    -0x6c(%rbp),%eax
cmp    -0x14(%rbp),%eax
setl   %al
mov    %al,-0x6d(%rbp)
mov    -0x6d(%rbp),%al
test   $0x1,%al
jne    118a <hiddev_read+0x7a>
jmp    11ab <hiddev_read+0x9b>
movslq -0x6c(%rbp),%rax
mov    -0x60(%rbp,%rax,1),%dl
mov    -0x10(%rbp),%rax
movslq -0x6c(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x6c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x6c(%rbp)
jmp    115d <hiddev_read+0x4d>
mov    -0x68(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hiddev_read>:
push   %rbp
push   %r14
push   %rbx
sub    $0x40,%rsp
mov    %esi,%ebx
mov    %rdi,%r14
mov    %rsp,%rsi
mov    %edx,%edi
mov    $0x40,%edx
call   1030 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
js     1167 <hiddev_read+0x47>
je     1167 <hiddev_read+0x47>
test   %ebx,%ebx
jle    1167 <hiddev_read+0x47>
mov    %ebp,%eax
add    $0xffffffff,%eax
add    $0xffffffff,%ebx
cmp    %ebx,%eax
cmovb  %eax,%ebx
add    $0x1,%rbx
mov    %rsp,%rsi
mov    %r14,%rdi
mov    %rbx,%rdx
call   1040 <memcpy@plt>
mov    %ebp,%eax
add    $0x40,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.hiddev_event = type { i8 }
define i32 @hiddev_read(i8* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca [64 x %struct.hiddev_event]
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%12 = load i32, i32* %7
%13 = getelementptr inbounds [64 x %struct.hiddev_event], [64 x %struct.hiddev_event]* %8, i64 0, i64 0
%14 = call i32 @read(i32 %12, %struct.hiddev_event* %13, i32 64)
store i32 %14, i32* %9
%15 = load i32, i32* %9
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %3
%18 = load i32, i32* %9
store i32 %18, i32* %4
br label %49
19: ; preds = %3
%20 = load i32, i32* %9
%21 = sext i32 %20 to i64
%22 = udiv i64 %21, 1
%23 = trunc i64 %22 to i32
store i32 %23, i32* %10
store i32 0, i32* %11
br label %24
24: ; preds = %44, %19
%25 = load i32, i32* %11
%26 = load i32, i32* %10
%27 = icmp slt i32 %25, %26
br i1 %27, label %28, label %32
28: ; preds = %24
%29 = load i32, i32* %11
%30 = load i32, i32* %6
%31 = icmp slt i32 %29, %30
br label %32
32: ; preds = %28, %24
%33 = phi i1 [ false, %24 ], [ %31, %28 ]
br i1 %33, label %34, label %47
34: ; preds = %32
%35 = load i32, i32* %11
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds [64 x %struct.hiddev_event], [64 x %struct.hiddev_event]* %8, i64 0, i64 %36
%38 = getelementptr inbounds %struct.hiddev_event, %struct.hiddev_event* %37, i32 0, i32 0
%39 = load i8, i8* %38
%40 = load i8*, i8** %5
%41 = load i32, i32* %11
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds i8, i8* %40, i64 %42
store i8 %39, i8* %43
br label %44
44: ; preds = %34
%45 = load i32, i32* %11
%46 = add nsw i32 %45, 1
store i32 %46, i32* %11
br label %24
47: ; preds = %32
%48 = load i32, i32* %10
store i32 %48, i32* %4
br label %49
49: ; preds = %47, %17
%50 = load i32, i32* %4
ret i32 %50
}

LLVM IR (-O2):
%struct.hiddev_event = type { i8 }
define i32 @hiddev_read(i8* nocapture writeonly %0, i32 %1, i32 %2) {
%4 = alloca [64 x %struct.hiddev_event]
%5 = getelementptr inbounds [64 x %struct.hiddev_event], [64 x %struct.hiddev_event]* %4, i64 0, i64 0, i32 0
%6 = getelementptr inbounds [64 x %struct.hiddev_event], [64 x %struct.hiddev_event]* %4, i64 0, i64 0, i32 0
%7 = getelementptr inbounds [64 x %struct.hiddev_event], [64 x %struct.hiddev_event]* %4, i64 0, i64 0
%8 = call i32 @read(i32 %2, %struct.hiddev_event* %7, i32 64)
%9 = icmp slt i32 %8, 0
br i1 %9, label %20, label %10
10: ; preds = %3
%11 = icmp ne i32 %8, 0
%12 = icmp sgt i32 %1, 0
%13 = and i1 %11, %12
br i1 %13, label %14, label %20
14: ; preds = %10
%15 = add nsw i32 %8, -1
%16 = add nsw i32 %1, -1
%17 = call i32 @llvm.umin.i32(i32 %15, i32 %16)
%18 = zext i32 %17 to i64
%19 = add nuw nsw i64 %18, 1
br label %20
20: ; preds = %14, %10, %3
ret i32 %8
}