#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 /*<<< orphan*/ events;
};

/* Variables and functions */
int /*<<< orphan*/ POLLIN;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ pipe(int *);
int poll(struct pollfd *, int, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ write(int, void const *, int);
int main(void) {
  int pipe_desc[2];
  pipe(pipe_desc);
  close(pipe_desc[1]);
  write(pipe_desc[1], (const void *)"foo", 3);
  struct pollfd pfd;
  pfd.fd = pipe_desc[0];
  pfd.events = POLLIN;
  int ready = poll(&pfd, 1, 0);
  printf("ready: %i\n", ready);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0xc(%rbp),%rdi
call   1060 <pipe@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
mov    -0x8(%rbp),%edi
lea    0xe86(%rip),%rsi
mov    $0x3,%edx
call   1030 <write@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    0x2e57(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
lea    -0x18(%rbp),%rdi
mov    $0x1,%esi
xor    %edx,%edx
call   1070 <poll@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%esi
lea    0xe51(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
sub    $0x18,%rsp
lea    0x8(%rsp),%rdi
call   1060 <pipe@plt>
mov    0xc(%rsp),%edi
call   1050 <close@plt>
mov    0xc(%rsp),%edi
lea    0xe8e(%rip),%rsi
mov    $0x3,%edx
call   1030 <write@plt>
mov    0x8(%rsp),%eax
mov    %eax,0x10(%rsp)
mov    0x2e5d(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
lea    0x10(%rsp),%rdi
mov    $0x1,%esi
xor    %edx,%edx
call   1070 <poll@plt>
lea    0xe5b(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x18,%rsp
ret

LLVM IR (-O0):
%struct.pollfd = type { i32, i32 }
@.str = [4 x i8] c"foo\00"
@POLLIN = global i32 0
@.str.1 = [11 x i8] c"ready: %i\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [2 x i32]
%3 = alloca %struct.pollfd
%4 = alloca i32
store i32 0, i32* %1
%5 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%6 = call i32 @pipe(i32* %5)
%7 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%8 = load i32, i32* %7
%9 = call i32 @close(i32 %8)
%10 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%11 = load i32, i32* %10
%12 = call i32 @write(i32 %11, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 3)
%13 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%14 = load i32, i32* %13
%15 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %3, i32 0, i32 0
store i32 %14, i32* %15
%16 = load i32, i32* @POLLIN
%17 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %3, i32 0, i32 1
store i32 %16, i32* %17
%18 = call i32 @poll(%struct.pollfd* %3, i32 1, i32 0)
store i32 %18, i32* %4
%19 = load i32, i32* %4
%20 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %19)
ret i32 0
}

LLVM IR (-O2):
%struct.pollfd = type { i32, i32 }
@.str = [4 x i8] c"foo\00"
@POLLIN = global i32 0
@.str.1 = [11 x i8] c"ready: %i\0A\00"
define i32 @main() {
%1 = alloca [2 x i32]
%2 = alloca %struct.pollfd
%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 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%7 = load i32, i32* %6
%8 = call i32 @close(i32 %7)
%9 = load i32, i32* %6
%10 = call i32 @write(i32 %9, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 3)
%11 = bitcast %struct.pollfd* %2 to i8*
%12 = load i32, i32* %4
%13 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %2, i64 0, i32 0
store i32 %12, i32* %13
%14 = load i32, i32* @POLLIN
%15 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %2, i64 0, i32 1
store i32 %14, i32* %15
%16 = call i32 @poll(%struct.pollfd* %2, i32 1, i32 0)
%17 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %16)
ret i32 0
}