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

/* Variables and functions */
int /*<<< orphan*/ POLLIN;
scalar_t__ poll(struct pollfd *, int, int /*<<< orphan*/);
int read(int, char *, int);
int gdb_check_for_interrupt(int fd) {
  struct pollfd pfd;
  pfd.fd = fd;
  pfd.events = POLLIN;
  if (poll(&pfd, 1, 0) != 0) {
    char c;
    if (read(fd, &c, 1) != 1)
      return -2;
    if (c == '\x03')
      return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<gdb_check_for_interrupt>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    0x2eb0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
lea    -0x10(%rbp),%rdi
mov    $0x1,%esi
xor    %edx,%edx
call   1040 <poll@plt>
cmp    $0x0,%rax
je     119b <gdb_check_for_interrupt+0x7b>
mov    -0x8(%rbp),%edi
lea    -0x11(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
cmp    $0x1,%eax
je     117d <gdb_check_for_interrupt+0x5d>
movl   $0xfffffffe,-0x4(%rbp)
jmp    11a2 <gdb_check_for_interrupt+0x82>
movsbl -0x11(%rbp),%eax
cmp    $0x3,%eax
jne    1196 <gdb_check_for_interrupt+0x76>
movl   $0x1,-0x4(%rbp)
jmp    11a2 <gdb_check_for_interrupt+0x82>
jmp    119b <gdb_check_for_interrupt+0x7b>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gdb_check_for_interrupt>:
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebx
mov    %edi,0x8(%rsp)
mov    0x2eb6(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
lea    0x8(%rsp),%rdi
mov    $0x1,%esi
xor    %edx,%edx
call   1040 <poll@plt>
test   %rax,%rax
je     1174 <gdb_check_for_interrupt+0x54>
lea    0x7(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1030 <read@plt>
xor    %ecx,%ecx
cmp    $0x1,%eax
sete   %cl
lea    -0x2(%rcx,%rcx,2),%eax
jne    1176 <gdb_check_for_interrupt+0x56>
cmpb   $0x3,0x7(%rsp)
je     1176 <gdb_check_for_interrupt+0x56>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.pollfd = type { i32, i32 }
@POLLIN = global i32 0
define i32 @gdb_check_for_interrupt(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.pollfd
%5 = alloca i8
store i32 %0, i32* %3
%6 = load i32, i32* %3
%7 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %4, i32 0, i32 0
store i32 %6, i32* %7
%8 = load i32, i32* @POLLIN
%9 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %4, i32 0, i32 1
store i32 %8, i32* %9
%10 = call i64 @poll(%struct.pollfd* %4, i32 1, i32 0)
%11 = icmp ne i64 %10, 0
br i1 %11, label %12, label %23
12: ; preds = %1
%13 = load i32, i32* %3
%14 = call i32 @read(i32 %13, i8* %5, i32 1)
%15 = icmp ne i32 %14, 1
br i1 %15, label %16, label %17
16: ; preds = %12
store i32 -2, i32* %2
br label %24
17: ; preds = %12
%18 = load i8, i8* %5
%19 = sext i8 %18 to i32
%20 = icmp eq i32 %19, 3
br i1 %20, label %21, label %22
21: ; preds = %17
store i32 1, i32* %2
br label %24
22: ; preds = %17
br label %23
23: ; preds = %22, %1
store i32 0, i32* %2
br label %24
24: ; preds = %23, %21, %16
%25 = load i32, i32* %2
ret i32 %25
}

LLVM IR (-O2):
%struct.pollfd = type { i32, i32 }
@POLLIN = global i32 0
define i32 @gdb_check_for_interrupt(i32 %0) {
%2 = alloca %struct.pollfd
%3 = alloca i8
%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 = load i32, i32* @POLLIN
%7 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %2, i64 0, i32 1
store i32 %6, i32* %7
%8 = call i64 @poll(%struct.pollfd* %2, i32 1, i32 0)
%9 = icmp eq i64 %8, 0
br i1 %9, label %17, label %10
10: ; preds = %1
%11 = call i32 @read(i32 %0, i8* %3, i32 1)
%12 = icmp eq i32 %11, 1
%13 = load i8, i8* %3
%14 = icmp ne i8 %13, 3
%15 = select i1 %12, i32 1, i32 -2
%16 = select i1 %12, i1 %14, i1 false
br i1 %16, label %17, label %18
17: ; preds = %10, %1
br label %18
18: ; preds = %10, %17
%19 = phi i32 [ 0, %17 ], [ %15, %10 ]
ret i32 %19
}