#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 */
scalar_t__ EAGAIN;
scalar_t__ errno;
int /*<<< orphan*/ exit_term(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *);
int read(int, unsigned char *, int);
char *strerror(scalar_t__);
unsigned char read_char(int fd) {
  unsigned char c;
  int n;
  while (1) {
    n = read(fd, &c, 1);
    if (n > 0)
      return c;
    else if ((n < 0) && (errno != EAGAIN)) {
      printf("Error reading from device: %s\n", strerror(errno));
      exit_term(0);
    }
  }
}


x86 assembly (clang, -O0):
<read_char>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x5(%rbp),%rsi
mov    $0x1,%edx
call   1050 <read@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jle    1173 <read_char+0x33>
movzbl -0x5(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret
cmpl   $0x0,-0xc(%rbp)
jge    11bc <read_char+0x7c>
mov    0x2e5c(%rip),%rax
mov    (%rax),%rax
mov    0x2e42(%rip),%rcx
cmp    (%rcx),%rax
je     11bc <read_char+0x7c>
mov    0x2e42(%rip),%rax
mov    (%rax),%rdi
call   1060 <strerror@plt>
mov    %rax,%rsi
lea    0xe50(%rip),%rdi
call   1040 <printf@plt>
xor    %edi,%edi
call   1030 <exit_term@plt>
jmp    11c1 <read_char+0x81>
jmp    114b <read_char+0xb>

x86 assembly (clang, -O2):
<read_char>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebx
lea    0xf(%rsp),%rsi
mov    $0x1,%edx
call   1050 <read@plt>
test   %eax,%eax
jle    1174 <read_char+0x34>
mov    0xf(%rsp),%al
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
mov    0x2e65(%rip),%r12
mov    0x2e4e(%rip),%r13
lea    0xe77(%rip),%r14
lea    0xf(%rsp),%r15
jmp    11a3 <read_char+0x63>
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1050 <read@plt>
test   %eax,%eax
jg     1162 <read_char+0x22>
test   %eax,%eax
jns    1190 <read_char+0x50>
mov    (%r12),%rdi
cmp    0x0(%r13),%rdi
je     1190 <read_char+0x50>
call   1060 <strerror@plt>
mov    %r14,%rdi
mov    %rax,%rsi
call   1040 <printf@plt>
xor    %edi,%edi
call   1030 <exit_term@plt>
jmp    1190 <read_char+0x50>

LLVM IR (-O0):
@errno = global i64 0
@EAGAIN = global i64 0
@.str = [31 x i8] c"Error reading from device: %s\0A\00"
define zeroext i8 @read_char(i32 %0) {
%2 = alloca i32
%3 = alloca i8
%4 = alloca i32
store i32 %0, i32* %2
br label %5
5: ; preds = %1, %25
%6 = load i32, i32* %2
%7 = call i32 @read(i32 %6, i8* %3, i32 1)
store i32 %7, i32* %4
%8 = load i32, i32* %4
%9 = icmp sgt i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %5
%11 = load i8, i8* %3
ret i8 %11
12: ; preds = %5
%13 = load i32, i32* %4
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %24
15: ; preds = %12
%16 = load i64, i64* @errno
%17 = load i64, i64* @EAGAIN
%18 = icmp ne i64 %16, %17
br i1 %18, label %19, label %24
19: ; preds = %15
%20 = load i64, i64* @errno
%21 = call i8* @strerror(i64 %20)
%22 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8* %21)
%23 = call i32 @exit_term(i32 0)
br label %24
24: ; preds = %19, %15, %12
br label %25
25: ; preds = %24
br label %5
}

LLVM IR (-O2):
@errno = global i64 0
@EAGAIN = global i64 0
@.str = [31 x i8] c"Error reading from device: %s\0A\00"
define zeroext i8 @read_char(i32 %0) {
%2 = alloca i8
%3 = call i32 @read(i32 %0, i8* %2, i32 1)
%4 = icmp sgt i32 %3, 0
br i1 %4, label %5, label %7
5: ; preds = %18, %1
%6 = load i8, i8* %2
ret i8 %6
7: ; preds = %1, %18
%8 = phi i32 [ %19, %18 ], [ %3, %1 ]
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %18
10: ; preds = %7
%11 = load i64, i64* @errno
%12 = load i64, i64* @EAGAIN
%13 = icmp eq i64 %11, %12
br i1 %13, label %18, label %14
14: ; preds = %10
%15 = tail call i8* @strerror(i64 %11)
%16 = tail call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8* %15)
%17 = tail call i32 @exit_term(i32 0)
br label %18
18: ; preds = %7, %10, %14
%19 = call i32 @read(i32 %0, i8* %2, i32 1)
%20 = icmp sgt i32 %19, 0
br i1 %20, label %5, label %7
}