#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) {
  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    $0x420,%rsp
mov    0x2e86(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
mov    %esi,%edi
call   1040 <lseek@plt>
mov    %eax,-0x41c(%rbp)
movl   $0x0,-0x418(%rbp)
cmpl   $0x3c,-0x418(%rbp)
jge    121a <keep_reading+0xca>
jmp    118f <keep_reading+0x3f>
lea    -0x410(%rbp),%rsi
xor    %edi,%edi
mov    $0x400,%edx
call   1050 <read@plt>
mov    %eax,-0x414(%rbp)
cmp    $0x0,%eax
jle    11e3 <keep_reading+0x93>
lea    -0x410(%rbp),%rsi
mov    -0x414(%rbp),%edx
mov    $0x1,%edi
call   1030 <write@plt>
cmp    $0x0,%rax
jge    11de <keep_reading+0x8e>
movl   $0x1,-0x4(%rbp)
jmp    1297 <keep_reading+0x147>
jmp    118f <keep_reading+0x3f>
cmpl   $0x0,-0x414(%rbp)
jge    11fc <keep_reading+0xac>
movl   $0x1,-0x4(%rbp)
jmp    1297 <keep_reading+0x147>
mov    $0x1,%edi
call   1060 <sleep@plt>
mov    -0x418(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x418(%rbp)
jmp    117d <keep_reading+0x2d>
cmpl   $0xffffffff,-0x41c(%rbp)
je     1292 <keep_reading+0x142>
xor    %edi,%edi
lea    -0x420(%rbp),%rsi
call   1070 <fstat@plt>
cmp    $0xffffffff,%eax
jne    124d <keep_reading+0xfd>
movl   $0xffffffff,-0x41c(%rbp)
ff ff ff
jmp    128d <keep_reading+0x13d>
mov    -0x420(%rbp),%eax
cmp    -0x41c(%rbp),%eax
jge    127c <keep_reading+0x12c>
mov    0x2d72(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
mov    %esi,%edi
call   1040 <lseek@plt>
mov    %eax,-0x41c(%rbp)
jmp    1288 <keep_reading+0x138>
mov    -0x420(%rbp),%eax
mov    %eax,-0x41c(%rbp)
jmp    128d <keep_reading+0x13d>
jmp    1292 <keep_reading+0x142>
jmp    1173 <keep_reading+0x23>
mov    -0x4(%rbp),%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<keep_reading>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x410,%rsp
mov    0x2e82(%rip),%rax
mov    (%rax),%edx
xor    %edi,%edi
xor    %esi,%esi
call   1040 <lseek@plt>
mov    %eax,%ebp
lea    0x10(%rsp),%rbx
lea    0x8(%rsp),%r14
mov    0x2e54(%rip),%r15
mov    %ebp,%r12d
xor    %ebp,%ebp
nopl   0x0(%rax)
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     1184 <keep_reading+0x34>
xor    %edi,%edi
mov    %r14,%rsi
call   1070 <fstat@plt>
cmp    $0xffffffff,%eax
je     1184 <keep_reading+0x34>
mov    0x8(%rsp),%ebp
cmp    %r12d,%ebp
jge    1184 <keep_reading+0x34>
mov    (%r15),%edx
xor    %edi,%edi
xor    %esi,%esi
call   1040 <lseek@plt>
mov    %eax,%ebp
jmp    1184 <keep_reading+0x34>
mov    $0x1,%eax
add    $0x410,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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