#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 */

/* Variables and functions */
int /*<<< orphan*/ POLLIN;
int /*<<< orphan*/ POLLOUT;
int /*<<< orphan*/ check_poll(int, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ close(int);
int /*<<< orphan*/ err(int, char *);
int pipe(int *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ write_byte(int, int /*<<< orphan*/);
void test_pipe(void) {
  printf("testing poll() on pipe...\n");
  int ret;
  int fds[2];
  ret = pipe(fds);
  if (ret < 0)
    err(1, "pipe");
  check_poll(fds[1], POLLOUT, POLLOUT);
  check_poll(fds[0], POLLIN, 0);
  write_byte(fds[1], 0);
  check_poll(fds[0], POLLIN, POLLIN);
  if (close(fds[0]) < 0 || close(fds[1]) < 0)
    err(1, "close");
}


x86 assembly (clang, -O0):
<test_pipe>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
lea    0xe91(%rip),%rdi
call   1030 <printf@plt>
lea    -0xc(%rbp),%rdi
call   1050 <pipe@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jge    119b <test_pipe+0x3b>
mov    $0x1,%edi
lea    0xe85(%rip),%rsi
call   1070 <err@plt>
mov    -0x8(%rbp),%edi
mov    0x2e53(%rip),%rax
mov    (%rax),%esi
mov    0x2e4a(%rip),%rax
mov    (%rax),%edx
call   1060 <check_poll@plt>
mov    -0xc(%rbp),%edi
mov    0x2e21(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <check_poll@plt>
mov    -0x8(%rbp),%edi
xor    %esi,%esi
call   1080 <write_byte@plt>
mov    -0xc(%rbp),%edi
mov    0x2e04(%rip),%rax
mov    (%rax),%esi
mov    0x2dfb(%rip),%rax
mov    (%rax),%edx
call   1060 <check_poll@plt>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
cmp    $0x0,%rax
jl     1210 <test_pipe+0xb0>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
cmp    $0x0,%rax
jge    1221 <test_pipe+0xc1>
mov    $0x1,%edi
lea    0xe04(%rip),%rsi
call   1070 <err@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_pipe>:
push   %rbx
sub    $0x10,%rsp
lea    0xe9f(%rip),%rdi
call   1030 <puts@plt>
lea    0x8(%rsp),%rdi
call   1050 <pipe@plt>
test   %eax,%eax
jns    1190 <test_pipe+0x30>
lea    0xe7a(%rip),%rsi
mov    $0x1,%edi
call   1070 <err@plt>
mov    0xc(%rsp),%edi
mov    0x2e5d(%rip),%rax
mov    (%rax),%edx
mov    %edx,%esi
call   1060 <check_poll@plt>
mov    0x8(%rsp),%edi
mov    0x2e31(%rip),%rbx
mov    (%rbx),%esi
xor    %edx,%edx
call   1060 <check_poll@plt>
mov    0xc(%rsp),%edi
xor    %esi,%esi
call   1080 <write_byte@plt>
mov    0x8(%rsp),%edi
mov    (%rbx),%edx
mov    %edx,%esi
call   1060 <check_poll@plt>
mov    0x8(%rsp),%edi
call   1040 <close@plt>
test   %rax,%rax
js     11ec <test_pipe+0x8c>
mov    0xc(%rsp),%edi
call   1040 <close@plt>
test   %rax,%rax
jns    11fd <test_pipe+0x9d>
lea    0xe12(%rip),%rsi
mov    $0x1,%edi
call   1070 <err@plt>
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [27 x i8] c"testing poll() on pipe...\0A\00"
@.str.1 = [5 x i8] c"pipe\00"
@POLLOUT = global i32 0
@POLLIN = global i32 0
@.str.2 = [6 x i8] c"close\00"
define void @test_pipe() {
%1 = alloca i32
%2 = alloca [2 x i32]
%3 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
%4 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%5 = call i32 @pipe(i32* %4)
store i32 %5, i32* %1
%6 = load i32, i32* %1
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = call i32 @err(i32 1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %10
10: ; preds = %8, %0
%11 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%12 = load i32, i32* %11
%13 = load i32, i32* @POLLOUT
%14 = load i32, i32* @POLLOUT
%15 = call i32 @check_poll(i32 %12, i32 %13, i32 %14)
%16 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%17 = load i32, i32* %16
%18 = load i32, i32* @POLLIN
%19 = call i32 @check_poll(i32 %17, i32 %18, i32 0)
%20 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%21 = load i32, i32* %20
%22 = call i32 @write_byte(i32 %21, i32 0)
%23 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%24 = load i32, i32* %23
%25 = load i32, i32* @POLLIN
%26 = load i32, i32* @POLLIN
%27 = call i32 @check_poll(i32 %24, i32 %25, i32 %26)
%28 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%29 = load i32, i32* %28
%30 = call i64 @close(i32 %29)
%31 = icmp slt i64 %30, 0
br i1 %31, label %37, label %32
32: ; preds = %10
%33 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%34 = load i32, i32* %33
%35 = call i64 @close(i32 %34)
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %32, %10
%38 = call i32 @err(i32 1, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %39
39: ; preds = %37, %32
ret void
}

LLVM IR (-O2):
@.str.1 = [5 x i8] c"pipe\00"
@POLLOUT = global i32 0
@POLLIN = global i32 0
@.str.2 = [6 x i8] c"close\00"
@str = [26 x i8] c"testing poll() on pipe...\00"
define void @test_pipe() {
%1 = alloca [2 x i32]
%2 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str, i64 0, i64 0))
%3 = bitcast [2 x i32]* %1 to i8*
%4 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%5 = call i32 @pipe(i32* %4)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = call i32 @err(i32 1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %9
9: ; preds = %7, %0
%10 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%11 = load i32, i32* %10
%12 = load i32, i32* @POLLOUT
%13 = call i32 @check_poll(i32 %11, i32 %12, i32 %12)
%14 = load i32, i32* %4
%15 = load i32, i32* @POLLIN
%16 = call i32 @check_poll(i32 %14, i32 %15, i32 0)
%17 = load i32, i32* %10
%18 = call i32 @write_byte(i32 %17, i32 0)
%19 = load i32, i32* %4
%20 = load i32, i32* @POLLIN
%21 = call i32 @check_poll(i32 %19, i32 %20, i32 %20)
%22 = load i32, i32* %4
%23 = call i64 @close(i32 %22)
%24 = icmp slt i64 %23, 0
br i1 %24, label %29, label %25
25: ; preds = %9
%26 = load i32, i32* %10
%27 = call i64 @close(i32 %26)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %25, %9
%30 = call i32 @err(i32 1, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %31
31: ; preds = %29, %25
ret void
}