#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 timespec {
  int tv_nsec;
  scalar_t__ tv_sec;
};
typedef int ssize_t;

/* Variables and functions */
scalar_t__ EBADF;
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ nanosleep(struct timespec *, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, char *);
int read(int, char *, int);
int /*<<< orphan*/ stderr;
char *strerror(scalar_t__);
int main(void) {
  int i;
  ssize_t n;
  char buf[20];
  struct timespec ts;
  for (i = 0; i < 10; i++) {
    n = read((10), buf, sizeof buf);
    if (n >= 0)
      break;
    if (errno != EBADF) {
      fprintf(stderr, "BUG: read: %s\n", strerror(errno));
      return 2;
    }
    ts.tv_sec = 0;
    ts.tv_nsec = 1000000;
    nanosleep(&ts, NULL);
  }
  if (n < 0) {
    fprintf(stderr, "BUG: failed to read any data from pipe\n");
    return 2;
  }
  if (n == 0) {
    fprintf(stderr, "BUG: unexpected EOF\n");
    return 2;
  }
  if (n == sizeof buf) {
    n--;
  }
  buf[n] = '\0';
  printf("%s\n", buf);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
cmpl   $0xa,-0x8(%rbp)
jge    1214 <main+0xc4>
lea    -0x20(%rbp),%rsi
mov    $0xa,%edi
mov    $0x14,%edx
call   1050 <read@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jl     1195 <main+0x45>
jmp    1214 <main+0xc4>
mov    0x2e3c(%rip),%rax
mov    (%rax),%rax
mov    0x2e2a(%rip),%rcx
cmp    (%rcx),%rax
je     11ea <main+0x9a>
mov    0x2e2a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    0x2e16(%rip),%rax
mov    (%rax),%rdi
call   1070 <strerror@plt>
mov    -0x34(%rbp),%edi
mov    %rax,%rdx
lea    0xe29(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x2,-0x4(%rbp)
jmp    12a5 <main+0x155>
movq   $0x0,-0x28(%rbp)
movl   $0xf4240,-0x30(%rbp)
lea    -0x30(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <nanosleep@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1166 <main+0x16>
cmpl   $0x0,-0xc(%rbp)
jge    1241 <main+0xf1>
mov    0x2dbb(%rip),%rax
mov    (%rax),%edi
lea    0xde1(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x2,-0x4(%rbp)
jmp    12a5 <main+0x155>
cmpl   $0x0,-0xc(%rbp)
jne    126e <main+0x11e>
mov    0x2d8e(%rip),%rax
mov    (%rax),%edi
lea    0xddc(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x2,-0x4(%rbp)
jmp    12a5 <main+0x155>
movslq -0xc(%rbp),%rax
cmp    $0x14,%rax
jne    1285 <main+0x135>
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
movslq -0xc(%rbp),%rax
movb   $0x0,-0x20(%rbp,%rax,1)
lea    -0x20(%rbp),%rsi
lea    0xd72(%rip),%rdi
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x30,%rsp
mov    $0xa,%ebp
lea    0x10(%rsp),%r14
mov    0x2e6b(%rip),%r15
mov    0x2e5c(%rip),%r12
mov    %rsp,%rbx
nopw   0x0(%rax,%rax,1)
mov    $0xa,%edi
mov    %r14,%rsi
mov    $0x14,%edx
call   1050 <read@plt>
test   %eax,%eax
jns    11d0 <main+0x80>
mov    (%r15),%rdi
cmp    (%r12),%rdi
jne    11f7 <main+0xa7>
movq   $0x0,0x8(%rsp)
movl   $0xf4240,(%rsp)
mov    %rbx,%rdi
xor    %esi,%esi
call   1040 <nanosleep@plt>
add    $0xffffffff,%ebp
jne    1180 <main+0x30>
mov    0x2e1b(%rip),%rax
mov    (%rax),%edi
lea    0xe41(%rip),%rsi
jmp    11e9 <main+0x99>
cmp    $0x14,%eax
je     121f <main+0xcf>
test   %eax,%eax
jne    1224 <main+0xd4>
mov    0x2e00(%rip),%rax
mov    (%rax),%edi
lea    0xe4e(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x2,%eax
jmp    123e <main+0xee>
mov    0x2de2(%rip),%rax
mov    (%rax),%ebx
call   1070 <strerror@plt>
lea    0xdf4(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x2,%eax
jmp    123e <main+0xee>
mov    $0x13,%eax
mov    %eax,%eax
movb   $0x0,0x10(%rsp,%rax,1)
lea    0xdd9(%rip),%rdi
lea    0x10(%rsp),%rsi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timespec = type { i32, i64 }
@errno = global i64 0
@EBADF = global i64 0
@stderr = global i32 0
@.str = [15 x i8] c"BUG: read: %s\0A\00"
@.str.1 = [40 x i8] c"BUG: failed to read any data from pipe\0A\00"
@.str.2 = [21 x i8] c"BUG: unexpected EOF\0A\00"
@.str.3 = [4 x i8] c"%s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca [20 x i8]
%5 = alloca %struct.timespec
store i32 0, i32* %1
store i32 0, i32* %2
br label %6
6: ; preds = %28, %0
%7 = load i32, i32* %2
%8 = icmp slt i32 %7, 10
br i1 %8, label %9, label %31
9: ; preds = %6
%10 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 0
%11 = call i32 @read(i32 10, i8* %10, i32 20)
store i32 %11, i32* %3
%12 = load i32, i32* %3
%13 = icmp sge i32 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %9
br label %31
15: ; preds = %9
%16 = load i64, i64* @errno
%17 = load i64, i64* @EBADF
%18 = icmp ne i64 %16, %17
br i1 %18, label %19, label %24
19: ; preds = %15
%20 = load i32, i32* @stderr
%21 = load i64, i64* @errno
%22 = call i8* @strerror(i64 %21)
%23 = call i32 (i32, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %22)
store i32 2, i32* %1
br label %56
24: ; preds = %15
%25 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 1
store i64 0, i64* %25
%26 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 0
store i32 1000000, i32* %26
%27 = call i32 @nanosleep(%struct.timespec* %5, i32* null)
br label %28
28: ; preds = %24
%29 = load i32, i32* %2
%30 = add nsw i32 %29, 1
store i32 %30, i32* %2
br label %6
31: ; preds = %14, %6
%32 = load i32, i32* %3
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %31
%35 = load i32, i32* @stderr
%36 = call i32 (i32, i8*, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0))
store i32 2, i32* %1
br label %56
37: ; preds = %31
%38 = load i32, i32* %3
%39 = icmp eq i32 %38, 0
br i1 %39, label %40, label %43
40: ; preds = %37
%41 = load i32, i32* @stderr
%42 = call i32 (i32, i8*, ...) @fprintf(i32 %41, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
store i32 2, i32* %1
br label %56
43: ; preds = %37
%44 = load i32, i32* %3
%45 = sext i32 %44 to i64
%46 = icmp eq i64 %45, 20
br i1 %46, label %47, label %50
47: ; preds = %43
%48 = load i32, i32* %3
%49 = add nsw i32 %48, -1
store i32 %49, i32* %3
br label %50
50: ; preds = %47, %43
%51 = load i32, i32* %3
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 %52
store i8 0, i8* %53
%54 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 0
%55 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %54)
store i32 0, i32* %1
br label %56
56: ; preds = %50, %40, %34, %19
%57 = load i32, i32* %1
ret i32 %57
}

LLVM IR (-O2):
%struct.timespec = type { i32, i64 }
@errno = global i64 0
@EBADF = global i64 0
@stderr = global i32 0
@.str = [15 x i8] c"BUG: read: %s\0A\00"
@.str.1 = [40 x i8] c"BUG: failed to read any data from pipe\0A\00"
@.str.2 = [21 x i8] c"BUG: unexpected EOF\0A\00"
@.str.3 = [4 x i8] c"%s\0A\00"
define i32 @main() {
%1 = alloca [20 x i8]
%2 = alloca %struct.timespec
%3 = getelementptr inbounds [20 x i8], [20 x i8]* %1, i64 0, i64 0
%4 = bitcast %struct.timespec* %2 to i8*
%5 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 1
%6 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
br label %7
7: ; preds = %0, %19
%8 = phi i32 [ 0, %0 ], [ %21, %19 ]
%9 = call i32 @read(i32 10, i8* %3, i32 20)
%10 = icmp sgt i32 %9, -1
br i1 %10, label %26, label %11
11: ; preds = %7
%12 = load i64, i64* @errno
%13 = load i64, i64* @EBADF
%14 = icmp eq i64 %12, %13
br i1 %14, label %19, label %15
15: ; preds = %11
%16 = load i32, i32* @stderr
%17 = call i8* @strerror(i64 %12)
%18 = call i32 (i32, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %17)
br label %36
19: ; preds = %11
store i64 0, i64* %5
store i32 1000000, i32* %6
%20 = call i32 @nanosleep(%struct.timespec* %2, i32* null)
%21 = add nuw nsw i32 %8, 1
%22 = icmp eq i32 %21, 10
br i1 %22, label %23, label %7
23: ; preds = %19
%24 = load i32, i32* @stderr
%25 = call i32 (i32, i8*, ...) @fprintf(i32 %24, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0))
br label %36
26: ; preds = %7
switch i32 %9, label %31 [
i32 0, label %27
i32 20, label %30
]
27: ; preds = %26
%28 = load i32, i32* @stderr
%29 = call i32 (i32, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
br label %36
30: ; preds = %26
br label %31
31: ; preds = %26, %30
%32 = phi i32 [ 19, %30 ], [ %9, %26 ]
%33 = zext i32 %32 to i64
%34 = getelementptr inbounds [20 x i8], [20 x i8]* %1, i64 0, i64 %33
store i8 0, i8* %34
%35 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %3)
br label %36
36: ; preds = %31, %27, %23, %15
%37 = phi i32 [ 2, %23 ], [ 2, %27 ], [ 0, %31 ], [ 2, %15 ]
ret i32 %37
}