#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 {
  scalar_t__ st_dev;
  scalar_t__ st_ino;
  size_t st_size;
};
typedef int /*<<< orphan*/ ssize_t;
typedef size_t off_t;
typedef int /*<<< orphan*/ buf;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
int _tail_error(char const *, int);
int fileno(int /*<<< orphan*/ *);
int /*<<< orphan*/ fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, size_t, int /*<<< orphan*/);
scalar_t__ fstat(int, struct stat *);
int /*<<< orphan*/ fwrite(char *, int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stdout;
int _tail_wait(FILE *fp, char const *filename) {
  int fd = fileno(fp);
  struct stat st1;
  struct stat st2;
  off_t seek;
  size_t size;
  size_t i;
  char buf[1024];
  ssize_t ssize;
  if (fstat(fd, &st1) != 0)
    return -_tail_error(filename, 1);
  for (;;) {
    sleep(1);
    if (fstat(fd, &st2) != 0)
      return -_tail_error(filename, 1);
    if (st1.st_dev != st2.st_dev || st1.st_ino != st2.st_ino) {
      seek = 0;
      size = st2.st_size;
    } else if (st2.st_size > st1.st_size) {
      seek = st1.st_size;
      size = st2.st_size - st1.st_size;
    } else
      continue;
    fseek(fp, seek, SEEK_SET);
    for (i = 0; i < size; i += sizeof(buf))
      if ((ssize = fread(buf, sizeof(*buf), sizeof(buf), fp)) > 0)
        fwrite(buf, sizeof(*buf), ssize, stdout);
    st1 = st2;
  }
  return 0;
}


x86 assembly (clang, -O0):
<_tail_wait>:
push   %rbp
mov    %rsp,%rbp
sub    $0x480,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <fileno@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x38(%rbp),%rsi
call   1080 <fstat@plt>
cmp    $0x0,%rax
je     11c1 <_tail_wait+0x51>
mov    -0x18(%rbp),%rdi
mov    $0x1,%esi
call   1090 <_tail_error@plt>
mov    %eax,%ecx
xor    %eax,%eax
sub    %ecx,%eax
mov    %eax,-0x4(%rbp)
jmp    130e <_tail_wait+0x19e>
jmp    11c6 <_tail_wait+0x56>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    -0x1c(%rbp),%edi
lea    -0x50(%rbp),%rsi
call   1080 <fstat@plt>
cmp    $0x0,%rax
je     1202 <_tail_wait+0x92>
mov    -0x18(%rbp),%rdi
mov    $0x1,%esi
call   1090 <_tail_error@plt>
mov    %eax,%ecx
xor    %eax,%eax
sub    %ecx,%eax
mov    %eax,-0x4(%rbp)
jmp    130e <_tail_wait+0x19e>
mov    -0x38(%rbp),%rax
cmp    -0x50(%rbp),%rax
jne    121e <_tail_wait+0xae>
mov    -0x30(%rbp),%rax
cmp    -0x48(%rbp),%rax
je     1233 <_tail_wait+0xc3>
movq   $0x0,-0x58(%rbp)
mov    -0x40(%rbp),%rax
mov    %rax,-0x60(%rbp)
jmp    1264 <_tail_wait+0xf4>
mov    -0x40(%rbp),%rax
cmp    -0x28(%rbp),%rax
jbe    125a <_tail_wait+0xea>
mov    -0x28(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x40(%rbp),%rax
sub    -0x28(%rbp),%rax
mov    %rax,-0x60(%rbp)
jmp    125f <_tail_wait+0xef>
jmp    11c6 <_tail_wait+0x56>
jmp    1264 <_tail_wait+0xf4>
mov    -0x10(%rbp),%rdi
mov    -0x58(%rbp),%rsi
mov    0x2d6d(%rip),%rax
mov    (%rax),%edx
call   1050 <fseek@plt>
movq   $0x0,-0x68(%rbp)
mov    -0x68(%rbp),%rax
cmp    -0x60(%rbp),%rax
jae    12f1 <_tail_wait+0x181>
lea    -0x470(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
mov    $0x400,%edx
call   1030 <fread@plt>
mov    %eax,-0x474(%rbp)
cmp    $0x0,%eax
jle    12d9 <_tail_wait+0x169>
lea    -0x470(%rbp),%rdi
mov    -0x474(%rbp),%edx
mov    0x2d0b(%rip),%rax
mov    (%rax),%ecx
mov    $0x1,%esi
call   1060 <fwrite@plt>
jmp    12de <_tail_wait+0x16e>
mov    -0x68(%rbp),%rax
add    $0x400,%rax
mov    %rax,-0x68(%rbp)
jmp    1282 <_tail_wait+0x112>
mov    -0x50(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x48(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x40(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    11c6 <_tail_wait+0x56>
mov    -0x4(%rbp),%eax
add    $0x480,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_tail_wait>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x448,%rsp
mov    %rsi,%rbp
mov    %rdi,%rbx
call   1040 <fileno@plt>
mov    %eax,%r15d
lea    0x20(%rsp),%rsi
mov    %eax,%edi
call   1080 <fstat@plt>
test   %rax,%rax
je     11a5 <_tail_wait+0x35>
mov    %rbp,%rdi
jmp    11cb <_tail_wait+0x5b>
mov    %rbp,0x38(%rsp)
mov    $0x1,%edi
call   1070 <sleep@plt>
lea    0x8(%rsp),%rsi
mov    %r15d,%edi
call   1080 <fstat@plt>
test   %rax,%rax
je     11e9 <_tail_wait+0x79>
mov    0x38(%rsp),%rdi
mov    $0x1,%esi
call   1090 <_tail_error@plt>
neg    %eax
add    $0x448,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0x40(%rsp),%rbp
mov    0x2de3(%rip),%r14
jmp    1230 <_tail_wait+0xc0>
nopw   0x0(%rax,%rax,1)
mov    0x18(%rsp),%rax
mov    %rax,0x30(%rsp)
movups 0x8(%rsp),%xmm0
movaps %xmm0,0x20(%rsp)
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    %r15d,%edi
lea    0x8(%rsp),%rsi
call   1080 <fstat@plt>
test   %rax,%rax
jne    11c6 <_tail_wait+0x56>
mov    0x20(%rsp),%rax
cmp    0x8(%rsp),%rax
jne    1260 <_tail_wait+0xf0>
mov    0x28(%rsp),%rax
cmp    0x10(%rsp),%rax
jne    1260 <_tail_wait+0xf0>
mov    0x18(%rsp),%r13
mov    0x30(%rsp),%rsi
sub    %rsi,%r13
jbe    1214 <_tail_wait+0xa4>
jmp    1267 <_tail_wait+0xf7>
nopl   0x0(%rax)
mov    0x18(%rsp),%r13
xor    %esi,%esi
mov    0x2d72(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
call   1050 <fseek@plt>
test   %r13,%r13
je     1200 <_tail_wait+0x90>
xor    %r12d,%r12d
jmp    12a0 <_tail_wait+0x130>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
add    $0x400,%r12
cmp    %r13,%r12
jae    1200 <_tail_wait+0x90>
mov    %rbp,%rdi
mov    $0x1,%esi
mov    $0x400,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
test   %eax,%eax
jle    1290 <_tail_wait+0x120>
mov    (%r14),%ecx
mov    %rbp,%rdi
mov    $0x1,%esi
mov    %eax,%edx
call   1060 <fwrite@plt>
jmp    1290 <_tail_wait+0x120>

LLVM IR (-O0):
%struct.stat = type { i64, i64, i64 }
@SEEK_SET = global i32 0
@stdout = global i32 0
define i32 @_tail_wait(i32* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca %struct.stat
%8 = alloca %struct.stat
%9 = alloca i64
%10 = alloca i64
%11 = alloca i64
%12 = alloca [1024 x i8]
%13 = alloca i32
store i32* %0, i32** %4
store i8* %1, i8** %5
%14 = load i32*, i32** %4
%15 = call i32 @fileno(i32* %14)
store i32 %15, i32* %6
%16 = load i32, i32* %6
%17 = call i64 @fstat(i32 %16, %struct.stat* %7)
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %23
19: ; preds = %2
%20 = load i8*, i8** %5
%21 = call i32 @_tail_error(i8* %20, i32 1)
%22 = sub nsw i32 0, %21
store i32 %22, i32* %3
br label %90
23: ; preds = %2
br label %24
24: ; preds = %87, %62, %23
%25 = call i32 @sleep(i32 1)
%26 = load i32, i32* %6
%27 = call i64 @fstat(i32 %26, %struct.stat* %8)
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %33
29: ; preds = %24
%30 = load i8*, i8** %5
%31 = call i32 @_tail_error(i8* %30, i32 1)
%32 = sub nsw i32 0, %31
store i32 %32, i32* %3
br label %90
33: ; preds = %24
%34 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%35 = load i64, i64* %34
%36 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%37 = load i64, i64* %36
%38 = icmp ne i64 %35, %37
br i1 %38, label %45, label %39
39: ; preds = %33
%40 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 1
%41 = load i64, i64* %40
%42 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 1
%43 = load i64, i64* %42
%44 = icmp ne i64 %41, %43
br i1 %44, label %45, label %48
45: ; preds = %39, %33
store i64 0, i64* %9
%46 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 2
%47 = load i64, i64* %46
store i64 %47, i64* %10
br label %64
48: ; preds = %39
%49 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 2
%50 = load i64, i64* %49
%51 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 2
%52 = load i64, i64* %51
%53 = icmp ugt i64 %50, %52
br i1 %53, label %54, label %62
54: ; preds = %48
%55 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 2
%56 = load i64, i64* %55
store i64 %56, i64* %9
%57 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 2
%58 = load i64, i64* %57
%59 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 2
%60 = load i64, i64* %59
%61 = sub i64 %58, %60
store i64 %61, i64* %10
br label %63
62: ; preds = %48
br label %24
63: ; preds = %54
br label %64
64: ; preds = %63, %45
%65 = load i32*, i32** %4
%66 = load i64, i64* %9
%67 = load i32, i32* @SEEK_SET
%68 = call i32 @fseek(i32* %65, i64 %66, i32 %67)
store i64 0, i64* %11
br label %69
69: ; preds = %84, %64
%70 = load i64, i64* %11
%71 = load i64, i64* %10
%72 = icmp ult i64 %70, %71
br i1 %72, label %73, label %87
73: ; preds = %69
%74 = getelementptr inbounds [1024 x i8], [1024 x i8]* %12, i64 0, i64 0
%75 = load i32*, i32** %4
%76 = call i32 @fread(i8* %74, i32 1, i32 1024, i32* %75)
store i32 %76, i32* %13
%77 = icmp sgt i32 %76, 0
br i1 %77, label %78, label %83
78: ; preds = %73
%79 = getelementptr inbounds [1024 x i8], [1024 x i8]* %12, i64 0, i64 0
%80 = load i32, i32* %13
%81 = load i32, i32* @stdout
%82 = call i32 @fwrite(i8* %79, i32 1, i32 %80, i32 %81)
br label %83
83: ; preds = %78, %73
br label %84
84: ; preds = %83
%85 = load i64, i64* %11
%86 = add i64 %85, 1024
store i64 %86, i64* %11
br label %69
87: ; preds = %69
%88 = bitcast %struct.stat* %7 to i8*
%89 = bitcast %struct.stat* %8 to i8*
br label %24
90: ; preds = %29, %19
%91 = load i32, i32* %3
ret i32 %91
}

LLVM IR (-O2):
%struct.stat = type { i64, i64, i64 }
@SEEK_SET = global i32 0
@stdout = global i32 0
define i32 @_tail_wait(i32* nocapture %0, i8* %1) {
%3 = alloca %struct.stat
%4 = alloca %struct.stat
%5 = alloca [1024 x i8]
%6 = tail call i32 @fileno(i32* %0)
%7 = bitcast %struct.stat* %3 to i8*
%8 = bitcast %struct.stat* %4 to i8*
%9 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%10 = call i64 @fstat(i32 %6, %struct.stat* %3)
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %23
12: ; preds = %2
%13 = call i32 @sleep(i32 1)
%14 = call i64 @fstat(i32 %6, %struct.stat* %4)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %25
16: ; preds = %12
%17 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%18 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%19 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 1
%20 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 1
%21 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 2
%22 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 2
br label %27
23: ; preds = %2
%24 = tail call i32 @_tail_error(i8* %1, i32 1)
br label %64
25: ; preds = %60, %12
%26 = call i32 @_tail_error(i8* %1, i32 1)
br label %64
27: ; preds = %16, %60
%28 = load i64, i64* %17
%29 = load i64, i64* %18
%30 = icmp eq i64 %28, %29
br i1 %30, label %31, label %35
31: ; preds = %27
%32 = load i64, i64* %19
%33 = load i64, i64* %20
%34 = icmp eq i64 %32, %33
br i1 %34, label %37, label %35
35: ; preds = %31, %27
%36 = load i64, i64* %21
br label %43
37: ; preds = %31
%38 = load i64, i64* %21
%39 = load i64, i64* %22
%40 = icmp ugt i64 %38, %39
br i1 %40, label %41, label %60
41: ; preds = %37
%42 = sub i64 %38, %39
br label %43
43: ; preds = %41, %35
%44 = phi i64 [ 0, %35 ], [ %39, %41 ]
%45 = phi i64 [ %36, %35 ], [ %42, %41 ]
%46 = load i32, i32* @SEEK_SET
%47 = call i32 @fseek(i32* %0, i64 %44, i32 %46)
%48 = icmp eq i64 %45, 0
br i1 %48, label %59, label %49
49: ; preds = %43, %56
%50 = phi i64 [ %57, %56 ], [ 0, %43 ]
%51 = call i32 @fread(i8* %9, i32 1, i32 1024, i32* %0)
%52 = icmp sgt i32 %51, 0
br i1 %52, label %53, label %56
53: ; preds = %49
%54 = load i32, i32* @stdout
%55 = call i32 @fwrite(i8* %9, i32 1, i32 %51, i32 %54)
br label %56
56: ; preds = %49, %53
%57 = add i64 %50, 1024
%58 = icmp ult i64 %57, %45
br i1 %58, label %49, label %59
59: ; preds = %56, %43
br label %60
60: ; preds = %59, %37
%61 = call i32 @sleep(i32 1)
%62 = call i64 @fstat(i32 %6, %struct.stat* %4)
%63 = icmp eq i64 %62, 0
br i1 %63, label %27, label %25
64: ; preds = %25, %23
%65 = phi i32 [ %24, %23 ], [ %26, %25 ]
%66 = sub nsw i32 0, %65
ret i32 %66
}