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

/* Variables and functions */
int EINVAL;
int O_ACCMODE;
int O_RDONLY;
int O_WRONLY;
int POLLERR;
int POLLIN;
int POLLOUT;
int native_pcm_poll_prepare(int fd, int fmode, struct pollfd *ufds) {
  if (fd < 0)
    return -EINVAL;
  ufds->fd = fd;
  ufds->events = ((fmode & O_ACCMODE) == O_WRONLY ? 0 : POLLIN) |
                 ((fmode & O_ACCMODE) == O_RDONLY ? 0 : POLLOUT) | POLLERR;
  return 1;
}


x86 assembly (clang, -O0):
<native_pcm_poll_prepare>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    112b <native_pcm_poll_prepare+0x2b>
xor    %eax,%eax
mov    0x2e97(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11bb <native_pcm_poll_prepare+0xbb>
mov    -0x8(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0xc(%rbp),%eax
mov    0x2eaa(%rip),%rcx
and    (%rcx),%eax
mov    0x2e61(%rip),%rcx
cmp    (%rcx),%eax
jne    1159 <native_pcm_poll_prepare+0x59>
xor    %eax,%eax
mov    %eax,-0x1c(%rbp)
jmp    1165 <native_pcm_poll_prepare+0x65>
mov    0x2e68(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0xc(%rbp),%eax
mov    0x2e73(%rip),%rcx
and    (%rcx),%eax
mov    0x2e52(%rip),%rcx
cmp    (%rcx),%eax
jne    1190 <native_pcm_poll_prepare+0x90>
xor    %eax,%eax
mov    %eax,-0x24(%rbp)
jmp    119c <native_pcm_poll_prepare+0x9c>
mov    0x2e61(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%ecx
mov    -0x24(%rbp),%eax
or     %eax,%ecx
mov    0x2e2d(%rip),%rax
or     (%rax),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,0x4(%rax)
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<native_pcm_poll_prepare>:
test   %edi,%edi
js     1152 <native_pcm_poll_prepare+0x52>
mov    %edi,(%rdx)
mov    0x2edb(%rip),%rax
and    (%rax),%esi
mov    0x2e92(%rip),%rdi
xor    %eax,%eax
mov    $0x0,%ecx
cmp    (%rdi),%esi
je     112a <native_pcm_poll_prepare+0x2a>
mov    0x2ea0(%rip),%rcx
mov    (%rcx),%ecx
mov    0x2e9f(%rip),%rdi
cmp    (%rdi),%esi
je     113e <native_pcm_poll_prepare+0x3e>
mov    0x2ebc(%rip),%rax
mov    (%rax),%eax
or     %ecx,%eax
mov    0x2e91(%rip),%rcx
or     (%rcx),%eax
mov    %eax,0x4(%rdx)
mov    $0x1,%eax
ret
mov    0x2e5f(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
ret

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

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