#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__ EINVAL;
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 < 100; i++) {
    n = read((100), buf, sizeof buf);
    if (n >= 0)
      break;
    if (errno != EBADF && errno != EINVAL) {
      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   $0x64,-0x8(%rbp)
jge    122e <main+0xde>
lea    -0x20(%rbp),%rsi
mov    $0x64,%edi
mov    $0x14,%edx
call   1050 <read@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jl     1195 <main+0x45>
jmp    122e <main+0xde>
mov    0x2e3c(%rip),%rax
mov    (%rax),%rax
mov    0x2e22(%rip),%rcx
cmp    (%rcx),%rax
je     1204 <main+0xb4>
mov    0x2e22(%rip),%rax
mov    (%rax),%rax
mov    0x2e10(%rip),%rcx
cmp    (%rcx),%rax
je     1204 <main+0xb4>
mov    0x2e10(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    0x2dfc(%rip),%rax
mov    (%rax),%rdi
call   1070 <strerror@plt>
mov    -0x34(%rbp),%edi
mov    %rax,%rdx
lea    0xe0f(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x2,-0x4(%rbp)
jmp    12bf <main+0x16f>
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    125b <main+0x10b>
mov    0x2da1(%rip),%rax
mov    (%rax),%edi
lea    0xdc7(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x2,-0x4(%rbp)
jmp    12bf <main+0x16f>
cmpl   $0x0,-0xc(%rbp)
jne    1288 <main+0x138>
mov    0x2d74(%rip),%rax
mov    (%rax),%edi
lea    0xdc2(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x2,-0x4(%rbp)
jmp    12bf <main+0x16f>
movslq -0xc(%rbp),%rax
cmp    $0x14,%rax
jne    129f <main+0x14f>
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    0xd58(%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   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    $0x64,%ebp
lea    0x10(%rsp),%r14
mov    0x2e69(%rip),%r15
mov    0x2e52(%rip),%r12
mov    0x2e53(%rip),%r13
mov    %rsp,%rbx
jmp    11af <main+0x5f>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movq   $0x0,0x8(%rsp)
movl   $0xf4240,(%rsp)
mov    %rbx,%rdi
xor    %esi,%esi
call   1040 <nanosleep@plt>
add    $0xffffffff,%ebp
je     1217 <main+0xc7>
mov    $0x64,%edi
mov    %r14,%rsi
mov    $0x14,%edx
call   1050 <read@plt>
test   %eax,%eax
jns    11fc <main+0xac>
mov    (%r15),%rdi
cmp    (%r12),%rdi
je     1190 <main+0x40>
cmp    0x0(%r13),%rdi
je     1190 <main+0x40>
mov    0x2e05(%rip),%rax
mov    (%rax),%ebx
call   1070 <strerror@plt>
lea    0xe17(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x2,%eax
jmp    1254 <main+0x104>
cmp    $0x14,%eax
je     1235 <main+0xe5>
test   %eax,%eax
jne    123a <main+0xea>
mov    0x2dd4(%rip),%rax
mov    (%rax),%edi
lea    0xe22(%rip),%rsi
jmp    1227 <main+0xd7>
mov    0x2dc2(%rip),%rax
mov    (%rax),%edi
lea    0xde8(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x2,%eax
jmp    1254 <main+0x104>
mov    $0x13,%eax
mov    %eax,%eax
movb   $0x0,0x10(%rsp,%rax,1)
lea    0xdc3(%rip),%rdi
lea    0x10(%rsp),%rsi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timespec = type { i32, i64 }
@errno = global i64 0
@EBADF = global i64 0
@EINVAL = 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 = %32, %0
%7 = load i32, i32* %2
%8 = icmp slt i32 %7, 100
br i1 %8, label %9, label %35
9: ; preds = %6
%10 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 0
%11 = call i32 @read(i32 100, 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 %35
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 %28
19: ; preds = %15
%20 = load i64, i64* @errno
%21 = load i64, i64* @EINVAL
%22 = icmp ne i64 %20, %21
br i1 %22, label %23, label %28
23: ; preds = %19
%24 = load i32, i32* @stderr
%25 = load i64, i64* @errno
%26 = call i8* @strerror(i64 %25)
%27 = call i32 (i32, i8*, ...) @fprintf(i32 %24, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %26)
store i32 2, i32* %1
br label %60
28: ; preds = %19, %15
%29 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 1
store i64 0, i64* %29
%30 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 0
store i32 1000000, i32* %30
%31 = call i32 @nanosleep(%struct.timespec* %5, i32* null)
br label %32
32: ; preds = %28
%33 = load i32, i32* %2
%34 = add nsw i32 %33, 1
store i32 %34, i32* %2
br label %6
35: ; preds = %14, %6
%36 = load i32, i32* %3
%37 = icmp slt i32 %36, 0
br i1 %37, label %38, label %41
38: ; preds = %35
%39 = load i32, i32* @stderr
%40 = call i32 (i32, i8*, ...) @fprintf(i32 %39, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0))
store i32 2, i32* %1
br label %60
41: ; preds = %35
%42 = load i32, i32* %3
%43 = icmp eq i32 %42, 0
br i1 %43, label %44, label %47
44: ; preds = %41
%45 = load i32, i32* @stderr
%46 = call i32 (i32, i8*, ...) @fprintf(i32 %45, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
store i32 2, i32* %1
br label %60
47: ; preds = %41
%48 = load i32, i32* %3
%49 = sext i32 %48 to i64
%50 = icmp eq i64 %49, 20
br i1 %50, label %51, label %54
51: ; preds = %47
%52 = load i32, i32* %3
%53 = add nsw i32 %52, -1
store i32 %53, i32* %3
br label %54
54: ; preds = %51, %47
%55 = load i32, i32* %3
%56 = sext i32 %55 to i64
%57 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 %56
store i8 0, i8* %57
%58 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 0
%59 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %58)
store i32 0, i32* %1
br label %60
60: ; preds = %54, %44, %38, %23
%61 = load i32, i32* %1
ret i32 %61
}

LLVM IR (-O2):
%struct.timespec = type { i32, i64 }
@errno = global i64 0
@EBADF = global i64 0
@EINVAL = 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, %22
%8 = phi i32 [ 0, %0 ], [ %24, %22 ]
%9 = call i32 @read(i32 100, i8* %3, i32 20)
%10 = icmp sgt i32 %9, -1
br i1 %10, label %29, label %11
11: ; preds = %7
%12 = load i64, i64* @errno
%13 = load i64, i64* @EBADF
%14 = icmp eq i64 %12, %13
%15 = load i64, i64* @EINVAL
%16 = icmp eq i64 %12, %15
%17 = select i1 %14, i1 true, i1 %16
br i1 %17, label %22, label %18
18: ; preds = %11
%19 = load i32, i32* @stderr
%20 = call i8* @strerror(i64 %12)
%21 = call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %20)
br label %39
22: ; preds = %11
store i64 0, i64* %5
store i32 1000000, i32* %6
%23 = call i32 @nanosleep(%struct.timespec* %2, i32* null)
%24 = add nuw nsw i32 %8, 1
%25 = icmp eq i32 %24, 100
br i1 %25, label %26, label %7
26: ; preds = %22
%27 = load i32, i32* @stderr
%28 = call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0))
br label %39
29: ; preds = %7
switch i32 %9, label %34 [
i32 0, label %30
i32 20, label %33
]
30: ; preds = %29
%31 = load i32, i32* @stderr
%32 = call i32 (i32, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
br label %39
33: ; preds = %29
br label %34
34: ; preds = %29, %33
%35 = phi i32 [ 19, %33 ], [ %9, %29 ]
%36 = zext i32 %35 to i64
%37 = getelementptr inbounds [20 x i8], [20 x i8]* %1, i64 0, i64 %36
store i8 0, i8* %37
%38 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %3)
br label %39
39: ; preds = %34, %30, %26, %18
%40 = phi i32 [ 2, %26 ], [ 2, %30 ], [ 0, %34 ], [ 2, %18 ]
ret i32 %40
}