#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 stat {
  int st_size;
};
typedef int off_t;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ SEEK_CUR;
int /*<<< orphan*/ SEEK_SET;
int fstat(int /*<<< orphan*/, struct stat *);
int lseek(int /*<<< orphan*/, int, int /*<<< orphan*/);
int read(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ sleep(int);
scalar_t__ write(int, char *, int);
int keep_reading(void) {
  static char buf[1024];
  int n;
  int i;
  off_t pos;
  struct stat st;
  pos = lseek(0, (off_t)0, SEEK_CUR);
  for (;;) {
    for (i = 0; i < 60; i++) {
      while ((n = read(0, buf, sizeof(buf))) > 0) {
        if (write(1, buf, n) < 0)
          return 1;
      }
      if (n < 0)
        return 1;
      sleep(1);
    }
    if (pos != -1) {
      if (fstat(0, &st) == -1) {
        pos = -1;
      } else if (st.st_size < pos) {
        pos = lseek(0, (off_t)0, SEEK_SET);
      } else {
        pos = st.st_size;
      }
    }
  }
}


x86 assembly (clang, -O0):
<keep_reading>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    0x2e89(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
mov    %esi,%edi
call   1040 <lseek@plt>
mov    %eax,-0x10(%rbp)
movl   $0x0,-0xc(%rbp)
cmpl   $0x3c,-0xc(%rbp)
jge    11ff <keep_reading+0xaf>
jmp    1183 <keep_reading+0x33>
xor    %edi,%edi
lea    0x2ed4(%rip),%rsi
mov    $0x400,%edx
call   1050 <read@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jle    11d1 <keep_reading+0x81>
mov    -0x8(%rbp),%edx
mov    $0x1,%edi
lea    0x2eaf(%rip),%rsi
call   1030 <write@plt>
cmp    $0x0,%rax
jge    11cc <keep_reading+0x7c>
movl   $0x1,-0x4(%rbp)
jmp    1264 <keep_reading+0x114>
jmp    1183 <keep_reading+0x33>
cmpl   $0x0,-0x8(%rbp)
jge    11e7 <keep_reading+0x97>
movl   $0x1,-0x4(%rbp)
jmp    1264 <keep_reading+0x114>
mov    $0x1,%edi
call   1060 <sleep@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1174 <keep_reading+0x24>
cmpl   $0xffffffff,-0x10(%rbp)
je     125f <keep_reading+0x10f>
xor    %edi,%edi
lea    -0x18(%rbp),%rsi
call   1070 <fstat@plt>
cmp    $0xffffffff,%eax
jne    1229 <keep_reading+0xd9>
movl   $0xffffffff,-0x10(%rbp)
jmp    125a <keep_reading+0x10a>
mov    -0x18(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    124f <keep_reading+0xff>
mov    0x2d9c(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
mov    %esi,%edi
call   1040 <lseek@plt>
mov    %eax,-0x10(%rbp)
jmp    1255 <keep_reading+0x105>
mov    -0x18(%rbp),%eax
mov    %eax,-0x10(%rbp)
jmp    125a <keep_reading+0x10a>
jmp    125f <keep_reading+0x10f>
jmp    116d <keep_reading+0x1d>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<keep_reading>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    0x2e85(%rip),%rax
mov    (%rax),%edx
xor    %edi,%edi
xor    %esi,%esi
call   1040 <lseek@plt>
mov    %eax,%ebp
lea    0x2ee9(%rip),%rbx
lea    0x8(%rsp),%r14
mov    0x2e55(%rip),%r15
mov    %ebp,%r12d
xor    %ebp,%ebp
nopl   0x0(%rax,%rax,1)
xor    %edi,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1050 <read@plt>
test   %eax,%eax
jle    11b9 <keep_reading+0x69>
mov    $0x1,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
test   %rax,%rax
jns    1190 <keep_reading+0x40>
jmp    1200 <keep_reading+0xb0>
js     1200 <keep_reading+0xb0>
mov    $0x1,%edi
call   1060 <sleep@plt>
add    $0x1,%ebp
cmp    $0x3c,%ebp
jne    1190 <keep_reading+0x40>
mov    $0xffffffff,%ebp
cmp    $0xffffffff,%r12d
je     1183 <keep_reading+0x33>
xor    %edi,%edi
mov    %r14,%rsi
call   1070 <fstat@plt>
cmp    $0xffffffff,%eax
je     1183 <keep_reading+0x33>
mov    0x8(%rsp),%ebp
cmp    %r12d,%ebp
jge    1183 <keep_reading+0x33>
mov    (%r15),%edx
xor    %edi,%edi
xor    %esi,%esi
call   1040 <lseek@plt>
mov    %eax,%ebp
jmp    1183 <keep_reading+0x33>
mov    $0x1,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@keep_reading.buf = internal global [1024 x i8] zeroinitializer
@SEEK_CUR = global i32 0
@SEEK_SET = global i32 0
define i32 @keep_reading() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.stat
%6 = load i32, i32* @SEEK_CUR
%7 = call i32 @lseek(i32 0, i32 0, i32 %6)
store i32 %7, i32* %4
br label %8
8: ; preds = %51, %0
store i32 0, i32* %3
br label %9
9: ; preds = %28, %8
%10 = load i32, i32* %3
%11 = icmp slt i32 %10, 60
br i1 %11, label %12, label %31
12: ; preds = %9
br label %13
13: ; preds = %21, %12
%14 = call i32 @read(i32 0, i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @keep_reading.buf, i64 0, i64 0), i32 1024)
store i32 %14, i32* %2
%15 = icmp sgt i32 %14, 0
br i1 %15, label %16, label %22
16: ; preds = %13
%17 = load i32, i32* %2
%18 = call i64 @write(i32 1, i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @keep_reading.buf, i64 0, i64 0), i32 %17)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %16
store i32 1, i32* %1
br label %52
21: ; preds = %16
br label %13
22: ; preds = %13
%23 = load i32, i32* %2
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %22
store i32 1, i32* %1
br label %52
26: ; preds = %22
%27 = call i32 @sleep(i32 1)
br label %28
28: ; preds = %26
%29 = load i32, i32* %3
%30 = add nsw i32 %29, 1
store i32 %30, i32* %3
br label %9
31: ; preds = %9
%32 = load i32, i32* %4
%33 = icmp ne i32 %32, -1
br i1 %33, label %34, label %51
34: ; preds = %31
%35 = call i32 @fstat(i32 0, %struct.stat* %5)
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %38
37: ; preds = %34
store i32 -1, i32* %4
br label %50
38: ; preds = %34
%39 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%40 = load i32, i32* %39
%41 = load i32, i32* %4
%42 = icmp slt i32 %40, %41
br i1 %42, label %43, label %46
43: ; preds = %38
%44 = load i32, i32* @SEEK_SET
%45 = call i32 @lseek(i32 0, i32 0, i32 %44)
store i32 %45, i32* %4
br label %49
46: ; preds = %38
%47 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%48 = load i32, i32* %47
store i32 %48, i32* %4
br label %49
49: ; preds = %46, %43
br label %50
50: ; preds = %49, %37
br label %51
51: ; preds = %50, %31
br label %8
52: ; preds = %25, %20
%53 = load i32, i32* %1
ret i32 %53
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@keep_reading.buf = internal global [1024 x i8] zeroinitializer
@SEEK_CUR = global i32 0
@SEEK_SET = global i32 0
define i32 @keep_reading() {
%1 = alloca %struct.stat
%2 = bitcast %struct.stat* %1 to i8*
%3 = load i32, i32* @SEEK_CUR
%4 = tail call i32 @lseek(i32 0, i32 0, i32 %3)
%5 = getelementptr inbounds %struct.stat, %struct.stat* %1, i64 0, i32 0
br label %6
6: ; preds = %30, %0
%7 = phi i32 [ %4, %0 ], [ %31, %30 ]
br label %8
8: ; preds = %6, %18
%9 = phi i32 [ 0, %6 ], [ %20, %18 ]
br label %10
10: ; preds = %8, %13
%11 = tail call i32 @read(i32 0, i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @keep_reading.buf, i64 0, i64 0), i32 1024)
%12 = icmp sgt i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %10
%14 = tail call i64 @write(i32 1, i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @keep_reading.buf, i64 0, i64 0), i32 %11)
%15 = icmp slt i64 %14, 0
br i1 %15, label %35, label %10
16: ; preds = %10
%17 = icmp slt i32 %11, 0
br i1 %17, label %35, label %18
18: ; preds = %16
%19 = tail call i32 @sleep(i32 1)
%20 = add nuw nsw i32 %9, 1
%21 = icmp eq i32 %20, 60
br i1 %21, label %22, label %8
22: ; preds = %18
%23 = icmp eq i32 %7, -1
br i1 %23, label %30, label %24
24: ; preds = %22
%25 = call i32 @fstat(i32 0, %struct.stat* %1)
%26 = icmp eq i32 %25, -1
br i1 %26, label %30, label %27
27: ; preds = %24
%28 = load i32, i32* %5
%29 = icmp slt i32 %28, %7
br i1 %29, label %32, label %30
30: ; preds = %27, %24, %32, %22
%31 = phi i32 [ %34, %32 ], [ -1, %22 ], [ -1, %24 ], [ %28, %27 ]
br label %6
32: ; preds = %27
%33 = load i32, i32* @SEEK_SET
%34 = tail call i32 @lseek(i32 0, i32 0, i32 %33)
br label %30
35: ; preds = %16, %13
ret i32 1
}