#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 revents;
  void *events;
};
typedef int /*<<< orphan*/ pollfds;

/* Variables and functions */
int POLLERR;
int POLLHUP;
void *POLLIN;
int STDIN_FILENO;
scalar_t__ handle_event(struct pollfd *, int);
scalar_t__ poll(struct pollfd *, int, int);
int loop(int sockfd) {
  struct pollfd pollfds[2];
  pollfds[0].fd = STDIN_FILENO;
  pollfds[0].events = POLLIN;
  pollfds[1].fd = sockfd;
  pollfds[1].events = POLLIN;
  while (poll(pollfds, sizeof(pollfds) / sizeof(pollfds[0]), -1) > 0) {
    if ((pollfds[0].revents & (POLLERR | POLLHUP)) ||
        (pollfds[1].revents & (POLLERR | POLLHUP)))
      return 0;
    if (handle_event(&pollfds[0], sockfd) < 0)
      return -1;
    if (handle_event(&pollfds[1], sockfd) < 0)
      return -1;
  }
  return -1;
}


x86 assembly (clang, -O0):
<loop>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    0x2e96(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2e9a(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    0x2e86(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x18(%rbp)
lea    -0x30(%rbp),%rdi
mov    $0x2,%esi
mov    $0xffffffff,%edx
call   1030 <poll@plt>
cmp    $0x0,%rax
jle    120f <loop+0xef>
mov    -0x2c(%rbp),%eax
mov    0x2e60(%rip),%rcx
mov    (%rcx),%ecx
mov    0x2e5f(%rip),%rdx
or     (%rdx),%ecx
and    %ecx,%eax
cmp    $0x0,%eax
jne    11b6 <loop+0x96>
mov    -0x1c(%rbp),%eax
mov    0x2e40(%rip),%rcx
mov    (%rcx),%ecx
mov    0x2e3f(%rip),%rdx
or     (%rdx),%ecx
and    %ecx,%eax
cmp    $0x0,%eax
je     11c2 <loop+0xa2>
movl   $0x0,-0x4(%rbp)
jmp    1216 <loop+0xf6>
lea    -0x30(%rbp),%rdi
mov    -0x8(%rbp),%esi
call   1040 <handle_event@plt>
cmp    $0x0,%rax
jge    11e4 <loop+0xc4>
movl   $0xffffffff,-0x4(%rbp)
jmp    1216 <loop+0xf6>
lea    -0x30(%rbp),%rdi
add    $0x10,%rdi
mov    -0x8(%rbp),%esi
call   1040 <handle_event@plt>
cmp    $0x0,%rax
jge    120a <loop+0xea>
movl   $0xffffffff,-0x4(%rbp)
jmp    1216 <loop+0xf6>
jmp    1159 <loop+0x39>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<loop>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %edi,%ebx
mov    0x2e92(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%rsp)
mov    0x2e96(%rip),%rax
mov    (%rax),%rax
mov    %rax,0x8(%rsp)
lea    0x10(%rsp),%r14
mov    %edi,0x10(%rsp)
mov    %rax,0x18(%rsp)
mov    %rsp,%r15
mov    0x2e7e(%rip),%r12
mov    0x2e7f(%rip),%r13
nopl   0x0(%rax)
mov    %r15,%rdi
mov    $0x2,%esi
mov    $0xffffffff,%edx
call   1030 <poll@plt>
test   %rax,%rax
jle    11c5 <loop+0xa5>
mov    0x0(%r13),%ecx
or     (%r12),%ecx
xor    %eax,%eax
test   %ecx,0x4(%rsp)
jne    11ca <loop+0xaa>
test   %ecx,0x14(%rsp)
jne    11ca <loop+0xaa>
mov    %r15,%rdi
mov    %ebx,%esi
call   1040 <handle_event@plt>
test   %rax,%rax
js     11c5 <loop+0xa5>
mov    %r14,%rdi
mov    %ebx,%esi
call   1040 <handle_event@plt>
mov    %rax,%rcx
mov    $0xffffffff,%eax
test   %rcx,%rcx
jns    1170 <loop+0x50>
jmp    11ca <loop+0xaa>
mov    $0xffffffff,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.pollfd = type { i32, i32, i8* }
@STDIN_FILENO = global i32 0
@POLLIN = global i8* null
@POLLERR = global i32 0
@POLLHUP = global i32 0
define i32 @loop(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [2 x %struct.pollfd]
store i32 %0, i32* %3
%5 = load i32, i32* @STDIN_FILENO
%6 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %4, i64 0, i64 0
%7 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %6, i32 0, i32 0
store i32 %5, i32* %7
%8 = load i8*, i8** @POLLIN
%9 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %4, i64 0, i64 0
%10 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %9, i32 0, i32 2
store i8* %8, i8** %10
%11 = load i32, i32* %3
%12 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %4, i64 0, i64 1
%13 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %12, i32 0, i32 0
store i32 %11, i32* %13
%14 = load i8*, i8** @POLLIN
%15 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %4, i64 0, i64 1
%16 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %15, i32 0, i32 2
store i8* %14, i8** %16
br label %17
17: ; preds = %52, %1
%18 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %4, i64 0, i64 0
%19 = call i64 @poll(%struct.pollfd* %18, i32 2, i32 -1)
%20 = icmp sgt i64 %19, 0
br i1 %20, label %21, label %53
21: ; preds = %17
%22 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %4, i64 0, i64 0
%23 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %22, i32 0, i32 1
%24 = load i32, i32* %23
%25 = load i32, i32* @POLLERR
%26 = load i32, i32* @POLLHUP
%27 = or i32 %25, %26
%28 = and i32 %24, %27
%29 = icmp ne i32 %28, 0
br i1 %29, label %39, label %30
30: ; preds = %21
%31 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %4, i64 0, i64 1
%32 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %31, i32 0, i32 1
%33 = load i32, i32* %32
%34 = load i32, i32* @POLLERR
%35 = load i32, i32* @POLLHUP
%36 = or i32 %34, %35
%37 = and i32 %33, %36
%38 = icmp ne i32 %37, 0
br i1 %38, label %39, label %40
39: ; preds = %30, %21
store i32 0, i32* %2
br label %54
40: ; preds = %30
%41 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %4, i64 0, i64 0
%42 = load i32, i32* %3
%43 = call i64 @handle_event(%struct.pollfd* %41, i32 %42)
%44 = icmp slt i64 %43, 0
br i1 %44, label %45, label %46
45: ; preds = %40
store i32 -1, i32* %2
br label %54
46: ; preds = %40
%47 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %4, i64 0, i64 1
%48 = load i32, i32* %3
%49 = call i64 @handle_event(%struct.pollfd* %47, i32 %48)
%50 = icmp slt i64 %49, 0
br i1 %50, label %51, label %52
51: ; preds = %46
store i32 -1, i32* %2
br label %54
52: ; preds = %46
br label %17
53: ; preds = %17
store i32 -1, i32* %2
br label %54
54: ; preds = %53, %51, %45, %39
%55 = load i32, i32* %2
ret i32 %55
}

LLVM IR (-O2):
%struct.pollfd = type { i32, i32, i8* }
@STDIN_FILENO = global i32 0
@POLLIN = global i8* null
@POLLERR = global i32 0
@POLLHUP = global i32 0
define i32 @loop(i32 %0) {
%2 = alloca [2 x %struct.pollfd]
%3 = bitcast [2 x %struct.pollfd]* %2 to i8*
%4 = load i32, i32* @STDIN_FILENO
%5 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %2, i64 0, i64 0
%6 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %2, i64 0, i64 0, i32 0
store i32 %4, i32* %6
%7 = load i8*, i8** @POLLIN
%8 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %2, i64 0, i64 0, i32 2
store i8* %7, i8** %8
%9 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %2, i64 0, i64 1
%10 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %9, i64 0, i32 0
store i32 %0, i32* %10
%11 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %2, i64 0, i64 1, i32 2
store i8* %7, i8** %11
%12 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %2, i64 0, i64 0, i32 1
%13 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %2, i64 0, i64 1, i32 1
br label %14
14: ; preds = %31, %1
%15 = call i64 @poll(%struct.pollfd* %5, i32 2, i32 -1)
%16 = icmp sgt i64 %15, 0
br i1 %16, label %17, label %34
17: ; preds = %14
%18 = load i32, i32* %12
%19 = load i32, i32* @POLLERR
%20 = load i32, i32* @POLLHUP
%21 = or i32 %20, %19
%22 = and i32 %21, %18
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %34
24: ; preds = %17
%25 = load i32, i32* %13
%26 = and i32 %25, %21
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %34
28: ; preds = %24
%29 = call i64 @handle_event(%struct.pollfd* %5, i32 %0)
%30 = icmp slt i64 %29, 0
br i1 %30, label %34, label %31
31: ; preds = %28
%32 = call i64 @handle_event(%struct.pollfd* %9, i32 %0)
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %14
34: ; preds = %14, %31, %28, %17, %24
%35 = phi i32 [ 0, %24 ], [ 0, %17 ], [ -1, %28 ], [ -1, %31 ], [ -1, %14 ]
ret i32 %35
}