#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 */
typedef int u_int8_t;
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
scalar_t__ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int select_nosignal(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                    int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int wait_event(int timer_event, int itc_event, u_int8_t *event) {
  fd_set read_event;
  int max, ret;
  FD_ZERO(&read_event);
  if (timer_event != -1)
    FD_SET(timer_event, &read_event);
  FD_SET(itc_event, &read_event);
  max = (timer_event > itc_event) ? timer_event : itc_event;
  ret = select_nosignal(max + 1, &read_event, NULL, NULL, NULL);
  if (ret == -1)
    return -1;
  *event = 0;
  if (timer_event != -1)
    if (FD_ISSET(timer_event, &read_event))
      *event |= 0x01;
  if (FD_ISSET(itc_event, &read_event))
    *event |= 0x02;
  return ret;
}


x86 assembly (clang, -O0):
<wait_event>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
lea    -0x1c(%rbp),%rdi
call   1050 <FD_ZERO@plt>
cmpl   $0xffffffff,-0x8(%rbp)
je     1171 <wait_event+0x31>
mov    -0x8(%rbp),%edi
lea    -0x1c(%rbp),%rsi
call   1060 <FD_SET@plt>
mov    -0xc(%rbp),%edi
lea    -0x1c(%rbp),%rsi
call   1060 <FD_SET@plt>
mov    -0x8(%rbp),%eax
cmp    -0xc(%rbp),%eax
jle    1194 <wait_event+0x54>
mov    -0x8(%rbp),%eax
mov    %eax,-0x28(%rbp)
jmp    119a <wait_event+0x5a>
mov    -0xc(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%edi
add    $0x1,%edi
lea    -0x1c(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rdx
mov    %r8,%rcx
call   1040 <select_nosignal@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    11d3 <wait_event+0x93>
movl   $0xffffffff,-0x4(%rbp)
jmp    1234 <wait_event+0xf4>
mov    -0x18(%rbp),%rax
movl   $0x0,(%rax)
cmpl   $0xffffffff,-0x8(%rbp)
je     120d <wait_event+0xcd>
mov    -0x8(%rbp),%edi
lea    -0x1c(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1208 <wait_event+0xc8>
mov    -0x18(%rbp),%rax
mov    (%rax),%ecx
or     $0x1,%ecx
mov    %ecx,(%rax)
jmp    120d <wait_event+0xcd>
mov    -0xc(%rbp),%edi
lea    -0x1c(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     122e <wait_event+0xee>
mov    -0x18(%rbp),%rax
mov    (%rax),%ecx
or     $0x2,%ecx
mov    %ecx,(%rax)
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wait_event>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %esi,%ebp
mov    %edi,%ebx
lea    0x4(%rsp),%rdi
call   1050 <FD_ZERO@plt>
cmp    $0xffffffff,%ebx
je     1169 <wait_event+0x29>
lea    0x4(%rsp),%rsi
mov    %ebx,%edi
call   1060 <FD_SET@plt>
lea    0x4(%rsp),%r15
mov    %ebp,%edi
mov    %r15,%rsi
call   1060 <FD_SET@plt>
cmp    %ebp,%ebx
mov    %ebp,%edi
cmovg  %ebx,%edi
add    $0x1,%edi
mov    %r15,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1040 <select_nosignal@plt>
mov    %eax,%r15d
cmp    $0xffffffff,%eax
je     11cf <wait_event+0x8f>
movl   $0x0,(%r14)
cmp    $0xffffffff,%ebx
je     11ba <wait_event+0x7a>
lea    0x4(%rsp),%rsi
mov    %ebx,%edi
call   1030 <FD_ISSET@plt>
test   %rax,%rax
je     11ba <wait_event+0x7a>
orb    $0x1,(%r14)
lea    0x4(%rsp),%rsi
mov    %ebp,%edi
call   1030 <FD_ISSET@plt>
test   %rax,%rax
je     11cf <wait_event+0x8f>
orb    $0x2,(%r14)
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @wait_event(i32 %0, i32 %1, i32* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32* %2, i32** %7
%11 = call i32 @FD_ZERO(i32* %8)
%12 = load i32, i32* %5
%13 = icmp ne i32 %12, -1
br i1 %13, label %14, label %17
14: ; preds = %3
%15 = load i32, i32* %5
%16 = call i32 @FD_SET(i32 %15, i32* %8)
br label %17
17: ; preds = %14, %3
%18 = load i32, i32* %6
%19 = call i32 @FD_SET(i32 %18, i32* %8)
%20 = load i32, i32* %5
%21 = load i32, i32* %6
%22 = icmp sgt i32 %20, %21
br i1 %22, label %23, label %25
23: ; preds = %17
%24 = load i32, i32* %5
br label %27
25: ; preds = %17
%26 = load i32, i32* %6
br label %27
27: ; preds = %25, %23
%28 = phi i32 [ %24, %23 ], [ %26, %25 ]
store i32 %28, i32* %9
%29 = load i32, i32* %9
%30 = add nsw i32 %29, 1
%31 = call i32 @select_nosignal(i32 %30, i32* %8, i32* null, i32* null, i32* null)
store i32 %31, i32* %10
%32 = load i32, i32* %10
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %35
34: ; preds = %27
store i32 -1, i32* %4
br label %58
35: ; preds = %27
%36 = load i32*, i32** %7
store i32 0, i32* %36
%37 = load i32, i32* %5
%38 = icmp ne i32 %37, -1
br i1 %38, label %39, label %48
39: ; preds = %35
%40 = load i32, i32* %5
%41 = call i64 @FD_ISSET(i32 %40, i32* %8)
%42 = icmp ne i64 %41, 0
br i1 %42, label %43, label %47
43: ; preds = %39
%44 = load i32*, i32** %7
%45 = load i32, i32* %44
%46 = or i32 %45, 1
store i32 %46, i32* %44
br label %47
47: ; preds = %43, %39
br label %48
48: ; preds = %47, %35
%49 = load i32, i32* %6
%50 = call i64 @FD_ISSET(i32 %49, i32* %8)
%51 = icmp ne i64 %50, 0
br i1 %51, label %52, label %56
52: ; preds = %48
%53 = load i32*, i32** %7
%54 = load i32, i32* %53
%55 = or i32 %54, 2
store i32 %55, i32* %53
br label %56
56: ; preds = %52, %48
%57 = load i32, i32* %10
store i32 %57, i32* %4
br label %58
58: ; preds = %56, %34
%59 = load i32, i32* %4
ret i32 %59
}

LLVM IR (-O2):
define i32 @wait_event(i32 %0, i32 %1, i32* nocapture %2) {
%4 = alloca i32
%5 = bitcast i32* %4 to i8*
%6 = call i32 @FD_ZERO(i32* %4)
%7 = icmp eq i32 %0, -1
br i1 %7, label %10, label %8
8: ; preds = %3
%9 = call i32 @FD_SET(i32 %0, i32* %4)
br label %10
10: ; preds = %8, %3
%11 = call i32 @FD_SET(i32 %1, i32* %4)
%12 = icmp sgt i32 %0, %1
%13 = select i1 %12, i32 %0, i32 %1
%14 = add nsw i32 %13, 1
%15 = call i32 @select_nosignal(i32 %14, i32* %4, i32* null, i32* null, i32* null)
%16 = icmp eq i32 %15, -1
br i1 %16, label %30, label %17
17: ; preds = %10
store i32 0, i32* %2
br i1 %7, label %24, label %18
18: ; preds = %17
%19 = call i64 @FD_ISSET(i32 %0, i32* %4)
%20 = icmp eq i64 %19, 0
br i1 %20, label %24, label %21
21: ; preds = %18
%22 = load i32, i32* %2
%23 = or i32 %22, 1
store i32 %23, i32* %2
br label %24
24: ; preds = %18, %21, %17
%25 = call i64 @FD_ISSET(i32 %1, i32* %4)
%26 = icmp eq i64 %25, 0
br i1 %26, label %30, label %27
27: ; preds = %24
%28 = load i32, i32* %2
%29 = or i32 %28, 2
store i32 %29, i32* %2
br label %30
30: ; preds = %24, %27, %10
ret i32 %15
}