#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;
};
typedef int /*<<< orphan*/ string;
typedef int ssize_t;

/* Variables and functions */
scalar_t__ EAGAIN;
scalar_t__ EINTR;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ POLLIN;
scalar_t__ errno;
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int pipe(int *);
int poll(struct pollfd *, int, int);
int /*<<< orphan*/ printf(char *, int);
int write(int, char *, int);
int main(int argc, char **argv) {
  int ret;
  char string[] = "Hello, world!\n";
  int pipefd[2];
  ret = pipe(pipefd);
  if (ret < 0) {
    perror("pipe creation");
    return 1;
  }
  ssize_t written = 0;
  while (written < sizeof(string)) {
    ssize_t n;
    if ((n = write(pipefd[1], string + written, sizeof(string) - written)) <
        0) {
      if (errno == EINTR || errno == EAGAIN)
        continue;
      perror("pipe write");
      return 1;
    }
    written += n;
  }
  int filefd = open(argv[0], O_RDONLY);
  if (filefd < 0) {
    perror("file open");
    return 1;
  }
  int devfd = open("/dev/urandom", O_RDONLY);
  if (devfd < 0) {
    perror("dev/urandom open");
    return 1;
  }
  struct pollfd infds[] = {
      {.fd = pipefd[0], .events = POLLIN},
      {.fd = filefd, .events = POLLIN},
      {.fd = devfd, .events = POLLIN},
  };
  ret = poll(infds, 3, -1);
  if (ret <= 0) {
    perror("poll with POLLIN");
    return 1;
  }
  printf("poll(POLLIN) returned %d file descriptors\n", ret);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0xe83(%rip),%rax
mov    %rax,-0x23(%rbp)
mov    0xe81(%rip),%eax
mov    %eax,-0x1b(%rbp)
mov    0xe7b(%rip),%ax
mov    %ax,-0x17(%rbp)
mov    0xe73(%rip),%al
mov    %al,-0x15(%rbp)
lea    -0x2c(%rbp),%rdi
call   1050 <pipe@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11cc <main+0x6c>
lea    0xe54(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1365 <main+0x205>
movl   $0x0,-0x30(%rbp)
movslq -0x30(%rbp),%rax
cmp    $0xf,%rax
jae    126d <main+0x10d>
mov    -0x28(%rbp),%edi
lea    -0x23(%rbp),%rsi
movslq -0x30(%rbp),%rax
add    %rax,%rsi
movslq -0x30(%rbp),%rcx
mov    $0xf,%eax
sub    %rcx,%rax
mov    %eax,%edx
call   1030 <write@plt>
mov    %eax,-0x34(%rbp)
cmp    $0x0,%eax
jge    125f <main+0xff>
mov    0x2dbb(%rip),%rax
mov    (%rax),%rax
mov    0x2da9(%rip),%rcx
cmp    (%rcx),%rax
je     1242 <main+0xe2>
mov    0x2da1(%rip),%rax
mov    (%rax),%rax
mov    0x2d7f(%rip),%rcx
cmp    (%rcx),%rax
jne    1247 <main+0xe7>
jmp    11d3 <main+0x73>
lea    0xdcf(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1365 <main+0x205>
mov    -0x34(%rbp),%eax
add    -0x30(%rbp),%eax
mov    %eax,-0x30(%rbp)
jmp    11d3 <main+0x73>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    0x2d6d(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0x0,-0x38(%rbp)
jge    12a7 <main+0x147>
lea    0xd92(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1365 <main+0x205>
mov    0x2d3a(%rip),%rax
mov    (%rax),%esi
lea    0xd7b(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jge    12e1 <main+0x181>
lea    0xd6f(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1365 <main+0x205>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x60(%rbp)
mov    0x2cf2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x5c(%rbp)
mov    -0x38(%rbp),%eax
mov    %eax,-0x58(%rbp)
mov    0x2ce0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x54(%rbp)
mov    -0x3c(%rbp),%eax
mov    %eax,-0x50(%rbp)
mov    0x2cce(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
lea    -0x60(%rbp),%rdi
mov    $0x3,%esi
mov    $0xffffffff,%edx
call   1060 <poll@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jg     134f <main+0x1ef>
lea    0xd12(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1365 <main+0x205>
mov    -0x14(%rbp),%esi
lea    0xd08(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x40,%rsp
mov    %rsi,%r14
movabs $0xa21646c726f77,%rax
mov    %rax,0x37(%rsp)
movabs $0x77202c6f6c6c6548,%rax
2c 20 77
mov    %rax,0x30(%rsp)
lea    0x8(%rsp),%rdi
call   1050 <pipe@plt>
test   %eax,%eax
js     1279 <main+0x119>
xor    %ebp,%ebp
mov    0x2e28(%rip),%r15
mov    0x2e19(%rip),%rbx
mov    0x2e02(%rip),%r12
jmp    11c7 <main+0x67>
nopl   0x0(%rax,%rax,1)
add    %eax,%ebp
cmp    $0xf,%ebp
jae    11ff <main+0x9f>
mov    %ebp,%eax
mov    0xc(%rsp),%edi
lea    (%rsp,%rax,1),%rsi
add    $0x30,%rsi
mov    $0xf,%edx
sub    %ebp,%edx
call   1030 <write@plt>
test   %eax,%eax
jns    11c0 <main+0x60>
mov    (%r15),%rax
cmp    (%rbx),%rax
je     11c2 <main+0x62>
cmp    (%r12),%rax
je     11c2 <main+0x62>
lea    0xe23(%rip),%rdi
jmp    129b <main+0x13b>
mov    (%r14),%rdi
mov    0x2ddf(%rip),%rbp
mov    0x0(%rbp),%esi
call   1070 <open@plt>
test   %eax,%eax
js     1282 <main+0x122>
mov    %eax,%ebx
mov    0x0(%rbp),%esi
lea    0xe11(%rip),%rdi
call   1070 <open@plt>
test   %eax,%eax
js     128b <main+0x12b>
mov    0x8(%rsp),%ecx
mov    %ecx,0x10(%rsp)
mov    0x2da7(%rip),%rcx
mov    (%rcx),%ecx
mov    %ecx,0x14(%rsp)
mov    %ebx,0x18(%rsp)
mov    %ecx,0x1c(%rsp)
mov    %eax,0x20(%rsp)
mov    %ecx,0x24(%rsp)
lea    0x10(%rsp),%rdi
mov    $0x3,%esi
mov    $0xffffffff,%edx
call   1060 <poll@plt>
test   %eax,%eax
jle    1294 <main+0x134>
lea    0xdf3(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
xor    %eax,%eax
jmp    12a5 <main+0x145>
lea    0xd8f(%rip),%rdi
jmp    129b <main+0x13b>
lea    0xd9f(%rip),%rdi
jmp    129b <main+0x13b>
lea    0xdad(%rip),%rdi
jmp    129b <main+0x13b>
lea    0xdb5(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%eax
add    $0x40,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.pollfd = type { i32, i32 }
@__const.main.string = [15 x i8] c"Hello, world!\0A\00"
@.str = [14 x i8] c"pipe creation\00"
@errno = global i64 0
@EINTR = global i64 0
@EAGAIN = global i64 0
@.str.1 = [11 x i8] c"pipe write\00"
@O_RDONLY = global i32 0
@.str.2 = [10 x i8] c"file open\00"
@.str.3 = [13 x i8] c"/dev/urandom\00"
@.str.4 = [17 x i8] c"dev/urandom open\00"
@POLLIN = global i32 0
@.str.5 = [17 x i8] c"poll with POLLIN\00"
@.str.6 = [43 x i8] c"poll(POLLIN) returned %d file descriptors\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca [15 x i8]
%8 = alloca [2 x i32]
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca [3 x %struct.pollfd]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%14 = bitcast [15 x i8]* %7 to i8*
%15 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 0
%16 = call i32 @pipe(i32* %15)
store i32 %16, i32* %6
%17 = load i32, i32* %6
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %2
%20 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %97
21: ; preds = %2
store i32 0, i32* %9
br label %22
22: ; preds = %50, %47, %21
%23 = load i32, i32* %9
%24 = sext i32 %23 to i64
%25 = icmp ult i64 %24, 15
br i1 %25, label %26, label %54
26: ; preds = %22
%27 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 1
%28 = load i32, i32* %27
%29 = getelementptr inbounds [15 x i8], [15 x i8]* %7, i64 0, i64 0
%30 = load i32, i32* %9
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %29, i64 %31
%33 = load i32, i32* %9
%34 = sext i32 %33 to i64
%35 = sub i64 15, %34
%36 = trunc i64 %35 to i32
%37 = call i32 @write(i32 %28, i8* %32, i32 %36)
store i32 %37, i32* %10
%38 = icmp slt i32 %37, 0
br i1 %38, label %39, label %50
39: ; preds = %26
%40 = load i64, i64* @errno
%41 = load i64, i64* @EINTR
%42 = icmp eq i64 %40, %41
br i1 %42, label %47, label %43
43: ; preds = %39
%44 = load i64, i64* @errno
%45 = load i64, i64* @EAGAIN
%46 = icmp eq i64 %44, %45
br i1 %46, label %47, label %48
47: ; preds = %43, %39
br label %22
48: ; preds = %43
%49 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %97
50: ; preds = %26
%51 = load i32, i32* %10
%52 = load i32, i32* %9
%53 = add nsw i32 %52, %51
store i32 %53, i32* %9
br label %22
54: ; preds = %22
%55 = load i8**, i8*** %5
%56 = getelementptr inbounds i8*, i8** %55, i64 0
%57 = load i8*, i8** %56
%58 = load i32, i32* @O_RDONLY
%59 = call i32 @open(i8* %57, i32 %58)
store i32 %59, i32* %11
%60 = load i32, i32* %11
%61 = icmp slt i32 %60, 0
br i1 %61, label %62, label %64
62: ; preds = %54
%63 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
br label %97
64: ; preds = %54
%65 = load i32, i32* @O_RDONLY
%66 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i32 %65)
store i32 %66, i32* %12
%67 = load i32, i32* %12
%68 = icmp slt i32 %67, 0
br i1 %68, label %69, label %71
69: ; preds = %64
%70 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %3
br label %97
71: ; preds = %64
%72 = getelementptr inbounds [3 x %struct.pollfd], [3 x %struct.pollfd]* %13, i64 0, i64 0
%73 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %72, i32 0, i32 0
%74 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 0
%75 = load i32, i32* %74
store i32 %75, i32* %73
%76 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %72, i32 0, i32 1
%77 = load i32, i32* @POLLIN
store i32 %77, i32* %76
%78 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %72, i64 1
%79 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %78, i32 0, i32 0
%80 = load i32, i32* %11
store i32 %80, i32* %79
%81 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %78, i32 0, i32 1
%82 = load i32, i32* @POLLIN
store i32 %82, i32* %81
%83 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %78, i64 1
%84 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %83, i32 0, i32 0
%85 = load i32, i32* %12
store i32 %85, i32* %84
%86 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %83, i32 0, i32 1
%87 = load i32, i32* @POLLIN
store i32 %87, i32* %86
%88 = getelementptr inbounds [3 x %struct.pollfd], [3 x %struct.pollfd]* %13, i64 0, i64 0
%89 = call i32 @poll(%struct.pollfd* %88, i32 3, i32 -1)
store i32 %89, i32* %6
%90 = load i32, i32* %6
%91 = icmp sle i32 %90, 0
br i1 %91, label %92, label %94
92: ; preds = %71
%93 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0))
store i32 1, i32* %3
br label %97
94: ; preds = %71
%95 = load i32, i32* %6
%96 = call i32 @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.6, i64 0, i64 0), i32 %95)
store i32 0, i32* %3
br label %97
97: ; preds = %94, %92, %69, %62, %48, %19
%98 = load i32, i32* %3
ret i32 %98
}

LLVM IR (-O2):
%struct.pollfd = type { i32, i32 }
@__const.main.string = [15 x i8] c"Hello, world!\0A\00"
@.str = [14 x i8] c"pipe creation\00"
@errno = global i64 0
@EINTR = global i64 0
@EAGAIN = global i64 0
@.str.1 = [11 x i8] c"pipe write\00"
@O_RDONLY = global i32 0
@.str.2 = [10 x i8] c"file open\00"
@.str.3 = [13 x i8] c"/dev/urandom\00"
@.str.4 = [17 x i8] c"dev/urandom open\00"
@POLLIN = global i32 0
@.str.5 = [17 x i8] c"poll with POLLIN\00"
@.str.6 = [43 x i8] c"poll(POLLIN) returned %d file descriptors\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [15 x i8]
%4 = alloca [2 x i32]
%5 = alloca [3 x %struct.pollfd]
%6 = getelementptr inbounds [15 x i8], [15 x i8]* %3, i64 0, i64 0
%7 = bitcast [2 x i32]* %4 to i8*
%8 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%9 = call i32 @pipe(i32* %8)
%10 = icmp slt i32 %9, 0
br i1 %10, label %13, label %11
11: ; preds = %2
%12 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
br label %15
13: ; preds = %2
%14 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %69
15: ; preds = %11, %34
%16 = phi i32 [ 0, %11 ], [ %35, %34 ]
%17 = zext i32 %16 to i64
%18 = load i32, i32* %12
%19 = getelementptr inbounds [15 x i8], [15 x i8]* %3, i64 0, i64 %17
%20 = sub nuw nsw i32 15, %16
%21 = call i32 @write(i32 %18, i8* %19, i32 %20)
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %32
23: ; preds = %15
%24 = load i64, i64* @errno
%25 = load i64, i64* @EINTR
%26 = icmp eq i64 %24, %25
%27 = load i64, i64* @EAGAIN
%28 = icmp eq i64 %24, %27
%29 = select i1 %26, i1 true, i1 %28
br i1 %29, label %34, label %30
30: ; preds = %23
%31 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %69
32: ; preds = %15
%33 = add nuw nsw i32 %21, %16
br label %34
34: ; preds = %23, %32
%35 = phi i32 [ %33, %32 ], [ %16, %23 ]
%36 = icmp ult i32 %35, 15
br i1 %36, label %15, label %37
37: ; preds = %34
%38 = load i8*, i8** %1
%39 = load i32, i32* @O_RDONLY
%40 = call i32 @open(i8* %38, i32 %39)
%41 = icmp slt i32 %40, 0
br i1 %41, label %42, label %44
42: ; preds = %37
%43 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
br label %69
44: ; preds = %37
%45 = load i32, i32* @O_RDONLY
%46 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i32 %45)
%47 = icmp slt i32 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %44
%49 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0))
br label %69
50: ; preds = %44
%51 = bitcast [3 x %struct.pollfd]* %5 to i8*
%52 = getelementptr inbounds [3 x %struct.pollfd], [3 x %struct.pollfd]* %5, i64 0, i64 0
%53 = getelementptr inbounds [3 x %struct.pollfd], [3 x %struct.pollfd]* %5, i64 0, i64 0, i32 0
%54 = load i32, i32* %8
store i32 %54, i32* %53
%55 = getelementptr inbounds [3 x %struct.pollfd], [3 x %struct.pollfd]* %5, i64 0, i64 0, i32 1
%56 = load i32, i32* @POLLIN
store i32 %56, i32* %55
%57 = getelementptr inbounds [3 x %struct.pollfd], [3 x %struct.pollfd]* %5, i64 0, i64 1, i32 0
store i32 %40, i32* %57
%58 = getelementptr inbounds [3 x %struct.pollfd], [3 x %struct.pollfd]* %5, i64 0, i64 1, i32 1
store i32 %56, i32* %58
%59 = getelementptr inbounds [3 x %struct.pollfd], [3 x %struct.pollfd]* %5, i64 0, i64 2, i32 0
store i32 %46, i32* %59
%60 = getelementptr inbounds [3 x %struct.pollfd], [3 x %struct.pollfd]* %5, i64 0, i64 2, i32 1
store i32 %56, i32* %60
%61 = call i32 @poll(%struct.pollfd* %52, i32 3, i32 -1)
%62 = icmp slt i32 %61, 1
br i1 %62, label %63, label %65
63: ; preds = %50
%64 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0))
br label %67
65: ; preds = %50
%66 = call i32 @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.6, i64 0, i64 0), i32 %61)
br label %67
67: ; preds = %65, %63
%68 = phi i32 [ 1, %63 ], [ 0, %65 ]
br label %69
69: ; preds = %30, %48, %67, %42, %13
%70 = phi i32 [ 1, %13 ], [ 1, %42 ], [ 1, %48 ], [ %68, %67 ], [ 1, %30 ]
ret i32 %70
}