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

/* Variables and functions */
int POLLHUP;
int /*<<< orphan*/ POLLIN;
int /*<<< orphan*/ ltr_int_my_perror(char *);
int poll(struct pollfd *, int, int);
int ltr_int_pipe_poll(int pipefd, int timeout, bool *hup) {
  struct pollfd downlink_poll = {.fd = pipefd, .events = POLLIN, .revents = 0};
  if (hup != NULL) {
    *hup = false;
  }
  int fds = poll(&downlink_poll, 1, timeout);
  if (fds > 0) {
    if (downlink_poll.revents & POLLHUP) {
      if (hup != NULL) {
        *hup = true;
      }
      return -1;
    }
    return fds;
  } else if (fds < 0) {
    ltr_int_my_perror("poll");
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<ltr_int_pipe_poll>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x28(%rbp)
movl   $0x0,-0x24(%rbp)
mov    0x2e9a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1160 <ltr_int_pipe_poll+0x40>
mov    -0x18(%rbp),%rax
movl   $0x0,(%rax)
mov    -0xc(%rbp),%edx
lea    -0x28(%rbp),%rdi
mov    $0x1,%esi
call   1040 <poll@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jle    11bf <ltr_int_pipe_poll+0x9f>
mov    -0x24(%rbp),%eax
mov    0x2e60(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     11b4 <ltr_int_pipe_poll+0x94>
cmpq   $0x0,-0x18(%rbp)
je     11a8 <ltr_int_pipe_poll+0x88>
mov    -0x18(%rbp),%rax
movl   $0x1,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11ed <ltr_int_pipe_poll+0xcd>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11ed <ltr_int_pipe_poll+0xcd>
cmpl   $0x0,-0x2c(%rbp)
jge    11e1 <ltr_int_pipe_poll+0xc1>
lea    0xe30(%rip),%rdi
call   1030 <ltr_int_my_perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ed <ltr_int_pipe_poll+0xcd>
jmp    11e6 <ltr_int_pipe_poll+0xc6>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ltr_int_pipe_poll>:
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%rbx
mov    %esi,%edx
mov    %edi,(%rsp)
movl   $0x0,0x4(%rsp)
mov    0x2ea4(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
test   %rbx,%rbx
je     114d <ltr_int_pipe_poll+0x2d>
movl   $0x0,(%rbx)
mov    %rsp,%rdi
mov    $0x1,%esi
call   1040 <poll@plt>
mov    %eax,%ecx
test   %eax,%eax
jle    1186 <ltr_int_pipe_poll+0x66>
mov    0x2e81(%rip),%rax
mov    (%rax),%edx
and    0x4(%rsp),%edx
mov    $0xffffffff,%eax
cmove  %ecx,%eax
test   %rbx,%rbx
je     119d <ltr_int_pipe_poll+0x7d>
test   %edx,%edx
je     119d <ltr_int_pipe_poll+0x7d>
movl   $0x1,(%rbx)
jmp    1198 <ltr_int_pipe_poll+0x78>
xor    %eax,%eax
test   %ecx,%ecx
jns    119d <ltr_int_pipe_poll+0x7d>
lea    0xe6d(%rip),%rdi
call   1030 <ltr_int_my_perror@plt>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
ret

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

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