#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 EAGAIN;
int /*<<< orphan*/ close_tty();
int errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ printconsole(char *, int, int);
int read(int, char *, int);
int strerror(int);
int readbyte(int fd, char *ch) {
  int ret;
  ret = read(fd, ch, 1);
  if (ret < 0) {
    if (errno != EAGAIN) {
      printconsole("ERROR: Failed to read from fd=%d: %s\n", fd,
                   strerror(errno));
      close_tty();
      exit(12);
    }
    return -EAGAIN;
  } else if (ret > 1) {
    printconsole("ERROR: Unexpected number of bytes read(%d) from fd=%d\n", ret,
                 fd);
    close_tty();
    exit(13);
  }
  return ret;
}


x86 assembly (clang, -O0):
<readbyte>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11de <readbyte+0x8e>
mov    0x2e5c(%rip),%rax
mov    (%rax),%eax
mov    0x2e43(%rip),%rcx
cmp    (%rcx),%eax
je     11cb <readbyte+0x7b>
mov    -0x8(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    0x2e3e(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x18(%rbp),%esi
mov    %eax,%edx
lea    0xe4b(%rip),%rdi
call   1030 <printconsole@plt>
mov    $0x0,%al
call   1050 <close_tty@plt>
mov    $0xc,%edi
call   1060 <exit@plt>
xor    %eax,%eax
mov    0x2dfc(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1216 <readbyte+0xc6>
cmpl   $0x1,-0x14(%rbp)
jle    120b <readbyte+0xbb>
mov    -0x14(%rbp),%esi
mov    -0x8(%rbp),%edx
lea    0xe31(%rip),%rdi
call   1030 <printconsole@plt>
mov    $0x0,%al
call   1050 <close_tty@plt>
mov    $0xd,%edi
call   1060 <exit@plt>
jmp    1210 <readbyte+0xc0>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readbyte>:
push   %rbx
mov    %edi,%ebx
mov    $0x1,%edx
call   1040 <read@plt>
test   %eax,%eax
js     1168 <readbyte+0x18>
cmp    $0x2,%eax
jae    1180 <readbyte+0x30>
pop    %rbx
ret
mov    0x2e71(%rip),%rax
mov    (%rax),%eax
mov    0x2e58(%rip),%rcx
cmp    (%rcx),%eax
jne    11a1 <readbyte+0x51>
neg    %eax
pop    %rbx
ret
lea    0xe9f(%rip),%rdi
mov    %eax,%esi
mov    %ebx,%edx
call   1030 <printconsole@plt>
xor    %eax,%eax
call   1050 <close_tty@plt>
mov    $0xd,%edi
call   1060 <exit@plt>
mov    %eax,%edi
call   1070 <strerror@plt>
lea    0xe51(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
call   1030 <printconsole@plt>
xor    %eax,%eax
call   1050 <close_tty@plt>
mov    $0xc,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@errno = global i32 0
@EAGAIN = global i32 0
@.str = [38 x i8] c"ERROR: Failed to read from fd=%d: %s\0A\00"
@.str.1 = [55 x i8] c"ERROR: Unexpected number of bytes read(%d) from fd=%d\0A\00"
define i32 @readbyte(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
%7 = load i32, i32* %4
%8 = load i8*, i8** %5
%9 = call i32 @read(i32 %7, i8* %8, i32 1)
store i32 %9, i32* %6
%10 = load i32, i32* %6
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %26
12: ; preds = %2
%13 = load i32, i32* @errno
%14 = load i32, i32* @EAGAIN
%15 = icmp ne i32 %13, %14
br i1 %15, label %16, label %23
16: ; preds = %12
%17 = load i32, i32* %4
%18 = load i32, i32* @errno
%19 = call i32 @strerror(i32 %18)
%20 = call i32 @printconsole(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i32 %17, i32 %19)
%21 = call i32 (...) @close_tty()
%22 = call i32 @exit(i32 12)
unreachable
23: ; preds = %12
%24 = load i32, i32* @EAGAIN
%25 = sub nsw i32 0, %24
store i32 %25, i32* %3
br label %38
26: ; preds = %2
%27 = load i32, i32* %6
%28 = icmp sgt i32 %27, 1
br i1 %28, label %29, label %35
29: ; preds = %26
%30 = load i32, i32* %6
%31 = load i32, i32* %4
%32 = call i32 @printconsole(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.1, i64 0, i64 0), i32 %30, i32 %31)
%33 = call i32 (...) @close_tty()
%34 = call i32 @exit(i32 13)
unreachable
35: ; preds = %26
br label %36
36: ; preds = %35
%37 = load i32, i32* %6
store i32 %37, i32* %3
br label %38
38: ; preds = %36, %23
%39 = load i32, i32* %3
ret i32 %39
}

LLVM IR (-O2):
@errno = global i32 0
@EAGAIN = global i32 0
@.str = [38 x i8] c"ERROR: Failed to read from fd=%d: %s\0A\00"
@.str.1 = [55 x i8] c"ERROR: Unexpected number of bytes read(%d) from fd=%d\0A\00"
define i32 @readbyte(i32 %0, i8* nocapture %1) {
%3 = tail call i32 @read(i32 %0, i8* %1, i32 1)
%4 = icmp slt i32 %3, 0
br i1 %4, label %5, label %16
5: ; preds = %2
%6 = load i32, i32* @errno
%7 = load i32, i32* @EAGAIN
%8 = icmp eq i32 %6, %7
br i1 %8, label %14, label %9
9: ; preds = %5
%10 = tail call i32 @strerror(i32 %6)
%11 = tail call i32 @printconsole(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i32 %0, i32 %10)
%12 = tail call i32 (...) @close_tty()
%13 = tail call i32 @exit(i32 12)
unreachable
14: ; preds = %5
%15 = sub nsw i32 0, %6
br label %22
16: ; preds = %2
%17 = icmp ugt i32 %3, 1
br i1 %17, label %18, label %22
18: ; preds = %16
%19 = tail call i32 @printconsole(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.1, i64 0, i64 0), i32 %3, i32 %0)
%20 = tail call i32 (...) @close_tty()
%21 = tail call i32 @exit(i32 13)
unreachable
22: ; preds = %16, %14
%23 = phi i32 [ %15, %14 ], [ %3, %16 ]
ret i32 %23
}