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

/* Variables and functions */
int O_EXCL;
int O_WRONLY;
int POLLIN;
int POLLOUT;
int STDIN_FILENO;
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int poll(struct pollfd *, int, int);
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, char *argv[]) {
  struct pollfd fds[2];
  int ret, fd, fd_max;
  fd = open(argv[1], O_WRONLY | O_EXCL, 0644);
  if (fd == -1) {
    perror("open");
    return 1;
  }
  fds[0].fd = STDIN_FILENO;
  fds[0].events = POLLIN;
  fds[1].fd = fd;
  fds[1].events = POLLOUT;
  ret = poll(fds, sizeof(fds) / sizeof(struct pollfd), 5 * 1000);
  if (ret == -1) {
    perror("poll");
    return 1;
  }
  if (!ret) {
    printf("%d seconds elapsed!\n", 5);
    return 0;
  }
  if (fds[0].revents & POLLIN)
    printf("stdin is readable!\n");
  if (fds[1].revents & POLLOUT)
    printf("opened fd is writable!\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e53(%rip),%rax
mov    (%rax),%esi
mov    0x2e62(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   1050 <open@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0xffffffff,-0x38(%rbp)
jne    119f <main+0x5f>
lea    0xe72(%rip),%rdi
call   1060 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1277 <main+0x137>
mov    0x2e22(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2e2e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x38(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    0x2e34(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
lea    -0x30(%rbp),%rdi
mov    $0x2,%esi
mov    $0x1388,%edx
call   1040 <poll@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
jne    1201 <main+0xc1>
lea    0xe15(%rip),%rdi
call   1060 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1277 <main+0x137>
cmpl   $0x0,-0x34(%rbp)
jne    122a <main+0xea>
lea    0xdf8(%rip),%rdi
mov    $0x5,%esi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1277 <main+0x137>
mov    -0x28(%rbp),%eax
mov    0x2dac(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     124d <main+0x10d>
lea    0xdd9(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x1c(%rbp),%eax
mov    0x2da1(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1270 <main+0x130>
lea    0xdca(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    0x8(%rsi),%rdi
mov    0x2e56(%rip),%rax
mov    0x2e67(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    $0x1a4,%edx
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     1212 <main+0xc2>
mov    0x2e41(%rip),%rcx
mov    (%rcx),%ecx
mov    %ecx,(%rsp)
mov    0x2e4d(%rip),%rbx
mov    (%rbx),%ecx
mov    %ecx,0x4(%rsp)
mov    %eax,0xc(%rsp)
mov    0x2e54(%rip),%r14
mov    (%r14),%eax
mov    %eax,0x10(%rsp)
mov    %rsp,%rdi
mov    $0x2,%esi
mov    $0x1388,%edx
call   1050 <poll@plt>
test   %eax,%eax
je     11f3 <main+0xa3>
cmp    $0xffffffff,%eax
je     121b <main+0xcb>
mov    (%rbx),%eax
test   %eax,0x8(%rsp)
je     11da <main+0x8a>
lea    0xe61(%rip),%rdi
call   1030 <puts@plt>
mov    (%r14),%eax
xor    %ebx,%ebx
test   %eax,0x14(%rsp)
je     1208 <main+0xb8>
lea    0xe33(%rip),%rdi
call   1030 <puts@plt>
jmp    1208 <main+0xb8>
lea    0xe10(%rip),%rdi
xor    %ebx,%ebx
mov    $0x5,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret
lea    0xde7(%rip),%rdi
jmp    1222 <main+0xd2>
lea    0xde3(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%ebx
jmp    1208 <main+0xb8>

LLVM IR (-O0):
%struct.pollfd = type { i32, i32, i32 }
@O_WRONLY = global i32 0
@O_EXCL = global i32 0
@.str = [5 x i8] c"open\00"
@STDIN_FILENO = global i32 0
@POLLIN = global i32 0
@POLLOUT = global i32 0
@.str.1 = [5 x i8] c"poll\00"
@.str.2 = [21 x i8] c"%d seconds elapsed!\0A\00"
@.str.3 = [20 x i8] c"stdin is readable!\0A\00"
@.str.4 = [24 x i8] c"opened fd is writable!\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [2 x %struct.pollfd]
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i8**, i8*** %5
%11 = getelementptr inbounds i8*, i8** %10, i64 1
%12 = load i8*, i8** %11
%13 = load i32, i32* @O_WRONLY
%14 = load i32, i32* @O_EXCL
%15 = or i32 %13, %14
%16 = call i32 @open(i8* %12, i32 %15, i32 420)
store i32 %16, i32* %8
%17 = load i32, i32* %8
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %21
19: ; preds = %2
%20 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %64
21: ; preds = %2
%22 = load i32, i32* @STDIN_FILENO
%23 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %6, i64 0, i64 0
%24 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %23, i32 0, i32 0
store i32 %22, i32* %24
%25 = load i32, i32* @POLLIN
%26 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %6, i64 0, i64 0
%27 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %26, i32 0, i32 1
store i32 %25, i32* %27
%28 = load i32, i32* %8
%29 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %6, i64 0, i64 1
%30 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %29, i32 0, i32 0
store i32 %28, i32* %30
%31 = load i32, i32* @POLLOUT
%32 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %6, i64 0, i64 1
%33 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %32, i32 0, i32 1
store i32 %31, i32* %33
%34 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %6, i64 0, i64 0
%35 = call i32 @poll(%struct.pollfd* %34, i32 2, i32 5000)
store i32 %35, i32* %7
%36 = load i32, i32* %7
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %40
38: ; preds = %21
%39 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %64
40: ; preds = %21
%41 = load i32, i32* %7
%42 = icmp ne i32 %41, 0
br i1 %42, label %45, label %43
43: ; preds = %40
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i32 5)
store i32 0, i32* %3
br label %64
45: ; preds = %40
%46 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %6, i64 0, i64 0
%47 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %46, i32 0, i32 2
%48 = load i32, i32* %47
%49 = load i32, i32* @POLLIN
%50 = and i32 %48, %49
%51 = icmp ne i32 %50, 0
br i1 %51, label %52, label %54
52: ; preds = %45
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
br label %54
54: ; preds = %52, %45
%55 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %6, i64 0, i64 1
%56 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %55, i32 0, i32 2
%57 = load i32, i32* %56
%58 = load i32, i32* @POLLOUT
%59 = and i32 %57, %58
%60 = icmp ne i32 %59, 0
br i1 %60, label %61, label %63
61: ; preds = %54
%62 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0))
br label %63
63: ; preds = %61, %54
store i32 0, i32* %3
br label %64
64: ; preds = %63, %43, %38, %19
%65 = load i32, i32* %3
ret i32 %65
}

LLVM IR (-O2):
%struct.pollfd = type { i32, i32, i32 }
@O_WRONLY = global i32 0
@O_EXCL = global i32 0
@.str = [5 x i8] c"open\00"
@STDIN_FILENO = global i32 0
@POLLIN = global i32 0
@POLLOUT = global i32 0
@.str.1 = [5 x i8] c"poll\00"
@.str.2 = [21 x i8] c"%d seconds elapsed!\0A\00"
@str = [23 x i8] c"opened fd is writable!\00"
@str.5 = [19 x i8] c"stdin is readable!\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [2 x %struct.pollfd]
%4 = bitcast [2 x %struct.pollfd]* %3 to i8*
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
%7 = load i32, i32* @O_WRONLY
%8 = load i32, i32* @O_EXCL
%9 = or i32 %8, %7
%10 = tail call i32 @open(i8* %6, i32 %9, i32 420)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %44
14: ; preds = %2
%15 = load i32, i32* @STDIN_FILENO
%16 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %3, i64 0, i64 0
%17 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %3, i64 0, i64 0, i32 0
store i32 %15, i32* %17
%18 = load i32, i32* @POLLIN
%19 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %3, i64 0, i64 0, i32 1
store i32 %18, i32* %19
%20 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %3, i64 0, i64 1, i32 0
store i32 %10, i32* %20
%21 = load i32, i32* @POLLOUT
%22 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %3, i64 0, i64 1, i32 1
store i32 %21, i32* %22
%23 = call i32 @poll(%struct.pollfd* %16, i32 2, i32 5000)
switch i32 %23, label %28 [
i32 -1, label %24
i32 0, label %26
]
24: ; preds = %14
%25 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %44
26: ; preds = %14
%27 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i32 5)
br label %44
28: ; preds = %14
%29 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %3, i64 0, i64 0, i32 2
%30 = load i32, i32* %29
%31 = load i32, i32* @POLLIN
%32 = and i32 %31, %30
%33 = icmp eq i32 %32, 0
br i1 %33, label %36, label %34
34: ; preds = %28
%35 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.5, i64 0, i64 0))
br label %36
36: ; preds = %34, %28
%37 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %3, i64 0, i64 1, i32 2
%38 = load i32, i32* %37
%39 = load i32, i32* @POLLOUT
%40 = and i32 %39, %38
%41 = icmp eq i32 %40, 0
br i1 %41, label %44, label %42
42: ; preds = %36
%43 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str, i64 0, i64 0))
br label %44
44: ; preds = %36, %42, %26, %24, %12
%45 = phi i32 [ 1, %12 ], [ 1, %24 ], [ 0, %26 ], [ 0, %42 ], [ 0, %36 ]
ret i32 %45
}