#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 */

/* Variables and functions */
int EOF;
int /*<<< orphan*/ FIONREAD;
scalar_t__ ioctl(int /*<<< orphan*/, int /*<<< orphan*/, int *);
int perror(char *);
int read(int /*<<< orphan*/, char *, int);
int term_testchar() {
  int n;
  char c;
  if (ioctl(0, FIONREAD, &n) < 0)
    return (perror("IOgetchar"), -1);
  if (n == 0)
    return -2;
  switch (read(0, &c, 1)) {
  case 1:
    return c;
  case 0:
    return EOF;
  default:
    return (perror("IOgetchar-read"), -1);
  }
}


x86 assembly (clang, -O0):
<term_testchar>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2ea9(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    -0x8(%rbp),%rdx
call   1030 <ioctl@plt>
cmp    $0x0,%rax
jge    116e <term_testchar+0x3e>
lea    0xea3(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e7 <term_testchar+0xb7>
cmpl   $0x0,-0x8(%rbp)
jne    1184 <term_testchar+0x54>
movl   $0xfffffffe,-0x4(%rbp)
jmp    11e7 <term_testchar+0xb7>
xor    %edi,%edi
lea    -0x9(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
mov    %eax,%ecx
mov    %ecx,-0x10(%rbp)
test   %eax,%eax
je     11c3 <term_testchar+0x93>
jmp    11a6 <term_testchar+0x76>
mov    -0x10(%rbp),%eax
sub    $0x1,%eax
jne    11d4 <term_testchar+0xa4>
jmp    11b7 <term_testchar+0x87>
movsbl -0x9(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11e7 <term_testchar+0xb7>
mov    0x2e16(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    11e7 <term_testchar+0xb7>
lea    0xe2f(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<term_testchar>:
push   %rax
mov    0x2eb0(%rip),%rax
mov    (%rax),%esi
lea    0x4(%rsp),%rdx
xor    %edi,%edi
call   1030 <ioctl@plt>
test   %rax,%rax
js     1185 <term_testchar+0x55>
cmpl   $0x0,0x4(%rsp)
je     1173 <term_testchar+0x43>
lea    0x3(%rsp),%rsi
xor    %edi,%edi
mov    $0x1,%edx
call   1040 <read@plt>
test   %eax,%eax
je     117a <term_testchar+0x4a>
cmp    $0x1,%eax
jne    118e <term_testchar+0x5e>
movsbl 0x3(%rsp),%eax
pop    %rcx
ret
mov    $0xfffffffe,%eax
pop    %rcx
ret
mov    0x2e5f(%rip),%rax
mov    (%rax),%eax
pop    %rcx
ret
lea    0xe74(%rip),%rdi
jmp    1195 <term_testchar+0x65>
lea    0xe75(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%eax
pop    %rcx
ret

LLVM IR (-O0):
@FIONREAD = global i32 0
@.str = [10 x i8] c"IOgetchar\00"
@EOF = global i32 0
@.str.1 = [15 x i8] c"IOgetchar-read\00"
define i32 @term_testchar() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8
%4 = load i32, i32* @FIONREAD
%5 = call i64 @ioctl(i32 0, i32 %4, i32* %2)
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %1
br label %22
9: ; preds = %0
%10 = load i32, i32* %2
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %9
store i32 -2, i32* %1
br label %22
13: ; preds = %9
%14 = call i32 @read(i32 0, i8* %3, i32 1)
switch i32 %14, label %20 [
i32 1, label %15
i32 0, label %18
]
15: ; preds = %13
%16 = load i8, i8* %3
%17 = sext i8 %16 to i32
store i32 %17, i32* %1
br label %22
18: ; preds = %13
%19 = load i32, i32* @EOF
store i32 %19, i32* %1
br label %22
20: ; preds = %13
%21 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %1
br label %22
22: ; preds = %20, %18, %15, %12, %7
%23 = load i32, i32* %1
ret i32 %23
}

LLVM IR (-O2):
@FIONREAD = global i32 0
@.str = [10 x i8] c"IOgetchar\00"
@EOF = global i32 0
@.str.1 = [15 x i8] c"IOgetchar-read\00"
define i32 @term_testchar() {
%1 = alloca i32
%2 = alloca i8
%3 = bitcast i32* %1 to i8*
%4 = load i32, i32* @FIONREAD
%5 = call i64 @ioctl(i32 0, i32 %4, i32* %1)
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
br label %21
9: ; preds = %0
%10 = load i32, i32* %1
%11 = icmp eq i32 %10, 0
br i1 %11, label %21, label %12
12: ; preds = %9
%13 = call i32 @read(i32 0, i8* %2, i32 1)
switch i32 %13, label %19 [
i32 1, label %14
i32 0, label %17
]
14: ; preds = %12
%15 = load i8, i8* %2
%16 = sext i8 %15 to i32
br label %21
17: ; preds = %12
%18 = load i32, i32* @EOF
br label %21
19: ; preds = %12
%20 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %21
21: ; preds = %9, %19, %17, %14, %7
%22 = phi i32 [ -1, %7 ], [ -1, %19 ], [ %18, %17 ], [ %16, %14 ], [ -2, %9 ]
ret i32 %22
}