#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 {
  scalar_t__ type;
  scalar_t__ code;
  scalar_t__ value;
};

/* Variables and functions */
scalar_t__ EV_KEY;
scalar_t__ KEY_MAX;
int /*<<< orphan*/ debug(char *, int, scalar_t__, scalar_t__, scalar_t__);
int /*<<< orphan*/ fatal(char *, int);
int read(int, struct input_event *, int);
int evreadkey(int fd) {
  struct input_event e;
  int n;
  n = read(fd, &e, sizeof e);
  if (n < 0)
    fatal("evreadkey: error reading from fd %d", fd);
  if (n != sizeof e)
    fatal("evreadkey: short read from fd %d", fd);
  debug("%-3d\t0x%.2x\t%3d\t0x%.8x\n", fd, e.type, e.code, e.value);
  if (e.type != EV_KEY)
    return -1;
  if (e.code >= KEY_MAX)
    return -1;
  if (e.value == 0)
    return -1;
  return e.code;
}


x86 assembly (clang, -O0):
<evreadkey>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x18,%edx
call   1040 <read@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1168 <evreadkey+0x38>
mov    -0x8(%rbp),%esi
lea    0xe9d(%rip),%rdi
call   1050 <fatal@plt>
movslq -0x24(%rbp),%rax
cmp    $0x18,%rax
je     1185 <evreadkey+0x55>
mov    -0x8(%rbp),%esi
lea    0xea4(%rip),%rdi
call   1050 <fatal@plt>
mov    -0x8(%rbp),%esi
mov    -0x20(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    -0x10(%rbp),%r8
lea    0xeaa(%rip),%rdi
call   1030 <debug@plt>
mov    -0x20(%rbp),%rax
mov    0x2e3d(%rip),%rcx
cmp    (%rcx),%rax
je     11c0 <evreadkey+0x90>
movl   $0xffffffff,-0x4(%rbp)
jmp    11fe <evreadkey+0xce>
mov    -0x18(%rbp),%rax
mov    0x2e0d(%rip),%rcx
cmp    (%rcx),%rax
jl     11e0 <evreadkey+0xb0>
movl   $0xffffffff,-0x4(%rbp)
jmp    11fe <evreadkey+0xce>
cmpq   $0x0,-0x10(%rbp)
jne    11f7 <evreadkey+0xc7>
movl   $0xffffffff,-0x4(%rbp)
jmp    11fe <evreadkey+0xce>
mov    -0x18(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<evreadkey>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebp
mov    %rsp,%rsi
mov    $0x18,%edx
call   1040 <read@plt>
test   %eax,%eax
js     1154 <evreadkey+0x24>
cmp    $0x18,%eax
jne    1162 <evreadkey+0x32>
jmp    1170 <evreadkey+0x40>
lea    0xea5(%rip),%rdi
mov    %ebp,%esi
call   1050 <fatal@plt>
lea    0xebb(%rip),%rdi
mov    %ebp,%esi
call   1050 <fatal@plt>
mov    (%rsp),%rbx
mov    0x8(%rsp),%r14
mov    0x10(%rsp),%r15
lea    0xec0(%rip),%rdi
mov    %ebp,%esi
mov    %rbx,%rdx
mov    %r14,%rcx
mov    %r15,%r8
call   1030 <debug@plt>
mov    0x2e4c(%rip),%rcx
mov    $0xffffffff,%eax
cmp    (%rcx),%rbx
jne    11c1 <evreadkey+0x91>
mov    0x2e2b(%rip),%rax
cmp    (%rax),%r14
mov    $0xffffffff,%ecx
cmovl  %r14d,%ecx
cmp    $0x1,%r15
sbb    %eax,%eax
or     %ecx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.input_event = type { i64, i64, i64 }
@.str = [36 x i8] c"evreadkey: error reading from fd %d\00"
@.str.1 = [33 x i8] c"evreadkey: short read from fd %d\00"
@.str.2 = [24 x i8] c"%-3d\090x%.2x\09%3d\090x%.8x\0A\00"
@EV_KEY = global i64 0
@KEY_MAX = global i64 0
define i32 @evreadkey(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.input_event
%5 = alloca i32
store i32 %0, i32* %3
%6 = load i32, i32* %3
%7 = call i32 @read(i32 %6, %struct.input_event* %4, i32 24)
store i32 %7, i32* %5
%8 = load i32, i32* %5
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %1
%11 = load i32, i32* %3
%12 = call i32 @fatal(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i32 %11)
br label %13
13: ; preds = %10, %1
%14 = load i32, i32* %5
%15 = sext i32 %14 to i64
%16 = icmp ne i64 %15, 24
br i1 %16, label %17, label %20
17: ; preds = %13
%18 = load i32, i32* %3
%19 = call i32 @fatal(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i32 %18)
br label %20
20: ; preds = %17, %13
%21 = load i32, i32* %3
%22 = getelementptr inbounds %struct.input_event, %struct.input_event* %4, i32 0, i32 0
%23 = load i64, i64* %22
%24 = getelementptr inbounds %struct.input_event, %struct.input_event* %4, i32 0, i32 1
%25 = load i64, i64* %24
%26 = getelementptr inbounds %struct.input_event, %struct.input_event* %4, i32 0, i32 2
%27 = load i64, i64* %26
%28 = call i32 @debug(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i32 %21, i64 %23, i64 %25, i64 %27)
%29 = getelementptr inbounds %struct.input_event, %struct.input_event* %4, i32 0, i32 0
%30 = load i64, i64* %29
%31 = load i64, i64* @EV_KEY
%32 = icmp ne i64 %30, %31
br i1 %32, label %33, label %34
33: ; preds = %20
store i32 -1, i32* %2
br label %49
34: ; preds = %20
%35 = getelementptr inbounds %struct.input_event, %struct.input_event* %4, i32 0, i32 1
%36 = load i64, i64* %35
%37 = load i64, i64* @KEY_MAX
%38 = icmp sge i64 %36, %37
br i1 %38, label %39, label %40
39: ; preds = %34
store i32 -1, i32* %2
br label %49
40: ; preds = %34
%41 = getelementptr inbounds %struct.input_event, %struct.input_event* %4, i32 0, i32 2
%42 = load i64, i64* %41
%43 = icmp eq i64 %42, 0
br i1 %43, label %44, label %45
44: ; preds = %40
store i32 -1, i32* %2
br label %49
45: ; preds = %40
%46 = getelementptr inbounds %struct.input_event, %struct.input_event* %4, i32 0, i32 1
%47 = load i64, i64* %46
%48 = trunc i64 %47 to i32
store i32 %48, i32* %2
br label %49
49: ; preds = %45, %44, %39, %33
%50 = load i32, i32* %2
ret i32 %50
}

LLVM IR (-O2):
%struct.input_event = type { i64, i64, i64 }
@.str = [36 x i8] c"evreadkey: error reading from fd %d\00"
@.str.1 = [33 x i8] c"evreadkey: short read from fd %d\00"
@.str.2 = [24 x i8] c"%-3d\090x%.2x\09%3d\090x%.8x\0A\00"
@EV_KEY = global i64 0
@KEY_MAX = global i64 0
define i32 @evreadkey(i32 %0) {
%2 = alloca %struct.input_event
%3 = bitcast %struct.input_event* %2 to i8*
%4 = call i32 @read(i32 %0, %struct.input_event* %2, i32 24)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %1
%7 = tail call i32 @fatal(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i32 %0)
br label %10
8: ; preds = %1
%9 = icmp eq i32 %4, 24
br i1 %9, label %12, label %10
10: ; preds = %6, %8
%11 = tail call i32 @fatal(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i32 %0)
br label %12
12: ; preds = %10, %8
%13 = getelementptr inbounds %struct.input_event, %struct.input_event* %2, i64 0, i32 0
%14 = load i64, i64* %13
%15 = getelementptr inbounds %struct.input_event, %struct.input_event* %2, i64 0, i32 1
%16 = load i64, i64* %15
%17 = getelementptr inbounds %struct.input_event, %struct.input_event* %2, i64 0, i32 2
%18 = load i64, i64* %17
%19 = tail call i32 @debug(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i32 %0, i64 %14, i64 %16, i64 %18)
%20 = load i64, i64* @EV_KEY
%21 = icmp eq i64 %14, %20
br i1 %21, label %22, label %29
22: ; preds = %12
%23 = load i64, i64* @KEY_MAX
%24 = icmp sge i64 %16, %23
%25 = icmp eq i64 %18, 0
%26 = or i1 %25, %24
%27 = trunc i64 %16 to i32
%28 = select i1 %26, i32 -1, i32 %27
br label %29
29: ; preds = %22, %12
%30 = phi i32 [ -1, %12 ], [ %28, %22 ]
ret i32 %30
}