#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*/ member_2;
  int /*<<< orphan*/ member_1;
  int /*<<< orphan*/ member_0;
};
typedef int ssize_t;

/* Variables and functions */
int EOF;
int /*<<< orphan*/ POLLIN;
int poll(struct pollfd *, int, int);
int read(int /*<<< orphan*/, unsigned char *, int);
int get_char(int timeout) {
  struct pollfd p[] = {{0, POLLIN, 0}};
  unsigned char c;
  ssize_t n;
  n = poll(p, sizeof p / sizeof p[0], timeout);
  if (n == 0)
    return -2;
  else if (n == -1)
    return -3;
  n = read(0, &c, 1);
  if (n == 0)
    return EOF;
  else if (n == -1)
    return -3;
  return c;
}


x86 assembly (clang, -O0):
<get_char>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x0,-0x18(%rbp)
mov    0x2ea7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
movl   $0x0,-0x10(%rbp)
lea    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%edx
mov    $0x1,%esi
call   1040 <poll@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jne    116f <get_char+0x4f>
movl   $0xfffffffe,-0x4(%rbp)
jmp    11da <get_char+0xba>
cmpl   $0xffffffff,-0x20(%rbp)
jne    1185 <get_char+0x65>
movl   $0xfffffffd,-0x4(%rbp)
jmp    11da <get_char+0xba>
jmp    118a <get_char+0x6a>
xor    %edi,%edi
lea    -0x19(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jne    11b8 <get_char+0x98>
mov    0x2e3a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    11da <get_char+0xba>
cmpl   $0xffffffff,-0x20(%rbp)
jne    11ce <get_char+0xae>
movl   $0xfffffffd,-0x4(%rbp)
jmp    11da <get_char+0xba>
jmp    11d3 <get_char+0xb3>
movzbl -0x19(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_char>:
sub    $0x18,%rsp
mov    %edi,%edx
movl   $0x0,0x8(%rsp)
mov    0x2eab(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
movl   $0x0,0x10(%rsp)
lea    0x8(%rsp),%rdi
mov    $0x1,%esi
call   1040 <poll@plt>
test   %eax,%eax
je     118d <get_char+0x6d>
cmp    $0xffffffff,%eax
je     1183 <get_char+0x63>
lea    0x7(%rsp),%rsi
xor    %edi,%edi
mov    $0x1,%edx
call   1030 <read@plt>
cmp    $0xffffffff,%eax
je     1183 <get_char+0x63>
test   %eax,%eax
jne    1197 <get_char+0x77>
mov    0x2e6c(%rip),%rax
mov    (%rax),%eax
add    $0x18,%rsp
ret
mov    $0xfffffffd,%eax
add    $0x18,%rsp
ret
mov    $0xfffffffe,%eax
add    $0x18,%rsp
ret
movzbl 0x7(%rsp),%eax
add    $0x18,%rsp
ret

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

LLVM IR (-O2):
%struct.pollfd = type { i32, i32, i32 }
@POLLIN = global i32 0
@EOF = global i32 0
define i32 @get_char(i32 %0) {
%2 = alloca [1 x %struct.pollfd]
%3 = alloca i8
%4 = bitcast [1 x %struct.pollfd]* %2 to i8*
%5 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %2, i64 0, i64 0
%6 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %2, i64 0, i64 0, i32 0
store i32 0, i32* %6
%7 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %2, i64 0, i64 0, i32 1
%8 = load i32, i32* @POLLIN
store i32 %8, i32* %7
%9 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %2, i64 0, i64 0, i32 2
store i32 0, i32* %9
%10 = call i32 @poll(%struct.pollfd* %5, i32 1, i32 %0)
switch i32 %10, label %12 [
i32 0, label %19
i32 -1, label %11
]
11: ; preds = %1
br label %19
12: ; preds = %1
%13 = call i32 @read(i32 0, i8* %3, i32 1)
switch i32 %13, label %16 [
i32 0, label %14
i32 -1, label %19
]
14: ; preds = %12
%15 = load i32, i32* @EOF
br label %19
16: ; preds = %12
%17 = load i8, i8* %3
%18 = zext i8 %17 to i32
br label %19
19: ; preds = %12, %1, %16, %14, %11
%20 = phi i32 [ -3, %11 ], [ %15, %14 ], [ %18, %16 ], [ -2, %1 ], [ -3, %12 ]
ret i32 %20
}