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

/* Variables and functions */
int /*<<< orphan*/ POLLIN;
int poll(struct pollfd *, int, int);
int read(int /*<<< orphan*/, unsigned char *, int);
int readchar(struct pollfd *fds) {
  fds->events = POLLIN;
  int rc = poll(fds, 1, 2000);
  if (rc < 0) {
    return -1;
  } else if (rc == 0) {
    return -1;
  }
  unsigned char c;
  rc = read(fds->fd, &c, 1);
  if (rc != 1)
    return -1;
  return c;
}


x86 assembly (clang, -O0):
<readchar>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2eb5(%rip),%rax
mov    (%rax),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x10(%rbp),%rdi
mov    $0x1,%esi
mov    $0x7d0,%edx
call   1040 <poll@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1168 <readchar+0x48>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b7 <readchar+0x97>
cmpl   $0x0,-0x14(%rbp)
jne    117e <readchar+0x5e>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b7 <readchar+0x97>
jmp    1183 <readchar+0x63>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
lea    -0x15(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x1,-0x14(%rbp)
je     11b0 <readchar+0x90>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b7 <readchar+0x97>
movzbl -0x15(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readchar>:
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
mov    0x2eb9(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rdi)
mov    $0x1,%esi
mov    $0x7d0,%edx
call   1040 <poll@plt>
test   %eax,%eax
jle    116a <readchar+0x4a>
mov    (%rbx),%edi
lea    0xf(%rsp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
cmp    $0x1,%eax
movzbl 0xf(%rsp),%ecx
mov    $0xffffffff,%eax
cmove  %ecx,%eax
jmp    116f <readchar+0x4f>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
ret

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

LLVM IR (-O2):
%struct.pollfd = type { i32, i32 }
@POLLIN = global i32 0
define i32 @readchar(%struct.pollfd* %0) {
%2 = alloca i8
%3 = load i32, i32* @POLLIN
%4 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %0, i64 0, i32 1
store i32 %3, i32* %4
%5 = tail call i32 @poll(%struct.pollfd* %0, i32 1, i32 2000)
%6 = icmp slt i32 %5, 1
br i1 %6, label %15, label %7
7: ; preds = %1
%8 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %0, i64 0, i32 0
%9 = load i32, i32* %8
%10 = call i32 @read(i32 %9, i8* %2, i32 1)
%11 = icmp eq i32 %10, 1
%12 = load i8, i8* %2
%13 = zext i8 %12 to i32
%14 = select i1 %11, i32 %13, i32 -1
br label %15
15: ; preds = %1, %7
%16 = phi i32 [ %14, %7 ], [ -1, %1 ]
ret i32 %16
}