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

/* Variables and functions */
int POLLIN;
int POLLOUT;
scalar_t__ poll(struct pollfd *, int, int);
extern int socket_wait(int fd, int flag, int timeout) {
  int flag_out = 0;
  struct pollfd pfd;
  pfd.fd = fd;
  pfd.events = 0;
  if (flag & 1) {
    pfd.events |= POLLIN;
  }
  if (flag & 2) {
    pfd.events |= POLLOUT;
  }
  if (poll(&pfd, 1, timeout) > 0) {
    if (pfd.revents & POLLIN) {
      flag_out |= 1;
    }
    if (pfd.revents & POLLOUT) {
      flag_out |= 2;
    }
  }
  return flag_out;
}


x86 assembly (clang, -O0):
<socket_wait>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x20(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x8(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     1153 <socket_wait+0x43>
mov    0x2e95(%rip),%rax
mov    (%rax),%eax
or     -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%eax
and    $0x2,%eax
cmp    $0x0,%eax
je     1171 <socket_wait+0x61>
mov    0x2e8f(%rip),%rax
mov    (%rax),%eax
or     -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0xc(%rbp),%edx
lea    -0x20(%rbp),%rdi
mov    $0x1,%esi
call   1030 <poll@plt>
cmp    $0x0,%rax
jle    11cd <socket_wait+0xbd>
mov    -0x18(%rbp),%eax
mov    0x2e4a(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     11aa <socket_wait+0x9a>
mov    -0x10(%rbp),%eax
or     $0x1,%eax
mov    %eax,-0x10(%rbp)
mov    -0x18(%rbp),%eax
mov    0x2e44(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     11c8 <socket_wait+0xb8>
mov    -0x10(%rbp),%eax
or     $0x2,%eax
mov    %eax,-0x10(%rbp)
jmp    11cd <socket_wait+0xbd>
mov    -0x10(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socket_wait>:
sub    $0x18,%rsp
mov    %edi,0x8(%rsp)
movl   $0x0,0xc(%rsp)
xor    %eax,%eax
test   $0x1,%sil
je     1135 <socket_wait+0x25>
mov    0x2eb1(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
test   $0x2,%sil
je     1148 <socket_wait+0x38>
mov    0x2eb6(%rip),%rcx
or     (%rcx),%eax
mov    %eax,0xc(%rsp)
lea    0x8(%rsp),%rdi
mov    $0x1,%esi
call   1030 <poll@plt>
test   %rax,%rax
jle    1182 <socket_wait+0x72>
mov    0x10(%rsp),%ecx
mov    0x2e79(%rip),%rax
xor    %edx,%edx
test   %ecx,(%rax)
setne  %dl
mov    0x2e83(%rip),%rsi
lea    0x2(%rdx),%eax
test   %ecx,(%rsi)
cmove  %edx,%eax
add    $0x18,%rsp
ret
xor    %eax,%eax
add    $0x18,%rsp
ret

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

LLVM IR (-O2):
%struct.pollfd = type { i32, i32, i32 }
@POLLIN = global i32 0
@POLLOUT = global i32 0
define i32 @socket_wait(i32 %0, i32 %1, i32 %2) {
%4 = alloca %struct.pollfd
%5 = bitcast %struct.pollfd* %4 to i8*
%6 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %4, i64 0, i32 0
store i32 %0, i32* %6
%7 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %4, i64 0, i32 1
store i32 0, i32* %7
%8 = and i32 %1, 1
%9 = icmp eq i32 %8, 0
br i1 %9, label %12, label %10
10: ; preds = %3
%11 = load i32, i32* @POLLIN
store i32 %11, i32* %7
br label %12
12: ; preds = %10, %3
%13 = phi i32 [ %11, %10 ], [ 0, %3 ]
%14 = and i32 %1, 2
%15 = icmp eq i32 %14, 0
br i1 %15, label %19, label %16
16: ; preds = %12
%17 = load i32, i32* @POLLOUT
%18 = or i32 %13, %17
store i32 %18, i32* %7
br label %19
19: ; preds = %16, %12
%20 = call i64 @poll(%struct.pollfd* %4, i32 1, i32 %2)
%21 = icmp sgt i64 %20, 0
br i1 %21, label %22, label %34
22: ; preds = %19
%23 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %4, i64 0, i32 2
%24 = load i32, i32* %23
%25 = load i32, i32* @POLLIN
%26 = and i32 %25, %24
%27 = icmp ne i32 %26, 0
%28 = zext i1 %27 to i32
%29 = load i32, i32* @POLLOUT
%30 = and i32 %29, %24
%31 = icmp eq i32 %30, 0
%32 = or i32 %28, 2
%33 = select i1 %31, i32 %28, i32 %32
br label %34
34: ; preds = %22, %19
%35 = phi i32 [ 0, %19 ], [ %33, %22 ]
ret i32 %35
}