#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 js_event {
  int type;
};

/* Variables and functions */
scalar_t__ EINTR;
int JS_EVENT_INIT;
scalar_t__ errno;
int /*<<< orphan*/ perror(char *);
int read(int, struct js_event *, int);
int read_event(int joy, int type, struct js_event *joydata) {
  int len;
  while (1) {
    len = read(joy, joydata, sizeof(struct js_event));
    if (len < 0) {
      if (errno != EINTR) {
        perror("read");
        return 0;
      } else {
        continue;
      }
    }
    if (joydata->type & JS_EVENT_INIT) {
      continue;
    }
    if (joydata->type & type) {
      break;
    }
  }
  return 1;
}


x86 assembly (clang, -O0):
<read_event>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    $0x4,%edx
call   1030 <read@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1187 <read_event+0x67>
mov    0x2e89(%rip),%rax
mov    (%rax),%rax
mov    0x2e77(%rip),%rcx
cmp    (%rcx),%rax
je     1182 <read_event+0x62>
lea    0xe8f(%rip),%rdi
call   1040 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    11c7 <read_event+0xa7>
jmp    1132 <read_event+0x12>
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
mov    0x2e3c(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     11a4 <read_event+0x84>
jmp    1132 <read_event+0x12>
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
and    -0xc(%rbp),%eax
cmp    $0x0,%eax
je     11bb <read_event+0x9b>
jmp    11c0 <read_event+0xa0>
jmp    1132 <read_event+0x12>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_event>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    %esi,%r14d
mov    %edi,%ebp
mov    0x2ea6(%rip),%r15
mov    0x2e97(%rip),%r12
mov    0x2e88(%rip),%r13
jmp    1159 <read_event+0x39>
nopw   0x0(%rax,%rax,1)
mov    (%r15),%rax
cmp    (%r12),%rax
jne    118d <read_event+0x6d>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x4,%edx
call   1030 <read@plt>
test   %eax,%eax
js     1150 <read_event+0x30>
mov    (%rbx),%eax
test   %r14d,%eax
je     1159 <read_event+0x39>
and    0x0(%r13),%eax
jne    1159 <read_event+0x39>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe6c(%rip),%rdi
call   1040 <perror@plt>
xor    %eax,%eax
jmp    117e <read_event+0x5e>

LLVM IR (-O0):
%struct.js_event = type { i32 }
@errno = global i64 0
@EINTR = global i64 0
@.str = [5 x i8] c"read\00"
@JS_EVENT_INIT = global i32 0
define i32 @read_event(i32 %0, i32 %1, %struct.js_event* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.js_event*
%8 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store %struct.js_event* %2, %struct.js_event** %7
br label %9
9: ; preds = %3, %21, %29, %38
%10 = load i32, i32* %5
%11 = load %struct.js_event*, %struct.js_event** %7
%12 = call i32 @read(i32 %10, %struct.js_event* %11, i32 4)
store i32 %12, i32* %8
%13 = load i32, i32* %8
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %22
15: ; preds = %9
%16 = load i64, i64* @errno
%17 = load i64, i64* @EINTR
%18 = icmp ne i64 %16, %17
br i1 %18, label %19, label %21
19: ; preds = %15
%20 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %4
br label %40
21: ; preds = %15
br label %9
22: ; preds = %9
%23 = load %struct.js_event*, %struct.js_event** %7
%24 = getelementptr inbounds %struct.js_event, %struct.js_event* %23, i32 0, i32 0
%25 = load i32, i32* %24
%26 = load i32, i32* @JS_EVENT_INIT
%27 = and i32 %25, %26
%28 = icmp ne i32 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %22
br label %9
30: ; preds = %22
%31 = load %struct.js_event*, %struct.js_event** %7
%32 = getelementptr inbounds %struct.js_event, %struct.js_event* %31, i32 0, i32 0
%33 = load i32, i32* %32
%34 = load i32, i32* %6
%35 = and i32 %33, %34
%36 = icmp ne i32 %35, 0
br i1 %36, label %37, label %38
37: ; preds = %30
br label %39
38: ; preds = %30
br label %9
39: ; preds = %37
store i32 1, i32* %4
br label %40
40: ; preds = %39, %19
%41 = load i32, i32* %4
ret i32 %41
}

LLVM IR (-O2):
%struct.js_event = type { i32 }
@errno = global i64 0
@EINTR = global i64 0
@.str = [5 x i8] c"read\00"
@JS_EVENT_INIT = global i32 0
define i32 @read_event(i32 %0, i32 %1, %struct.js_event* nocapture %2) {
%4 = getelementptr inbounds %struct.js_event, %struct.js_event* %2, i64 0, i32 0
br label %5
5: ; preds = %12, %3
%6 = tail call i32 @read(i32 %0, %struct.js_event* %2, i32 4)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %15
8: ; preds = %5
%9 = load i64, i64* @errno
%10 = load i64, i64* @EINTR
%11 = icmp eq i64 %9, %10
br i1 %11, label %12, label %13
12: ; preds = %8, %15
br label %5
13: ; preds = %8
%14 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %23
15: ; preds = %5
%16 = load i32, i32* %4
%17 = load i32, i32* @JS_EVENT_INIT
%18 = and i32 %17, %16
%19 = icmp ne i32 %18, 0
%20 = and i32 %16, %1
%21 = icmp eq i32 %20, 0
%22 = or i1 %21, %19
br i1 %22, label %12, label %23
23: ; preds = %15, %13
%24 = phi i32 [ 0, %13 ], [ 1, %15 ]
ret i32 %24
}