#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 pollfd {
  int events;
};

/* Variables and functions */
int EINVAL;
int POLLERR;
int POLLIN;
int POLLOUT;
int native_pcm_poll_result(int fd, struct pollfd *ufds) {
  int result = 0;
  if (fd < 0)
    return -EINVAL;
  if (ufds->events & POLLIN)
    result |= (1 << 0);
  if (ufds->events & POLLOUT)
    result |= (1 << 1);
  if (ufds->events & POLLERR)
    result |= (1 << 2);
  return result;
}


x86 assembly (clang, -O0):
<native_pcm_poll_result>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    112f <native_pcm_poll_result+0x2f>
xor    %eax,%eax
mov    0x2ea3(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1198 <native_pcm_poll_result+0x98>
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    0x2e9c(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1150 <native_pcm_poll_result+0x50>
mov    -0x14(%rbp),%eax
or     $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    0x2e9b(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1171 <native_pcm_poll_result+0x71>
mov    -0x14(%rbp),%eax
or     $0x2,%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    0x2e62(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1192 <native_pcm_poll_result+0x92>
mov    -0x14(%rbp),%eax
or     $0x4,%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<native_pcm_poll_result>:
test   %edi,%edi
js     1133 <native_pcm_poll_result+0x33>
mov    (%rsi),%ecx
mov    0x2ecb(%rip),%rax
xor    %edx,%edx
test   %ecx,(%rax)
setne  %dl
mov    0x2edd(%rip),%rax
lea    0x2(%rdx),%esi
test   %ecx,(%rax)
cmove  %edx,%esi
mov    0x2eb6(%rip),%rdx
lea    0x4(%rsi),%eax
test   %ecx,(%rdx)
cmove  %esi,%eax
ret
mov    0x2e8e(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
ret

LLVM IR (-O0):
%struct.pollfd = type { i32 }
@EINVAL = global i32 0
@POLLIN = global i32 0
@POLLOUT = global i32 0
@POLLERR = global i32 0
define i32 @native_pcm_poll_result(i32 %0, %struct.pollfd* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.pollfd*
%6 = alloca i32
store i32 %0, i32* %4
store %struct.pollfd* %1, %struct.pollfd** %5
store i32 0, i32* %6
%7 = load i32, i32* %4
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %2
%10 = load i32, i32* @EINVAL
%11 = sub nsw i32 0, %10
store i32 %11, i32* %3
br label %44
12: ; preds = %2
%13 = load %struct.pollfd*, %struct.pollfd** %5
%14 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %13, i32 0, i32 0
%15 = load i32, i32* %14
%16 = load i32, i32* @POLLIN
%17 = and i32 %15, %16
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %12
%20 = load i32, i32* %6
%21 = or i32 %20, 1
store i32 %21, i32* %6
br label %22
22: ; preds = %19, %12
%23 = load %struct.pollfd*, %struct.pollfd** %5
%24 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %23, i32 0, i32 0
%25 = load i32, i32* %24
%26 = load i32, i32* @POLLOUT
%27 = and i32 %25, %26
%28 = icmp ne i32 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %22
%30 = load i32, i32* %6
%31 = or i32 %30, 2
store i32 %31, i32* %6
br label %32
32: ; preds = %29, %22
%33 = load %struct.pollfd*, %struct.pollfd** %5
%34 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %33, i32 0, i32 0
%35 = load i32, i32* %34
%36 = load i32, i32* @POLLERR
%37 = and i32 %35, %36
%38 = icmp ne i32 %37, 0
br i1 %38, label %39, label %42
39: ; preds = %32
%40 = load i32, i32* %6
%41 = or i32 %40, 4
store i32 %41, i32* %6
br label %42
42: ; preds = %39, %32
%43 = load i32, i32* %6
store i32 %43, i32* %3
br label %44
44: ; preds = %42, %9
%45 = load i32, i32* %3
ret i32 %45
}

LLVM IR (-O2):
%struct.pollfd = type { i32 }
@EINVAL = global i32 0
@POLLIN = global i32 0
@POLLOUT = global i32 0
@POLLERR = global i32 0
define i32 @native_pcm_poll_result(i32 %0, %struct.pollfd* nocapture %1) {
%3 = icmp slt i32 %0, 0
br i1 %3, label %4, label %7
4: ; preds = %2
%5 = load i32, i32* @EINVAL
%6 = sub nsw i32 0, %5
br label %24
7: ; preds = %2
%8 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %1, i64 0, i32 0
%9 = load i32, i32* %8
%10 = load i32, i32* @POLLIN
%11 = and i32 %10, %9
%12 = icmp ne i32 %11, 0
%13 = zext i1 %12 to i32
%14 = load i32, i32* @POLLOUT
%15 = and i32 %14, %9
%16 = icmp eq i32 %15, 0
%17 = or i32 %13, 2
%18 = select i1 %16, i32 %13, i32 %17
%19 = load i32, i32* @POLLERR
%20 = and i32 %19, %9
%21 = icmp eq i32 %20, 0
%22 = or i32 %18, 4
%23 = select i1 %21, i32 %18, i32 %22
br label %24
24: ; preds = %7, %4
%25 = phi i32 [ %6, %4 ], [ %23, %7 ]
ret i32 %25
}