#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 /*<<< orphan*/ O_RDWR;
int POLLIN;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int poll(struct pollfd *, int, int);
int /*<<< orphan*/ printf(char *, char *, ...);
int read(int, char **, int /*<<< orphan*/);
int main(int argc, char **argv) {
  int fd;
  int rc = 0;
  char *rd_buf[16];
  int ret;
  struct pollfd fds;
  printf("%s: entered\n", argv[0]);
  fd = open("/dev/cdata", O_RDWR);
  if (fd == -1) {
    perror("open failed");
    rc = fd;
    exit(-1);
  }
  printf("%s: open: successful\n", argv[0]);
  fds.fd = fd;
  fds.events = POLLIN;
  while (1) {
    ret = poll(&fds, sizeof(fds), 2000);
    if (ret == -1) {
      perror("poll failed\n");
      return -1;
    }
    if (fds.revents & POLLIN) {
      rc = read(fd, rd_buf, 0);
      if (rc == -1) {
        perror("read failed");
        close(fd);
        exit(-1);
      }
      printf("%s: read: returning %d bytes!\n", argv[0], rc);
      break;
    }
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe62(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2e3c(%rip),%rax
mov    (%rax),%esi
lea    0xe58(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11e3 <main+0x73>
lea    0xe4a(%rip),%rdi
call   1080 <perror@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    $0xffffffff,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe33(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0xb0(%rbp)
mov    0x2dd8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xac(%rbp)
lea    -0xb0(%rbp),%rdi
mov    $0xc,%esi
mov    $0x7d0,%edx
call   1060 <poll@plt>
mov    %eax,-0xa4(%rbp)
cmpl   $0xffffffff,-0xa4(%rbp)
jne    1251 <main+0xe1>
lea    0xdfa(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d6 <main+0x166>
mov    -0xa8(%rbp),%eax
mov    0x2d82(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     12c2 <main+0x152>
mov    -0x14(%rbp),%edi
lea    -0xa0(%rbp),%rsi
xor    %edx,%edx
call   1050 <read@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    12a5 <main+0x135>
lea    0xdb9(%rip),%rdi
call   1080 <perror@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
mov    -0x18(%rbp),%edx
lea    0xd9d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    12c7 <main+0x157>
jmp    1210 <main+0xa0>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x90,%rsp
mov    %rsi,%r15
mov    (%rsi),%rsi
lea    0xe74(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
mov    0x2e4e(%rip),%rax
mov    (%rax),%esi
lea    0xe6a(%rip),%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     1259 <main+0xe9>
mov    %eax,%r12d
mov    (%r15),%rsi
lea    0xe66(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
mov    %r12d,(%rsp)
mov    0x2e10(%rip),%rbp
mov    0x0(%rbp),%eax
mov    %eax,0x4(%rsp)
mov    %rsp,%rbx
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    $0xc,%esi
mov    $0x7d0,%edx
call   1060 <poll@plt>
cmp    $0xffffffff,%eax
je     1245 <main+0xd5>
mov    0x0(%rbp),%eax
test   %eax,0x8(%rsp)
je     11e0 <main+0x70>
lea    0x10(%rsp),%rsi
xor    %r14d,%r14d
mov    %r12d,%edi
xor    %edx,%edx
call   1050 <read@plt>
cmp    $0xffffffff,%eax
je     126f <main+0xff>
mov    (%r15),%rsi
lea    0xe32(%rip),%rdi
mov    %eax,%edx
xor    %eax,%eax
call   1030 <printf@plt>
mov    %r12d,%edi
call   1040 <close@plt>
mov    %r14d,%eax
add    $0x90,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdee(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%r14d
jmp    1232 <main+0xc2>
lea    0xdb8(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
lea    0xdd1(%rip),%rdi
call   1080 <perror@plt>
mov    %r12d,%edi
call   1040 <close@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.pollfd = type { i32, i32, i32 }
@.str = [13 x i8] c"%s: entered\0A\00"
@.str.1 = [11 x i8] c"/dev/cdata\00"
@O_RDWR = global i32 0
@.str.2 = [12 x i8] c"open failed\00"
@.str.3 = [22 x i8] c"%s: open: successful\0A\00"
@POLLIN = global i32 0
@.str.4 = [13 x i8] c"poll failed\0A\00"
@.str.5 = [12 x i8] c"read failed\00"
@.str.6 = [31 x i8] c"%s: read: returning %d bytes!\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca [16 x i8*]
%9 = alloca i32
%10 = alloca %struct.pollfd
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %7
%11 = load i8**, i8*** %5
%12 = getelementptr inbounds i8*, i8** %11, i64 0
%13 = load i8*, i8** %12
%14 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* %13)
%15 = load i32, i32* @O_RDWR
%16 = call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %15)
store i32 %16, i32* %6
%17 = load i32, i32* %6
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %23
19: ; preds = %2
%20 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%21 = load i32, i32* %6
store i32 %21, i32* %7
%22 = call i32 @exit(i32 -1)
unreachable
23: ; preds = %2
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 0
%26 = load i8*, i8** %25
%27 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i8* %26)
%28 = load i32, i32* %6
%29 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %10, i32 0, i32 0
store i32 %28, i32* %29
%30 = load i32, i32* @POLLIN
%31 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %10, i32 0, i32 1
store i32 %30, i32* %31
br label %32
32: ; preds = %23, %61
%33 = call i32 @poll(%struct.pollfd* %10, i32 12, i32 2000)
store i32 %33, i32* %9
%34 = load i32, i32* %9
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %38
36: ; preds = %32
%37 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %3
br label %65
38: ; preds = %32
%39 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %10, i32 0, i32 2
%40 = load i32, i32* %39
%41 = load i32, i32* @POLLIN
%42 = and i32 %40, %41
%43 = icmp ne i32 %42, 0
br i1 %43, label %44, label %61
44: ; preds = %38
%45 = load i32, i32* %6
%46 = getelementptr inbounds [16 x i8*], [16 x i8*]* %8, i64 0, i64 0
%47 = call i32 @read(i32 %45, i8** %46, i32 0)
store i32 %47, i32* %7
%48 = load i32, i32* %7
%49 = icmp eq i32 %48, -1
br i1 %49, label %50, label %55
50: ; preds = %44
%51 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0))
%52 = load i32, i32* %6
%53 = call i32 @close(i32 %52)
%54 = call i32 @exit(i32 -1)
unreachable
55: ; preds = %44
%56 = load i8**, i8*** %5
%57 = getelementptr inbounds i8*, i8** %56, i64 0
%58 = load i8*, i8** %57
%59 = load i32, i32* %7
%60 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.6, i64 0, i64 0), i8* %58, i32 %59)
br label %62
61: ; preds = %38
br label %32
62: ; preds = %55
%63 = load i32, i32* %6
%64 = call i32 @close(i32 %63)
store i32 0, i32* %3
br label %65
65: ; preds = %62, %36
%66 = load i32, i32* %3
ret i32 %66
}

LLVM IR (-O2):
%struct.pollfd = type { i32, i32, i32 }
@.str = [13 x i8] c"%s: entered\0A\00"
@.str.1 = [11 x i8] c"/dev/cdata\00"
@O_RDWR = global i32 0
@.str.2 = [12 x i8] c"open failed\00"
@.str.3 = [22 x i8] c"%s: open: successful\0A\00"
@POLLIN = global i32 0
@.str.4 = [13 x i8] c"poll failed\0A\00"
@.str.5 = [12 x i8] c"read failed\00"
@.str.6 = [31 x i8] c"%s: read: returning %d bytes!\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [16 x i8*]
%4 = alloca %struct.pollfd
%5 = bitcast [16 x i8*]* %3 to i8*
%6 = bitcast %struct.pollfd* %4 to i8*
%7 = load i8*, i8** %1
%8 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* %7)
%9 = load i32, i32* @O_RDWR
%10 = tail call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %9)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%14 = tail call i32 @exit(i32 -1)
unreachable
15: ; preds = %2
%16 = load i8*, i8** %1
%17 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i8* %16)
%18 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %4, i64 0, i32 0
store i32 %10, i32* %18
%19 = load i32, i32* @POLLIN
%20 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %4, i64 0, i32 1
store i32 %19, i32* %20
%21 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %4, i64 0, i32 2
br label %22
22: ; preds = %27, %15
%23 = call i32 @poll(%struct.pollfd* %4, i32 12, i32 2000)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %27
25: ; preds = %22
%26 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
br label %44
27: ; preds = %22
%28 = load i32, i32* %21
%29 = load i32, i32* @POLLIN
%30 = and i32 %29, %28
%31 = icmp eq i32 %30, 0
br i1 %31, label %22, label %32
32: ; preds = %27
%33 = getelementptr inbounds [16 x i8*], [16 x i8*]* %3, i64 0, i64 0
%34 = call i32 @read(i32 %10, i8** %33, i32 0)
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %40
36: ; preds = %32
%37 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0))
%38 = call i32 @close(i32 %10)
%39 = call i32 @exit(i32 -1)
unreachable
40: ; preds = %32
%41 = load i8*, i8** %1
%42 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.6, i64 0, i64 0), i8* %41, i32 %34)
%43 = call i32 @close(i32 %10)
br label %44
44: ; preds = %40, %25
%45 = phi i32 [ -1, %25 ], [ 0, %40 ]
ret i32 %45
}