#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 events;
  int revents;
};

/* Variables and functions */
int POLLIN;
int /*<<< orphan*/ perror(char *);
int poll(struct pollfd *, int, int);
int read(int, unsigned char *, int);
int /*<<< orphan*/ skytraq_dbg_char(int, unsigned char);
int skytraq_waitchar(int fd, int timeout) {
  struct pollfd fds[1];
  int ret;
  unsigned char c;
  for (;;) {
    fds[0].fd = fd;
    fds[0].events = POLLIN;
    ret = poll(fds, 1, timeout);
    if (ret >= 1) {
      if (fds[0].revents & POLLIN) {
        ret = read(fd, &c, 1);
        if (ret == 1) {
          skytraq_dbg_char(1, c);
          return c;
        }
      }
    } else if (ret == 0)
      return -2;
    else {
      perror("poll");
      return -1;
    }
  }
}


x86 assembly (clang, -O0):
<skytraq_waitchar>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    0x2e8d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
lea    -0x18(%rbp),%rdi
mov    -0xc(%rbp),%edx
mov    $0x1,%esi
call   1040 <poll@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x1,-0x1c(%rbp)
jl     11d5 <skytraq_waitchar+0x95>
mov    -0x10(%rbp),%eax
mov    0x2e60(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     11d0 <skytraq_waitchar+0x90>
mov    -0x8(%rbp),%edi
lea    -0x1d(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x1,-0x1c(%rbp)
jne    11cb <skytraq_waitchar+0x8b>
mov    $0x1,%edi
movzbl -0x1d(%rbp),%esi
call   1060 <skytraq_dbg_char@plt>
movzbl -0x1d(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1208 <skytraq_waitchar+0xc8>
jmp    11d0 <skytraq_waitchar+0x90>
jmp    1203 <skytraq_waitchar+0xc3>
cmpl   $0x0,-0x1c(%rbp)
jne    11eb <skytraq_waitchar+0xab>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1208 <skytraq_waitchar+0xc8>
lea    0xe0e(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1208 <skytraq_waitchar+0xc8>
jmp    114e <skytraq_waitchar+0xe>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<skytraq_waitchar>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %esi,%ebx
mov    %edi,%ebp
mov    %edi,0x10(%rsp)
mov    0x2e8d(%rip),%r12
mov    (%r12),%eax
mov    %eax,0x14(%rsp)
lea    0x10(%rsp),%rdi
mov    $0x1,%esi
mov    %ebx,%edx
call   1040 <poll@plt>
test   %eax,%eax
jle    11cf <skytraq_waitchar+0x8f>
lea    0x10(%rsp),%r14
lea    0xf(%rsp),%r15
jmp    11ab <skytraq_waitchar+0x6b>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %ebp,0x10(%rsp)
mov    %eax,0x14(%rsp)
mov    %r14,%rdi
mov    $0x1,%esi
mov    %ebx,%edx
call   1040 <poll@plt>
test   %eax,%eax
jle    11cf <skytraq_waitchar+0x8f>
mov    (%r12),%eax
test   %eax,0x18(%rsp)
je     1190 <skytraq_waitchar+0x50>
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
cmp    $0x1,%eax
je     11eb <skytraq_waitchar+0xab>
mov    (%r12),%eax
jmp    1190 <skytraq_waitchar+0x50>
mov    $0xfffffffe,%ebx
test   %eax,%eax
je     11fc <skytraq_waitchar+0xbc>
lea    0xe21(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%ebx
jmp    11fc <skytraq_waitchar+0xbc>
movzbl 0xf(%rsp),%ebx
mov    $0x1,%edi
mov    %ebx,%esi
call   1060 <skytraq_dbg_char@plt>
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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