#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 */
typedef int ssize_t;

/* Variables and functions */
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
unsigned long mstime();
int read(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ usleep(int);
int /*<<< orphan*/ write(int /*<<< orphan*/, char *, int);
void rw_loop(unsigned long rate) {
  char buf[16384];
  ssize_t n;
  unsigned long start, end, delta, adjust;
  adjust = 0;
  while (1) {
    start = mstime();
    n = read(STDIN_FILENO, buf, 16384);
    if (n == 0)
      break;
    write(STDOUT_FILENO, buf, n);
    end = mstime();
    delta = 1000 * n / rate - (end - start) + adjust;
    if (delta > 100) {
      start = mstime();
      usleep(1000 * delta);
      end = mstime();
      adjust = delta - (end - start);
    } else {
      adjust += delta;
    }
  }
}


x86 assembly (clang, -O0):
<rw_loop>:
push   %rbp
mov    %rsp,%rbp
sub    $0x4040,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x4038(%rbp)
mov    $0x0,%al
call   1050 <mstime@plt>
mov    %rax,-0x4020(%rbp)
mov    0x2e69(%rip),%rax
mov    (%rax),%edi
lea    -0x4010(%rbp),%rsi
mov    $0x4000,%edx
call   1040 <read@plt>
mov    %eax,-0x4014(%rbp)
cmpl   $0x0,-0x4014(%rbp)
jne    119a <rw_loop+0x5a>
jmp    1270 <rw_loop+0x130>
mov    0x2e4f(%rip),%rax
mov    (%rax),%edi
lea    -0x4010(%rbp),%rsi
mov    -0x4014(%rbp),%edx
call   1030 <write@plt>
mov    $0x0,%al
call   1050 <mstime@plt>
mov    %rax,-0x4028(%rbp)
imul   $0x3e8,-0x4014(%rbp),%eax
cltq
xor    %ecx,%ecx
mov    %ecx,%edx
divq   -0x8(%rbp)
mov    -0x4028(%rbp),%rcx
sub    -0x4020(%rbp),%rcx
sub    %rcx,%rax
add    -0x4038(%rbp),%rax
mov    %rax,-0x4030(%rbp)
cmpq   $0x64,-0x4030(%rbp)
64
jbe    1256 <rw_loop+0x116>
mov    $0x0,%al
call   1050 <mstime@plt>
mov    %rax,-0x4020(%rbp)
imul   $0x3e8,-0x4030(%rbp),%rax
mov    %eax,%edi
call   1060 <usleep@plt>
mov    $0x0,%al
call   1050 <mstime@plt>
mov    %rax,-0x4028(%rbp)
mov    -0x4030(%rbp),%rax
mov    -0x4028(%rbp),%rcx
sub    -0x4020(%rbp),%rcx
sub    %rcx,%rax
mov    %rax,-0x4038(%rbp)
jmp    126b <rw_loop+0x12b>
mov    -0x4030(%rbp),%rax
add    -0x4038(%rbp),%rax
mov    %rax,-0x4038(%rbp)
jmp    115a <rw_loop+0x1a>
add    $0x4040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rw_loop>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x4008,%rsp
mov    %rdi,%r14
xor    %eax,%eax
call   1050 <mstime@plt>
mov    %rax,%rbx
mov    0x2e73(%rip),%rax
mov    (%rax),%edi
mov    %rsp,%rsi
mov    $0x4000,%edx
call   1040 <read@plt>
test   %eax,%eax
je     1228 <rw_loop+0xe8>
mov    %eax,%ebp
mov    0x2e6b(%rip),%r13
mov    %rsp,%r15
xor    %r12d,%r12d
jmp    11b9 <rw_loop+0x79>
nopl   (%rax)
add    %rbx,%r12
xor    %eax,%eax
call   1050 <mstime@plt>
mov    %rax,%rbx
mov    0x2e34(%rip),%rax
mov    (%rax),%edi
mov    %r15,%rsi
mov    $0x4000,%edx
call   1040 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
je     1228 <rw_loop+0xe8>
mov    0x0(%r13),%edi
mov    %r15,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
xor    %eax,%eax
call   1050 <mstime@plt>
mov    %rax,%rcx
imul   $0x3e8,%ebp,%eax
movslq %eax,%rdx
mov    %rdx,%rsi
or     %r14,%rsi
shr    $0x20,%rsi
je     11f0 <rw_loop+0xb0>
mov    %rdx,%rax
xor    %edx,%edx
div    %r14
jmp    11f5 <rw_loop+0xb5>
xor    %edx,%edx
div    %r14d
add    %r12,%rbx
sub    %rcx,%rbx
add    %rax,%rbx
cmp    $0x65,%rbx
jb     1190 <rw_loop+0x50>
xor    %eax,%eax
call   1050 <mstime@plt>
mov    %rax,%r12
imul   $0x3e8,%ebx,%edi
call   1060 <usleep@plt>
xor    %eax,%eax
call   1050 <mstime@plt>
sub    %rax,%r12
jmp    1190 <rw_loop+0x50>
add    $0x4008,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
define void @rw_loop(i64 %0) {
%2 = alloca i64
%3 = alloca [16384 x i8]
%4 = alloca i32
%5 = alloca i64
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
store i64 %0, i64* %2
store i64 0, i64* %8
br label %9
9: ; preds = %1, %52
%10 = call i64 (...) @mstime()
store i64 %10, i64* %5
%11 = load i32, i32* @STDIN_FILENO
%12 = getelementptr inbounds [16384 x i8], [16384 x i8]* %3, i64 0, i64 0
%13 = call i32 @read(i32 %11, i8* %12, i32 16384)
store i32 %13, i32* %4
%14 = load i32, i32* %4
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %9
br label %53
17: ; preds = %9
%18 = load i32, i32* @STDOUT_FILENO
%19 = getelementptr inbounds [16384 x i8], [16384 x i8]* %3, i64 0, i64 0
%20 = load i32, i32* %4
%21 = call i32 @write(i32 %18, i8* %19, i32 %20)
%22 = call i64 (...) @mstime()
store i64 %22, i64* %6
%23 = load i32, i32* %4
%24 = mul nsw i32 1000, %23
%25 = sext i32 %24 to i64
%26 = load i64, i64* %2
%27 = udiv i64 %25, %26
%28 = load i64, i64* %6
%29 = load i64, i64* %5
%30 = sub i64 %28, %29
%31 = sub i64 %27, %30
%32 = load i64, i64* %8
%33 = add i64 %31, %32
store i64 %33, i64* %7
%34 = load i64, i64* %7
%35 = icmp ugt i64 %34, 100
br i1 %35, label %36, label %48
36: ; preds = %17
%37 = call i64 (...) @mstime()
store i64 %37, i64* %5
%38 = load i64, i64* %7
%39 = mul i64 1000, %38
%40 = trunc i64 %39 to i32
%41 = call i32 @usleep(i32 %40)
%42 = call i64 (...) @mstime()
store i64 %42, i64* %6
%43 = load i64, i64* %7
%44 = load i64, i64* %6
%45 = load i64, i64* %5
%46 = sub i64 %44, %45
%47 = sub i64 %43, %46
store i64 %47, i64* %8
br label %52
48: ; preds = %17
%49 = load i64, i64* %7
%50 = load i64, i64* %8
%51 = add i64 %50, %49
store i64 %51, i64* %8
br label %52
52: ; preds = %48, %36
br label %9
53: ; preds = %16
ret void
}

LLVM IR (-O2):
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
define void @rw_loop(i64 %0) {
%2 = alloca [16384 x i8]
%3 = getelementptr inbounds [16384 x i8], [16384 x i8]* %2, i64 0, i64 0
%4 = tail call i64 (...) @mstime()
%5 = load i32, i32* @STDIN_FILENO
%6 = call i32 @read(i32 %5, i8* %3, i32 16384)
%7 = icmp eq i32 %6, 0
br i1 %7, label %36, label %8
8: ; preds = %1, %29
%9 = phi i32 [ %34, %29 ], [ %6, %1 ]
%10 = phi i64 [ %32, %29 ], [ %4, %1 ]
%11 = phi i64 [ %31, %29 ], [ 0, %1 ]
%12 = load i32, i32* @STDOUT_FILENO
%13 = call i32 @write(i32 %12, i8* %3, i32 %9)
%14 = tail call i64 (...) @mstime()
%15 = mul nsw i32 %9, 1000
%16 = sext i32 %15 to i64
%17 = udiv i64 %16, %0
%18 = add i64 %10, %11
%19 = sub i64 %18, %14
%20 = add i64 %19, %17
%21 = icmp ugt i64 %20, 100
br i1 %21, label %22, label %29
22: ; preds = %8
%23 = tail call i64 (...) @mstime()
%24 = trunc i64 %20 to i32
%25 = mul i32 %24, 1000
%26 = tail call i32 @usleep(i32 %25)
%27 = tail call i64 (...) @mstime()
%28 = sub i64 %23, %27
br label %29
29: ; preds = %8, %22
%30 = phi i64 [ %28, %22 ], [ %11, %8 ]
%31 = add i64 %30, %20
%32 = tail call i64 (...) @mstime()
%33 = load i32, i32* @STDIN_FILENO
%34 = call i32 @read(i32 %33, i8* %3, i32 16384)
%35 = icmp eq i32 %34, 0
br i1 %35, label %36, label %8
36: ; preds = %29, %1
ret void
}