#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;
  scalar_t__ revents;
  scalar_t__ events;
};

/* Variables and functions */
scalar_t__ POLLHUP;
int errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, scalar_t__, ...);
int poll(struct pollfd *, int, int);
int /*<<< orphan*/ stderr;
char *strerror(int);
void monitor_leader(int fd) {
  struct pollfd fds;
  fds.fd = fd;
  fds.events = 0;
  int rc = poll(&fds, 1, -1);
  if (rc < 0) {
    fprintf(stderr, "Get poll error %d: %s", errno, strerror(errno));
    exit(1);
  }
  if (fds.revents != POLLHUP) {
    fprintf(stderr, "revents = %x", fds.revents);
  }
}


x86 assembly (clang, -O0):
<monitor_leader>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x20(%rbp)
movq   $0x0,-0x10(%rbp)
lea    -0x20(%rbp),%rdi
mov    $0x1,%esi
mov    $0xffffffff,%edx
call   1040 <poll@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11c3 <monitor_leader+0x83>
mov    0x2e58(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    0x2e44(%rip),%rax
movslq (%rax),%rax
mov    %rax,-0x30(%rbp)
mov    0x2e36(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rdx
mov    %rax,%rcx
lea    0xe4e(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
mov    -0x18(%rbp),%rax
mov    0x2e1a(%rip),%rcx
cmp    (%rcx),%rax
je     11f2 <monitor_leader+0xb2>
mov    0x2dfa(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xe2b(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<monitor_leader>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %edi,(%rsp)
movq   $0x0,0x10(%rsp)
mov    %rsp,%rdi
mov    $0x1,%esi
mov    $0xffffffff,%edx
call   1040 <poll@plt>
test   %eax,%eax
js     1197 <monitor_leader+0x57>
mov    0x8(%rsp),%rdx
mov    0x2e74(%rip),%rax
cmp    (%rax),%rdx
je     1190 <monitor_leader+0x50>
mov    0x2e58(%rip),%rax
mov    (%rax),%edi
lea    0xe8d(%rip),%rsi
xor    %eax,%eax
call   1030 <fprintf@plt>
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2e3a(%rip),%rax
mov    (%rax),%ebp
mov    0x2e29(%rip),%rax
mov    (%rax),%eax
movslq %eax,%rbx
mov    %ebx,%edi
call   1060 <strerror@plt>
lea    0xe46(%rip),%rsi
mov    %ebp,%edi
mov    %rbx,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
%struct.pollfd = type { i32, i64, i64 }
@stderr = global i32 0
@.str = [22 x i8] c"Get poll error %d: %s\00"
@errno = global i32 0
@POLLHUP = global i64 0
@.str.1 = [13 x i8] c"revents = %x\00"
define void @monitor_leader(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.pollfd
%4 = alloca i32
store i32 %0, i32* %2
%5 = load i32, i32* %2
%6 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %3, i32 0, i32 0
store i32 %5, i32* %6
%7 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %3, i32 0, i32 2
store i64 0, i64* %7
%8 = call i32 @poll(%struct.pollfd* %3, i32 1, i32 -1)
store i32 %8, i32* %4
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %19
11: ; preds = %1
%12 = load i32, i32* @stderr
%13 = load i32, i32* @errno
%14 = sext i32 %13 to i64
%15 = load i32, i32* @errno
%16 = call i8* @strerror(i32 %15)
%17 = call i32 (i32, i8*, i64, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i64 %14, i8* %16)
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %1
%20 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %3, i32 0, i32 1
%21 = load i64, i64* %20
%22 = load i64, i64* @POLLHUP
%23 = icmp ne i64 %21, %22
br i1 %23, label %24, label %29
24: ; preds = %19
%25 = load i32, i32* @stderr
%26 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %3, i32 0, i32 1
%27 = load i64, i64* %26
%28 = call i32 (i32, i8*, i64, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i64 %27)
br label %29
29: ; preds = %24, %19
ret void
}

LLVM IR (-O2):
%struct.pollfd = type { i32, i64, i64 }
@stderr = global i32 0
@.str = [22 x i8] c"Get poll error %d: %s\00"
@errno = global i32 0
@POLLHUP = global i64 0
@.str.1 = [13 x i8] c"revents = %x\00"
define void @monitor_leader(i32 %0) {
%2 = alloca %struct.pollfd
%3 = bitcast %struct.pollfd* %2 to i8*
%4 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %2, i64 0, i32 0
store i32 %0, i32* %4
%5 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %2, i64 0, i32 2
store i64 0, i64* %5
%6 = call i32 @poll(%struct.pollfd* %2, i32 1, i32 -1)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %15
8: ; preds = %1
%9 = load i32, i32* @stderr
%10 = load i32, i32* @errno
%11 = sext i32 %10 to i64
%12 = call i8* @strerror(i32 %10)
%13 = call i32 (i32, i8*, i64, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i64 %11, i8* %12)
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %1
%16 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %2, i64 0, i32 1
%17 = load i64, i64* %16
%18 = load i64, i64* @POLLHUP
%19 = icmp eq i64 %17, %18
br i1 %19, label %23, label %20
20: ; preds = %15
%21 = load i32, i32* @stderr
%22 = call i32 (i32, i8*, i64, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i64 %17)
br label %23
23: ; preds = %20, %15
ret void
}