#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 type;
  int code;
  int value;
};
typedef int ssize_t;
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
int /*<<< orphan*/ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ printf(char *, int);
int read(int, struct input_event *, int);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int get_data(int input_dev_fd) {
  struct input_event iev;
  int err = 0;
  fd_set read_fds;
  ssize_t nbyte;

  if (input_dev_fd < 0) {
    return -1;
  }
  FD_ZERO(&read_fds);
  FD_SET(input_dev_fd, &read_fds);
  err = select(input_dev_fd + 1, &read_fds, NULL, NULL, NULL);
  if (err <= 0) {
    return -1;
  }
  if (!FD_ISSET(input_dev_fd, &read_fds)) {
    return -1;
  }
  nbyte = read(input_dev_fd, &iev, sizeof(struct input_event));
  if (nbyte < 0 || nbyte != sizeof(struct input_event)) {
    fprintf(stderr, "Input read error: num=%d", nbyte);
    return -1;
  }
  printf("=== type=%d ===\n", iev.type);
  printf("  code=%d\n", iev.code);
  printf("  val =%d\n\n", iev.value);
  return 0;
}


x86 assembly (clang, -O0):
<get_data>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    1198 <get_data+0x28>
movl   $0xffffffff,-0x4(%rbp)
jmp    1285 <get_data+0x115>
lea    -0x20(%rbp),%rdi
call   1070 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1090 <FD_SET@plt>
mov    -0x8(%rbp),%edi
add    $0x1,%edi
lea    -0x20(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rdx
mov    %r8,%rcx
call   1080 <select@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jg     11e0 <get_data+0x70>
movl   $0xffffffff,-0x4(%rbp)
jmp    1285 <get_data+0x115>
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%eax
jne    1201 <get_data+0x91>
movl   $0xffffffff,-0x4(%rbp)
jmp    1285 <get_data+0x115>
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1050 <read@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jl     122d <get_data+0xbd>
movslq -0x24(%rbp),%rax
cmp    $0xc,%rax
je     1251 <get_data+0xe1>
mov    0x2dac(%rip),%rax
mov    (%rax),%edi
mov    -0x24(%rbp),%edx
lea    0xdc0(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1285 <get_data+0x115>
mov    -0x18(%rbp),%esi
lea    0xdbe(%rip),%rdi
call   1040 <printf@plt>
mov    -0x14(%rbp),%esi
lea    0xdc0(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10(%rbp),%esi
lea    0xdbc(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_data>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    $0xffffffff,%r14d
test   %edi,%edi
js     1227 <get_data+0xb7>
mov    %edi,%ebx
lea    0xc(%rsp),%rbp
mov    %rbp,%rdi
call   1070 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
call   1090 <FD_SET@plt>
lea    0x1(%rbx),%edi
mov    %rbp,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1080 <select@plt>
test   %eax,%eax
jle    1227 <get_data+0xb7>
lea    0xc(%rsp),%rsi
mov    %ebx,%edi
call   1030 <FD_ISSET@plt>
test   %eax,%eax
je     1227 <get_data+0xb7>
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1050 <read@plt>
cmp    $0xc,%eax
jne    1210 <get_data+0xa0>
mov    0x10(%rsp),%esi
lea    0xe33(%rip),%rdi
call   1040 <printf@plt>
mov    0x14(%rsp),%esi
lea    0xe34(%rip),%rdi
call   1040 <printf@plt>
mov    0x18(%rsp),%esi
lea    0xe2f(%rip),%rdi
call   1040 <printf@plt>
xor    %r14d,%r14d
jmp    1227 <get_data+0xb7>
mov    0x2dc9(%rip),%rcx
mov    (%rcx),%edi
lea    0xde0(%rip),%rsi
mov    %eax,%edx
call   1060 <fprintf@plt>
mov    %r14d,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.input_event = type { i32, i32, i32 }
@stderr = global i32 0
@.str = [25 x i8] c"Input read error: num=%d\00"
@.str.1 = [17 x i8] c"=== type=%d ===\0A\00"
@.str.2 = [11 x i8] c" code=%d\0A\00"
@.str.3 = [12 x i8] c" val =%d\0A\0A\00"
define i32 @get_data(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.input_event
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %3
store i32 0, i32* %5
%8 = load i32, i32* %3
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %11
10: ; preds = %1
store i32 -1, i32* %2
br label %49
11: ; preds = %1
%12 = call i32 @FD_ZERO(i32* %6)
%13 = load i32, i32* %3
%14 = call i32 @FD_SET(i32 %13, i32* %6)
%15 = load i32, i32* %3
%16 = add nsw i32 %15, 1
%17 = call i32 @select(i32 %16, i32* %6, i32* null, i32* null, i32* null)
store i32 %17, i32* %5
%18 = load i32, i32* %5
%19 = icmp sle i32 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %11
store i32 -1, i32* %2
br label %49
21: ; preds = %11
%22 = load i32, i32* %3
%23 = call i32 @FD_ISSET(i32 %22, i32* %6)
%24 = icmp ne i32 %23, 0
br i1 %24, label %26, label %25
25: ; preds = %21
store i32 -1, i32* %2
br label %49
26: ; preds = %21
%27 = load i32, i32* %3
%28 = call i32 @read(i32 %27, %struct.input_event* %4, i32 12)
store i32 %28, i32* %7
%29 = load i32, i32* %7
%30 = icmp slt i32 %29, 0
br i1 %30, label %35, label %31
31: ; preds = %26
%32 = load i32, i32* %7
%33 = sext i32 %32 to i64
%34 = icmp ne i64 %33, 12
br i1 %34, label %35, label %39
35: ; preds = %31, %26
%36 = load i32, i32* @stderr
%37 = load i32, i32* %7
%38 = call i32 @fprintf(i32 %36, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 %37)
store i32 -1, i32* %2
br label %49
39: ; preds = %31
%40 = getelementptr inbounds %struct.input_event, %struct.input_event* %4, i32 0, i32 0
%41 = load i32, i32* %40
%42 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %41)
%43 = getelementptr inbounds %struct.input_event, %struct.input_event* %4, i32 0, i32 1
%44 = load i32, i32* %43
%45 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 %44)
%46 = getelementptr inbounds %struct.input_event, %struct.input_event* %4, i32 0, i32 2
%47 = load i32, i32* %46
%48 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32 %47)
store i32 0, i32* %2
br label %49
49: ; preds = %39, %35, %25, %20, %10
%50 = load i32, i32* %2
ret i32 %50
}

LLVM IR (-O2):
%struct.input_event = type { i32, i32, i32 }
@stderr = global i32 0
@.str = [25 x i8] c"Input read error: num=%d\00"
@.str.1 = [17 x i8] c"=== type=%d ===\0A\00"
@.str.2 = [11 x i8] c" code=%d\0A\00"
@.str.3 = [12 x i8] c" val =%d\0A\0A\00"
define i32 @get_data(i32 %0) {
%2 = alloca %struct.input_event
%3 = alloca i32
%4 = bitcast %struct.input_event* %2 to i8*
%5 = bitcast i32* %3 to i8*
%6 = icmp slt i32 %0, 0
br i1 %6, label %32, label %7
7: ; preds = %1
%8 = call i32 @FD_ZERO(i32* %3)
%9 = call i32 @FD_SET(i32 %0, i32* %3)
%10 = add nuw nsw i32 %0, 1
%11 = call i32 @select(i32 %10, i32* %3, i32* null, i32* null, i32* null)
%12 = icmp slt i32 %11, 1
br i1 %12, label %32, label %13
13: ; preds = %7
%14 = call i32 @FD_ISSET(i32 %0, i32* %3)
%15 = icmp eq i32 %14, 0
br i1 %15, label %32, label %16
16: ; preds = %13
%17 = call i32 @read(i32 %0, %struct.input_event* %2, i32 12)
%18 = icmp eq i32 %17, 12
br i1 %18, label %22, label %19
19: ; preds = %16
%20 = load i32, i32* @stderr
%21 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 %17)
br label %32
22: ; preds = %16
%23 = getelementptr inbounds %struct.input_event, %struct.input_event* %2, i64 0, i32 0
%24 = load i32, i32* %23
%25 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %24)
%26 = getelementptr inbounds %struct.input_event, %struct.input_event* %2, i64 0, i32 1
%27 = load i32, i32* %26
%28 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 %27)
%29 = getelementptr inbounds %struct.input_event, %struct.input_event* %2, i64 0, i32 2
%30 = load i32, i32* %29
%31 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32 %30)
br label %32
32: ; preds = %13, %7, %1, %22, %19
%33 = phi i32 [ -1, %19 ], [ 0, %22 ], [ -1, %1 ], [ -1, %7 ], [ -1, %13 ]
ret i32 %33
}