#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 */
typedef int ssize_t;

/* Variables and functions */
scalar_t__ EAGAIN;
scalar_t__ errno;
int read(int, char *, int);
int /*<<< orphan*/ usleep(int);
int isfd_read(int sd) {
  char c;
  ssize_t byte;
  while (1) {
    if ((byte = read(sd, &c, sizeof(char))) == sizeof(char))
      break;
    if (!byte)
      return -1;
    else if (byte == -1) {
      if (errno == EAGAIN) {
        usleep(250);
        continue;
      }
      return -1;
    }
  }
  return (int)c;
}


x86 assembly (clang, -O0):
<isfd_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x9(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
mov    %eax,-0x10(%rbp)
cltq
cmp    $0x1,%rax
jne    1150 <isfd_read+0x30>
jmp    11af <isfd_read+0x8f>
cmpl   $0x0,-0x10(%rbp)
jne    1166 <isfd_read+0x46>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b6 <isfd_read+0x96>
cmpl   $0xffffffff,-0x10(%rbp)
jne    11a5 <isfd_read+0x85>
mov    0x2e69(%rip),%rax
mov    (%rax),%rax
mov    0x2e4f(%rip),%rcx
cmp    (%rcx),%rax
jne    1199 <isfd_read+0x79>
mov    $0xfa,%edi
call   1040 <usleep@plt>
jmp    112b <isfd_read+0xb>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b6 <isfd_read+0x96>
jmp    11aa <isfd_read+0x8a>
jmp    112b <isfd_read+0xb>
movsbl -0x9(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<isfd_read>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %edi,%ebx
lea    0x7(%rsp),%r14
mov    0x2eaa(%rip),%r15
mov    0x2e93(%rip),%r12
nopl   (%rax)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    $0xffffffff,%ecx
je     1170 <isfd_read+0x50>
test   %ecx,%ecx
je     118a <isfd_read+0x6a>
cmp    $0x1,%ecx
jne    1140 <isfd_read+0x20>
jmp    1185 <isfd_read+0x65>
cs nopw 0x0(%rax,%rax,1)
mov    (%r15),%rcx
cmp    (%r12),%rcx
jne    118a <isfd_read+0x6a>
mov    $0xfa,%edi
call   1040 <usleep@plt>
jmp    1140 <isfd_read+0x20>
movsbl 0x7(%rsp),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@errno = global i64 0
@EAGAIN = global i64 0
define i32 @isfd_read(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8
%5 = alloca i32
store i32 %0, i32* %3
br label %6
6: ; preds = %1, %23, %27
%7 = load i32, i32* %3
%8 = call i32 @read(i32 %7, i8* %4, i32 1)
store i32 %8, i32* %5
%9 = sext i32 %8 to i64
%10 = icmp eq i64 %9, 1
br i1 %10, label %11, label %12
11: ; preds = %6
br label %28
12: ; preds = %6
%13 = load i32, i32* %5
%14 = icmp ne i32 %13, 0
br i1 %14, label %16, label %15
15: ; preds = %12
store i32 -1, i32* %2
br label %31
16: ; preds = %12
%17 = load i32, i32* %5
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %26
19: ; preds = %16
%20 = load i64, i64* @errno
%21 = load i64, i64* @EAGAIN
%22 = icmp eq i64 %20, %21
br i1 %22, label %23, label %25
23: ; preds = %19
%24 = call i32 @usleep(i32 250)
br label %6
25: ; preds = %19
store i32 -1, i32* %2
br label %31
26: ; preds = %16
br label %27
27: ; preds = %26
br label %6
28: ; preds = %11
%29 = load i8, i8* %4
%30 = sext i8 %29 to i32
store i32 %30, i32* %2
br label %31
31: ; preds = %28, %25, %15
%32 = load i32, i32* %2
ret i32 %32
}

LLVM IR (-O2):
@errno = global i64 0
@EAGAIN = global i64 0
define i32 @isfd_read(i32 %0) {
%2 = alloca i8
br label %3
3: ; preds = %11, %1
%4 = call i32 @read(i32 %0, i8* %2, i32 1)
switch i32 %4, label %11 [
i32 1, label %12
i32 0, label %15
i32 -1, label %5
]
5: ; preds = %3
%6 = load i64, i64* @errno
%7 = load i64, i64* @EAGAIN
%8 = icmp eq i64 %6, %7
br i1 %8, label %9, label %15
9: ; preds = %5
%10 = tail call i32 @usleep(i32 250)
br label %11
11: ; preds = %9, %3
br label %3
12: ; preds = %3
%13 = load i8, i8* %2
%14 = sext i8 %13 to i32
br label %15
15: ; preds = %5, %3, %12
%16 = phi i32 [ %14, %12 ], [ -1, %3 ], [ -1, %5 ]
ret i32 %16
}