#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 */
typedef int uint32_t;
struct pollfd {
  int fd;
  int /*<<< orphan*/ events;
};
typedef int ssize_t;
typedef int /*<<< orphan*/ info;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ POLLIN;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int poll(struct pollfd *, int, int);
int /*<<< orphan*/ printf(char *, int);
int read(int, int *, int);
int waitForInterrupt(int fd) {
  struct pollfd fds = {
      .fd = fd,
      .events = POLLIN,
  };
  uint32_t info;
  ssize_t nb;
  int ret = poll(&fds, 1, -1);
  if (ret <= 0) {
    perror("poll()");
    close(fd);
    exit(EXIT_FAILURE);
  }
  nb = read(fd, &info, sizeof(info));
  if (nb == sizeof(info)) {
    return info;
  }
  printf("Interrupt read returned wrong size #%u!\n", info);
  return 0;
}


x86 assembly (clang, -O0):
<waitForInterrupt>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e70(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
lea    -0x10(%rbp),%rdi
mov    $0x1,%esi
mov    $0xffffffff,%edx
call   1060 <poll@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jg     11bf <waitForInterrupt+0x5f>
lea    0xe5c(%rip),%rdi
call   1070 <perror@plt>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
mov    0x2e20(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x8(%rbp),%edi
lea    -0x14(%rbp),%rsi
mov    $0x4,%edx
call   1050 <read@plt>
mov    %eax,-0x18(%rbp)
movslq -0x18(%rbp),%rax
cmp    $0x4,%rax
jne    11ec <waitForInterrupt+0x8c>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1202 <waitForInterrupt+0xa2>
mov    -0x14(%rbp),%esi
lea    0xe11(%rip),%rdi
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<waitForInterrupt>:
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebx
mov    %edi,0x8(%rsp)
mov    0x2e76(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
lea    0x8(%rsp),%rdi
mov    $0x1,%esi
mov    $0xffffffff,%edx
call   1060 <poll@plt>
test   %eax,%eax
jle    11c2 <waitForInterrupt+0x62>
lea    0x4(%rsp),%rsi
mov    %ebx,%edi
mov    $0x4,%edx
call   1050 <read@plt>
mov    %eax,%ecx
mov    0x4(%rsp),%eax
cmp    $0x4,%ecx
je     11bc <waitForInterrupt+0x5c>
lea    0xe54(%rip),%rdi
mov    %eax,%esi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe37(%rip),%rdi
call   1070 <perror@plt>
mov    %ebx,%edi
call   1040 <close@plt>
mov    0x2dfc(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.pollfd = type { i32, i32 }
@POLLIN = global i32 0
@.str = [7 x i8] c"poll()\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [41 x i8] c"Interrupt read returned wrong size #%u!\0A\00"
define i32 @waitForInterrupt(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.pollfd
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %3
%8 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %4, i32 0, i32 0
%9 = load i32, i32* %3
store i32 %9, i32* %8
%10 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %4, i32 0, i32 1
%11 = load i32, i32* @POLLIN
store i32 %11, i32* %10
%12 = call i32 @poll(%struct.pollfd* %4, i32 1, i32 -1)
store i32 %12, i32* %7
%13 = load i32, i32* %7
%14 = icmp sle i32 %13, 0
br i1 %14, label %15, label %21
15: ; preds = %1
%16 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%17 = load i32, i32* %3
%18 = call i32 @close(i32 %17)
%19 = load i32, i32* @EXIT_FAILURE
%20 = call i32 @exit(i32 %19)
unreachable
21: ; preds = %1
%22 = load i32, i32* %3
%23 = call i32 @read(i32 %22, i32* %5, i32 4)
store i32 %23, i32* %6
%24 = load i32, i32* %6
%25 = sext i32 %24 to i64
%26 = icmp eq i64 %25, 4
br i1 %26, label %27, label %29
27: ; preds = %21
%28 = load i32, i32* %5
store i32 %28, i32* %2
br label %32
29: ; preds = %21
%30 = load i32, i32* %5
%31 = call i32 @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0), i32 %30)
store i32 0, i32* %2
br label %32
32: ; preds = %29, %27
%33 = load i32, i32* %2
ret i32 %33
}

LLVM IR (-O2):
%struct.pollfd = type { i32, i32 }
@POLLIN = global i32 0
@.str = [7 x i8] c"poll()\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [41 x i8] c"Interrupt read returned wrong size #%u!\0A\00"
define i32 @waitForInterrupt(i32 %0) {
%2 = alloca %struct.pollfd
%3 = alloca i32
%4 = bitcast %struct.pollfd* %2 to i8*
%5 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %2, i64 0, i32 0
store i32 %0, i32* %5
%6 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %2, i64 0, i32 1
%7 = load i32, i32* @POLLIN
store i32 %7, i32* %6
%8 = bitcast i32* %3 to i8*
%9 = call i32 @poll(%struct.pollfd* %2, i32 1, i32 -1)
%10 = icmp slt i32 %9, 1
br i1 %10, label %11, label %16
11: ; preds = %1
%12 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%13 = call i32 @close(i32 %0)
%14 = load i32, i32* @EXIT_FAILURE
%15 = call i32 @exit(i32 %14)
unreachable
16: ; preds = %1
%17 = call i32 @read(i32 %0, i32* %3, i32 4)
%18 = icmp eq i32 %17, 4
%19 = load i32, i32* %3
br i1 %18, label %22, label %20
20: ; preds = %16
%21 = call i32 @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0), i32 %19)
br label %22
22: ; preds = %16, %20
%23 = phi i32 [ 0, %20 ], [ %19, %16 ]
ret i32 %23
}